Completed
Push — master ( 1efa22...cfa6a3 )
by Sam
22s
created

testManifestWarnsAboutDuplicateClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Tests\Manifest;
4
5
use Exception;
6
use SilverStripe\Core\Manifest\ClassManifest;
7
use SilverStripe\Dev\SapphireTest;
8
9
/**
10
 * Tests for the {@link ClassManifest} class.
11
 */
12
class ClassManifestTest extends SapphireTest
13
{
14
15
    /**
16
     * @var string
17
     */
18
    protected $base;
19
20
    /**
21
     * @var ClassManifest
22
     */
23
    protected $manifest;
24
25
    /**
26
     * @var ClassManifest
27
     */
28
    protected $manifestTests;
29
30
    public function setUp()
31
    {
32
        parent::setUp();
33
34
        $this->base = dirname(__FILE__) . '/fixtures/classmanifest';
35
        $this->manifest      = new ClassManifest($this->base, false);
36
        $this->manifestTests = new ClassManifest($this->base, true);
37
    }
38
39
    public function testGetItemPath()
40
    {
41
        $expect = array(
42
            'CLASSA'     => 'module/classes/ClassA.php',
43
            'ClassA'     => 'module/classes/ClassA.php',
44
            'classa'     => 'module/classes/ClassA.php',
45
            'INTERFACEA' => 'module/interfaces/InterfaceA.php',
46
            'InterfaceA' => 'module/interfaces/InterfaceA.php',
47
            'interfacea' => 'module/interfaces/InterfaceA.php',
48
            'TestTraitA' => 'module/traits/TestTraitA.php',
49
            'TestNamespace\Testing\TestTraitB' => 'module/traits/TestTraitB.php'
50
        );
51
52
        foreach ($expect as $name => $path) {
53
            $this->assertEquals("{$this->base}/$path", $this->manifest->getItemPath($name));
54
        }
55
    }
56
57
    public function testGetClasses()
58
    {
59
        $expect = array(
60
            'classa'                   => "{$this->base}/module/classes/ClassA.php",
61
            'classb'                   => "{$this->base}/module/classes/ClassB.php",
62
            'classc'                   => "{$this->base}/module/classes/ClassC.php",
63
            'classd'                   => "{$this->base}/module/classes/ClassD.php",
64
            'classe'                   => "{$this->base}/module/classes/ClassE.php",
65
        );
66
        $this->assertEquals($expect, $this->manifest->getClasses());
67
    }
68
69
    public function testGetClassNames()
70
    {
71
        $this->assertEquals(
72
            ['classa', 'classb', 'classc', 'classd', 'classe'],
73
            $this->manifest->getClassNames()
74
        );
75
    }
76
77
    public function testGetTraitNames()
78
    {
79
        $this->assertEquals(
80
            array('testtraita', 'testnamespace\testing\testtraitb'),
81
            $this->manifest->getTraitNames()
82
        );
83
    }
84
85
    public function testGetDescendants()
86
    {
87
        $expect = array(
88
            'classa' => array('ClassC', 'ClassD'),
89
            'classc' => array('ClassD')
90
        );
91
        $this->assertEquals($expect, $this->manifest->getDescendants());
92
    }
93
94
    public function testGetDescendantsOf()
95
    {
96
        $expect = array(
97
            'CLASSA' => array('ClassC', 'ClassD'),
98
            'classa' => array('ClassC', 'ClassD'),
99
            'CLASSC' => array('ClassD'),
100
            'classc' => array('ClassD')
101
        );
102
103
        foreach ($expect as $class => $desc) {
104
            $this->assertEquals($desc, $this->manifest->getDescendantsOf($class));
105
        }
106
    }
107
108
    public function testGetInterfaces()
109
    {
110
        $expect = array(
111
            'interfacea' => "{$this->base}/module/interfaces/InterfaceA.php",
112
            'interfaceb' => "{$this->base}/module/interfaces/InterfaceB.php"
113
        );
114
        $this->assertEquals($expect, $this->manifest->getInterfaces());
115
    }
116
117
    public function testGetImplementors()
118
    {
119
        $expect = array(
120
            'interfacea' => array('ClassB'),
121
            'interfaceb' => array('ClassC')
122
        );
123
        $this->assertEquals($expect, $this->manifest->getImplementors());
124
    }
125
126
    public function testGetImplementorsOf()
127
    {
128
        $expect = array(
129
            'INTERFACEA' => array('ClassB'),
130
            'interfacea' => array('ClassB'),
131
            'INTERFACEB' => array('ClassC'),
132
            'interfaceb' => array('ClassC')
133
        );
134
135
        foreach ($expect as $interface => $impl) {
136
            $this->assertEquals($impl, $this->manifest->getImplementorsOf($interface));
137
        }
138
    }
139
140
    public function testTestManifestIncludesTestClasses()
141
    {
142
        $this->assertNotContains('testclassa', array_keys($this->manifest->getClasses()));
143
        $this->assertContains('testclassa', array_keys($this->manifestTests->getClasses()));
144
    }
145
146
    public function testManifestExcludeFilesPrefixedWithUnderscore()
147
    {
148
        $this->assertNotContains('ignore', array_keys($this->manifest->getClasses()));
149
    }
150
151
    /**
152
     * Assert that ClassManifest throws an exception when it encounters two files
153
     * which contain classes with the same name
154
     */
155
    public function testManifestWarnsAboutDuplicateClasses()
156
    {
157
        $this->setExpectedException(Exception::class);
158
        new ClassManifest(dirname(__FILE__) . '/fixtures/classmanifest_duplicates', false);
159
    }
160
}
161