Completed
Pull Request — master (#671)
by Antonio
05:52
created

testConvertPathToNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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