public function test_convertFilePathToClassPath_returnsProperNamespace() { $expected = 'tests\\MockMaker\\Entities\\SimpleEntity'; $filePath = $this->rootDir . 'tests/MockMaker/Entities/SimpleEntity'; $actual = PathWorker::convertFilePathToClassPath($filePath, $this->rootDir); $this->assertEquals($expected, $actual); }
/** * Determines a class's namespace by iterating over a filepath=>namespace conversion * * @param string $filePath Fully qualified target file path * @param string $rootPath Project root path * @return string * @throws MockMakerException */ private function determineClassNamespace($filePath, $rootPath) { $className = PathWorker::getClassNameFromFilePath($filePath); if ($result = $this->checkClassUsingValidNamespacesArray($className)) { return $result; } // not already in our array, so we have to find it the hard way $classPath = PathWorker::convertFilePathToClassPath($filePath, $rootPath); if (!($result = $this->getClassNamespaceFromFilePath($classPath))) { $args = array('class' => $className); throw new MockMakerException(MockMakerErrors::generateMessage(MockMakerErrors::INVALID_CLASS_TYPE, $args)); } $this->addValidNamespaces($result); return $result; }
/** * Generates the mock file's namespace * * If a mockFileBaseNamespace is not defined, this will use composer * to try to find a valid one. Failing that, a wild-assed-guess is returned. * * @param string $mockSavePath Fully qualified file save path of file to be mocked * @param ConfigData $config ConfigData object * Required data points are: * - getMockFileBaseNamespace() * - getProjectRootPath() * - getMockWriteDir() * @return string */ private function generateMockFileNamespace($mockSavePath, ConfigData $config) { $wagNamespace = PathWorker::convertFilePathToClassPath($mockSavePath, $config->getProjectRootPath()); if ($config->getMockFileBaseNamespace()) { return $this->determineWithBaseNamespace($mockSavePath, $config->getMockFileBaseNamespace()); } $composerWorker = new ComposerWorker(); $composerData = $composerWorker->getNamespaceFromComposer($mockSavePath, $config->getProjectRootPath()); if (!$composerData) { return $wagNamespace; } $relNamespacePath = str_replace($composerData['path'], '', $mockSavePath); $namespace = $composerData['namespace'] . str_replace('/', '\\', $relNamespacePath); return rtrim($namespace, '\\'); }