Completed
Push — master ( d6b303...8271f2 )
by Sullivan
02:14
created

VersionsCheckTest::testOutdatedWithLinks()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 8.8571
cc 1
eloc 17
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, $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
        // To test root package detection
180
        $this->localRepository->addPackage($this->rootPackage);
181
182
        $this->checkPackages();
183
184
        // Must have one outdatedPackage if this should be updated
185
        $this->assertAttributeCount(1, 'outdatedPackages', $this->versionsCheck);
186
        $this->assertSame(<<<'EOF'
187
<warning>1 package is not up to date:</warning>
188
189
  - <info>foo/bar</info> (<comment>1.1</comment>) latest is <comment>2.0</comment>
190
    Required by <info>dummy/link</info> (<comment>1.*</comment>)
191
192
193
EOF
194
            , $this->versionsCheck->getOutput());
195
        // Test with disabled show-links option
196
        $this->assertSame(<<<'EOF'
197
<warning>1 package is not up to date:</warning>
198
199
  - <info>foo/bar</info> (<comment>1.1</comment>) latest is <comment>2.0</comment>
200
201
202
EOF
203
            , $this->versionsCheck->getOutput(false));
204
    }
205
206
    /**
207
     * Calls VersionsCheck::checkPackages.
208
     */
209
    private function checkPackages()
210
    {
211
        $this->versionsCheck->checkPackages($this->distRepository, $this->localRepository, $this->rootPackage);
212
    }
213
}
214