Completed
Pull Request — master (#46)
by Alessandro
02:01
created

E2EInstaller   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 267
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
    public function testRemovingPluginWithNoDevDoesNotAttemptToGenerateVersions()
142
    {
143
        $this->createPackageVersionsArtifact();
144
        $this->createArtifact();
145
146
        $this->writeComposerJsonFile(
147
            [
148
                'name'         => 'package-versions/e2e-local',
149
                'require-dev'      => [
150
                    'ocramius/package-versions' => '1.0.0'
151
                ],
152
                'repositories' => [
153
                    [
154
                        'packagist' => false,
155
                    ],
156
                    [
157
                        'type' => 'artifact',
158
                        'url' => $this->tempArtifact,
159
                    ]
160
                ]
161
            ],
162
            $this->tempLocalComposerHome
163
        );
164
165
        $this->execComposerInDir('update', $this->tempLocalComposerHome);
166
        $this->assertFileExists(
167
            $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
168
        );
169
170
        $this->execComposerInDir('install --no-dev', $this->tempLocalComposerHome);
171
172
        $this->assertFileNotExists(
173
            $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
174
        );
175
    }
176
177
    private function createPackageVersionsArtifact()
178
    {
179
        $zip = new ZipArchive();
180
181
        $zip->open($this->tempArtifact . '/ocramius-package-versions-1.0.0.zip', ZipArchive::CREATE);
182
183
        $files = array_filter(
184
            iterator_to_array(new RecursiveIteratorIterator(
185
                new RecursiveCallbackFilterIterator(
186
                    new RecursiveDirectoryIterator(realpath(__DIR__ . '/../../'), RecursiveDirectoryIterator::SKIP_DOTS),
187
                    function (SplFileInfo $file, $key, RecursiveDirectoryIterator $iterator) {
188
                        return $iterator->getSubPathname()[0]  !== '.' && $iterator->getSubPathname() !== 'vendor';
189
                    }
190
                ),
191
                RecursiveIteratorIterator::LEAVES_ONLY
192
            )),
193
            function (SplFileInfo $file) {
194
                return !$file->isDir();
195
            }
196
        );
197
198
        array_walk(
199
            $files,
200
            function (SplFileInfo $file) use ($zip) {
201
                if ($file->getFilename() === 'composer.json') {
202
                    $contents = json_decode(file_get_contents($file->getRealPath()), true);
203
                    $contents['version'] = '1.0.0';
204
205
                    return $zip->addFromString('composer.json', json_encode($contents));
206
                }
207
208
                $zip->addFile(
209
                    $file->getRealPath(),
210
                    substr($file->getRealPath(), strlen(realpath(__DIR__ . '/../../')) + 1)
211
                );
212
            }
213
        );
214
215
        $zip->close();
216
    }
217
218
    private function createArtifact()
219
    {
220
        $zip = new ZipArchive();
221
222
        $zip->open($this->tempArtifact . '/test-package-2.0.0.zip', ZipArchive::CREATE);
223
        $zip->addFromString(
224
            'composer.json',
225
            json_encode(
226
                [
227
                    'name'    => 'test/package',
228
                    'version' => '2.0.0'
229
                ],
230
                JSON_PRETTY_PRINT
231
            )
232
        );
233
        $zip->close();
234
    }
235
236
    private function writeComposerJsonFile(array $config, string $directory)
237
    {
238
        file_put_contents(
239
            $directory . '/composer.json',
240
            json_encode($config, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
241
        );
242
    }
243
244
    private function execComposerInDir(string $command, string $dir) : array
245
    {
246
        $currentDir = getcwd();
247
        chdir($dir);
248
        exec(__DIR__ . '/../../vendor/bin/composer ' . $command . ' 2> /dev/null', $output, $exitCode);
249
        $this->assertEquals(0, $exitCode);
250
        chdir($currentDir);
251
        return $output;
252
    }
253
254
    /**
255
     * @param string $directory
256
     *
257
     * @return void
258
     */
259
    private function rmDir(string $directory)
260
    {
261
        if (! is_dir($directory)) {
262
            unlink($directory);
263
264
            return;
265
        }
266
267
        array_map(
268
            function ($item) use ($directory) {
269
                $this->rmDir($directory . '/' . $item);
270
            },
271
            array_filter(
272
                scandir($directory),
273
                function (string $dirItem) {
274
                    return ! in_array($dirItem, ['.', '..'], true);
275
                }
276
            )
277
        );
278
279
        rmdir($directory);
280
    }
281
}
282