Completed
Pull Request — master (#19)
by Sullivan
05:19 queued 02:35
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\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 array $packagesData
105
     * @param bool  $preferStable
106
     * @param int   $outdatedPackagesCount
107
     */
108
    public function testMultiplePackagesComparison(array $packagesData, $preferStable = false, $outdatedPackagesCount)
109
    {
110
        $this->rootPackage->setMinimumStability('dev');
111
        $this->rootPackage->setPreferStable($preferStable);
112
113
        $shouldBeUpdatedOutput = array();
114
115
        foreach ($packagesData as $name => $packageData) {
116
            list($actualVersion, $availableVersions, $expectedVersion) = $packageData;
117
            $this->localRepository->addPackage(new Package($name, $actualVersion, $actualVersion));
118
            foreach ($availableVersions as $availableVersion) {
119
                $this->distRepository->addPackage(new Package($name, $availableVersion, $availableVersion));
120
            }
121
122
            if (false !== $expectedVersion) {
123
                $shouldBeUpdatedOutput[] = sprintf(
124
                    '  - <info>%s</info> (<comment>%s</comment>) latest is <comment>%s</comment>',
125
                    $name, $actualVersion, $expectedVersion
126
                );
127
            }
128
        }
129
130
        $this->checkPackages();
131
132
        $this->assertSame(sprintf(<<<EOF
133
<warning>%d packages are not up to date:</warning>
134
135
%s
136
137
138
EOF
139
        , $outdatedPackagesCount, implode("\n\n", $shouldBeUpdatedOutput)), $this->versionsCheck->getOutput());
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function getMultiplePackagesComparisonTestsData()
146
    {
147
        return array(
148
            array(array(
149
                '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'),
150
                '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'),
151
                'vendor/package-1'     => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
152
                'vendor/up-to-date'    => array('9.9.0', array('8.0.0', '9.9.0', '1.0-dev'), false),
153
                'vendor/dev-master'    => array('9.9.0', array('8.0.0', '9.9.0', '10.0-dev'), '10.0-dev'),
154
                'vendor/package-2'     => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
155
                'vendor/package-3'     => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
156
                'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
157
            ), false, 6),
158
            array(array(
159
                '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'),
160
                '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'),
161
                'vendor/package-1'     => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
162
                'vendor/up-to-date'    => array('9.9.0', array('8.0.0', '9.9.0', 'dev-master'), false),
163
                'vendor/package-2'     => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
164
                'vendor/package-3'     => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
165
                'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
166
            ), true, 3),
167
        );
168
    }
169
170
    public function testOutdatedWithLinks()
171
    {
172
        $this->distRepository->addPackage(new Package('foo/bar', '2.0', '2.0'));
173
        $this->localRepository->addPackage(new Package('foo/bar', '1.1', '1.1'));
174
175
        $linkedPackage = new Package('dummy/link', '1.0', '1.0');
176
        $linkedPackage->setRequires(array(new Link('dummy/link', 'foo/bar', null, '', '1.*')));
177
        $this->localRepository->addPackage($linkedPackage);
178
179
        $this->checkPackages();
180
181
        // Must have one outdatedPackage if this should be updated
182
        $this->assertAttributeCount(1, 'outdatedPackages', $this->versionsCheck);
183
        $this->assertSame(<<<EOF
184
<warning>1 package is not up to date:</warning>
185
186
  - <info>foo/bar</info> (<comment>1.1</comment>) latest is <comment>2.0</comment>
187
    Required by <info>dummy/link</info> (<comment>1.*</comment>)
188
189
190
EOF
191
            , $this->versionsCheck->getOutput());
192
    }
193
194
    /**
195
     * Calls VersionsCheck::checkPackages.
196
     */
197
    private function checkPackages()
198
    {
199
        $this->versionsCheck->checkPackages($this->distRepository, $this->localRepository, $this->rootPackage);
200
    }
201
}
202