Completed
Pull Request — master (#6706)
by Damian
09:02
created

NamespacedClassManifestTest::testGetModules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Tests\Manifest;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\Core\Manifest\ClassManifest;
7
use SilverStripe\Core\Manifest\ClassLoader;
8
use SilverStripe\Dev\SapphireTest;
9
use ReflectionMethod;
10
11
/**
12
 * Tests for the {@link ClassManifest} class.
13
 */
14
class NamespacedClassManifestTest extends SapphireTest
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $base;
20
21
    /**
22
     * @var ClassManifest
23
     */
24
    protected $manifest;
25
26
    public function setUp()
27
    {
28
        parent::setUp();
29
30
        $this->base = dirname(__FILE__) . '/fixtures/namespaced_classmanifest';
31
        $this->manifest = new ClassManifest($this->base, false);
32
        ClassLoader::instance()->pushManifest($this->manifest, false);
33
    }
34
35
    public function tearDown()
36
    {
37
        parent::tearDown();
38
        ClassLoader::instance()->popManifest();
39
    }
40
41
    public function testClassInfoIsCorrect()
42
    {
43
        $this->assertContains('SilverStripe\Framework\Tests\ClassI', ClassInfo::implementorsOf('SilverStripe\\Security\\PermissionProvider'));
44
45
        // because we're using a nested manifest we have to "coalesce" the descendants again to correctly populate the
46
        // descendants of the core classes we want to test against - this is a limitation of the test manifest not
47
        // including all core classes
48
        $method = new ReflectionMethod($this->manifest, 'coalesceDescendants');
49
        $method->setAccessible(true);
50
        $method->invoke($this->manifest, 'SilverStripe\\Admin\\ModelAdmin');
51
52
        $this->assertContains('SilverStripe\Framework\Tests\ClassI', ClassInfo::subclassesFor('SilverStripe\\Admin\\ModelAdmin'));
53
    }
54
55
    public function testGetItemPath()
56
    {
57
        $expect = array(
58
            'SILVERSTRIPE\TEST\CLASSA'     => 'module/classes/ClassA.php',
59
            'Silverstripe\Test\ClassA'     => 'module/classes/ClassA.php',
60
            'silverstripe\test\classa'     => 'module/classes/ClassA.php',
61
            'SILVERSTRIPE\TEST\INTERFACEA' => 'module/interfaces/InterfaceA.php',
62
            'Silverstripe\Test\InterfaceA' => 'module/interfaces/InterfaceA.php',
63
            'silverstripe\test\interfacea' => 'module/interfaces/InterfaceA.php'
64
        );
65
66
        foreach ($expect as $name => $path) {
67
            $this->assertEquals("{$this->base}/$path", $this->manifest->getItemPath($name));
68
        }
69
    }
70
71
    public function testGetClasses()
72
    {
73
        $expect = array(
74
            'silverstripe\test\classa' => "{$this->base}/module/classes/ClassA.php",
75
            'silverstripe\test\classb' => "{$this->base}/module/classes/ClassB.php",
76
            'silverstripe\test\classc' => "{$this->base}/module/classes/ClassC.php",
77
            'silverstripe\test\classd' => "{$this->base}/module/classes/ClassD.php",
78
            'silverstripe\test\classe' => "{$this->base}/module/classes/ClassE.php",
79
            'silverstripe\test\classf' => "{$this->base}/module/classes/ClassF.php",
80
            'silverstripe\test\classg' => "{$this->base}/module/classes/ClassG.php",
81
            'silverstripe\test\classh' => "{$this->base}/module/classes/ClassH.php",
82
            'silverstripe\framework\tests\classi' => "{$this->base}/module/classes/ClassI.php",
83
        );
84
85
        $this->assertEquals($expect, $this->manifest->getClasses());
86
    }
87
88
    public function testGetClassNames()
89
    {
90
        $this->assertEquals(
91
            array('silverstripe\test\classa',
92
                'silverstripe\test\classb', 'silverstripe\test\classc', 'silverstripe\test\classd',
93
                'silverstripe\test\classe', 'silverstripe\test\classf', 'silverstripe\test\classg',
94
                'silverstripe\test\classh', 'silverstripe\framework\tests\classi'),
95
            $this->manifest->getClassNames()
96
        );
97
    }
98
99
    public function testGetDescendants()
100
    {
101
        $expect = array(
102
            'silverstripe\test\classa' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
103
        );
104
105
        $this->assertEquals($expect, $this->manifest->getDescendants());
106
    }
107
108
    public function testGetDescendantsOf()
109
    {
110
        $expect = array(
111
            'SILVERSTRIPE\TEST\CLASSA' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
112
            'silverstripe\test\classa' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
113
        );
114
115
        foreach ($expect as $class => $desc) {
116
            $this->assertEquals($desc, $this->manifest->getDescendantsOf($class));
117
        }
118
    }
119
120
    public function testGetInterfaces()
121
    {
122
        $expect = array(
123
            'silverstripe\test\interfacea' => "{$this->base}/module/interfaces/InterfaceA.php",
124
        );
125
        $this->assertEquals($expect, $this->manifest->getInterfaces());
126
    }
127
128
    public function testGetImplementors()
129
    {
130
        $expect = array(
131
            'silverstripe\test\interfacea' => array('silverstripe\test\ClassE'),
132
            'interfacea' => array('silverstripe\test\ClassF'),
133
            'silverstripe\test\subtest\interfacea' => array('silverstripe\test\ClassG'),
134
            'silverstripe\security\permissionprovider' => array('SilverStripe\Framework\Tests\ClassI'),
135
        );
136
        $this->assertEquals($expect, $this->manifest->getImplementors());
137
    }
138
139
    public function testGetImplementorsOf()
140
    {
141
        $expect = array(
142
            'SILVERSTRIPE\TEST\INTERFACEA' => array('silverstripe\test\ClassE'),
143
            'silverstripe\test\interfacea' => array('silverstripe\test\ClassE'),
144
            'INTERFACEA' => array('silverstripe\test\ClassF'),
145
            'interfacea' => array('silverstripe\test\ClassF'),
146
            'SILVERSTRIPE\TEST\SUBTEST\INTERFACEA' => array('silverstripe\test\ClassG'),
147
            'silverstripe\test\subtest\interfacea' => array('silverstripe\test\ClassG'),
148
        );
149
150
        foreach ($expect as $interface => $impl) {
151
            $this->assertEquals($impl, $this->manifest->getImplementorsOf($interface));
152
        }
153
    }
154
}
155