diff --git a/CHANGELOG.md b/CHANGELOG.md index 80b6223..b72b083 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,24 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 0.1.2 (2016-01-22) + +### Added + +* Nothing + +### Deprecated + +* Nothing + +### Removed + +* Nothing + +### Fixed + +* Fixed wrong path and namespace handling depending on user input + ## 0.1.1 (2016-01-20) ### Added diff --git a/src/Console/Helper/Psr4Info.php b/src/Console/Helper/Psr4Info.php index 85b38ae..5bb6696 100644 --- a/src/Console/Helper/Psr4Info.php +++ b/src/Console/Helper/Psr4Info.php @@ -85,11 +85,7 @@ public function getClassNamespace($path) { $namespace = $this->filterDirectoryToNamespace()->filter($path); - if ($packagePrefix = $this->getPackagePrefix()) { - $namespace = $packagePrefix . '\\' . $namespace; - } - - return rtrim($namespace, '\\'); + return $this->normalizeNamespace($this->getPackagePrefix() . '\\' . $namespace); } /** @@ -97,7 +93,7 @@ public function getClassNamespace($path) */ public function getPath($fcqn) { - $fcqn = ltrim($fcqn, '\\'); + $fcqn = $this->normalizeNamespace($fcqn); $namespace = str_replace($this->getPackagePrefix(), '', $fcqn); $namespace = ltrim(substr($namespace, 0, strrpos($namespace, '\\')), '\\'); return $this->filterNamespaceToDirectory()->filter($namespace); @@ -108,13 +104,13 @@ public function getPath($fcqn) */ public function getFilename($path, $name) { - $filename = $this->getSourceFolder() . DIRECTORY_SEPARATOR; + $filePath = $this->getSourceFolder() . DIRECTORY_SEPARATOR; if ($path = trim($path, '/')) { - $filename .= $path . DIRECTORY_SEPARATOR; + $filePath .= $this->normalizePath($path) . DIRECTORY_SEPARATOR; } - return $filename . $name . '.php'; + return $filePath . ucfirst($name) . '.php'; } /** @@ -133,6 +129,37 @@ public function getFileDocBlock() return $this->fileDocBlock; } + /** + * Removes duplicates of backslashes and trims backslashes + * + * @param string $namespace + * @return string + */ + private function normalizeNamespace($namespace) + { + $namespace = str_replace('\\\\', '\\', $namespace); + $namespace = explode('\\', $namespace); + + array_walk($namespace, function (&$item) { $item = ucfirst($item); }); + + return trim(implode('\\', $namespace), '\\'); + } + + /** + * PSR-4 folders must be upper camel case + * + * @param string $path + * @return string + */ + private function normalizePath($path) + { + $path = explode('/', $path); + + array_walk($path, function (&$item) { $item = ucfirst($item); }); + + return implode('/', $path); + } + /** * @return FilterChain */ @@ -141,10 +168,10 @@ private function filterDirectoryToNamespace() if (null === $this->filterDirectoryToNamespace) { $this->filterDirectoryToNamespace = new FilterChain(); $this->filterDirectoryToNamespace->attachByName( - 'wordseparatortocamelcase', ['separator' => DIRECTORY_SEPARATOR] + 'wordseparatortoseparator', ['search_separator' => DIRECTORY_SEPARATOR, 'replacement_separator' => '|'] ); $this->filterDirectoryToNamespace->attachByName( - 'wordcamelcasetoseparator', ['separator' => '\\\\'] + 'wordseparatortoseparator', ['search_separator' => '|', 'replacement_separator' => '\\\\'] ); } return $this->filterDirectoryToNamespace; @@ -158,10 +185,10 @@ private function filterNamespaceToDirectory() if (null === $this->filterNamespaceToDirectory) { $this->filterNamespaceToDirectory = new FilterChain(); $this->filterNamespaceToDirectory->attachByName( - 'wordseparatortocamelcase', ['separator' => '\\'] + 'wordseparatortoseparator', ['search_separator' => '\\', 'replacement_separator' => '|'] ); $this->filterNamespaceToDirectory->attachByName( - 'wordcamelcasetoseparator', ['separator' => DIRECTORY_SEPARATOR] + 'wordseparatortoseparator', ['search_separator' => '|', 'replacement_separator' => DIRECTORY_SEPARATOR] ); } return $this->filterNamespaceToDirectory; diff --git a/tests/Console/Helper/Psr4InfoTest.php b/tests/Console/Helper/Psr4InfoTest.php new file mode 100644 index 0000000..04b3dc2 --- /dev/null +++ b/tests/Console/Helper/Psr4InfoTest.php @@ -0,0 +1,175 @@ +getClassNamespace($path)); + } + + /** + * Values are expected, sourceFolder, packagePrefix and path + * + * @return array + */ + public function providerForGetClassNamespace() + { + return [ + [ + 'MyVendor\MyPackage\ModelPath\UserPath', + 'src', + '\MyVendor\MyPackage\\', + 'ModelPath/UserPath', + ], + [ + 'MyVendor\MyPackage\ModelPath\UserPath', + 'src', + '\\MyVendor\\MyPackage\\', + '/ModelPath/UserPath/', + ], + [ + 'Vendor\Package\Model\User', + 'src', + 'vendor\package', + 'model/user/', + ], + [ + 'Vendor\Package', + 'src', + 'vendor\package', + '', + ], + [ + 'Vendor', + 'src', + 'vendor', + '', + ], + [ + '', + 'src', + '', + '', + ], + ]; + } + + /** + * @test + * @dataProvider providerForGetPath + * @covers Prooph\Cli\Console\Helper\Psr4Info::__construct + * @covers Prooph\Cli\Console\Helper\Psr4Info::getPath + * @covers Prooph\Cli\Console\Helper\Psr4Info::filterNamespaceToDirectory + * @covers Prooph\Cli\Console\Helper\Psr4Info::normalizeNamespace + */ + public function it_returns_path_from_namespace($expected, $sourceFolder, $packagePrefix, $fcqn) + { + $psr4Info = new Psr4Info($sourceFolder, $packagePrefix); + + self::assertSame($expected, $psr4Info->getPath($fcqn)); + } + + /** + * Values are expected, sourceFolder, packagePrefix and fcqn + * + * @return array + */ + public function providerForGetPath() + { + return [ + [ + 'ModelPath/UserPath', + 'src', + '\MyVendor\MyPackage\\', + '\MyVendor\MyPackage\ModelPath\UserPath\User', + ], + [ + 'ModelPath/UserPath', + 'src', + '\\MyVendor\\MyPackage\\', + '\\MyVendor\\MyPackage\\ModelPath\\UserPath\\User', + ], + [ + '', + 'src', + 'MyVendor\MyPackage', + 'MyVendor\MyPackage\User', + ], + ]; + } + + /** + * @test + * @dataProvider providerForGetFilename + * @covers Prooph\Cli\Console\Helper\Psr4Info::__construct + * @covers Prooph\Cli\Console\Helper\Psr4Info::getFilename + * @covers Prooph\Cli\Console\Helper\Psr4Info::normalizePath + */ + public function it_returns_filename($expected, $sourceFolder, $packagePrefix, $path, $name) + { + $psr4Info = new Psr4Info($sourceFolder, $packagePrefix); + + self::assertSame($expected, $psr4Info->getFilename($path, $name)); + } + + /** + * Values are expected, sourceFolder, packagePrefix, path and name + * + * @return array + */ + public function providerForGetFilename() + { + return [ + [ + 'src/ModelPath/UserPath/User.php', + 'src', + '\MyVendor\MyPackage\\', + 'ModelPath/UserPath', + 'User' + ], + [ + 'src/ModelPath/UserPath/User.php', + 'src', + '\\MyVendor\\MyPackage\\', + 'ModelPath/UserPath/', + 'User' + ], + [ + 'src/Model/User.php', + 'src', + 'vendor\package', + '/model/', + 'user' + ], + [ + '/src/User.php', + '/src/', + 'vendor\package', + '', + 'user' + ], + ]; + } +}