Completed
Pull Request — master (#19)
by Sullivan
03:06
created

VersionsCheckTest::testOutdatedWithLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0857
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace SLLH\ComposerVersionsCheck\Tests;
4
5
use Composer\Package\Link;
6
use Composer\Package\LinkConstraint\VersionConstraint;
7
use Composer\Package\Package;
8
use Composer\Package\RootPackage;
9
use Composer\Repository\ArrayRepository;
10
use Composer\Repository\WritableArrayRepository;
11
use Composer\Semver\Constraint\Constraint;
12
use SLLH\ComposerVersionsCheck\VersionsCheck;
13
14
/**
15
 * @author Sullivan Senechal <[email protected]>
16
 */
17
class VersionsCheckTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var ArrayRepository
21
     */
22
    private $distRepository;
23
24
    /**
25
     * @var WritableArrayRepository
26
     */
27
    private $localRepository;
28
29
    /**
30
     * @var RootPackage
31
     */
32
    private $rootPackage;
33
34
    /**
35
     * @var VersionsCheck
36
     */
37
    private $versionsCheck;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function setUp()
43
    {
44
        $this->distRepository = new ArrayRepository();
45
        $this->localRepository = new WritableArrayRepository();
46
47
        $this->rootPackage = new RootPackage('my/project', '1.0.0', '1.0.0');
48
        $this->versionsCheck = new VersionsCheck();
49
    }
50
51
    /**
52
     * @dataProvider getOutdatedDetectionTestData
53
     *
54
     * @param string $actualVersion
55
     * @param string $higherVersion
56
     * @param bool   $shouldBeUpdated
57
     * @param bool   $preferStable
58
     */
59
    public function testOutdatedDetection($actualVersion, $higherVersion, $shouldBeUpdated, $preferStable = false)
60
    {
61
        $this->distRepository->addPackage(new Package('foo/bar', $higherVersion, $higherVersion));
62
        $this->localRepository->addPackage(new Package('foo/bar', $actualVersion, $actualVersion));
63
64
        $this->rootPackage->setPreferStable($preferStable);
65
        $this->checkPackages();
66
67
        // Must have one outdatedPackage if this should be updated
68
        if (true === $shouldBeUpdated) {
69
            $this->assertAttributeCount(1, 'outdatedPackages', $this->versionsCheck);
70
            $this->assertSame(sprintf(<<<EOF
71
<warning>1 package is not up to date:</warning>
72
73
  - <info>foo/bar</info> (<comment>%s</comment>) latest is <comment>%s</comment>
74
75
76
EOF
77
                , $actualVersion, $higherVersion), $this->versionsCheck->getOutput());
78
        } else {
79
            $this->assertAttributeCount(0, 'outdatedPackages', $this->versionsCheck);
80
            $this->assertSame("<info>All packages are up to date.</info>\n", $this->versionsCheck->getOutput());
81
        }
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getOutdatedDetectionTestData()
88
    {
89
        return array(
90
            array('1.0.0', '1.0.0', false),
91
            array('1.0.0', '1.0.0', false),
92
            array('1.0.0', '2.0.0', true),
93
            array('2.0.0', '1.0.0', false),
94
            array('2.0.0', '2.0.0', false),
95
            array('2.0.0', '1.9.9', false),
96
            array('1.9.9', '2.0.0', true),
97
            array('2.0.0', '2.1.0-alpha1', true),        // Should be true because no stable package available
98
            array('2.0.0', '2.1.0-alpha1', false, true), // Should be false because of minimum stability
99
            array('2.0.0-alpha1', '2.1.0-alpha2', true),
100
        );
101
    }
102
103
    /**
104
     * @dataProvider getMultiplePackagesComparisonTestsData
105
     *
106
     * @param array $packagesData
107
     * @param bool  $preferStable
108
     * @param int   $outdatedPackagesCount
109
     */
110
    public function testMultiplePackagesComparison(array $packagesData, $preferStable = false, $outdatedPackagesCount)
111
    {
112
        $this->rootPackage->setMinimumStability('dev');
113
        $this->rootPackage->setPreferStable($preferStable);
114
115
        $shouldBeUpdatedOutput = array();
116
117
        foreach ($packagesData as $name => $packageData) {
118
            list($actualVersion, $availableVersions, $expectedVersion) = $packageData;
119
            $this->localRepository->addPackage(new Package($name, $actualVersion, $actualVersion));
120
            foreach ($availableVersions as $availableVersion) {
121
                $this->distRepository->addPackage(new Package($name, $availableVersion, $availableVersion));
122
            }
123
124
            if (false !== $expectedVersion) {
125
                $shouldBeUpdatedOutput[] = sprintf(
126
                    '  - <info>%s</info> (<comment>%s</comment>) latest is <comment>%s</comment>',
127
                    $name, $actualVersion, $expectedVersion
128
                );
129
            }
130
        }
131
132
        $this->checkPackages();
133
134
        $this->assertSame(sprintf(<<<EOF
135
<warning>%d packages are not up to date:</warning>
136
137
%s
138
139
140
EOF
141
        , $outdatedPackagesCount, implode("\n\n", $shouldBeUpdatedOutput)), $this->versionsCheck->getOutput());
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function getMultiplePackagesComparisonTestsData()
148
    {
149
        return array(
150
            array(array(
151
                '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'),
152
                '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'),
153
                'vendor/package-1'     => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
154
                'vendor/up-to-date'    => array('9.9.0', array('8.0.0', '9.9.0', '1.0-dev'), false),
155
                'vendor/dev-master'    => array('9.9.0', array('8.0.0', '9.9.0', '10.0-dev'), '10.0-dev'),
156
                'vendor/package-2'     => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
157
                'vendor/package-3'     => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
158
                'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
159
            ), false, 6),
160
            array(array(
161
                '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'),
162
                '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'),
163
                'vendor/package-1'     => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
164
                'vendor/up-to-date'    => array('9.9.0', array('8.0.0', '9.9.0', 'dev-master'), false),
165
                'vendor/package-2'     => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
166
                'vendor/package-3'     => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
167
                'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
168
            ), true, 3),
169
        );
170
    }
171
172
    public function testOutdatedWithLinks()
173
    {
174
        $this->distRepository->addPackage(new Package('foo/bar', '2.0', '2.0'));
175
        $this->localRepository->addPackage(new Package('foo/bar', '1.1', '1.1'));
176
177
        $linkedPackage = new Package('dummy/link', '1.0', '1.0');
178
        $linkedPackage->setRequires(array(new Link('dummy/link', 'foo/bar', null, '', '1.*')));
179
        $this->localRepository->addPackage($linkedPackage);
180
181
        $this->checkPackages();
182
183
        // Must have one outdatedPackage if this should be updated
184
        $this->assertAttributeCount(1, 'outdatedPackages', $this->versionsCheck);
185
        $this->assertSame(<<<EOF
186
<warning>1 package is not up to date:</warning>
187
188
  - <info>foo/bar</info> (<comment>1.1</comment>) latest is <comment>2.0</comment>
189
    Required by <info>dummy/link</info> (<comment>1.*</comment>)
190
191
192
EOF
193
            , $this->versionsCheck->getOutput());
194
    }
195
196
    /**
197
     * Calls VersionsCheck::checkPackages.
198
     */
199
    private function checkPackages()
200
    {
201
        $this->versionsCheck->checkPackages($this->distRepository, $this->localRepository, $this->rootPackage);
202
    }
203
}
204