-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathNewCommandIntegrationTest.php
More file actions
196 lines (151 loc) · 5.63 KB
/
NewCommandIntegrationTest.php
File metadata and controls
196 lines (151 loc) · 5.63 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
<?php
namespace Statamic\Cli\Tests;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Statamic\Cli\NewCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
class NewCommandIntegrationTest extends TestCase
{
protected $scaffoldName;
protected $scaffoldDirectory;
public function setUp(): void
{
parent::setUp();
$this->scaffoldName = 'tests-output/my-app';
$this->scaffoldDirectory = __DIR__.'/../'.$this->scaffoldName;
$this->clearScaffoldDirectory();
}
public function tearDown(): void
{
$this->clearScaffoldDirectory();
parent::tearDown();
}
/** @test */
public function it_can_scaffold_a_new_statamic_app()
{
$this->assertAppNotExists();
$statusCode = $this->scaffoldNewApp();
$this->assertSame(0, $statusCode);
$this->assertBasicAppScaffolded();
}
/** @test */
public function it_can_scaffold_a_new_statamic_app_with_a_starter_kit()
{
$this->assertAppNotExists();
$statusCode = $this->scaffoldNewApp(['starter-kit' => 'statamic/starter-kit-cool-writings']);
$this->assertSame(0, $statusCode);
$this->assertBasicAppScaffolded();
$this->assertFileExists($this->appPath('content/collections/articles/1994-07-05.magic.md'));
$this->assertFileExists($this->appPath('resources/blueprints/collections/articles/article.yaml'));
$this->assertFileNotExists($this->appPath('starter-kit.yaml'));
}
/** @test */
public function it_can_scaffold_with_starter_kit_config()
{
$this->assertAppNotExists();
$statusCode = $this->scaffoldNewApp(['starter-kit' => 'statamic/starter-kit-cool-writings', '--with-config' => true]);
$this->assertSame(0, $statusCode);
$this->assertBasicAppScaffolded();
$this->assertFileExists($this->appPath('content/collections/articles/1994-07-05.magic.md'));
$this->assertFileExists($this->appPath('resources/blueprints/collections/articles/article.yaml'));
$this->assertFileExists($this->appPath('starter-kit.yaml'));
}
/** @test */
public function it_fails_if_application_folder_already_exists()
{
mkdir($this->appPath());
$this->assertRuntimeException(function () {
$this->scaffoldNewApp();
});
$this->assertFileExists($this->appPath());
$this->assertFileNotExists($this->appPath('vendor'));
$this->assertFileNotExists($this->appPath('.env'));
$this->assertFileNotExists($this->appPath('artisan'));
$this->assertFileNotExists($this->appPath('please'));
}
/** @test */
public function it_overwrites_application_when_using_force_option()
{
mkdir($this->appPath());
file_put_contents($this->appPath('test.md'), 'test content');
$this->assertFileExists($this->appPath('test.md'));
$this->scaffoldNewApp(['--force' => true]);
$this->assertBasicAppScaffolded();
$this->assertFileNotExists($this->appPath('test.md'));
}
/** @test */
public function it_fails_if_using_force_option_to_cwd()
{
$this->assertRuntimeException(function () {
$this->scaffoldNewApp(['name' => '.', '--force' => true]);
});
$this->assertAppNotExists();
}
/** @test */
public function it_fails_if_invalid_starter_kit_repo_is_passed()
{
$this->assertRuntimeException(function () {
$this->scaffoldNewApp(['starter-kit' => 'not-a-valid-repo']);
});
$this->assertAppNotExists();
}
/** @test */
public function it_fails_when_there_is_starter_kit_error_but_leaves_base_installation()
{
$this->assertRuntimeException(function () {
$this->scaffoldNewApp(['starter-kit' => 'statamic/not-an-actual-starter-kit']);
});
$this->assertBasicAppScaffolded();
}
protected function assertRuntimeException($callback)
{
$error = false;
try {
$callback();
} catch (RuntimeException $exception) {
$error = true;
}
$this->assertTrue($error);
}
protected function clearScaffoldDirectory()
{
if (file_exists($this->scaffoldDirectory)) {
if (PHP_OS_FAMILY == 'Windows') {
exec("rd /s /q \"$this->scaffoldDirectory\"");
} else {
exec("rm -rf \"$this->scaffoldDirectory\"");
}
}
}
protected function appPath($path = null)
{
if ($path) {
return $this->scaffoldDirectory.'/'.$path;
}
return $this->scaffoldDirectory;
}
protected function scaffoldNewApp($args = [])
{
$app = new Application('Statamic Installer');
$app->add(new NewCommand);
$tester = new CommandTester($app->find('new'));
$args = array_merge(['name' => $this->scaffoldName], $args);
$statusCode = $tester->execute($args);
return $statusCode;
}
protected function assertBasicAppScaffolded()
{
$this->assertFileExists($this->appPath('vendor'));
$this->assertFileExists($this->appPath('.env'));
$this->assertFileExists($this->appPath('artisan'));
$this->assertFileExists($this->appPath('please'));
$envFile = file_get_contents($this->appPath('.env'));
$this->assertStringContainsString('APP_URL=http://my-app.test', $envFile);
$this->assertStringContainsString('DB_DATABASE=my_app', $envFile);
}
protected function assertAppNotExists()
{
$this->assertFileNotExists($this->appPath());
}
}