Completed
Push — master ( 0c9b9b...10ad9a )
by Sullivan
01:46 queued 19s
created

VersionsCheckPluginTest::makeWritableRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace SLLH\ComposerVersionsCheck\Tests;
4
5
use Composer\Command\UpdateCommand;
6
use Composer\Composer;
7
use Composer\Config;
8
use Composer\EventDispatcher\EventDispatcher;
9
use Composer\IO\BufferIO;
10
use Composer\Package\Package;
11
use Composer\Package\RootPackage;
12
use Composer\Plugin\CommandEvent;
13
use Composer\Plugin\PluginEvents;
14
use Composer\Plugin\PluginInterface;
15
use Composer\Plugin\PluginManager;
16
use Composer\Repository\ArrayRepository;
17
use Composer\Repository\InstalledArrayRepository;
18
use Composer\Repository\RepositoryManager;
19
use Composer\Repository\WritableArrayRepository;
20
use Composer\Script\ScriptEvents;
21
use Composer\Util\HttpDownloader;
22
use SLLH\ComposerVersionsCheck\VersionsCheckPlugin;
23
use Symfony\Component\Console\Input\ArrayInput;
24
use Symfony\Component\Console\Output\NullOutput;
25
26
/**
27
 * @author Sullivan Senechal <[email protected]>
28
 */
29
class VersionsCheckPluginTest extends \PHPUnit_Framework_TestCase
30
{
31
    /**
32
     * @var BufferIO
33
     */
34
    private $io;
35
36
    /**
37
     * @var Composer|\PHPUnit_Framework_MockObject_MockObject
38
     */
39
    private $composer;
40
41
    /**
42
     * @var Config
43
     */
44
    private $config;
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function setUp()
50
    {
51
        $this->io = new BufferIO();
52
        $this->composer = $this->getMock('Composer\Composer');
53
        $this->config = new Config(false);
54
55
        $this->composer->expects($this->any())->method('getConfig')
56
            ->willReturn($this->config);
57
        $this->composer->expects($this->any())->method('getPackage')
58
            ->willReturn(new RootPackage('my/project', '1.0.0', '1.0.0'));
59
        $this->composer->expects($this->any())->method('getPluginManager')
60
            ->willReturn(new PluginManager($this->io, $this->composer));
61
        $this->composer->expects($this->any())->method('getEventDispatcher')
62
            ->willReturn(new EventDispatcher($this->composer, $this->io));
63
        $repositoryManager = version_compare(PluginInterface::PLUGIN_API_VERSION, '2.0.0') >= 0
64
            ? new RepositoryManager($this->io, $this->config, new HttpDownloader($this->io, $this->config))
65
            : new RepositoryManager($this->io, $this->config)
66
        ;
67
        $this->composer->expects($this->any())->method('getRepositoryManager')
68
            ->willReturn($repositoryManager);
69
    }
70
71
    /**
72
     * @dataProvider getTestOptionsData
73
     *
74
     * @param array|null $configData
75
     */
76
    public function testOptions($configData, array $expectedOptions)
77
    {
78
        if (null === $configData) {
79
            $this->composer->expects($this->any())->method('getConfig')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Composer\Composer>.

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...
80
                ->willReturn(null);
81
        } else {
82
            $this->config->merge($configData);
83
        }
84
85
        $plugin = new VersionsCheckPlugin();
86
        $plugin->activate($this->composer, $this->io);
87
88
        $this->assertAttributeSame($expectedOptions, 'options', $plugin);
89
    }
90
91
    public function getTestOptionsData()
92
    {
93
        return array(
94
            'No option' => array(
95
                null,
96
                array(
97
                    'show-links' => false,
98
                ),
99
            ),
100
            'Empty array options' => array(
101
                array(),
102
                array(
103
                    'show-links' => false,
104
                ),
105
            ),
106
            'Empty array plugin options' => array(
107
                array(
108
                    'config' => array(
109
                        'sllh-composer-versions-check' => array(),
110
                    ),
111
                ),
112
                array(
113
                    'show-links' => false,
114
                ),
115
            ),
116
            'Empty plugin options' => array(
117
                array(
118
                    'config' => array(
119
                        'sllh-composer-versions-check' => null,
120
                    ),
121
                ),
122
                array(
123
                    'show-links' => false,
124
                ),
125
            ),
126
            'False plugin options' => array(
127
                array(
128
                    'config' => array(
129
                        'sllh-composer-versions-check' => false,
130
                    ),
131
                ),
132
                array(
133
                    'show-links' => false,
134
                ),
135
            ),
136
            'Activate show-links' => array(
137
                array(
138
                    'config' => array(
139
                        'sllh-composer-versions-check' => array(
140
                            'show-links' => true,
141
                        ),
142
                    ),
143
                ),
144
                array(
145
                    'show-links' => true,
146
                ),
147
            ),
148
            'Disable show-links' => array(
149
                array(
150
                    'config' => array(
151
                        'sllh-composer-versions-check' => array(
152
                            'show-links' => false,
153
                        ),
154
                    ),
155
                ),
156
                array(
157
                    'show-links' => false,
158
                ),
159
            ),
160
        );
161
    }
162
163
    public function testPluginRegister()
164
    {
165
        $plugin = new VersionsCheckPlugin();
166
        $this->addComposerPlugin($plugin);
167
168
        $this->assertSame(array($plugin), $this->composer->getPluginManager()->getPlugins());
169
        $this->assertAttributeInstanceOf('Composer\Composer', 'composer', $plugin);
170
        $this->assertAttributeInstanceOf('Composer\IO\IOInterface', 'io', $plugin);
171
        $this->assertAttributeInstanceOf('SLLH\ComposerVersionsCheck\VersionsCheck', 'versionsCheck', $plugin);
172
    }
173
174
    public function testUpdateCommand()
175
    {
176
        $this->addComposerPlugin(new VersionsCheckPlugin());
177
178
        $localRepository = $this->makeWritableRepository();
179
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
180
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
181
182
        $distRepository = new ArrayRepository();
183
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
184
        $distRepository->addPackage(new Package('foo/bar', '1.0.1', '1.0.1'));
185
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
186
        $this->composer->getRepositoryManager()->addRepository($distRepository);
187
188
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
189
190
        $this->assertSameOutput(<<<'EOF'
191
<warning>1 package is not up to date:</warning>
192
193
  - foo/bar (1.0.0) latest is 2.0.0
194
195
196
EOF
197
);
198
    }
199
200
    public function testPreferLowest()
201
    {
202
        $this->addComposerPlugin(new VersionsCheckPlugin());
203
204
        $localRepository = $this->makeWritableRepository();
205
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
206
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
207
208
        $distRepository = new ArrayRepository();
209
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
210
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
211
        $this->composer->getRepositoryManager()->addRepository($distRepository);
212
213
        $updateCommand = new UpdateCommand();
214
        $input = new ArrayInput(array('update'), $updateCommand->getDefinition());
215
        $input->setOption('prefer-lowest', true);
216
        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, new NullOutput());
217
        $this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
218
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
219
220
        $this->assertSameOutput('', 'Plugin should not be runned.');
221
    }
