Completed
Push — master ( e9cea3...c094b1 )
by Marco
02:13
created

E2EInstaller   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 271
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 271
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A tearDown() 0 8 1
A testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProject() 0 50 1
B testRemovingPluginDoesNotAttemptToGenerateVersions() 0 36 1
B testRemovingPluginWithNoDevDoesNotAttemptToGenerateVersions() 0 35 1
B createPackageVersionsArtifact() 0 40 3
A createArtifact() 0 17 1
A writeComposerJsonFile() 0 7 1
A execComposerInDir() 0 9 1
A rmDir() 0 22 2
1
<?php
2
3
namespace PackageVersionsTest;
4
5
use PHPUnit_Framework_TestCase;
6
use RecursiveCallbackFilterIterator;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use SplFileInfo;
10
use ZipArchive;
11
12
/**
13
 * @coversNothing
14
 */
15
class E2EInstaller extends PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @var string
19
     */
20
    private $tempGlobalComposerHome;
21
22
    /**
23
     * @var string
24
     */
25
    private $tempLocalComposerHome;
26
27
    /**
28
     * @var string
29
     */
30
    private $tempArtifact;
31
32
    public function setUp()
33
    {
34
        $this->tempGlobalComposerHome = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/global';
35
        $this->tempLocalComposerHome = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/local';
36
        $this->tempArtifact = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/artifacts';
37
        mkdir($this->tempGlobalComposerHome, 0700, true);
38
        mkdir($this->tempLocalComposerHome, 0700, true);
39
        mkdir($this->tempArtifact, 0700, true);
40
41
        putenv('COMPOSER_HOME=' . $this->tempGlobalComposerHome);
42
    }
43
44
    public function tearDown()
45
    {
46
        $this->rmDir($this->tempGlobalComposerHome);
47
        $this->rmDir($this->tempLocalComposerHome);
48
        $this->rmDir($this->tempArtifact);
49
50
        putenv('COMPOSER_HOME');
51
    }
52
53
    public function testGloballyInstalledPluginDoesNotGenerateVersionsForLocalProject()
54
    {
55
        $this->createPackageVersionsArtifact();
56
57
        $this->writeComposerJsonFile(
58
            [
59
                'name'         => 'package-versions/e2e-global',
60
                'require'      => [
61
                    'ocramius/package-versions' => '1.0.0'
62
                ],
63
                'repositories' => [
64
                    [
65
                        'packagist' => false,
66
                    ],
67
                    [
68
                        'type' => 'artifact',
69
                        'url' => $this->tempArtifact,
70
                    ]
71
                ]
72
            ],
73
            $this->tempGlobalComposerHome
74
        );
75
76
        $this->execComposerInDir('global update', $this->tempGlobalComposerHome);
77
78
        $this->createArtifact();
79
        $this->writeComposerJsonFile(
80
            [
81
                'name'         => 'package-versions/e2e-local',
82
                'require'      => [
83
                    'test/package' => '2.0.0'
84
                ],
85
                'repositories' => [
86
                    [
87
                        'packagist' => false,
88
                    ],
89
                    [
90
                        'type' => 'artifact',
91
                        'url' => $this->tempArtifact,
92
                    ]
93
                ]
94
            ],
95
            $this->tempLocalComposerHome
96
        );
97
98
        $this->execComposerInDir('update', $this->tempLocalComposerHome);
99
        $this->assertFileNotExists(
100
            $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
101
        );
102
    }
103
104
    public function testRemovingPluginDoesNotAttemptToGenerateVersions()
105
    {
106
        $this->createPackageVersionsArtifact();
107
        $this->createArtifact();
108
109
        $this->writeComposerJsonFile(
110
            [
111
                'name'         => 'package-versions/e2e-local',
112
                'require'      => [
113
                    'test/package' => '2.0.0',
114
                    'ocramius/package-versions' => '1.0.0'
115
                ],
116
                'repositories' => [
117
                    [
118
                        'packagist' => false,
119
                    ],
120
                    [
121
                        'type' => 'artifact',
122
                        'url' => $this->tempArtifact,
123
                    ]
124
                ]
125
            ],
126
            $this->tempLocalComposerHome
127
        );
128
129
        $this->execComposerInDir('update', $this->tempLocalComposerHome);
130
        $this->assertFileExists(
131
            $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
132
        );
133
134
        $this->execComposerInDir('remove ocramius/package-versions', $this->tempLocalComposerHome);
135
136
        $this->assertFileNotExists(
137
            $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
138
        );
139
    }
