Completed
Pull Request — master (#19)
by Sullivan
06:02 queued 03:47
created

VersionsCheckPluginTest::getTestOptionsData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 71
rs 9.1369
cc 1
eloc 42
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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