222
223
    public function testPreferLowestNotExists()
224
    {
225
        $this->addComposerPlugin(new VersionsCheckPlugin());
226
227
        $localRepository = $this->makeWritableRepository();
228
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
229
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
230
231
        $distRepository = new ArrayRepository();
232
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
233
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
234
        $this->composer->getRepositoryManager()->addRepository($distRepository);
235
236
        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', new ArrayInput(array()), new NullOutput());
237
        $this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
238
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
239
240
        $this->assertSameOutput(<<<'EOF'
241
<warning>1 package is not up to date:</warning>
242
243
  - foo/bar (1.0.0) latest is 2.0.0
244
245
246
EOF
247
);
248
    }
249
250
    private function addComposerPlugin(PluginInterface $plugin)
251
    {
252
        $pluginManagerReflection = new \ReflectionClass($this->composer->getPluginManager());
253
        $addPluginReflection = $pluginManagerReflection->getMethod('addPlugin');
254
        $addPluginReflection->setAccessible(true);
255
        $addPluginReflection->invoke($this->composer->getPluginManager(), $plugin);
256
    }
257
258
    private function assertSameOutput($expectedOutput, $message = '')
259
    {
260
        $this->assertSame($expectedOutput, $this->io->getOutput(), $message);
261
    }
262
263
    private function makeWritableRepository()
264
    {
265
        return version_compare(PluginInterface::PLUGIN_API_VERSION, '2.0.0') >= 0
266
            ? new InstalledArrayRepository()
267
            : new WritableArrayRepository()
268
        ;
269
    }
270
}
271