Completed
Push — master ( 419b5f...f7351b )
by Sullivan
02:34
created

tests/VersionsCheckPluginTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\RepositoryManager;
18
use Composer\Repository\WritableArrayRepository;
19
use Composer\Script\ScriptEvents;
20
use SLLH\ComposerVersionsCheck\VersionsCheckPlugin;
21
use Symfony\Component\Console\Input\ArrayInput;
22
use Symfony\Component\Console\Output\NullOutput;
23
24
/**
25
 * @author Sullivan Senechal <[email protected]>
26
 */
27
class VersionsCheckPluginTest extends \PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * @var BufferIO
31
     */
32
    private $io;
33
34
    /**
35
     * @var Composer|\PHPUnit_Framework_MockObject_MockObject
36
     */
37
    private $composer;
38
39
    /**
40
     * @var Config
41
     */
42
    private $config;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function setUp()
48
    {
49
        if (!VersionsCheckPlugin::satisfiesComposerVersion()) {
50
            $this->markTestSkipped('Composer version not compatible.');
51
        }
52
53
        $this->io = new BufferIO();
54
        $this->composer = $this->getMock('Composer\Composer');
55
        $this->config = new Config(false);
56
57
        $this->composer->expects($this->any())->method('getConfig')
58
            ->willReturn($this->config);
59
        $this->composer->expects($this->any())->method('getPackage')
60
            ->willReturn(new RootPackage('my/project', '1.0.0', '1.0.0'));
61
        $this->composer->expects($this->any())->method('getPluginManager')
62
            ->willReturn(new PluginManager($this->io, $this->composer));
63
        $this->composer->expects($this->any())->method('getEventDispatcher')
64
            ->willReturn(new EventDispatcher($this->composer, $this->io));
65
        $this->composer->expects($this->any())->method('getRepositoryManager')
66
            ->willReturn(new RepositoryManager($this->io, new Config()));
67
    }
68
69
    /**
70
     * @dataProvider getTestOptionsData
71
     *
72
     * @param array|null $configData
73
     * @param array      $expectedOptions
74
     */
75
    public function testOptions($configData, array $expectedOptions)
76
    {
77
        if (null === $configData) {
78
            $this->composer->expects($this->any())->method('getConfig')
0 ignored issues
show
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...
79
                ->willReturn(null);
80
        } else {
81
            $this->config->merge($configData);
82
        }
83
84
        $plugin = new VersionsCheckPlugin();
85
        $plugin->activate($this->composer, $this->io);
86
87
        $this->assertAttributeSame($expectedOptions, 'options', $plugin);
88
    }
89
90
    public function getTestOptionsData()
91
    {
92
        return array(
93
            'No option' => array(
94
                null,
95
                array(
96
                    'show-links' => true,
97
                ),
98
            ),
99
            'Empty array options' => array(
100
                array(),
101
                array(
102
                    'show-links' => true,
103
                ),
104
            ),
105
            'Empty array plugin options' => array(
106
                array(
107
                    'config' => array(
108
                        'sllh-composer-versions-check' => array(),
109
                    ),
110
                ),
111
                array(
112
                    'show-links' => true,
113
                ),
114
            ),
115
            'Empty plugin options' => array(
116
                array(
117
                    'config' => array(
118
                        'sllh-composer-versions-check' => null,
119
                    ),
120
                ),
121
                array(
122
                    'show-links' => true,
123
                ),
124
            ),
125
            'False plugin options' => array(
126
                array(
127
                    'config' => array(
128
                        'sllh-composer-versions-check' => false,
129
                    ),
130
                ),
131
                array(
132
                    'show-links' => true,
133
                ),
134
            ),
135
            'Activate show-links' => array(
136
                array(
137
                    'config' => array(
138
                        'sllh-composer-versions-check' => array(
139
                            'show-links' => true,
140
                        ),
141
                    ),
142
                ),
143
                array(
144
                    'show-links' => true,
145
                ),
146
            ),
147
            'Disable show-links' => array(
148
                array(
149
                    'config' => array(
150
                        'sllh-composer-versions-check' => array(
151
                            'show-links' => false,
152
                        ),
153
                    ),
154
                ),
155
                array(
156
                    'show-links' => false,
157
                ),
158
            ),
159
        );
160
    }
