Completed
Pull Request — master (#671)
by Antonio
06:19
created

RelativeNamespaceDiscoveryTest::testGetClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    public function testGetClasses()
12
    {
13
        $service = $this->getServiceInstance('\Commands');
14
        $service->getClassLoader()->addPsr4('\\Robo\\', [realpath(__DIR__.'/../../src')]);
15
16
        $classes = $service->getClasses();
17
18
        $this->assertContains('\Robo\Commands\FirstCustomCommands', $classes);
19
        $this->assertContains('\Robo\Commands\SecondCustomCommands', $classes);
20
    }
21
22
    public function testGetFile()
23
    {
24
        $service = $this->getServiceInstance('\Commands');
25
        $service->getClassLoader()->addPsr4('\\Robo\\', [realpath(__DIR__.'/../../src')]);
26
27
        $actual = $service->getFile('\Robo\Commands\FirstCustomCommands');
28
        $this->assertStringEndsWith($this->normalizePath('tests/src/Commands/FirstCustomCommands.php'), $actual);
29
30
        $actual = $service->getFile('\Robo\Commands\SecondCustomCommands');
31
        $this->assertStringEndsWith($this->normalizePath('tests/src/Commands/SecondCustomCommands.php'), $actual);
32
    }
33
34
    /**
35
     * @dataProvider testConvertPathToNamespaceData
36
     *
37
     * @param $path
38
     * @param $expected
39
     */
40
    public function testConvertPathToNamespace($path, $expected)
41
    {
42
        $discovery = new RelativeNamespaceDiscovery();
43
        $actual = $this->callProtected($discovery, 'convertPathToNamespace', [$path]);
44
        $this->assertEquals($expected, $actual);
45
    }
46
47
    public function testConvertPathToNamespaceData()
48
    {
49
        return [
50
          ['/A/B/C', $this->normalizePath('A\B\C')],
51
          ['A/B/C', $this->normalizePath('A\B\C')],
52
          ['A/B/C', $this->normalizePath('A\B\C')],
53
          ['A/B/C.php', $this->normalizePath('A\B\C')],
54
        ];
55
    }
56
57
    /**
58
     * @dataProvider testConvertNamespaceToPathData
59
     *
60
     * @param $namespace
61
     * @param $expected
62
     */
63
    public function testConvertNamespaceToPath($namespace, $expected)
64
    {
65
        $discovery = new RelativeNamespaceDiscovery();
66
        $actual = $this->callProtected($discovery, 'convertNamespaceToPath', [$namespace]);
67
        $this->assertEquals($expected, $actual);
68
    }
69
70
    public function testConvertNamespaceToPathData()
71
    {
72
        return [
73
          ['A\B\C', $this->normalizePath('/A/B/C')],
74
          ['\A\B\C\\', $this->normalizePath('/A/B/C')],
75
          ['A\B\C\\', $this->normalizePath('/A/B/C')],
76
        ];
77
    }
78
79
    /**
80
     * @param $relativeNamespace
81
     *
82
     * @return \Robo\ClassDiscovery\RelativeNamespaceDiscovery
83
     */
84
    protected function getServiceInstance($relativeNamespace)
85
    {
86
        return (new RelativeNamespaceDiscovery())
87
            ->setRelativeNamespace($relativeNamespace)
88
            ->setClassLoader(new ClassLoader());
89
    }
90
91
    protected function callProtected($object, $method, $args = [])
92
    {
93
        $r = new \ReflectionMethod($object, $method);
94
        $r->setAccessible(true);
95
        return $r->invokeArgs($object, $args);
96
    }
97
98
    protected function normalizePath($path)
99
    {
100
        return str_replace('/', DIRECTORY_SEPARATOR, $path);
101
    }
102
}
103