Completed
Pull Request — master (#671)
by Antonio
02:53
created

RelativeNamespaceDiscoveryTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 2
dl 0
loc 90
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetClasses() 0 10 1
A testGetFile() 0 11 1
A testConvertPathToNamespace() 0 6 1
A testConvertPathToNamespaceData() 0 9 1
A testConvertNamespaceToPath() 0 6 1
A testConvertNamespaceToPathData() 0 8 1
A getServiceInstance() 0 7 1
A callProtected() 0 6 1
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('tests/src/Commands/FirstCustomCommands.php', $actual);
29
30
        $actual = $service->getFile('\Robo\Commands\SecondCustomCommands');
31
        $this->assertStringEndsWith('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', 'A\B\C'],
51
          ['A/B/C', 'A\B\C'],
52
          ['A/B/C', 'A\B\C'],
53
          ['A/B/C.php', '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', '/A/B/C'],
74
          ['\A\B\C\\', '/A/B/C'],
75
          ['A\B\C\\', '/A/B/C'],
76
        ];
77
    }
78
79
    /**
80
     * @param $relativeNamespace
81
     *
82
     * @return \Robo\ClassDiscovery\RelativeNamespaceDiscovery
83
     */
84
    protected function getServiceInstance($relativeNamespace)
85
    {
86
        $classLoader = new ClassLoader();
87
        $discovery = new RelativeNamespaceDiscovery($relativeNamespace);
88
        $discovery->setClassLoader($classLoader);
89
        return $discovery;
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
}