140
141
    /**
142
     * @group #41
143
     * @group #46
144
     */
145
    public function testRemovingPluginWithNoDevDoesNotAttemptToGenerateVersions()
146
    {
147
        $this->createPackageVersionsArtifact();
148
        $this->createArtifact();
149
150
        $this->writeComposerJsonFile(
151
            [
152
                'name'         => 'package-versions/e2e-local',
153
                'require-dev'      => [
154
                    'ocramius/package-versions' => '1.0.0'
155
                ],
156
                'repositories' => [
157
                    [
158
                        'packagist' => false,
159
                    ],
160
                    [
161
                        'type' => 'artifact',
162
                        'url' => $this->tempArtifact,
163
                    ]
164
                ]
165
            ],
166
            $this->tempLocalComposerHome
167
        );
168
169
        $this->execComposerInDir('update', $this->tempLocalComposerHome);
170
        $this->assertFileExists(
171
            $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
172
        );
173
174
        $this->execComposerInDir('install --no-dev', $this->tempLocalComposerHome);
175
176
        $this->assertFileNotExists(
177
            $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
178
        );
179
    }
180
181
    private function createPackageVersionsArtifact()
182
    {
183
        $zip = new ZipArchive();
184
185
        $zip->open($this->tempArtifact . '/ocramius-package-versions-1.0.0.zip', ZipArchive::CREATE);
186
187
        $files = array_filter(
188
            iterator_to_array(new RecursiveIteratorIterator(
189
                new RecursiveCallbackFilterIterator(
190
                    new RecursiveDirectoryIterator(realpath(__DIR__ . '/../../'), RecursiveDirectoryIterator::SKIP_DOTS),
191
                    function (SplFileInfo $file, $key, RecursiveDirectoryIterator $iterator) {
192
                        return $iterator->getSubPathname()[0]  !== '.' && $iterator->getSubPathname() !== 'vendor';
193
                    }
194
                ),
195
                RecursiveIteratorIterator::LEAVES_ONLY
196
            )),
197
            function (SplFileInfo $file) {
198
                return !$file->isDir();
199
            }
200
        );
201
202
        array_walk(
203
            $files,
204
            function (SplFileInfo $file) use ($zip) {
205
                if ($file->getFilename() === 'composer.json') {
206
                    $contents = json_decode(file_get_contents($file->getRealPath()), true);
207
                    $contents['version'] = '1.0.0';
208
209
                    return $zip->addFromString('composer.json', json_encode($contents));
210
                }
211
212
                $zip->addFile(
213
                    $file->getRealPath(),
214
                    substr($file->getRealPath(), strlen(realpath(__DIR__ . '/../../')) + 1)
215
                );
216
            }
217
        );
218
219
        $zip->close();
220
    }
221
222
    private function createArtifact()
223
    {
224
        $zip = new ZipArchive();
225
226
        $zip->open($this->tempArtifact . '/test-package-2.0.0.zip', ZipArchive::CREATE);
227
        $zip->addFromString(
228
            'composer.json',
229
            json_encode(
230
                [
231
                    'name'    => 'test/package',
232
                    'version' => '2.0.0'
233
                ],
234
                JSON_PRETTY_PRINT
235
            )
236
        );
237
        $zip->close();
238
    }
239
240
    private function writeComposerJsonFile(array $config, string $directory)
241
    {
242
        file_put_contents(
243
            $directory . '/composer.json',
244
            json_encode($config, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
245
        );
246
    }
247
248
    private function execComposerInDir(string $command, string $dir) : array
249
    {
250
        $currentDir = getcwd();
251
        chdir($dir);
252
        exec(__DIR__ . '/../../vendor/bin/composer ' . $command . ' 2> /dev/null', $output, $exitCode);
253
        $this->assertEquals(0, $exitCode);
254
        chdir($currentDir);
255
        return $output;
256
    }
257
258
    /**
259
     * @param string $directory
260
     *
261
     * @return void
262
     */
263
    private function rmDir(string $directory)
264
    {
265
        if (! is_dir($directory)) {
266
            unlink($directory);
267
268
            return;
269
        }
270
271
        array_map(
272
            function ($item) use ($directory) {
273
                $this->rmDir($directory . '/' . $item);
274
            },
275
            array_filter(
276
                scandir($directory),
277
                function (string $dirItem) {
278
                    return ! in_array($dirItem, ['.', '..'], true);
279
                }
280
            )
281
        );
282
283
        rmdir($directory);
284
    }
285
}
286