Passed
Push — 4.6 ( 85242b...27c1c7 )
by Steve
08:07
created

ModuleManifestTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetResourcePathOnRoot() 0 7 1
A testGetComposerModule() 0 20 1
A testGetModules() 0 12 1
A testGetResourcePath() 0 9 1
A testGetResourcePathsInVendor() 0 9 1
A testGetLegacyModule() 0 8 1
A setUp() 0 7 1
A providerTestGetModuleByPath() 0 5 1
A testGetModuleByPath() 0 15 1
1
<?php
2
3
namespace SilverStripe\Core\Tests\Manifest;
4
5
use SilverStripe\Core\Manifest\ModuleLoader;
6
use SilverStripe\Core\Manifest\ModuleManifest;
7
use SilverStripe\Dev\SapphireTest;
8
9
class ModuleManifestTest extends SapphireTest
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $base;
15
16
    /**
17
     * @var ModuleManifest
18
     */
19
    protected $manifest;
20
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        $this->base = dirname(__FILE__) . '/fixtures/classmanifest';
26
        $this->manifest = new ModuleManifest($this->base);
27
        $this->manifest->init();
28
    }
29
30
    public function testGetModules()
31
    {
32
        $modules = $this->manifest->getModules();
33
        $this->assertEquals(
34
            [
35
                'module',
36
                'silverstripe/awesome-module',
37
                'silverstripe/modulec',
38
                'silverstripe/modulecbetter',
39
                'silverstripe/root-module',
40
            ],
41
            array_keys($modules)
42
        );
43
    }
44
45
    public function testGetLegacyModule()
46
    {
47
        $module = $this->manifest->getModule('module');
48
        $this->assertNotEmpty($module);
49
        $this->assertEquals('module', $module->getName());
50
        $this->assertEquals('module', $module->getShortName());
51
        $this->assertEquals('module', $module->getRelativePath());
52
        $this->assertEmpty($module->getComposerName());
53
    }
54
55
    public function testGetComposerModule()
56
    {
57
        // Get by installer-name (folder)
58
        $moduleByShortName = $this->manifest->getModule('moduleb');
59
        $this->assertNotEmpty($moduleByShortName);
60
61
        // Can also get this by full composer name
62
        $module = $this->manifest->getModule('silverstripe/awesome-module');
63
        $this->assertNotEmpty($module);
64
        $this->assertEquals($moduleByShortName->getPath(), $module->getPath());
65
66
        // correctly respects vendor
67
        $this->assertEmpty($this->manifest->getModule('wrongvendor/awesome-module'));
68
        $this->assertEmpty($this->manifest->getModule('wrongvendor/moduleb'));
69
70
        // Properties of module
71
        $this->assertEquals('silverstripe/awesome-module', $module->getName());
72
        $this->assertEquals('silverstripe/awesome-module', $module->getComposerName());
73
        $this->assertEquals('moduleb', $module->getShortName());
74
        $this->assertEquals('moduleb', $module->getRelativePath());
75
    }
76
77
    public function testGetResourcePath()
78
    {
79
        // Root module
80
        $moduleb = $this->manifest->getModule('moduleb');
81
        $this->assertTrue($moduleb->getResource('composer.json')->exists());
82
        $this->assertFalse($moduleb->getResource('package.json')->exists());
83
        $this->assertEquals(
84
            'moduleb/composer.json',
85
            $moduleb->getResource('composer.json')->getRelativePath()
86
        );
87
    }
88
89
    public function testGetResourcePathsInVendor()
90
    {
91
        // Vendor module
92
        $modulec = $this->manifest->getModule('silverstripe/modulec');
93
        $this->assertTrue($modulec->getResource('composer.json')->exists());
94
        $this->assertFalse($modulec->getResource('package.json')->exists());
95
        $this->assertEquals(
96
            'vendor/silverstripe/modulec/composer.json',
97
            $modulec->getResource('composer.json')->getRelativePath()
98
        );
99
    }
100
101
    public function testGetResourcePathOnRoot()
102
    {
103
        $module = $this->manifest->getModule('silverstripe/root-module');
104
        $this->assertTrue($module->getResource('composer.json')->exists());
105
        $this->assertEquals(
106
            'composer.json',
107
            $module->getResource('composer.json')->getRelativePath()
108
        );
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function providerTestGetModuleByPath()
115
    {
116
        return [
117
            ['vendor/silverstripe/modulec/code/VendorClassA.php', 'silverstripe/modulec'],
118
            ['vendor/silverstripe/modulecbetter/code/VendorClassX.php', 'silverstripe/modulecbetter'],
119
        ];
120
    }
121
122
    /**
123
     * @dataProvider providerTestGetModuleByPath
124
     * @param string $path
125
     * @param string $expectedModuleName
126
     */
127
    public function testGetModuleByPath($path, $expectedModuleName)
128
    {
129
        // important - load the manifest that we are working with to the ModuleLoader
130
        ModuleLoader::inst()->pushManifest($this->manifest);
131
132
        // work out variables
133
        $path = $this->base . '/' . $path;
134
        $module = $this->manifest->getModuleByPath($path);
135
        $actualModuleName = $module->getName();
136
137
        // it is testing time!
138
        $this->assertEquals($expectedModuleName, $actualModuleName);
139
140
        // bye bye bogus manifest
141
        ModuleLoader::inst()->popManifest();
142
    }
143
}
144