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

RelativeNamespaceDiscoveryTest::testGetFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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('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('');
0 ignored issues
show
Unused Code introduced by
The call to RelativeNamespaceDiscovery::__construct() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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('');
0 ignored issues
show
Unused Code introduced by
The call to RelativeNamespaceDiscovery::__construct() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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