-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathComposerTest.php
More file actions
330 lines (257 loc) · 14.5 KB
/
ComposerTest.php
File metadata and controls
330 lines (257 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
namespace Tests\Composer;
use Facades\Statamic\Console\Processes\Composer;
use Illuminate\Support\Facades\Cache;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\Process\Process;
use Tests\Fakes\Composer\Package\PackToTheFuture;
use Tests\TestCase;
class ComposerTest extends TestCase
{
private $files;
public function setUp(): void
{
$this->markTestSkippedInWindows();
parent::setUp();
(new Process(['tar', '-xzvf', 'vendor.tar.gz'], $this->basePath()))->mustRun();
copy($this->basePath('composer.json'), $this->basePath('composer.json.bak'));
copy($this->basePath('composer.lock'), $this->basePath('composer.lock.bak'));
Cache::forget('composer.test/package');
Composer::swap(new \Statamic\Console\Processes\Composer($this->basePath()));
$this->files = app('files');
if (! $this->files->exists($tmpDir = $this->basePath('tmp'))) {
$this->files->makeDirectory($tmpDir, 0755, true);
}
}
public function tearDown(): void
{
// If the test was skipped, avoid trying to clean up. The setUp would've never happened.
if (! $this->files) {
parent::tearDown();
return;
}
$this->files->deleteDirectory($this->basePath('tmp'));
$this->files->deleteDirectory($this->basePath('vendor'));
$this->files->delete($this->basePath('composer.json'));
$this->files->delete($this->basePath('composer.lock'));
$this->files->move($this->basePath('composer.lock.bak'), $this->basePath('composer.lock'));
$this->files->move($this->basePath('composer.json.bak'), $this->basePath('composer.json'));
parent::tearDown();
}
#[Group('integration')]
#[Test]
public function it_can_list_installed_packages_with_details()
{
$installed = Composer::installed();
$this->assertNotEmpty($installed);
$this->assertContains('statamic/composer-test-example-dependency', $installed->keys());
$this->assertEquals('1.2.3', $installed->get('statamic/composer-test-example-dependency')->version);
$this->assertFalse($installed->get('statamic/composer-test-example-dependency')->dev);
$this->assertContains('statamic/composer-test-example-dev-dependency', $installed->keys());
$this->assertEquals('1.2.4', $installed->get('statamic/composer-test-example-dev-dependency')->version);
$this->assertTrue($installed->get('statamic/composer-test-example-dev-dependency')->dev);
}
#[Group('integration')]
#[Test]
public function it_can_get_installed_version_of_a_package_directly_from_composer_lock()
{
$this->assertEquals('1.2.3', Composer::installedVersion('statamic/composer-test-example-dependency'));
}
#[Group('integration')]
#[Test]
public function it_can_check_if_package_is_installed()
{
$this->assertTrue(Composer::isInstalled('statamic/composer-test-example-dependency'));
$this->assertFalse(Composer::isInstalled('statamic/another-dependency'));
}
#[Group('integration')]
#[Test]
public function it_can_get_installed_path_of_a_package()
{
$this->assertEquals(
__DIR__.'/__fixtures__/vendor/statamic/composer-test-example-dependency',
Composer::installedPath('statamic/composer-test-example-dependency')
);
}
#[Group('integration')]
#[Test]
public function it_gracefully_fails_when_lock_file_does_not_exist()
{
unlink($this->basePath('composer.lock'));
$installed = Composer::installed();
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $installed);
$this->assertEmpty($installed);
$this->assertNull(Composer::installedVersion('statamic/composer-test-example-dependency'));
}
#[Group('integration')]
#[Group('slow')]
#[Test]
/**
* This method is intentionally doing way too much, for the sake of test suite performance.
*/
public function it_can_require_update_downgrade_and_remove_a_package()
{
// Test that the package isn't installed yet...
$this->assertNotContains('test/package', Composer::installed()->keys());
$this->assertFileDoesNotExist($this->basePath('vendor/test/package'));
$this->assertFalse(Cache::has('composer.test/package'));
// Test that we can require a package...
PackToTheFuture::setVersion('1.0.0');
Composer::require('test/package');
$installed = Composer::installed();
$this->assertTrue($installed->keys()->contains('test/package'));
$this->assertFileExists($this->basePath('vendor/test/package'));
$this->assertEquals('1.0.0', $installed->get('test/package')->version);
$this->assertFalse($installed->get('test/package')->dev);
$this->assertStringContainsString('Installing test/package', Cache::get('composer.test/package')['output']);
// Test that we can update the package...
PackToTheFuture::setVersion('1.0.1');
Composer::update('test/package');
$installed = Composer::installed();
$this->assertTrue($installed->keys()->contains('test/package'));
$this->assertFileExists($this->basePath('vendor/test/package'));
$this->assertEquals('1.0.1', $installed->get('test/package')->version);
$this->assertFalse($installed->get('test/package')->dev);
$this->assertStringContainsString('Upgrading test/package', Cache::get('composer.test/package')['output']);
// Test that we can downgrade to a specific version...
PackToTheFuture::setVersion('1.0.0');
Composer::require('test/package', '1.0.0');
$installed = Composer::installed();
$this->assertTrue($installed->keys()->contains('test/package'));
$this->assertFileExists($this->basePath('vendor/test/package'));
$this->assertEquals('1.0.0', $installed->get('test/package')->version);
$this->assertFalse($installed->get('test/package')->dev);
$this->assertStringContainsString('Downgrading test/package', Cache::get('composer.test/package')['output']);
// Test that we can remove the package...
Composer::remove('test/package');
$this->assertStringNotContainsString('test/package', Composer::installed()->keys());
$this->assertFileDoesNotExist($this->basePath('vendor/test/package'));
$this->assertStringContainsString('Removing test/package', Cache::get('composer.test/package')['output']);
// Test that we can add extra params when requiring...
PackToTheFuture::setVersion('1.0.0');
Composer::require('test/package', '1.0.0', '--dry-run');
$installed = Composer::installed();
$this->assertFalse($installed->keys()->contains('test/package'));
$this->assertFileDoesNotExist($this->basePath('vendor/test/package'));
$this->assertStringContainsString('Installing test/package', Cache::get('composer.test/package')['output']);
// Test that we can add extra params when requiring a dev dependency...
PackToTheFuture::setVersion('1.0.0');
Composer::requireDev('test/package', '1.0.0', '--dry-run');
$installed = Composer::installed();
$this->assertFalse($installed->keys()->contains('test/package'));
$this->assertFileDoesNotExist($this->basePath('vendor/test/package'));
$this->assertStringContainsString('Installing test/package', Cache::get('composer.test/package')['output']);
// Test that we can require a package as a dev dependency...
PackToTheFuture::setVersion('1.0.0');
Composer::requireDev('test/package');
$installed = Composer::installed();
$this->assertTrue($installed->keys()->contains('test/package'));
$this->assertFileExists($this->basePath('vendor/test/package'));
$this->assertEquals('1.0.0', $installed->get('test/package')->version);
$this->assertTrue($installed->get('test/package')->dev);
$this->assertStringContainsString('Installing test/package', Cache::get('composer.test/package')['output']);
// Test that we can remove a dev package...
Composer::removeDev('test/package');
$this->assertStringNotContainsString('test/package', Composer::installed()->keys());
$this->assertFileDoesNotExist($this->basePath('vendor/test/package'));
$this->assertStringContainsString('Removing test/package', Cache::get('composer.test/package')['output']);
}
#[Group('integration')]
#[Group('slow')]
#[Test]
/**
* This method is intentionally doing way too much, for the sake of test suite performance.
*/
public function it_can_require_and_remove_multiple_packages_in_one_shot()
{
PackToTheFuture::generateComposerJson('test/one', '1.0.0', [], $this->basePath('tmp/one/composer.json'));
PackToTheFuture::generateComposerJson('test/two', '2.0.0', [], $this->basePath('tmp/two/composer.json'));
$repositories = [
'repositories' => [
['type' => 'path', 'url' => $this->basePath('tmp/one'), 'options' => ['symlink' => false]],
['type' => 'path', 'url' => $this->basePath('tmp/two'), 'options' => ['symlink' => false]],
],
];
PackToTheFuture::generateComposerJson('statamic/composer-test-app', '1.0.0', $repositories, $this->basePath('composer.json'));
// Test that the packages aren't installed yet...
$this->assertNotContains('test/one', Composer::installed()->keys());
$this->assertNotContains('test/two', Composer::installed()->keys());
$this->assertFileDoesNotExist($this->basePath('vendor/test/one'));
$this->assertFileDoesNotExist($this->basePath('vendor/test/two'));
// Test that we can require multiple packages...
Composer::requireMultiple([
'test/one',
'test/two' => '2.0.0', // Test it can require explicit version.
]);
$installed = Composer::installed();
$output = Cache::get('composer.test/one')['output'];
$this->assertTrue($installed->keys()->contains('test/one'));
$this->assertTrue($installed->keys()->contains('test/two'));
$this->assertFileExists($this->basePath('vendor/test/one'));
$this->assertFileExists($this->basePath('vendor/test/two'));
$this->assertEquals('1.0.0', $installed->get('test/one')->version);
$this->assertEquals('2.0.0', $installed->get('test/two')->version);
$this->assertFalse($installed->get('test/one')->dev);
$this->assertFalse($installed->get('test/two')->dev);
$this->assertStringContainsString('Installing test/one', $output);
$this->assertStringContainsString('Installing test/two', $output);
// Test that we can remove multiple packages...
Composer::removeMultiple(['test/one', 'test/two']);
$output = Cache::get('composer.test/one')['output'];
$this->assertStringNotContainsString('test/one', Composer::installed()->keys());
$this->assertStringNotContainsString('test/two', Composer::installed()->keys());
$this->assertFileDoesNotExist($this->basePath('vendor/test/one'));
$this->assertFileDoesNotExist($this->basePath('vendor/test/two'));
$this->assertStringContainsString('Removing test/one', $output);
$this->assertStringContainsString('Removing test/two', $output);
// Test that we can add extra params when requiring...
Composer::requireMultiple(['test/one', 'test/two'], '--dry-run');
$output = Cache::get('composer.test/one')['output'];
$this->assertStringNotContainsString('test/one', Composer::installed()->keys());
$this->assertStringNotContainsString('test/two', Composer::installed()->keys());
$this->assertFileDoesNotExist($this->basePath('vendor/test/one'));
$this->assertFileDoesNotExist($this->basePath('vendor/test/two'));
$this->assertStringContainsString('Installing test/one', $output);
$this->assertStringContainsString('Installing test/two', $output);
// Test that we can add extra params when requiring dev dependencies...
Composer::requireMultipleDev(['test/one', 'test/two'], '--dry-run');
$output = Cache::get('composer.test/one')['output'];
$this->assertStringNotContainsString('test/one', Composer::installed()->keys());
$this->assertStringNotContainsString('test/two', Composer::installed()->keys());
$this->assertFileDoesNotExist($this->basePath('vendor/test/one'));
$this->assertFileDoesNotExist($this->basePath('vendor/test/two'));
$this->assertStringContainsString('Installing test/one', $output);
$this->assertStringContainsString('Installing test/two', $output);
// Test that we can require multiple packages as dev dependencies...
Composer::requireMultipleDev([
'test/one',
'test/two' => '2.0.0', // Test it can require explicit version.
]);
$installed = Composer::installed();
$output = Cache::get('composer.test/one')['output'];
$this->assertTrue($installed->keys()->contains('test/one'));
$this->assertTrue($installed->keys()->contains('test/two'));
$this->assertFileExists($this->basePath('vendor/test/one'));
$this->assertFileExists($this->basePath('vendor/test/two'));
$this->assertEquals('1.0.0', $installed->get('test/one')->version);
$this->assertEquals('2.0.0', $installed->get('test/two')->version);
$this->assertTrue($installed->get('test/one')->dev);
$this->assertTrue($installed->get('test/two')->dev);
$this->assertStringContainsString('Installing test/one', $output);
$this->assertStringContainsString('Installing test/two', $output);
// Test that we can remove multiple dev dependencies...
Composer::removeMultipleDev(['test/one', 'test/two']);
$output = Cache::get('composer.test/one')['output'];
$this->assertStringNotContainsString('test/one', Composer::installed()->keys());
$this->assertStringNotContainsString('test/two', Composer::installed()->keys());
$this->assertFileDoesNotExist($this->basePath('vendor/test/one'));
$this->assertFileDoesNotExist($this->basePath('vendor/test/two'));
$this->assertStringContainsString('Removing test/one', $output);
$this->assertStringContainsString('Removing test/two', $output);
}
private function basePath($path = null)
{
return __DIR__.'/__fixtures__/'.$path;
}
}