161
162
    public function testPluginRegister()
163
    {
164
        $plugin = new VersionsCheckPlugin();
165
        $this->addComposerPlugin($plugin);
166
167
        $this->assertSame(array($plugin), $this->composer->getPluginManager()->getPlugins());
168
        $this->assertAttributeInstanceOf('Composer\Composer', 'composer', $plugin);
169
        $this->assertAttributeInstanceOf('Composer\IO\IOInterface', 'io', $plugin);
170
        $this->assertAttributeInstanceOf('SLLH\ComposerVersionsCheck\VersionsCheck', 'versionsCheck', $plugin);
171
    }
172
173
    public function testUpdateCommand()
174
    {
175
        $this->addComposerPlugin(new VersionsCheckPlugin());
176
177
        $localRepository = new WritableArrayRepository();
178
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
179
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
180
181
        $distRepository = new ArrayRepository();
182
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
183
        $distRepository->addPackage(new Package('foo/bar', '1.0.1', '1.0.1'));
184
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
185
        $this->composer->getRepositoryManager()->addRepository($distRepository);
186
187
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
188
189
        $this->assertSameOutput(<<<'EOF'
190
<warning>1 package is not up to date:</warning>
191
192
  - foo/bar (1.0.0) latest is 2.0.0
193
194
195
EOF
196
);
197
    }
198
199
    public function testPreferLowest()
200
    {
201
        $this->addComposerPlugin(new VersionsCheckPlugin());
202
203
        $localRepository = new WritableArrayRepository();
204
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
205
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
206
207
        $distRepository = new ArrayRepository();
208
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
209
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
210
        $this->composer->getRepositoryManager()->addRepository($distRepository);
211
212
        $updateCommand = new UpdateCommand();
213
        $input = new ArrayInput(array('update'), $updateCommand->getDefinition());
214
        $input->setOption('prefer-lowest', true);
215
        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, new NullOutput());
216
        $this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
217
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
218
219
        $this->assertSameOutput('', 'Plugin should not be runned.');
220
    }
221
222
    public function testPreferLowestNotExists()
223
    {
224
        $this->addComposerPlugin(new VersionsCheckPlugin());
225
226
        $localRepository = new WritableArrayRepository();
227
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
228
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
229
230
        $distRepository = new ArrayRepository();
231
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
232
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
233
        $this->composer->getRepositoryManager()->addRepository($distRepository);
234
235
        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', new ArrayInput(array()), new NullOutput());
236
        $this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
237
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
238
239
        $this->assertSameOutput(<<<'EOF'
240
<warning>1 package is not up to date:</warning>
241
242
  - foo/bar (1.0.0) latest is 2.0.0
243
244
245
EOF
246
);
247
    }
248
249 View Code Duplication
    private function addComposerPlugin(PluginInterface $plugin)
0 ignored issues
show
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...
250
    {
251
        $pluginManagerReflection = new \ReflectionClass($this->composer->getPluginManager());
252
        $addPluginReflection = $pluginManagerReflection->getMethod('addPlugin');
253
        $addPluginReflection->setAccessible(true);
254
        $addPluginReflection->invoke($this->composer->getPluginManager(), $plugin);
255
    }
256
257
    private function assertSameOutput($expectedOutput, $message = '')
258
    {
259
        if ('@package_version@' === Composer::VERSION) {
260
            $expectedOutput = '<warning>You are running an unstable version of composer.'
261
                ." The sllh/composer-versions-check plugin might not works as expected.</warning>\n"
262
                .$expectedOutput;
263
        }
264
265
        $this->assertSame($expectedOutput, $this->io->getOutput(), $message);
266
    }
267
}
268