Completed
Pull Request — master (#63)
by
unknown
01:21
created

VersionsCheckTest::testRootOnlyPackage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
3
namespace SLLH\ComposerVersionsCheck\Tests;
4
5
use Composer\Package\Link;
6
use Composer\Package\Package;
7
use Composer\Package\RootPackage;
8
use Composer\Repository\ArrayRepository;
9
use Composer\Repository\WritableArrayRepository;
10
use SLLH\ComposerVersionsCheck\VersionsCheck;
11
12
/**
13
 * @author Sullivan Senechal <[email protected]>
14
 */
15
class VersionsCheckTest extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @var ArrayRepository
19
     */
20
    private $distRepository;
21
22
    /**
23
     * @var WritableArrayRepository
24
     */
25
    private $localRepository;
26
27
    /**
28
     * @var RootPackage
29
     */
30
    private $rootPackage;
31
32
    /**
33
     * @var VersionsCheck
34
     */
35
    private $versionsCheck;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function setUp()
41
    {
42
        $this->distRepository = new ArrayRepository();
43
        $this->localRepository = new WritableArrayRepository();
44
45
        $this->rootPackage = new RootPackage('my/project', '1.0.0', '1.0.0');
46
        $this->versionsCheck = new VersionsCheck();
47
    }
48
49
    /**
50
     * @dataProvider getOutdatedDetectionTestData
51
     *
52
     * @param string $actualVersion
53
     * @param string $higherVersion
54
     * @param bool   $shouldBeUpdated
55
     * @param bool   $preferStable
56
     */
57
    public function testOutdatedDetection($actualVersion, $higherVersion, $shouldBeUpdated, $preferStable = false)
58
    {
59
        $this->distRepository->addPackage(new Package('foo/bar', $higherVersion, $higherVersion));
60
        $this->localRepository->addPackage(new Package('foo/bar', $actualVersion, $actualVersion));
61
62
        $this->rootPackage->setPreferStable($preferStable);
63
        $this->checkPackages();
64
65
        // Must have one outdatedPackage if this should be updated
66
        if (true === $shouldBeUpdated) {
67
            $this->assertAttributeCount(1, 'outdatedPackages', $this->versionsCheck);
68
            $this->assertSame(sprintf(<<<'EOF'
69
<warning>1 package is not up to date:</warning>
70
71
  - <info>foo/bar</info> (<comment>%s</comment>) latest is <comment>%s</comment>
72
73
74
EOF
75
                , $actualVersion, $higherVersion), $this->versionsCheck->getOutput());
76
        } else {
77
            $this->assertAttributeCount(0, 'outdatedPackages', $this->versionsCheck);
78
            $this->assertSame("<info>All packages are up to date.</info>\n", $this->versionsCheck->getOutput());
79
        }
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function getOutdatedDetectionTestData()
86
    {
87
        return array(
88
            array('1.0.0', '1.0.0', false),
89
            array('1.0.0', '1.0.0', false),
90
            array('1.0.0', '2.0.0', true),
91
            array('2.0.0', '1.0.0', false),
92
            array('2.0.0', '2.0.0', false),
93
            array('2.0.0', '1.9.9', false),
94
            array('1.9.9', '2.0.0', true),
95
            array('2.0.0', '2.1.0-alpha1', true),        // Should be true because no stable package available
96
            array('2.0.0', '2.1.0-alpha1', false, true), // Should be false because of minimum stability
97
            array('2.0.0-alpha1', '2.1.0-alpha2', true),
98
        );
99
    }
100
101
    /**
102
     * @dataProvider getMultiplePackagesComparisonTestsData
103
     *
104
     * @param bool $preferStable
105
     * @param int  $outdatedPackagesCount
106
     */
107 View Code Duplication
    public function testMultiplePackagesComparison(array $packagesData, $preferStable, $outdatedPackagesCount)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $this->rootPackage->setMinimumStability('dev');
110
        $this->rootPackage->setPreferStable($preferStable);
111
112
        $shouldBeUpdatedOutput = $this->addPackagesAndGetShouldBeUpdatedOutput($packagesData);
113
114
        $this->checkPackages();
115
116
        $this->assertSame(sprintf(<<<'EOF'
117
<warning>%d packages are not up to date:</warning>
118
119
%s
120
121
122
EOF
123
        , $outdatedPackagesCount, implode("\n\n", $shouldBeUpdatedOutput)), $this->versionsCheck->getOutput());
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function getMultiplePackagesComparisonTestsData()
130
    {
131
        return array(
132
            array(array(
133
                'foo/bar' => array('1.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
134
                'some/package' => array('1.0.4', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
135
                'vendor/package-1' => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
136
                'vendor/up-to-date' => array('9.9.0', array('8.0.0', '9.9.0', '1.0-dev'), false),
137
                'vendor/dev-master' => array('9.9.0', array('8.0.0', '9.9.0', '10.0-dev'), '10.0-dev'),
138
                'vendor/package-2' => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
139
                'vendor/package-3' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
140
                'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
141
            ), false, 6),
142
            array(array(
143
                'foo/bar' => array('1.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
144
                'some/package' => array('1.0.4', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
145
                'vendor/package-1' => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
146
                'vendor/up-to-date' => array('9.9.0', array('8.0.0', '9.9.0', 'dev-master'), false),
147
                'vendor/package-2' => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
148
                'vendor/package-3' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
149
                'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
150
            ), true, 3),
151
        );
152
    }
153
154
    public function testOutdatedWithLinks()
155
    {
156
        $this->distRepository->addPackage(new Package('foo/bar', '2.0', '2.0'));
157
        $this->localRepository->addPackage(new Package('foo/bar', '1.1', '1.1'));
158
159
        $linkedPackage = new Package('dummy/link', '1.0', '1.0');
160
        $linkedPackage->setRequires(array(new Link('dummy/link', 'foo/bar', null, '', '1.*')));
161
        $this->localRepository->addPackage($linkedPackage);
162
163
        // To test root package detection
164
        $this->localRepository->addPackage($this->rootPackage);
165
166
        $this->checkPackages();
167
168
        // Must have one outdatedPackage if this should be updated
169
        $this->assertAttributeCount(1, 'outdatedPackages', $this->versionsCheck);
170
        $this->assertSame(<<<'EOF'
171
<warning>1 package is not up to date:</warning>
172
173
  - <info>foo/bar</info> (<comment>1.1</comment>) latest is <comment>2.0</comment>
174
    Required by <info>dummy/link</info> (<comment>1.*</comment>)
175
176
177
EOF
178
            , $this->versionsCheck->getOutput());
179
        // Test with disabled show-links option
180
        $this->assertSame(<<<'EOF'
181
<warning>1 package is not up to date:</warning>
182
183
  - <info>foo/bar</info> (<comment>1.1</comment>) latest is <comment>2.0</comment>
184
185
186
EOF
187
            , $this->versionsCheck->getOutput(false));
188
    }
189
190
    /**
191
     * @dataProvider getRootOnlyPackageTestsData
192
     */
193 View Code Duplication
    public function testRootOnlyPackage(array $packagesData, array $packagesRequired, array $packagesDevRequired, $rootOnly, $outdatedPackagesCount)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        $shouldBeUpdatedOutput = $this->addPackagesAndGetShouldBeUpdatedOutput($packagesData);
196
        $this->rootPackage->setRequires($packagesRequired);
197
        $this->rootPackage->setDevRequires($packagesDevRequired);
198
199
        $this->checkPackages($rootOnly);
200
201
        $this->assertSame(sprintf(<<<'EOF'
202
<warning>%d packages are not up to date:</warning>
203
204
%s
205
206
207
EOF
208
            , $outdatedPackagesCount, implode("\n\n", $shouldBeUpdatedOutput)), $this->versionsCheck->getOutput());
209
    }
210
211
    /**
212
     * @return array
213
     */
214
    public function getRootOnlyPackageTestsData()
215
    {
216
        return array(
217
            array(array(
218
                'foo/bar' => array('1.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
219
                'some/package' => array('1.0.4', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
220
                'vendor/package-1' => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
221
                'vendor/up-to-date' => array('9.9.0', array('8.0.0', '9.9.0', '1.0-dev'), false),
222
                'vendor/dev-master' => array('9.9.0', array('8.0.0', '9.9.0', '10.0-dev'), false),
223
                'vendor/package-2' => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
224
                'vendor/package-3' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
225
                'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
226
            ), array(
227
                'foo/bar' => new Link('foo/bar', 'foo/bar'),
228
            ), array(
229
                'vendor/package-2' => new Link('vendor/package-2', 'vendor/package-2'),
230
            ), true, 2),
231
            array(array(
232
                'foo/bar' => array('1.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
233
                'some/package' => array('1.0.4', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
234
                'vendor/package-1' => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
235
                'vendor/up-to-date' => array('9.9.0', array('8.0.0', '9.9.0', '1.0-dev'), false),
236
                'vendor/dev-master' => array('9.9.0', array('8.0.0', '9.9.0', '10.0-dev'), '10.0-dev'),
237
                'vendor/package-2' => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
238
                'vendor/package-3' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
239
                'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
240
            ), array(
241
                'foo/bar' => new Link('foo/bar', 'foo/bar'),
242
            ), array(
243
                'vendor/package-2' => new Link('vendor/package-2', 'vendor/package-2'),
244
            ), false, 6),
245
        );
246
    }
247
248
    private function addPackagesAndGetShouldBeUpdatedOutput(array $packagesData)
249
    {
250
        $shouldBeUpdatedOutput = array();
251
252
        foreach ($packagesData as $name => $packageData) {
253
            list($actualVersion, $availableVersions, $expectedVersion) = $packageData;
254
            $this->localRepository->addPackage(new Package($name, $actualVersion, $actualVersion));
255
            foreach ($availableVersions as $availableVersion) {
256
                $this->distRepository->addPackage(new Package($name, $availableVersion, $availableVersion));
257
            }
258
259
            if (false !== $expectedVersion) {
260
                $shouldBeUpdatedOutput[] = sprintf(
261
                    '  - <info>%s</info> (<comment>%s</comment>) latest is <comment>%s</comment>',
262
                    $name, $actualVersion, $expectedVersion
263
                );
264
            }
265
        }
266
267
        return $shouldBeUpdatedOutput;
268
    }
269
270
    /**
271
     * Calls VersionsCheck::checkPackages.
272
     */
273
    private function checkPackages($rootPackageOnly = false)
274
    {
275
        $this->versionsCheck->checkPackages($this->distRepository, $this->localRepository, $this->rootPackage, $rootPackageOnly);
276
    }
277
}
278