|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* MageSpec |
|
4
|
|
|
* |
|
5
|
|
|
* NOTICE OF LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the MIT License, that is bundled with this |
|
8
|
|
|
* package in the file LICENSE. |
|
9
|
|
|
* It is also available through the world-wide-web at this URL: |
|
10
|
|
|
* |
|
11
|
|
|
* http://opensource.org/licenses/MIT |
|
12
|
|
|
* |
|
13
|
|
|
* If you did not receive a copy of the license and are unable to obtain it |
|
14
|
|
|
* through the world-wide-web, please send an email |
|
15
|
|
|
* to <[email protected]> so we can send you a copy immediately. |
|
16
|
|
|
* |
|
17
|
|
|
* @category MageTest |
|
18
|
|
|
* @package PhpSpec_MagentoExtension |
|
19
|
|
|
* |
|
20
|
|
|
* @copyright Copyright (c) 2012-2013 MageTest team and contributors. |
|
21
|
|
|
*/ |
|
22
|
|
|
namespace MageTest\PhpSpec\MagentoExtension\Locator\Magento; |
|
23
|
|
|
|
|
24
|
|
|
use PhpSpec\Locator\Resource as ResourceInterface; |
|
25
|
|
|
use PhpSpec\Locator\ResourceLocator as ResourceLocatorInterface; |
|
26
|
|
|
use PhpSpec\Util\Filesystem; |
|
27
|
|
|
|
|
28
|
|
|
abstract class AbstractResourceLocator |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $srcPath; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $specPath; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $srcNamespace; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $specNamespace; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $fullSrcPath; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $fullSpecPath; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var Filesystem |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $filesystem; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $codePool; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $srcNamespace |
|
72
|
|
|
* @param string $specNamespacePrefix |
|
73
|
|
|
* @param string $srcPath |
|
74
|
|
|
* @param string $specPath |
|
75
|
|
|
* @param Filesystem $filesystem |
|
76
|
|
|
* @param string $codePool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function __construct( |
|
79
|
|
|
$srcNamespace = '', |
|
80
|
|
|
$specNamespacePrefix = '', |
|
81
|
|
|
$srcPath = 'src', |
|
82
|
|
|
$specPath = 'spec', |
|
83
|
|
|
Filesystem $filesystem = null, |
|
84
|
|
|
$codePool = 'local' |
|
85
|
|
|
) { |
|
86
|
|
|
$this->filesystem = $filesystem ?: new Filesystem(); |
|
87
|
|
|
$this->codePool = $codePool; |
|
88
|
|
|
|
|
89
|
|
|
$this->srcPath = rtrim(realpath($srcPath), '/\\') . DIRECTORY_SEPARATOR . $this->codePool . DIRECTORY_SEPARATOR; |
|
90
|
|
|
$this->specPath = rtrim(realpath($specPath), '/\\') . DIRECTORY_SEPARATOR . $this->codePool . DIRECTORY_SEPARATOR; |
|
91
|
|
|
$this->srcNamespace = ltrim(trim($srcNamespace, ' \\') . '\\', '\\'); |
|
92
|
|
|
$this->specNamespace = trim($specNamespacePrefix, ' \\') . '\\'; |
|
93
|
|
|
$this->fullSrcPath = $this->srcPath; |
|
94
|
|
|
$this->fullSpecPath = $this->specPath; |
|
95
|
|
|
|
|
96
|
|
|
$this->validatePaths($srcPath, $specPath); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getFullSrcPath() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->fullSrcPath; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getFullSpecPath() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->fullSpecPath; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getSrcNamespace() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->srcNamespace; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getSpecNamespace() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->specNamespace; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getCodePool() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->codePool; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getAllResources() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->findSpecResources($this->fullSpecPath); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $query |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
|
|
public function supportsQuery($query) |
|
152
|
|
|
{ |
|
153
|
|
|
return (bool) preg_match($this->getValidator(), $query) || $this->isSupported($query); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $query |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
public function findResources($query) |
|
161
|
|
|
{ |
|
162
|
|
|
$path = $this->getCleanPath($query); |
|
163
|
|
|
|
|
164
|
|
|
foreach (array($this->fullSrcPath, $this->srcPath) as $srcPath) { |
|
165
|
|
|
if (0 === strpos($path, $srcPath)) { |
|
166
|
|
|
$path = $srcPath.substr($path, strlen($srcPath)); |
|
167
|
|
|
$path = preg_replace('/\.php/', 'Spec.php', $path); |
|
168
|
|
|
|
|
169
|
|
|
return $this->findSpecResources($path); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (0 === strpos($path, $this->specPath)) { |
|
174
|
|
|
return $this->findSpecResources($path); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return array(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $classname |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
|
|
public function supportsClass($classname) |
|
185
|
|
|
{ |
|
186
|
|
|
$parts = explode('_', $classname); |
|
187
|
|
|
|
|
188
|
|
|
if (count($parts) < 2) { |
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return ( |
|
193
|
|
|
$this->supportsQuery($classname) || |
|
194
|
|
|
$classname === implode('_', array($parts[0], $parts[1], $this->getClassType(), $parts[count($parts)-1])) |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $classname |
|
200
|
|
|
* @return ResourceInterface |
|
201
|
|
|
*/ |
|
202
|
|
|
public function createResource($classname) |
|
203
|
|
|
{ |
|
204
|
|
|
preg_match($this->getValidator(), $classname, $matches); |
|
205
|
|
|
|
|
206
|
|
|
if (!empty($matches)) { |
|
207
|
|
|
array_shift($matches); |
|
208
|
|
|
array_shift($matches); |
|
209
|
|
|
|
|
210
|
|
|
$classname = $this->getClassnameFromMatches($matches); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $this->getResource(explode('_', $classname), $this); |
|
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return int |
|
218
|
|
|
*/ |
|
219
|
|
|
abstract public function getPriority(); |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param string $path |
|
223
|
|
|
* @return array |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function findSpecResources($path) |
|
226
|
|
|
{ |
|
227
|
|
|
if (!$this->filesystem->pathExists($path)) { |
|
228
|
|
|
return array(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
if ('.php' === substr($path, -4)) { |
|
232
|
|
|
if (!$this->isSupported($path)) { |
|
233
|
|
|
return array(); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return array($this->createResourceFromSpecFile(realpath($path))); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$resources = array(); |
|
240
|
|
|
foreach ($this->filesystem->findSpecFilesIn($path) as $file) { |
|
241
|
|
|
$specFile = $file->getRealPath(); |
|
242
|
|
|
if ($this->isSupported($specFile)) { |
|
243
|
|
|
$resources[] = $this->createResourceFromSpecFile($specFile); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return $resources; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @param string $path |
|
252
|
|
|
* @return ResourceInterface |
|
253
|
|
|
*/ |
|
254
|
|
|
private function createResourceFromSpecFile($path) |
|
255
|
|
|
{ |
|
256
|
|
|
$relative = $this->getRelative($path); |
|
257
|
|
|
|
|
258
|
|
|
return $this->getResource(explode(DIRECTORY_SEPARATOR, $relative), $this); |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @param string $srcPath |
|
263
|
|
|
* @param string $specPath |
|
264
|
|
|
* @throws \InvalidArgumentException |
|
265
|
|
|
*/ |
|
266
|
|
|
private function validatePaths($srcPath, $specPath) |
|
267
|
|
|
{ |
|
268
|
|
|
$invalidPath = DIRECTORY_SEPARATOR . $this->codePool . DIRECTORY_SEPARATOR; |
|
269
|
|
|
|
|
270
|
|
|
if ($invalidPath === $this->srcPath) { |
|
271
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
272
|
|
|
'Source code path should be existing filesystem path, but "%s" given.', |
|
273
|
|
|
$srcPath |
|
274
|
|
|
)); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
if ($invalidPath === $this->specPath) { |
|
278
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
279
|
|
|
'Specs code path should be existing filesystem path, but "%s" given.', |
|
280
|
|
|
$specPath |
|
281
|
|
|
)); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param string $query |
|
287
|
|
|
* @return string |
|
288
|
|
|
*/ |
|
289
|
|
|
private function getCleanPath($query) |
|
290
|
|
|
{ |
|
291
|
|
|
$path = rtrim(realpath(str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $query)), DIRECTORY_SEPARATOR); |
|
292
|
|
|
|
|
293
|
|
|
if ('.php' !== substr($path, -4)) { |
|
294
|
|
|
$path .= DIRECTORY_SEPARATOR; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return $path; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @param array $matches |
|
302
|
|
|
* @return string |
|
303
|
|
|
*/ |
|
304
|
|
|
private function getClassnameFromMatches(array $matches) |
|
305
|
|
|
{ |
|
306
|
|
|
$vendor = ucfirst(array_shift($matches)); |
|
307
|
|
|
$module = ucfirst(array_shift($matches)); |
|
308
|
|
|
|
|
309
|
|
|
return implode('_', array($vendor, $module, $this->getObjectName($matches))); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @param array $matches |
|
314
|
|
|
* @return string |
|
315
|
|
|
*/ |
|
316
|
|
|
protected function getObjectName(array $matches) |
|
317
|
|
|
{ |
|
318
|
|
|
return $this->getClassType() . '_' . implode('_', array_map('ucfirst', explode('_', implode($matches)))); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* @param string $path |
|
323
|
|
|
* @return string |
|
324
|
|
|
*/ |
|
325
|
|
|
protected function getRelative($path) |
|
326
|
|
|
{ |
|
327
|
|
|
// cut "Spec.php" from the end |
|
328
|
|
|
$relative = substr($path, strlen($this->fullSpecPath), -4); |
|
329
|
|
|
return preg_replace('/Spec$/', '', $relative); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @param string $file |
|
334
|
|
|
* @return bool |
|
335
|
|
|
*/ |
|
336
|
|
|
abstract protected function isSupported($file); |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* @param array $parts |
|
340
|
|
|
* @param ResourceLocatorInterface $locator |
|
341
|
|
|
* @return ResourceInterface |
|
342
|
|
|
*/ |
|
343
|
|
|
abstract protected function getResource(array $parts, ResourceLocatorInterface $locator); |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* @return string |
|
347
|
|
|
*/ |
|
348
|
|
|
abstract protected function getClassType(); |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* @return string |
|
352
|
|
|
*/ |
|
353
|
|
|
abstract protected function getValidator(); |
|
354
|
|
|
} |
|
355
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: