Skip to content

Commit

Permalink
Merge pull request #6 from proophsoftware/develop
Browse files Browse the repository at this point in the history
merge develop into master
  • Loading branch information
sandrokeil committed Jan 22, 2016
2 parents e580520 + ea12acd commit 7755f81
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 13 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 40 additions & 13 deletions src/Console/Helper/Psr4Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,15 @@ 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);
}

/**
* @inheritDoc
*/
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);
Expand All @@ -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';
}

/**
Expand All @@ -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
*/
Expand All @@ -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;
Expand All @@ -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;
Expand Down
175 changes: 175 additions & 0 deletions tests/Console/Helper/Psr4InfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php
/**
* prooph (http://getprooph.org/)
*
* @see https://github.com/proophsoftware/prooph-cli for the canonical source repository
* @copyright Copyright (c) 2016 prooph software GmbH (http://prooph-software.com/)
* @license https://github.com/proophsoftware/prooph-cli/blob/master/LICENSE.md New BSD License
*/

namespace ProophTest\Cli\Console\Helper;

use PHPUnit_Framework_TestCase as TestCase;
use Prooph\Cli\Console\Helper\Psr4Info;

class Psr4InfoTest extends TestCase
{
/**
* @test
* @dataProvider providerForGetClassNamespace
* @covers Prooph\Cli\Console\Helper\Psr4Info::__construct
* @covers Prooph\Cli\Console\Helper\Psr4Info::getClassNamespace
* @covers Prooph\Cli\Console\Helper\Psr4Info::filterDirectoryToNamespace
* @covers Prooph\Cli\Console\Helper\Psr4Info::normalizeNamespace
*/
public function it_returns_class_namespace_from_path($expected, $sourceFolder, $packagePrefix, $path)
{
$psr4Info = new Psr4Info($sourceFolder, $packagePrefix);

self::assertSame($expected, $psr4Info->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'
],
];
}
}

0 comments on commit 7755f81

Please sign in to comment.