Skip to content

Commit 7755f81

Browse files
committed
Merge pull request #6 from proophsoftware/develop
merge develop into master
2 parents e580520 + ea12acd commit 7755f81

File tree

3 files changed

+233
-13
lines changed

3 files changed

+233
-13
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 0.1.2 (2016-01-22)
6+
7+
### Added
8+
9+
* Nothing
10+
11+
### Deprecated
12+
13+
* Nothing
14+
15+
### Removed
16+
17+
* Nothing
18+
19+
### Fixed
20+
21+
* Fixed wrong path and namespace handling depending on user input
22+
523
## 0.1.1 (2016-01-20)
624

725
### Added

src/Console/Helper/Psr4Info.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,15 @@ public function getClassNamespace($path)
8585
{
8686
$namespace = $this->filterDirectoryToNamespace()->filter($path);
8787

88-
if ($packagePrefix = $this->getPackagePrefix()) {
89-
$namespace = $packagePrefix . '\\' . $namespace;
90-
}
91-
92-
return rtrim($namespace, '\\');
88+
return $this->normalizeNamespace($this->getPackagePrefix() . '\\' . $namespace);
9389
}
9490

9591
/**
9692
* @inheritDoc
9793
*/
9894
public function getPath($fcqn)
9995
{
100-
$fcqn = ltrim($fcqn, '\\');
96+
$fcqn = $this->normalizeNamespace($fcqn);
10197
$namespace = str_replace($this->getPackagePrefix(), '', $fcqn);
10298
$namespace = ltrim(substr($namespace, 0, strrpos($namespace, '\\')), '\\');
10399
return $this->filterNamespaceToDirectory()->filter($namespace);
@@ -108,13 +104,13 @@ public function getPath($fcqn)
108104
*/
109105
public function getFilename($path, $name)
110106
{
111-
$filename = $this->getSourceFolder() . DIRECTORY_SEPARATOR;
107+
$filePath = $this->getSourceFolder() . DIRECTORY_SEPARATOR;
112108

113109
if ($path = trim($path, '/')) {
114-
$filename .= $path . DIRECTORY_SEPARATOR;
110+
$filePath .= $this->normalizePath($path) . DIRECTORY_SEPARATOR;
115111
}
116112

117-
return $filename . $name . '.php';
113+
return $filePath . ucfirst($name) . '.php';
118114
}
119115

120116
/**
@@ -133,6 +129,37 @@ public function getFileDocBlock()
133129
return $this->fileDocBlock;
134130
}
135131

132+
/**
133+
* Removes duplicates of backslashes and trims backslashes
134+
*
135+
* @param string $namespace
136+
* @return string
137+
*/
138+
private function normalizeNamespace($namespace)
139+
{
140+
$namespace = str_replace('\\\\', '\\', $namespace);
141+
$namespace = explode('\\', $namespace);
142+
143+
array_walk($namespace, function (&$item) { $item = ucfirst($item); });
144+
145+
return trim(implode('\\', $namespace), '\\');
146+
}
147+
148+
/**
149+
* PSR-4 folders must be upper camel case
150+
*
151+
* @param string $path
152+
* @return string
153+
*/
154+
private function normalizePath($path)
155+
{
156+
$path = explode('/', $path);
157+
158+
array_walk($path, function (&$item) { $item = ucfirst($item); });
159+
160+
return implode('/', $path);
161+
}
162+
136163
/**
137164
* @return FilterChain
138165
*/
@@ -141,10 +168,10 @@ private function filterDirectoryToNamespace()
141168
if (null === $this->filterDirectoryToNamespace) {
142169
$this->filterDirectoryToNamespace = new FilterChain();
143170
$this->filterDirectoryToNamespace->attachByName(
144-
'wordseparatortocamelcase', ['separator' => DIRECTORY_SEPARATOR]
171+
'wordseparatortoseparator', ['search_separator' => DIRECTORY_SEPARATOR, 'replacement_separator' => '|']
145172
);
146173
$this->filterDirectoryToNamespace->attachByName(
147-
'wordcamelcasetoseparator', ['separator' => '\\\\']
174+
'wordseparatortoseparator', ['search_separator' => '|', 'replacement_separator' => '\\\\']
148175
);
149176
}
150177
return $this->filterDirectoryToNamespace;
@@ -158,10 +185,10 @@ private function filterNamespaceToDirectory()
158185
if (null === $this->filterNamespaceToDirectory) {
159186
$this->filterNamespaceToDirectory = new FilterChain();
160187
$this->filterNamespaceToDirectory->attachByName(
161-
'wordseparatortocamelcase', ['separator' => '\\']
188+
'wordseparatortoseparator', ['search_separator' => '\\', 'replacement_separator' => '|']
162189
);
163190
$this->filterNamespaceToDirectory->attachByName(
164-
'wordcamelcasetoseparator', ['separator' => DIRECTORY_SEPARATOR]
191+
'wordseparatortoseparator', ['search_separator' => '|', 'replacement_separator' => DIRECTORY_SEPARATOR]
165192
);
166193
}
167194
return $this->filterNamespaceToDirectory;
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
/**
3+
* prooph (http://getprooph.org/)
4+
*
5+
* @see https://github.com/proophsoftware/prooph-cli for the canonical source repository
6+
* @copyright Copyright (c) 2016 prooph software GmbH (http://prooph-software.com/)
7+
* @license https://github.com/proophsoftware/prooph-cli/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace ProophTest\Cli\Console\Helper;
11+
12+
use PHPUnit_Framework_TestCase as TestCase;
13+
use Prooph\Cli\Console\Helper\Psr4Info;
14+
15+
class Psr4InfoTest extends TestCase
16+
{
17+
/**
18+
* @test
19+
* @dataProvider providerForGetClassNamespace
20+
* @covers Prooph\Cli\Console\Helper\Psr4Info::__construct
21+
* @covers Prooph\Cli\Console\Helper\Psr4Info::getClassNamespace
22+
* @covers Prooph\Cli\Console\Helper\Psr4Info::filterDirectoryToNamespace
23+
* @covers Prooph\Cli\Console\Helper\Psr4Info::normalizeNamespace
24+
*/
25+
public function it_returns_class_namespace_from_path($expected, $sourceFolder, $packagePrefix, $path)
26+
{
27+
$psr4Info = new Psr4Info($sourceFolder, $packagePrefix);
28+
29+
self::assertSame($expected, $psr4Info->getClassNamespace($path));
30+
}
31+
32+
/**
33+
* Values are expected, sourceFolder, packagePrefix and path
34+
*
35+
* @return array
36+
*/
37+
public function providerForGetClassNamespace()
38+
{
39+
return [
40+
[
41+
'MyVendor\MyPackage\ModelPath\UserPath',
42+
'src',
43+
'\MyVendor\MyPackage\\',
44+
'ModelPath/UserPath',
45+
],
46+
[
47+
'MyVendor\MyPackage\ModelPath\UserPath',
48+
'src',
49+
'\\MyVendor\\MyPackage\\',
50+
'/ModelPath/UserPath/',
51+
],
52+
[
53+
'Vendor\Package\Model\User',
54+
'src',
55+
'vendor\package',
56+
'model/user/',
57+
],
58+
[
59+
'Vendor\Package',
60+
'src',
61+
'vendor\package',
62+
'',
63+
],
64+
[
65+
'Vendor',
66+
'src',
67+
'vendor',
68+
'',
69+
],
70+
[
71+
'',
72+
'src',
73+
'',
74+
'',
75+
],
76+
];
77+
}
78+
79+
/**
80+
* @test
81+
* @dataProvider providerForGetPath
82+
* @covers Prooph\Cli\Console\Helper\Psr4Info::__construct
83+
* @covers Prooph\Cli\Console\Helper\Psr4Info::getPath
84+
* @covers Prooph\Cli\Console\Helper\Psr4Info::filterNamespaceToDirectory
85+
* @covers Prooph\Cli\Console\Helper\Psr4Info::normalizeNamespace
86+
*/
87+
public function it_returns_path_from_namespace($expected, $sourceFolder, $packagePrefix, $fcqn)
88+
{
89+
$psr4Info = new Psr4Info($sourceFolder, $packagePrefix);
90+
91+
self::assertSame($expected, $psr4Info->getPath($fcqn));
92+
}
93+
94+
/**
95+
* Values are expected, sourceFolder, packagePrefix and fcqn
96+
*
97+
* @return array
98+
*/
99+
public function providerForGetPath()
100+
{
101+
return [
102+
[
103+
'ModelPath/UserPath',
104+
'src',
105+
'\MyVendor\MyPackage\\',
106+
'\MyVendor\MyPackage\ModelPath\UserPath\User',
107+
],
108+
[
109+
'ModelPath/UserPath',
110+
'src',
111+
'\\MyVendor\\MyPackage\\',
112+
'\\MyVendor\\MyPackage\\ModelPath\\UserPath\\User',
113+
],
114+
[
115+
'',
116+
'src',
117+
'MyVendor\MyPackage',
118+
'MyVendor\MyPackage\User',
119+
],
120+
];
121+
}
122+
123+
/**
124+
* @test
125+
* @dataProvider providerForGetFilename
126+
* @covers Prooph\Cli\Console\Helper\Psr4Info::__construct
127+
* @covers Prooph\Cli\Console\Helper\Psr4Info::getFilename
128+
* @covers Prooph\Cli\Console\Helper\Psr4Info::normalizePath
129+
*/
130+
public function it_returns_filename($expected, $sourceFolder, $packagePrefix, $path, $name)
131+
{
132+
$psr4Info = new Psr4Info($sourceFolder, $packagePrefix);
133+
134+
self::assertSame($expected, $psr4Info->getFilename($path, $name));
135+
}
136+
137+
/**
138+
* Values are expected, sourceFolder, packagePrefix, path and name
139+
*
140+
* @return array
141+
*/
142+
public function providerForGetFilename()
143+
{
144+
return [
145+
[
146+
'src/ModelPath/UserPath/User.php',
147+
'src',
148+
'\MyVendor\MyPackage\\',
149+
'ModelPath/UserPath',
150+
'User'
151+
],
152+
[
153+
'src/ModelPath/UserPath/User.php',
154+
'src',
155+
'\\MyVendor\\MyPackage\\',
156+
'ModelPath/UserPath/',
157+
'User'
158+
],
159+
[
160+
'src/Model/User.php',
161+
'src',
162+
'vendor\package',
163+
'/model/',
164+
'user'
165+
],
166+
[
167+
'/src/User.php',
168+
'/src/',
169+
'vendor\package',
170+
'',
171+
'user'
172+
],
173+
];
174+
}
175+
}

0 commit comments

Comments
 (0)