Skip to content
This repository was archived by the owner on Feb 14, 2021. It is now read-only.

Commit 5b2773d

Browse files
committed
Implemented finding resources.
1 parent 9da564a commit 5b2773d

File tree

2 files changed

+212
-35
lines changed

2 files changed

+212
-35
lines changed

spec/PhpSpec/Symfony2Extension/Locator/PSR0LocatorSpec.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,67 @@ function it_does_not_support_any_other_queries()
9191
$this->supportsQuery('/')->shouldReturn(false);
9292
}
9393

94+
function it_finds_spec_resources_via_the_srcPath(Filesystem $fs, \SplFileInfo $file)
95+
{
96+
$fs->findPhpFilesIn(array($this->srcPath.'Acme/Bundle/DemoBundle/Spec', $this->srcPath.'Acme/Model/Spec'))->willReturn(array($file));
97+
$file->getRealPath()->willReturn($this->srcPath.'Acme/Bundle/DemoBundle/Spec/Model/UserSpec.php');
98+
99+
$resources = $this->findResources('src');
100+
$resources->shouldHaveCount(1);
101+
$resources[0]->shouldBeAnInstanceOf('PhpSpec\\Symfony2Extension\\Locator\\PSR0Resource');
102+
$resources[0]->getSrcClassname()->shouldReturn('Acme\\Bundle\\DemoBundle\\Model\\User');
103+
}
104+
105+
function it_finds_spec_resources_via_a_subSrcPath(Filesystem $fs, \SplFileInfo $file)
106+
{
107+
$fs->findPhpFilesIn(array($this->srcPath.'Acme/Bundle/DemoBundle/Spec'))->willReturn(array($file));
108+
$file->getRealPath()->willReturn($this->srcPath.'Acme/Bundle/DemoBundle/Spec/Model/UserSpec.php');
109+
110+
$resources = $this->findResources('src/Acme/Bundle/DemoBundle');
111+
$resources->shouldHaveCount(1);
112+
$resources[0]->shouldBeAnInstanceOf('PhpSpec\\Symfony2Extension\\Locator\\PSR0Resource');
113+
$resources[0]->getSrcClassname()->shouldReturn('Acme\\Bundle\\DemoBundle\\Model\\User');
114+
}
115+
116+
function it_finds_spec_resources_via_a_specPath(Filesystem $fs, \SplFileInfo $file)
117+
{
118+
$fs->findPhpFilesIn(array($this->srcPath.'Acme/Bundle/DemoBundle/Spec/Model'))->willReturn(array($file));
119+
$file->getRealPath()->willReturn($this->srcPath.'Acme/Bundle/DemoBundle/Spec/Model/UserSpec.php');
120+
121+
$resources = $this->findResources('src/Acme/Bundle/DemoBundle/Spec/Model');
122+
$resources->shouldHaveCount(1);
123+
$resources[0]->shouldBeAnInstanceOf('PhpSpec\\Symfony2Extension\\Locator\\PSR0Resource');
124+
$resources[0]->getSrcClassname()->shouldReturn('Acme\\Bundle\\DemoBundle\\Model\\User');
125+
}
126+
127+
function it_finds_a_single_spec_via_a_srcPath()
128+
{
129+
$resources = $this->findResources('src/Acme/Bundle/DemoBundle/Model/User.php');
130+
$resources->shouldHaveCount(1);
131+
$resources[0]->shouldBeAnInstanceOf('PhpSpec\\Symfony2Extension\\Locator\\PSR0Resource');
132+
$resources[0]->getSrcClassname()->shouldReturn('Acme\\Bundle\\DemoBundle\\Model\\User');
133+
}
134+
135+
function it_finds_a_single_spec_via_a_specPath()
136+
{
137+
$resources = $this->findResources('src/Acme/Bundle/DemoBundle/Spec/Model/UserSpec.php');
138+
$resources->shouldHaveCount(1);
139+
$resources[0]->shouldBeAnInstanceOf('PhpSpec\\Symfony2Extension\\Locator\\PSR0Resource');
140+
$resources[0]->getSrcClassname()->shouldReturn('Acme\\Bundle\\DemoBundle\\Model\\User');
141+
}
142+
143+
function it_returns_an_empty_array_if_path_does_not_exist()
144+
{
145+
$this->findResources('src/Acme/Bundle/MissingBundle')->shouldHaveCount(0);
146+
}
147+
148+
function it_returns_an_empty_array_if_nothing_is_found()
149+
{
150+
$this->filesystem->remove($this->srcPath.'Acme');
151+
152+
$this->findResources('src')->shouldHaveCount(0);
153+
}
154+
94155
function it_supports_classes_from_srcNamespace(Filesystem $fs)
95156
{
96157
$this->beConstructedWith('Acme\\Model', 'Spec', 'src', array('src/*/*/Spec'), $fs);
@@ -155,7 +216,7 @@ function it_creates_a_resource_from_a_spec_class()
155216
$resource->getSpecClassname()->shouldReturn('Acme\Bundle\DemoBundle\Spec\Model\UserSpec');
156217
}
157218

