Completed
Push — master ( 03a881...28de70 )
by Damian
09:16
created

NamespacedClassManifestTest::testFindClassOrInterfaceFromCandidateImports()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 131
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 83
nc 1
nop 0
dl 0
loc 131
rs 8.2857
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A NamespacedClassManifestTest::testGetInterfaces() 0 7 1
A NamespacedClassManifestTest::testGetImplementors() 0 10 1
A NamespacedClassManifestTest::testGetImplementorsOf() 0 15 2
A NamespacedClassManifestTest::testGetConfigs() 0 5 1
A NamespacedClassManifestTest::testGetModules() 0 8 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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