-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
MakeCommandTest.php
541 lines (419 loc) · 17.3 KB
/
MakeCommandTest.php
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<?php
namespace Tests;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Laravel\Homestead\MakeCommand;
use Laravel\Homestead\Traits\GeneratesSlugs;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Yaml\Yaml;
use Tests\Traits\GeneratesTestDirectory;
class MakeCommandTest extends TestCase
{
use ArraySubsetAsserts, GeneratesSlugs, GeneratesTestDirectory;
/** @test */
public function it_displays_a_success_message()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertStringContainsString('Homestead Installed!', $tester->getDisplay());
}
/** @test */
public function it_returns_a_success_status_code()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertEquals(0, $tester->getStatusCode());
}
/** @test */
public function a_vagrantfile_is_created_if_it_does_not_exists()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Vagrantfile');
$this->assertFileEquals(
self::$testDirectory.DIRECTORY_SEPARATOR.'Vagrantfile',
__DIR__.'/../resources/localized/Vagrantfile'
);
}
/** @test */
public function an_existing_vagrantfile_is_not_overwritten()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Vagrantfile',
'Already existing Vagrantfile'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertStringEqualsFile(
self::$testDirectory.DIRECTORY_SEPARATOR.'Vagrantfile',
'Already existing Vagrantfile'
);
}
/** @test */
public function an_aliases_file_is_created_by_default()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'aliases');
$this->assertFileEquals(
__DIR__.'/../resources/aliases',
self::$testDirectory.DIRECTORY_SEPARATOR.'aliases'
);
}
/** @test */
public function a_localized_aliases_file_is_created_by_default_in_per_project_installations()
{
$this->markTestSkipped('Currently unable to emulate a per project installation');
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'aliases');
$this->assertFileEquals(
__DIR__.'/../resources/localized/aliases',
self::$testDirectory.DIRECTORY_SEPARATOR.'aliases'
);
}
/** @test */
public function an_existing_aliases_file_is_not_overwritten()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'aliases',
'Already existing aliases'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'aliases');
$this->assertStringEqualsFile(
self::$testDirectory.DIRECTORY_SEPARATOR.'aliases',
'Already existing aliases'
);
}
/** @test */
public function an_aliases_file_is_not_created_if_it_is_explicitly_told_to()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--no-aliases' => true,
]);
$this->assertFileDoesNotExist(self::$testDirectory.DIRECTORY_SEPARATOR.'aliases');
}
/** @test */
public function an_after_shell_script_is_created_by_default()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'after.sh');
$this->assertFileEquals(
__DIR__.'/../resources/after.sh',
self::$testDirectory.DIRECTORY_SEPARATOR.'after.sh'
);
}
/** @test */
public function an_existing_after_shell_script_is_not_overwritten()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'after.sh',
'Already existing after.sh'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'after.sh');
$this->assertStringEqualsFile(
self::$testDirectory.DIRECTORY_SEPARATOR.'after.sh',
'Already existing after.sh'
);
}
/** @test */
public function an_after_file_is_not_created_if_it_is_explicitly_told_to()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--no-after' => true,
]);
$this->assertFileDoesNotExist(self::$testDirectory.DIRECTORY_SEPARATOR.'after.sh');
}
/** @test */
public function an_example_homestead_yaml_settings_is_created_if_requested()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--example' => true,
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml.example');
}
/** @test */
public function an_existing_example_homestead_yaml_settings_is_not_overwritten()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml.example',
'name: Already existing Homestead.yaml.example'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--example' => true,
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml.example');
$this->assertStringEqualsFile(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml.example',
'name: Already existing Homestead.yaml.example'
);
}
/** @test */
public function an_example_homestead_json_settings_is_created_if_requested()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--example' => true,
'--json' => true,
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json.example');
}
/** @test */
public function an_existing_example_homestead_json_settings_is_not_overwritten()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json.example',
'{"name": "Already existing Homestead.json.example"}'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--example' => true,
'--json' => true,
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json.example');
$this->assertStringEqualsFile(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json.example',
'{"name": "Already existing Homestead.json.example"}'
);
}
/** @test */
public function a_homestead_yaml_settings_is_created_if_it_is_does_not_exists()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml');
}
/** @test */
public function an_existing_homestead_yaml_settings_is_not_overwritten()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml',
'name: Already existing Homestead.yaml'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertStringEqualsFile(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml',
'name: Already existing Homestead.yaml'
);
}
/** @test */
public function a_homestead_json_settings_is_created_if_it_is_requested_and_it_does_not_exists()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--json' => true,
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json');
}
/** @test */
public function an_existing_homestead_json_settings_is_not_overwritten()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json',
'{"message": "Already existing Homestead.json"}'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertStringEqualsFile(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json',
'{"message": "Already existing Homestead.json"}'
);
}
/** @test */
public function a_homestead_yaml_settings_is_created_from_a_homestead_yaml_example_if_it_exists()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml.example',
"message: 'Already existing Homestead.yaml.example'"
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml');
$this->assertStringContainsString(
"message: 'Already existing Homestead.yaml.example'",
file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml')
);
}
/** @test */
public function a_homestead_yaml_settings_created_from_a_homestead_yaml_example_can_override_the_ip_address()
{
copy(
__DIR__.'/../resources/Homestead.yaml',
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml.example'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--ip' => '192.168.10.11',
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml');
$settings = Yaml::parse(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml'));
$this->assertEquals('192.168.10.11', $settings['ip']);
}
/** @test */
public function a_homestead_json_settings_is_created_from_a_homestead_json_example_if_is_requested_and_if_it_exists()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json.example',
'{"message": "Already existing Homestead.json.example"}'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--json' => true,
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json');
$this->assertStringContainsString(
'"message": "Already existing Homestead.json.example"',
file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json')
);
}
/** @test */
public function a_homestead_json_settings_created_from_a_homestead_json_example_can_override_the_ip_address()
{
copy(
__DIR__.'/../resources/Homestead.json',
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json.example'
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--json' => true,
'--ip' => '192.168.10.11',
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json');
$settings = json_decode(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json'), true);
$this->assertEquals('192.168.10.11', $settings['ip']);
}
/** @test */
public function a_homestead_yaml_settings_can_be_created_with_some_command_options_overrides()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--name' => 'test_name',
'--hostname' => 'test_hostname',
'--ip' => '127.0.0.1',
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml');
$settings = Yaml::parse(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml'));
self::assertArraySubset([
'name' => 'test_name',
'hostname' => 'test_hostname',
'ip' => '127.0.0.1',
], $settings);
}
/** @test */
public function a_homestead_json_settings_can_be_created_with_some_command_options_overrides()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--json' => true,
'--name' => 'test_name',
'--hostname' => 'test_hostname',
'--ip' => '127.0.0.1',
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json');
$settings = json_decode(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json'), true);
self::assertArraySubset([
'name' => 'test_name',
'hostname' => 'test_hostname',
'ip' => '127.0.0.1',
], $settings);
}
/** @test */
public function a_homestead_yaml_settings_has_preconfigured_sites()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml');
$settings = Yaml::parse(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml'));
$this->assertEquals([
'map' => 'homestead.test',
'to' => '/home/vagrant/code/public',
], $settings['sites'][0]);
}
/** @test */
public function a_homestead_json_settings_has_preconfigured_sites()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--json' => true,
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json');
$settings = json_decode(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json'), true);
$this->assertEquals([
'map' => 'homestead.test',
'to' => '/home/vagrant/code/public',
], $settings['sites'][0]);
}
/** @test */
public function a_homestead_yaml_settings_has_preconfigured_shared_folders()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml');
$projectDirectory = basename(getcwd());
$projectName = $this->slug($projectDirectory);
$settings = Yaml::parse(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml'));
// The "map" is not tested for equality because getcwd() (The method to obtain the project path)
// returns a directory in a different location that the test directory itself.
//
// Example:
// - project directory: /private/folders/...
// - test directory: /var/folders/...
//
// The curious thing is that both directories point to the same location.
//
$this->assertMatchesRegularExpression("/{$projectDirectory}/", $settings['folders'][0]['map']);
$this->assertEquals('/home/vagrant/code', $settings['folders'][0]['to']);
$this->assertEquals($projectName, $settings['name']);
$this->assertEquals($projectName, $settings['hostname']);
}
/** @test */
public function a_homestead_json_settings_has_preconfigured_shared_folders()
{
$tester = new CommandTester(new MakeCommand());
$tester->execute([
'--json' => true,
]);
$this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json');
$projectDirectory = basename(getcwd());
$projectName = $this->slug($projectDirectory);
$settings = json_decode(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json'), true);
// The "map" is not tested for equality because getcwd() (The method to obtain the project path)
// returns a directory in a different location that the test directory itself.
//
// Example:
// - project directory: /private/folders/...
// - test directory: /var/folders/...
//
// The curious thing is that both directories point to the same location.
//
$this->assertMatchesRegularExpression("/{$projectDirectory}/", $settings['folders'][0]['map']);
$this->assertEquals('/home/vagrant/code', $settings['folders'][0]['to']);
$this->assertEquals($projectName, $settings['name']);
$this->assertEquals($projectName, $settings['hostname']);
}
/** @test */
public function a_warning_is_thrown_if_the_homestead_settings_json_and_yaml_exists_at_the_same_time()
{
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json',
'{"message": "Already existing Homestead.json"}'
);
file_put_contents(
self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml',
"message: 'Already existing Homestead.yaml'"
);
$tester = new CommandTester(new MakeCommand());
$tester->execute([]);
$this->assertStringContainsString('WARNING! You have Homestead.yaml AND Homestead.json configuration files', $tester->getDisplay());
}
}