Passed
Push — pulls/manymanylist-add-callbac... ( 77b6c5...aed064 )
by Sam
07:52
created

VersionProviderTest::testGetVersionRecipeCmsCore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 33
rs 9.52
1
<?php
2
3
namespace SilverStripe\Core\Tests\Manifest;
4
5
use SebastianBergmann\Version;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Core\Manifest\VersionProvider;
8
use SilverStripe\Dev\SapphireTest;
9
10
class VersionProviderTest extends SapphireTest
11
{
12
13
    /**
14
     * @var VersionProvider
15
     */
16
    protected $provider;
17
18
    public function getMockProvider($composerLockPath = '')
19
    {
20
        if ($composerLockPath == '') {
21
            // composer.lock file without silverstripe/recipe-core or silverstripe/recipe-cms
22
            $composerLockPath = __DIR__ . '/fixtures/VersionProviderTest/composer.no-recipe.testlock';
23
        }
24
        /** @var VersionProvider $provider */
25
        $provider = $this->getMockBuilder(VersionProvider::class)
26
            ->setMethods(['getComposerLockPath'])
27
            ->getMock();
28
        $provider->method('getComposerLockPath')->willReturn($composerLockPath);
0 ignored issues
show
Bug introduced by
The method method() does not exist on SilverStripe\Core\Manifest\VersionProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $provider->/** @scrutinizer ignore-call */ 
29
                   method('getComposerLockPath')->willReturn($composerLockPath);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
        return $provider;
30
    }
31
32
    public function testGetModules()
33
    {
34
        Config::modify()->set(VersionProvider::class, 'modules', [
35
            'silverstripe/mypackage' => 'My Package',
36
            'silverstripe/somepackage' => 'Some Package',
37
            'silverstripe/another' => 'Another',
38
            'cwp/cwp-something' => 'CWP something',
39
        ]);
40
        $result = $this->getMockProvider()->getModules();
41
        $this->assertArrayHasKey('silverstripe/mypackage', $result);
42
        $this->assertArrayHasKey('silverstripe/somepackage', $result);
43
        $this->assertArrayHasKey('silverstripe/another', $result);
44
        $this->assertArrayHasKey('cwp/cwp-something', $result);
45
    }
46
47
    public function testGetModulesEmpty()
48
    {
49
        Config::modify()->set(VersionProvider::class, 'modules', []);
50
        $this->assertEquals(
51
            ['silverstripe/framework' => 'Framework'],
52
            $this->getMockProvider()->getModules()
53
        );
54
    }
55
56
    public function testGetModulesNone()
57
    {
58
        Config::modify()->remove(VersionProvider::class, 'modules');
59
        $this->assertEquals(
60
            ['silverstripe/framework' => 'Framework'],
61
            $this->getMockProvider()->getModules()
62
        );
63
    }
64
65
    public function testGetModuleVersionFromComposer()
66
    {
67
        Config::modify()->set(VersionProvider::class, 'modules', [
68
            'silverstripe/siteconfig' => 'SiteConfig',
69
            'silverstripe/framework' => 'Framework',
70
        ]);
71
72
        $result = $this->getMockProvider()->getModules(['silverstripe/framework']);
0 ignored issues
show
Unused Code introduced by
The call to SilverStripe\Core\Manife...nProvider::getModules() has too many arguments starting with array('silverstripe/framework'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $result = $this->getMockProvider()->/** @scrutinizer ignore-call */ getModules(['silverstripe/framework']);

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. Please note the @ignore annotation hint above.

Loading history...
73
        $this->assertArrayHasKey('silverstripe/framework', $result);
74
        $this->assertNotEmpty($result['silverstripe/framework']);
75
    }
76
77
    public function testGetVersion()
78
    {
79
        Config::modify()->set(VersionProvider::class, 'modules', [
80
            'silverstripe/siteconfig' => 'SiteConfig',
81
            'silverstripe/framework' => 'Framework'
82
        ]);
83
        $result = $this->getMockProvider()->getVersion();
84
        $this->assertNotContains('SiteConfig: ', $result);
85
        $this->assertContains('Framework: ', $result);
86
        $this->assertNotContains(', ', $result);
87
    }
88
89
    public function testGetVersionNoRecipe()
90
    {
91
        // composer.lock file without silverstripe/recipe-core or silverstripe/recipe-cms
92
        $provider = $this->getMockProvider(__DIR__ . '/fixtures/VersionProviderTest/composer.no-recipe.testlock');
93
94
        Config::modify()->set(VersionProvider::class, 'modules', []);
95
        $result = $provider->getVersion();
96
        $this->assertContains('Framework: 1.2.3', $result);
97
98
        Config::modify()->set(VersionProvider::class, 'modules', [
99
            'silverstripe/framework' => 'Framework',
100
            'silverstripe/recipe-core' => 'Core Recipe',
101
            'silverstripe/cms' => 'CMS',
102
            'silverstripe/recipe-cms' => 'CMS Recipe',
103
        ]);
104
        $result = $provider->getVersion();
105
        $this->assertNotContains('Framework: 1.2.3', $result);
106
        $this->assertContains('CMS: 4.5.6', $result);
107
        $this->assertNotContains('Core Recipe: 7.7.7', $result);
108
        $this->assertNotContains('CMS Recipe: 8.8.8', $result);
109
    }
110
111
    public function testGetVersionRecipeCore()
112
    {
113
        // composer.lock file with silverstripe/recipe-core but not silverstripe/recipe-cms
114
        $provider = $this->getMockProvider(__DIR__ . '/fixtures/VersionProviderTest/composer.recipe-core.testlock');
115
        Config::modify()->set(VersionProvider::class, 'modules', [
116
            'silverstripe/framework' => 'Framework',
117
            'silverstripe/recipe-core' => 'Core Recipe',
118
            'silverstripe/cms' => 'CMS',
119
            'silverstripe/recipe-cms' => 'CMS Recipe',
120
        ]);
121
        $result = $provider->getVersion();
122
        $this->assertNotContains('Framework: 1.2.3', $result);
123
        $this->assertNotContains('Core Recipe: 7.7.7', $result);
124
        $this->assertContains('CMS: 4.5.6', $result);
125
        $this->assertNotContains('CMS Recipe: 8.8.8', $result);
126
    }
127
128
    public function testGetVersionRecipeCmsCore()
129
    {
130
        // composer.lock file with silverstripe/recipe-core and silverstripe/recipe-cms
131
        $path = __DIR__ . '/fixtures/VersionProviderTest/composer.recipe-cms-core-and-cwpcore.testlock';
132
        $provider = $this->getMockProvider($path);
133
134
        Config::modify()->set(VersionProvider::class, 'modules', [
135
            'silverstripe/framework' => 'Framework',
136
            'silverstripe/recipe-core' => 'Core Recipe',
137
            'silverstripe/cms' => 'CMS',
138
            'silverstripe/recipe-cms' => 'CMS Recipe',
139
        ]);
140
        $result = $provider->getVersion();
141
142
        $this->assertNotContains('Framework: 1.2.3', $result);
143
        $this->assertNotContains('CMS: 4.5.6', $result);
144
        $this->assertNotContains('Core Recipe: 7.7.7', $result);
145
        $this->assertContains('CMS Recipe: 8.8.8', $result);
146
        $this->assertNotContains('CWP: 9.9.9', $result);
147
148
        Config::modify()->set(VersionProvider::class, 'modules', [
149
            'silverstripe/framework' => 'Framework',
150
            'silverstripe/recipe-core' => 'Core Recipe',
151
            'silverstripe/cms' => 'CMS',
152
            'silverstripe/recipe-cms' => 'CMS Recipe',
153
            'cwp/cwp-core' => 'CWP',
154
        ]);
155
        $result = $provider->getVersion();
156
        $this->assertNotContains('Framework: 1.2.3', $result);
157
        $this->assertNotContains('CMS: 4.5.6', $result);
158
        $this->assertNotContains('Core Recipe: 7.7.7', $result);
159
        $this->assertContains('CMS Recipe:', $result);
160
        $this->assertContains('CWP: 9.9.9', $result);
161
    }
162
163
    public function testGetModulesFromComposerLock()
164
    {
165
        $mock = $this->getMockBuilder(VersionProvider::class)
166
            ->setMethods(['getComposerLock'])
167
            ->getMock();
168
169
        $mock->expects($this->exactly(1))
170
            ->method('getComposerLock')
171
            ->will($this->returnValue([
172
                'packages' => [
173
                    [
174
                        'name' => 'silverstripe/somepackage',
175
                        'version' => '1.2.3'
176
                    ],
177
                    [
178
                        'name' => 'silverstripe/another',
179
                        'version' => '2.3.4'
180
                    ]
181
                ]
182
            ]));
183
184
        Config::modify()->set(VersionProvider::class, 'modules', [
185
            'silverstripe/somepackage' => 'Some Package'
186
        ]);
187
188
        $result = $mock->getVersion();
189
        $this->assertContains('Some Package: 1.2.3', $result);
190
    }
191
}
192