/** * Saves code to a file * * Does not save if file exists and overwrites are disallowed. * * @param string $filePath Fully qualified path to new file * @param string $code Code to write in file * @return bool * @throws MockMakerException */ protected function writeFile($filePath, $code) { // ensure that the directory we're attempting to write to exists // important for recursive writes! $writeDir = PathWorker::getPathUpToName($filePath); DirectoryWorker::validateWriteDir($writeDir); if (!file_put_contents($filePath, $code)) { throw new MockMakerException(MockMakerErrors::generateMessage(MockMakerErrors::DATA_POINT_WORKER_WRITE_ERR, array('file' => $filePath))); } return true; }
public function test_getClassNameFromFilePath_returnsClassName() { $expected = 'TestEntity'; $path = $this->rootDir . $expected . '.php'; $actual = PathWorker::getClassNameFromFilePath($path); $this->assertEquals($expected, $actual); }
/** * Recurses over a filepath => namespace converted string in an attempt * to find a valid class namespace path * * @param string $classPath File path converted to class path * @return string|bool */ private function getClassNamespaceFromFilePath($classPath) { if (!class_exists($classPath)) { if (($pos = strpos($classPath, '\\')) === false) { return false; } return $this->getClassNamespaceFromFilePath(substr($classPath, $pos + 1)); } return PathWorker::getPathUpToName($classPath, '\\'); }
/** * Sets the project's root directory path * * Validates path before setting it. * * @param $projectRootPath string Path to your project's root directory */ public function setProjectRootPath($projectRootPath) { $path = Formatter::formatDirectoryPath($projectRootPath); DirectoryWorker::checkIsValidDirectory($path, MockMakerErrors::INVALID_PROJECT_ROOT_PATH); $this->projectRootPath = $path; }
/** * Generates a valid mock file namespace using a user-supplied base * * @param string $mockPath Fully qualified file save path of file to be mocked * @param string $baseNamespace User-supplied base namespace * @return string */ private function determineWithBaseNamespace($mockPath, $baseNamespace) { $modifiedNamespace = $baseNamespace; $lastBaseNamespaceDir = PathWorker::getLastElementInPath($baseNamespace, '\\'); if (($strPos = strpos($mockPath, $lastBaseNamespaceDir)) !== false) { $subStr = substr($mockPath, $strPos + strlen($lastBaseNamespaceDir)); $modifiedNamespace = $baseNamespace . str_replace('/', '\\', $subStr); } return rtrim($modifiedNamespace, '\\'); }