RelativeNamespaceDiscoveryTest::testGetFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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