158-
function it_creates_a_resource_from_a_spec_class_with_a_custom_specSubNamespace()
219+
function it_creates_a_resource_from_a_spec_class_with_a_custom_specSubNamespace(Filesystem $fs)
159220
{
160221
$this->beConstructedWith('Acme', 'Specs', 'src', array('src/*/Bundle/*Bundle/Specs', 'src/*/*/Specs'), $fs);
161222

src/PhpSpec/Symfony2Extension/Locator/PSR0Locator.php

Lines changed: 150 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,38 @@
77

88
class PSR0Locator implements ResourceLocatorInterface
99
{
10+
/**
11+
* @var string
12+
*/
1013
private $srcNamespace;
1114

15+
/**
16+
* @var string
17+
*/
1218
private $specSubNamespace;
1319

20+
/**
21+
* @var string
22+
*/
1423
private $srcPath;
1524

25+
/**
26+
* @var array
27+
*/
1628
private $specPaths = array();
1729

30+
/**
31+
* @var Filesystem
32+
*/
1833
private $filesystem;
1934

35+
/**
36+
* @param string $srcNamespace
37+
* @param string $specSubNamespace
38+
* @param string $srcPath
39+
* @param array $specPaths
40+
* @param Filesystem $filesystem
41+
*/
2042
public function __construct($srcNamespace = '', $specSubNamespace = 'Spec', $srcPath = 'src', $specPaths = array(), Filesystem $filesystem = null)
2143
{
2244
$this->srcNamespace = $srcNamespace;
@@ -26,45 +48,156 @@ public function __construct($srcNamespace = '', $specSubNamespace = 'Spec', $src
2648
$this->filesystem = $filesystem ?: new Filesystem();
2749
}
2850

51+
/**
52+
* @param array $specPaths
53+
*
54+
* @return array
55+
*/
56+
private function expandSpecPaths(array $specPaths)
57+
{
58+
$result = array();
59+
60+
foreach ($specPaths as $specPath) {
61+
$paths = glob($specPath, GLOB_ONLYDIR);
62+
if (!empty($paths)) {
63+
$paths = array_filter(array_map('realpath', $paths));
64+
$result = array_merge($result, $paths);
65+
}
66+
}
67+
68+
return $result;
69+
}
70+
71+
/**
72+
* @return array
73+
*/
2974
public function getAllResources()
3075
{
31-
if (empty($this->specPaths)) {
76+
return $this->findResources($this->srcPath);
77+
}
78+
79+
/**
80+
* @param string $query
81+
*
82+
* @return boolean
83+
*/
84+
public function supportsQuery($query)
85+
{
86+
$path = rtrim(realpath($query), DIRECTORY_SEPARATOR);
87+
88+
return 0 === strpos($path, rtrim($this->srcPath, DIRECTORY_SEPARATOR));
89+
}
90+
91+
/**
92+
* @param string $query
93+
*
94+
* @return PSR0Resource[]
95+
*/
96+
public function findResources($query)
97+
{
98+
$path = realpath($query);
99+
100+
if (!$path) {
32101
return array();
33102
}
34103

35-
$files = $this->filesystem->findPhpFilesIn($this->specPaths);
36-
$resources = array();
104+
if ('.php' === substr($path, -4)) {
105+
return array($this->createResourceFromSpecFile($path));
106+
}
37107

38-
foreach ($files as $file) {
39-
$path = $file->getRealPath();
40-
$relative = substr($path, strlen($this->srcPath), -4);
41-
$relative = str_replace('Spec', '', $relative);
108+
return $this->findResourcesInSpecPaths($path);
109+
}
110+
111+
/**
112+
* @param string $path
113+
*
114+
* @return PSR0Resource|null
115+
*/
116+
private function createResourceFromSpecFile($path)
117+
{
118+
$relativePath = substr($path, strlen($this->srcPath), -4);
119+
$relativePath = str_replace('Spec', '', $relativePath);
120+
121+
return $this->createResource($relativePath);
122+
}
42123

43-
$resources[] = $this->createResource($relative);
124+
/**
125+
* @param string $path
126+
*
127+
* @return array
128+
*/
129+
private function findResourcesInSpecPaths($path)
130+
{
131+
$paths = $this->filterSpecPaths($path);
132+
133+
if (empty($paths)) {
134+
return array();
44135
}
45136

46-
return $resources;
137+
$files = $this->filesystem->findPhpFilesIn($paths);
138+
139+
return $this->createResourcesFromSpecFiles($files);
47140
}
48141

49-
public function supportsQuery($query)
142+
/**
143+
* Filters out the spec paths which are not child or parent of the path.
144+
*
145+
* @param string $path
146+
*
147+
* @return array
148+
*/
149+
private function filterSpecPaths($path)
50150
{
51-
$path = rtrim(realpath($query), DIRECTORY_SEPARATOR);
151+
$specPaths = array_map(
152+
function ($value) use ($path) {
153+
return 0 === strpos($path, $value) ? $path : $value;
154+
},
155+
$this->specPaths
156+
);
157+
158+
$specPaths = array_filter(
159+
$specPaths,
160+
function ($value) use ($path) {
161+
return 0 === strpos($value, $path);
162+
}
163+
);
52164

53-
return 0 === strpos($path, rtrim($this->srcPath, DIRECTORY_SEPARATOR));
165+
return $specPaths;
54166
}
55167

56-
public function findResources($query)
168+
/**
169+
* @param array $files
170+
*
171+
* @return PSR0Resource[]
172+
*/
173+
private function createResourcesFromSpecFiles(array $files)
57174
{
58-
// @todo: Implement findResources() method.
175+
$resources = array();
176+
177+
foreach ($files as $file) {
178+
$resources[] = $this->createResourceFromSpecFile($file->getRealPath());
179+
}
180+
181+
return $resources;
59182
}
60183

184+
/**
185+
* @param string $classname
186+
*
187+
* @return boolean
188+
*/
61189
public function supportsClass($classname)
62190
{
63191
$classname = str_replace('/', '\\', $classname);
64192

65193
return '' === $this->srcNamespace || 0 === strpos($classname, $this->srcNamespace);
66194
}
67195

196+
/**
197+
* @param string $classname
198+
*
199+
* @return PSR0Resource|null
200+
*/
68201
public function createResource($classname)
69202
{
70203
$classname = str_replace('/', '\\', $classname);
@@ -77,28 +210,11 @@ public function createResource($classname)
77210
return null;
78211
}
79212

80-
public function getPriority()
81-
{
82-
return 0;
83-
}
84-
85213
/**
86-
* @param array $specPaths
87-
*
88-
* @return array
214+
* @return integer
89215
*/
90-
private function expandSpecPaths(array $specPaths)
216+
public function getPriority()
91217
{
92-
$result = array();
93-
94-
foreach ($specPaths as $specPath) {
95-
$paths = glob($specPath, GLOB_ONLYDIR);
96-
if (!empty($paths)) {
97-
$paths = array_filter(array_map('realpath', $paths));
98-
$result = array_merge($result, $paths);
99-
}
100-
}
101-
102-
return $result;
218+
return 0;
103219
}
104220
}

0 commit comments

Comments
 (0)