Completed
Pull Request — master (#19)
by Sullivan
04:22
created

testPreferLowestNotExists()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 1
eloc 16
nc 1
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\Config\JsonConfigSource;
9
use Composer\EventDispatcher\EventDispatcher;
10
use Composer\IO\BufferIO;
11
use Composer\Json\JsonFile;
12
use Composer\Package\Package;
13
use Composer\Package\RootPackage;
14
use Composer\Plugin\CommandEvent;
15
use Composer\Plugin\PluginEvents;
16
use Composer\Plugin\PluginInterface;
17
use Composer\Plugin\PluginManager;
18
use Composer\Repository\ArrayRepository;
19
use Composer\Repository\RepositoryManager;
20
use Composer\Repository\WritableArrayRepository;
21
use Composer\Script\ScriptEvents;
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
     * {@inheritdoc}
43
     */
44
    protected function setUp()
45
    {
46
        $this->io = new BufferIO();
47
        $this->composer = $this->getMock('Composer\Composer');
48
49
        $this->composer->expects($this->any())->method('getPackage')
50
            ->willReturn(new RootPackage('my/project', '1.0.0', '1.0.0'));
51
        $this->composer->expects($this->any())->method('getPluginManager')
52
            ->willReturn(new PluginManager($this->io, $this->composer));
53
        $this->composer->expects($this->any())->method('getEventDispatcher')
54
            ->willReturn(new EventDispatcher($this->composer, $this->io));
55
        $this->composer->expects($this->any())->method('getRepositoryManager')
56
            ->willReturn(new RepositoryManager($this->io, new Config()));
57
    }
58
59
    /**
60
     * @dataProvider getTestOptionsData
61
     *
62
     * @param array|null $configData
63
     * @param array      $expectedOptions
64
     */
65
    public function testOptions($configData, array $expectedOptions)
66
    {
67
        if (null === $configData) {
68
            $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...
69
                ->willReturn(null);
70
        } else {
71
            $config = new Config(false);
72
            $config->merge($configData);
73
            $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...
74
                ->willReturn($config);
75
        }
76
77
        $plugin = new VersionsCheckPlugin();
78
        $plugin->activate($this->composer, $this->io);
79
80
        $this->assertAttributeEquals($expectedOptions, 'options', $plugin);
81
    }
82
83
    public function getTestOptionsData()
84
    {
85
        return array(
86
            array(
87
                null,
88
                array(
89
                    'show-links' => true,
90
                )
91
            ),
92
            array(
93
                array(),
94
                array(
95
                    'show-links' => true,
96
                )
97
            ),
98
            array(
99
                array(
100
                    'config' => array(
101
                        'sllh-composer-versions-check' => array(),
102
                    )
103
                ),
104
                array(
105
                    'show-links' => true,
106
                )
107
            ),
108
            array(
109
                array(
110
                    'config' => array(
111
                        'sllh-composer-versions-check' => null,
112
                    )
113
                ),
114
                array(
115
                    'show-links' => true,
116
                )
117
            ),
118
            array(
119
                array(
120
                    'config' => array(
121
                        'sllh-composer-versions-check' => false,
122
                    )
123
                ),
124
                array(
125
                    'show-links' => true,
126
                )
127
            ),
128
            array(
129
                array(
130
                    'config' => array(
131
                        'sllh-composer-versions-check' => array(
132
                            'show-links' => true
133
                        ),
134
                    )
135
                ),
136
                array(
137
                    'show-links' => true,
138
                )
139
            ),
140
            array(
141
                array(
142
                    'config' => array(
143
                        'sllh-composer-versions-check' => array(
144
                            'show-links' => false
145
                        ),
146
                    )
147
                ),
148
                array(
149
                    'show-links' => false,
150
                )
151
            ),
152
        );
153
    }
154
155
    public function testPluginRegister()
156
    {
157
        $plugin = new VersionsCheckPlugin();
158
        $this->addComposerPlugin($plugin);
159
160
        $this->assertSame(array($plugin), $this->composer->getPluginManager()->getPlugins());
161
        $this->assertAttributeInstanceOf('Composer\Composer', 'composer', $plugin);
162
        $this->assertAttributeInstanceOf('Composer\IO\IOInterface', 'io', $plugin);
163
        $this->assertAttributeInstanceOf('SLLH\ComposerVersionsCheck\VersionsCheck', 'versionsCheck', $plugin);
164
    }
165
166
    public function testUpdateCommand()
167
    {
168
        $this->addComposerPlugin(new VersionsCheckPlugin());
169
170
        $localRepository = new WritableArrayRepository();
171
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
172
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
173
174
        $distRepository = new ArrayRepository();
175
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
176
        $distRepository->addPackage(new Package('foo/bar', '1.0.1', '1.0.1'));
177
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
178
        $this->composer->getRepositoryManager()->addRepository($distRepository);
179
180
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
181
182
        $this->assertSame(<<<EOF
183
<warning>1 package is not up to date:</warning>
184
185
  - foo/bar (1.0.0) latest is 2.0.0
186
187
188
EOF
189
            , $this->io->getOutput());
190
    }
191
192
    public function testPreferLowest()
193
    {
194
        $this->addComposerPlugin(new VersionsCheckPlugin());
195
196
        $localRepository = new WritableArrayRepository();
197
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
198
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
199
200
        $distRepository = new ArrayRepository();
201
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
202
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
203
        $this->composer->getRepositoryManager()->addRepository($distRepository);
204
205
        $updateCommand = new UpdateCommand();
206
        $input = new ArrayInput(array('update'), $updateCommand->getDefinition());
207
        $input->setOption('prefer-lowest', true);
208
        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, new NullOutput());
209
        $this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
210
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
211
212
        $this->assertSame('', $this->io->getOutput(), 'Plugin should not be runned.');
213
    }
214
215
    public function testPreferLowestNotExists()
216
    {
217
        $this->addComposerPlugin(new VersionsCheckPlugin());
218
219
        $localRepository = new WritableArrayRepository();
220
        $localRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
221
        $this->composer->getRepositoryManager()->setLocalRepository($localRepository);
222
223
        $distRepository = new ArrayRepository();
224
        $distRepository->addPackage(new Package('foo/bar', '1.0.0', '1.0.0'));
225
        $distRepository->addPackage(new Package('foo/bar', '2.0.0', '2.0.0'));
226
        $this->composer->getRepositoryManager()->addRepository($distRepository);
227
228
        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', new ArrayInput(array()), new NullOutput());
229
        $this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
230
        $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD);
231
232
        $this->assertSame(<<<EOF
233
<warning>1 package is not up to date:</warning>
234
235
  - foo/bar (1.0.0) latest is 2.0.0
236
237
238
EOF
239
            , $this->io->getOutput());
240
    }
241
242
    private function addComposerPlugin(PluginInterface $plugin)
243
    {
244
        $pluginManagerReflection = new \ReflectionClass($this->composer->getPluginManager());
245
        $addPluginReflection = $pluginManagerReflection->getMethod('addPlugin');
246
        $addPluginReflection->setAccessible(true);
247
        $addPluginReflection->invoke($this->composer->getPluginManager(), $plugin);
248
    }
249
}
250