Completed
Pull Request — master (#64)
by Sullivan
01:29
created

VersionsCheckPluginTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 16
dl 0
loc 248
rs 10
c 0
b 0
f 0

10 Methods

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