UpgradeCommandTest::testUpgrade()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 134
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 99
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 Modera\UpgradeBundle\Tests\Functional\Command;
4
5
use Modera\UpgradeBundle\Json\JsonFile;
6
use Modera\FoundationBundle\Testing\FunctionalTestCase;
7
use Symfony\Bundle\FrameworkBundle\Console\Application;
8
use Symfony\Component\Console\Input\StringInput;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
use Symfony\Component\Console\Output\NullOutput;
12
13
/**
14
 * @author    Sergei Vizel <[email protected]>
15
 * @copyright 2014 Modera Foundation
16
 */
17
class UpgradeCommandTest extends FunctionalTestCase
18
{
19
    /**
20
     * @var string
21
     */
22
    private static $basePath;
23
24
    /**
25
     * @var JsonFile
26
     */
27
    private static $composerFile;
28
29
    /**
30
     * @var array
31
     */
32
    private static $composerBackup;
33
34
    /**
35
     * @var string
36
     */
37
    private static $versionFilePath;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public static function doSetUpBeforeClass()
43
    {
44
        self::$basePath = dirname(self::$kernel->getRootdir());
45
        self::$composerFile = new JsonFile(self::$basePath.'/composer.json');
46
        self::$composerBackup = self::$composerFile->read();
0 ignored issues
show
Documentation Bug introduced by
It seems like self::$composerFile->read() of type * is incompatible with the declared type array of property $composerBackup.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
48
        self::$versionFilePath = getcwd().'/modera-version.txt';
49
        if (file_exists(self::$versionFilePath)) {
50
            unlink(self::$versionFilePath);
51
        }
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public static function doTearDownAfterClass()
58
    {
59
        self::$composerFile->write(self::$composerBackup);
60
        unlink(self::$versionFilePath);
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    private function getCurrentVersion()
67
    {
68
        return @file_get_contents(self::$versionFilePath);
69
    }
70
71
    /**
72
     * @param null|string|array $config
0 ignored issues
show
Bug introduced by
There is no parameter named $config. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73
     */
74
    private function getApplication()
75
    {
76
        $app = new Application(self::$kernel);
77
        $app->setAutoExit(false);
78
79
        return $app;
80
    }
81
82 View Code Duplication
    private function runUpdateDependenciesCommand(OutputInterface $output = null)
0 ignored issues
show
Duplication introduced by
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...
83
    {
84
        $input = new StringInput('modera:upgrade --dependencies '.__DIR__.'/versions.json');
85
        $input->setInteractive(false);
86
        $app = $this->getApplication();
87
        $result = $app->run($input, $output ?: new NullOutput());
88
        $this->assertEquals(0, $result);
89
    }
90
91 View Code Duplication
    private function runCommandsCommand(OutputInterface $output = null)
0 ignored issues
show
Duplication introduced by
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...
92
    {
93
        $input = new StringInput('modera:upgrade --run-commands '.__DIR__.'/versions.json');
94
        $input->setInteractive(false);
95
        $app = $this->getApplication();
96
        $result = $app->run($input, $output ?: new NullOutput());
97
        $this->assertEquals(0, $result);
98
    }
99
100
    public function testUpgrade()
101
    {
102
        $output = new BufferedOutput();
103
104
        // null version check
105
        $expectedData = array(
106
            'name' => 'modera/upgrade-bundle-test',
107
            'repositories' => array(
108
                array(
109
                    'type' => 'composer',
110
                    'url' => 'http://packages.org',
111
                ),
112
            ),
113
            'require' => array(
114
                'test/dependency_1' => 'dev-master',
115
            ),
116
        );
117
        $this->assertEquals($expectedData, self::$composerFile->read());
118
119
        // 0.1.0 version check
120
        $expectedData['require'] = array(
121
            'test/dependency_1' => '0.1.0',
122
            'test/dependency_2' => '0.1.0',
123
            'test/dependency_3' => '0.1.0',
124
        );
125
        $expectedData['repositories'][] = array(
126
            'type' => 'vcs',
127
            'url' => 'ssh://[email protected]',
128
        );
129
        $this->runUpdateDependenciesCommand($output);
130
        $str = $output->fetch();
131
        $this->assertEquals('0.1.0', $this->getCurrentVersion());
132
        $this->assertEquals(1, substr_count($str, 'new Test\AddBundle\TestAddBundle()'));
133
        $this->assertEquals(0, substr_count($str, 'new Test\RemoveBundle\TestRemoveBundle()'));
134
        $this->assertEquals($expectedData, self::$composerFile->read());
135
        $this->assertTrue(is_file(self::$basePath.'/composer.backup.json'));
136
        unlink(self::$basePath.'/composer.backup.json');
137
138
        // 0.1.0 version run commands
139
        $this->runCommandsCommand($output);
140
        $str = $output->fetch();
141
        $this->assertEquals(0, substr_count($str, 'help --format=json'));
142
        $this->assertEquals(0, substr_count($str, 'help --format=txt'));
143
144
        // emulate composer.json changes
145
        $tmp = self::$composerFile->read();
146
        $tmp['require']['test/dependency_1'] = 'dev-master';
147
        self::$composerFile->write($tmp);
148
149
        // 0.1.1 version check
150
        $expectedData['require'] = array(
151
            'test/dependency_1' => '0.1.1',
152
            'test/dependency_2' => '0.1.0',
153
        );
154
        unset($expectedData['repositories'][1]);
155
        $expectedData['repositories'] = array_values($expectedData['repositories']);
156
        $this->runUpdateDependenciesCommand($output);
157
        $str = $output->fetch();
158
        $this->assertEquals('0.1.1', $this->getCurrentVersion());
159
        $this->assertEquals(0, substr_count($str, 'new Test\AddBundle\TestAddBundle()'));
160
        $this->assertEquals(1, substr_count($str, 'new Test\RemoveBundle\TestRemoveBundle()'));
161
        $this->assertEquals($expectedData, self::$composerFile->read());
162
        $this->assertTrue(is_file(self::$basePath.'/composer.v0.1.0.backup.json'));
163
        unlink(self::$basePath.'/composer.v0.1.0.backup.json');
164
165
        // 0.1.1 version run commands
166
        $this->runCommandsCommand($output);
167
        $str = $output->fetch();
168
        $this->assertEquals(1, substr_count($str, 'help --format=json'));
169
        $this->assertEquals(0, substr_count($str, 'help --format=txt'));
170
171
        // emulate composer.json changes
172
        $tmp = self::$composerFile->read();
173
        $tmp['require']['test/dependency_2'] = 'dev-master';
174
        self::$composerFile->write($tmp);
175
176
        // 0.1.2 version check
177
        $expectedData['require'] = array(
178
            'test/dependency_1' => '0.1.1',
179
            'test/dependency_2' => '0.1.0',
180
            'test/dependency_4' => '0.1.0',
181
        );
182
        $this->runUpdateDependenciesCommand($output);
183
        $str = $output->fetch();
184
        $this->assertEquals('0.1.2', $this->getCurrentVersion());
185
        $this->assertEquals(0, substr_count($str, 'new Test\AddBundle\TestAddBundle()'));
186
        $this->assertEquals(0, substr_count($str, 'new Test\RemoveBundle\TestRemoveBundle()'));
187
        $this->assertEquals($expectedData, self::$composerFile->read());
188
        $this->assertTrue(is_file(self::$basePath.'/composer.v0.1.1.backup.json'));
189
        unlink(self::$basePath.'/composer.v0.1.1.backup.json');
190
191
        // 0.1.2 version run commands
192
        $this->runCommandsCommand($output);
193
        $str = $output->fetch();
194
        $this->assertEquals(0, substr_count($str, 'help --format=json'));
195
        $this->assertEquals(1, substr_count($str, 'help --format=txt'));
196
197
        // 0.1.3 version check
198
        $this->runUpdateDependenciesCommand($output);
199
        $str = $output->fetch();
200
        $this->assertEquals('0.1.3', $this->getCurrentVersion());
201
        $this->assertEquals(1, substr_count($str, 'Some foo instruction'));
202
        $this->assertEquals(0, substr_count($str, 'Some bar instruction'));
203
        $this->assertEquals(0, substr_count($str, 'Some baz instruction'));
204
        $this->assertEquals($expectedData, self::$composerFile->read());
205
        $this->assertTrue(is_file(self::$basePath.'/composer.v0.1.2.backup.json'));
206
        unlink(self::$basePath.'/composer.v0.1.2.backup.json');
207
208
        // 0.1.4 version check
209
        $this->runUpdateDependenciesCommand($output);
210
        $str = $output->fetch();
211
        $this->assertEquals('0.1.4', $this->getCurrentVersion());
212
        $this->assertEquals(0, substr_count($str, 'Some foo instruction'));
213
        $this->assertEquals(1, substr_count($str, 'Some bar instruction'));
214
        $this->assertEquals(0, substr_count($str, 'Some baz instruction'));
215
        $this->assertEquals($expectedData, self::$composerFile->read());
216
        $this->assertTrue(is_file(self::$basePath.'/composer.v0.1.3.backup.json'));
217
        unlink(self::$basePath.'/composer.v0.1.3.backup.json');
218
219
        // 0.1.5 version check
220
        $expectedData['require'] = array(
221
            'test/dependency_1' => '0.1.1',
222
            'test/dependency_4' => '0.1.1',
223
        );
224
        $this->runUpdateDependenciesCommand($output);
225
        $str = $output->fetch();
226
        $this->assertEquals('0.1.5', $this->getCurrentVersion());
227
        $this->assertEquals(0, substr_count($str, 'Some foo instruction'));
228
        $this->assertEquals(0, substr_count($str, 'Some bar instruction'));
229
        $this->assertEquals(1, substr_count($str, 'Some baz instruction'));
230
        $this->assertEquals($expectedData, self::$composerFile->read());
231
        $this->assertTrue(is_file(self::$basePath.'/composer.v0.1.4.backup.json'));
232
        unlink(self::$basePath.'/composer.v0.1.4.backup.json');
233
    }
234
}
235