1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
use Robo\ClassDiscovery\RelativeNamespaceDiscovery; |
5
|
|
|
use Composer\Autoload\ClassLoader; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class RelativeNamespaceDiscoveryTest |
9
|
|
|
*/ |
10
|
|
|
class RelativeNamespaceDiscoveryTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testGetClasses() |
13
|
|
|
{ |
14
|
|
|
$classLoader = new ClassLoader(); |
15
|
|
|
$classLoader->addPsr4('\\Robo\\PluginTest\\', [realpath(__DIR__.'/../../plugins')]); |
16
|
|
|
$service = new RelativeNamespaceDiscovery($classLoader); |
17
|
|
|
$service->setRelativeNamespace('Robo\Plugin'); |
18
|
|
|
$service->setSearchPattern('/.*Commands?\.php$/'); |
19
|
|
|
$classes = $service->getClasses(); |
20
|
|
|
|
21
|
|
|
$this->assertContains('\Robo\PluginTest\Robo\Plugin\Commands\FirstCustomCommands', $classes); |
22
|
|
|
$this->assertContains('\Robo\PluginTest\Robo\Plugin\Commands\SecondCustomCommands', $classes); |
23
|
|
|
$this->assertContains('\Robo\PluginTest\Robo\Plugin\Commands\ThirdCustomCommand', $classes); |
24
|
|
|
$this->assertNotContains('\Robo\PluginTest\Robo\Plugin\Commands\NotValidClassName', $classes); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testGetFile() |
28
|
|
|
{ |
29
|
|
|
$classLoader = new ClassLoader(); |
30
|
|
|
$classLoader->addPsr4('\\Robo\\PluginTest\\', [realpath(__DIR__.'/../../plugins')]); |
31
|
|
|
$service = new RelativeNamespaceDiscovery($classLoader); |
32
|
|
|
$service->setRelativeNamespace('Robo\Plugin'); |
33
|
|
|
|
34
|
|
|
$actual = $service->getFile('\Robo\PluginTest\Robo\Plugin\Commands\FirstCustomCommands'); |
35
|
|
|
$this->assertStringEndsWith('FirstCustomCommands.php', $actual); |
36
|
|
|
|
37
|
|
|
$actual = $service->getFile('\Robo\PluginTest\Robo\Plugin\Commands\SecondCustomCommands'); |
38
|
|
|
$this->assertStringEndsWith('SecondCustomCommands.php', $actual); |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
$actual = $service->getFile('\Robo\PluginTest\Robo\Plugin\Commands\ThirdCustomCommand'); |
42
|
|
|
$this->assertStringEndsWith('ThirdCustomCommand.php', $actual); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @dataProvider convertPathToNamespaceData |
47
|
|
|
* |
48
|
|
|
* @param $path |
49
|
|
|
* @param $expected |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testConvertPathToNamespace($path, $expected) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$classLoader = new ClassLoader(); |
54
|
|
|
$discovery = new RelativeNamespaceDiscovery($classLoader); |
55
|
|
|
$actual = $this->callProtected($discovery, 'convertPathToNamespace', [$path]); |
56
|
|
|
$this->assertEquals($expected, $actual); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function convertPathToNamespaceData() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
['/A/B/C', 'A\B\C'], |
63
|
|
|
['A/B/C', 'A\B\C'], |
64
|
|
|
['A/B/C', 'A\B\C'], |
65
|
|
|
['A/B/C.php', 'A\B\C'], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @dataProvider convertNamespaceToPathData |
71
|
|
|
* |
72
|
|
|
* @param $namespace |
73
|
|
|
* @param $expected |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function testConvertNamespaceToPath($namespace, $expected) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$classLoader = new ClassLoader(); |
78
|
|
|
$discovery = new RelativeNamespaceDiscovery($classLoader); |
79
|
|
|
$actual = $this->callProtected($discovery, 'convertNamespaceToPath', [$namespace]); |
80
|
|
|
$this->assertEquals($expected, $actual); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function convertNamespaceToPathData() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
['A\B\C', '/A/B/C'], |
87
|
|
|
['\A\B\C\\', '/A/B/C'], |
88
|
|
|
['A\B\C\\', '/A/B/C'], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function callProtected($object, $method, $args = []) |
93
|
|
|
{ |
94
|
|
|
$r = new \ReflectionMethod($object, $method); |
95
|
|
|
$r->setAccessible(true); |
96
|
|
|
return $r->invokeArgs($object, $args); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.