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