1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Robo\ClassDiscovery\RelativeNamespaceDiscovery; |
4
|
|
|
use Composer\Autoload\ClassLoader; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class RelativeNamespaceDiscoveryTest |
8
|
|
|
*/ |
9
|
|
|
class RelativeNamespaceDiscoveryTest extends \Codeception\Test\Unit |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
public function testGetClasses() |
12
|
|
|
{ |
13
|
|
|
$classLoader = new ClassLoader(); |
14
|
|
|
$classLoader->addPsr4('\\Robo\\', [realpath(__DIR__.'/../../src')]); |
15
|
|
|
|
16
|
|
|
$discovery = new RelativeNamespaceDiscovery('\Commands'); |
17
|
|
|
$discovery->setClassLoader($classLoader); |
18
|
|
|
|
19
|
|
|
$classes = $discovery->getClasses(); |
20
|
|
|
|
21
|
|
|
$this->assertContains('\Robo\FirstCustomCommands', $classes); |
22
|
|
|
$this->assertContains('\Robo\SecondCustomCommands', $classes); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @dataProvider testConvertPathToNamespaceData |
27
|
|
|
* |
28
|
|
|
* @param $path |
29
|
|
|
* @param $expected |
30
|
|
|
*/ |
31
|
|
|
public function testConvertPathToNamespace($path, $expected) |
32
|
|
|
{ |
33
|
|
|
$discovery = new RelativeNamespaceDiscovery(''); |
34
|
|
|
$actual = $this->callProtected($discovery, 'convertPathToNamespace', [$path]); |
35
|
|
|
$this->assertEquals($expected, $actual); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testConvertPathToNamespaceData() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
['/A/B/C', 'A\B\C'], |
42
|
|
|
['A/B/C', 'A\B\C'], |
43
|
|
|
['A/B/C', 'A\B\C'], |
44
|
|
|
['A/B/C.php', 'A\B\C'], |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider testConvertNamespaceToPathData |
50
|
|
|
* |
51
|
|
|
* @param $namespace |
52
|
|
|
* @param $expected |
53
|
|
|
*/ |
54
|
|
|
public function testConvertNamespaceToPath($namespace, $expected) |
55
|
|
|
{ |
56
|
|
|
$discovery = new RelativeNamespaceDiscovery(''); |
57
|
|
|
$actual = $this->callProtected($discovery, 'convertNamespaceToPath', [$namespace]); |
58
|
|
|
$this->assertEquals($expected, $actual); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testConvertNamespaceToPathData() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
['A\B\C', '/A/B/C'], |
65
|
|
|
['\A\B\C\\', '/A/B/C'], |
66
|
|
|
['A\B\C\\', '/A/B/C'], |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
function callProtected($object, $method, $args = []) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$r = new \ReflectionMethod($object, $method); |
73
|
|
|
$r->setAccessible(true); |
74
|
|
|
return $r->invokeArgs($object, $args); |
75
|
|
|
} |
76
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.