Completed
Push — master ( 79c04e...cd88c2 )
by Greg
03:02
created

RelativeNamespaceDiscoveryTest::testGetFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

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