ApplicationTest.php
8.17 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
<?php
use Illuminate\Config\Repository as ConfigRepository;
use Roots\Acorn\Application;
use Roots\Acorn\Tests\Test\Stubs\BootableServiceProvider;
use Roots\Acorn\Tests\Test\TestCase;
use function Roots\Acorn\Tests\mock;
use function Roots\Acorn\Tests\temp;
uses(TestCase::class);
it('instantiates with custom paths', function () {
$app = new Application(null, [
'app' => $this->fixture('use_paths/app'),
'config' => $this->fixture('use_paths/config'),
]);
expect($app['path'])->toBe($this->fixture('use_paths/app'));
expect($app['path.config'])->toBe($this->fixture('use_paths/config'));
});
it('rejects invalid custom path types', function () {
$app = new Application();
$app->usePaths([
'app' => $this->fixture('use_paths/app'),
'not_a_valid_path_type' => $this->fixture('use_paths/resources/lang'),
]);
})->throws(Exception::class);
it('allows invalid public path', function () {
$app = new Application();
$app->usePaths([
'app' => $this->fixture('use_paths/app'),
'public' => __DIR__ . '/this/does/not/exist',
]);
})->not->throws(Exception::class);
it('allows invalid lang path', function () {
$app = new Application();
$app->usePaths([
'app' => $this->fixture('use_paths/app'),
'lang' => __DIR__ . '/this/does/not/exist',
]);
})->not->throws(Exception::class);
it('accepts an array of custom paths', function () {
$app = new Application(temp('base_path'));
expect($app['path'])->not->toBe($this->fixture('use_paths/app'));
expect($app['path.lang'])->not->toBe($this->fixture('use_paths/resources/lang'));
expect($app['path.config'])->not->toBe($this->fixture('use_paths/config'));
expect($app['path.public'])->not->toBe($this->fixture('use_paths/public'));
expect($app['path.storage'])->not->toBe($this->fixture('use_paths/storage'));
expect($app['path.database'])->not->toBe($this->fixture('use_paths/database'));
expect($app['path.resources'])->not->toBe($this->fixture('use_paths/resources'));
expect($app['path.bootstrap'])->not->toBe($this->fixture('use_paths/bootstrap'));
$app->usePaths([
'app' => $this->fixture('use_paths/app'),
'lang' => $this->fixture('use_paths/resources/lang'),
'config' => $this->fixture('use_paths/config'),
'public' => $this->fixture('use_paths/public'),
'storage' => $this->fixture('use_paths/storage'),
'database' => $this->fixture('use_paths/database'),
'resources' => $this->fixture('use_paths/resources'),
'bootstrap' => $this->fixture('use_paths/bootstrap'),
]);
expect($app['path'])->toBe($this->fixture('use_paths/app'));
expect($app['path.lang'])->toBe($this->fixture('use_paths/resources/lang'));
expect($app['path.config'])->toBe($this->fixture('use_paths/config'));
expect($app['path.public'])->toBe($this->fixture('use_paths/public'));
expect($app['path.storage'])->toBe($this->fixture('use_paths/storage'));
expect($app['path.database'])->toBe($this->fixture('use_paths/database'));
expect($app['path.resources'])->toBe($this->fixture('use_paths/resources'));
expect($app['path.bootstrap'])->toBe($this->fixture('use_paths/bootstrap'));
});
it('allows specific paths to be changed', function () {
$app = new Application(temp('not_a_path'));
expect($app['path.bootstrap'])->not->toBe($this->fixture('use_paths/bootstrap'));
$app->useBootstrapPath($this->fixture('use_paths/bootstrap'));
expect($app['path.bootstrap'])->toBe($this->fixture('use_paths/bootstrap'));
expect($app['path.config'])->not->toBe($this->fixture('use_paths/config'));
$app->useConfigPath($this->fixture('use_paths/config'));
expect($app['path.config'])->toBe($this->fixture('use_paths/config'));
expect($app['path.public'])->not->toBe($this->fixture('use_paths/public'));
$app->usePublicPath($this->fixture('use_paths/public'));
expect($app['path.public'])->toBe($this->fixture('use_paths/public'));
expect($app['path.resources'])->not->toBe($this->fixture('use_paths/resource'));
$app->useResourcePath($this->fixture('use_paths/resource'));
expect($app['path.resources'])->toBe($this->fixture('use_paths/resource'));
});
it('goes down for maintenance when acorn maintenance file exists', function () {
$app = new Application();
expect($app->isDownForMaintenance())->toBeFalse();
$app->useStoragePath($this->fixture('is_down_for_maintenance/storage'));
expect($app->isDownForMaintenance())->toBeTrue();
});
it('goes down for maintenance when wordpress maintenance file exists', function () {
$app = new Application();
expect($app->isDownForMaintenance())->toBeFalse();
touch(temp(('wp/.maintenance')));
expect($app->isDownForMaintenance())->toBeTrue();
});
it('throws an exception if app namespace cannot be determined', function () {
(new Application($this->fixture('get_namespace/a_bedrock_site/a_random_library')))->getNamespace();
})->throws(RuntimeException::class);
it('determines namespace based on app composer.json', function () {
$app = new Application($this->fixture('get_namespace/a_sage_theme'));
expect($app->getNamespace())->toBe('Sage\\');
});
it('determines namespace based on app ancestor composer.json', function () {
$app = new Application($this->fixture('get_namespace/a_bedrock_site/a_sage_theme'));
expect($app->getNamespace())->toBe('Bedrock\\Sage\\');
});
it('allows the app namespace to changed arbitrarily', function () {
$app = new Application($this->fixture('get_namespace/a_sage_theme'));
expect($app->getNamespace())->not->toBe('App\\');
$app->useNamespace('App');
expect($app->getNamespace())->toBe('App\\');
});
it('makes a thing', function () {
$app = new Application();
$app->bind('config', fn () => new ConfigRepository());
expect($app->make('config'))->toBeInstanceOf(ConfigRepository::class);
});
it('boots a provider', function () {
$provider = mock(BootableServiceProvider::class)->makePartial();
$app = new Application();
$provider->shouldReceive('register', 'boot')->once();
$app->register($provider);
$app->boot();
});
it('gracefully skips a provider that fails to boot', function () {
$handler = mock(\Illuminate\Contracts\Debug\ExceptionHandler::class);
$manifest = mock(\Roots\Acorn\PackageManifest::class);
$app = new Application();
$app->singleton(\Illuminate\Contracts\Debug\ExceptionHandler::class, fn () => $handler);
$app->singleton(\Illuminate\Foundation\PackageManifest::class, fn () => $manifest);
// the core of this test is to make sure that when a class or function is called that
// does not exist, things don't blow up.
$provider = new class($app) extends BootableServiceProvider {
public function boot()
{
new \kjo();
}
};
$handler
->shouldReceive('report')
->withArgs(fn (\Roots\Acorn\Exceptions\SkipProviderException $e) => expect($e->getMessage())->toContain('Skipping provider') || true)
->once();
$manifest
->shouldReceive('getPackage')
->andReturn(get_class($provider));
$app->register($provider);
$app->boot();
});
it('gracefully skips a provider that does not exist', function () {
$handler = mock(\Illuminate\Contracts\Debug\ExceptionHandler::class);
$manifest = mock(\Roots\Acorn\PackageManifest::class);
$app = new Application();
$app->singleton(\Illuminate\Contracts\Debug\ExceptionHandler::class, fn () => $handler);
$app->singleton(\Illuminate\Foundation\PackageManifest::class, fn () => $manifest);
$handler
->shouldReceive('report')
->withArgs(fn (\Roots\Acorn\Exceptions\SkipProviderException $e) => expect($e->getMessage())->toContain('Skipping provider') || true)
->once();
$manifest
->shouldReceive('getPackage')
->andReturn(ThisProviderDoesNotExist::class);
$app->register(ThisProviderDoesNotExist::class);
$app->boot();
});
it('uses custom aliases', function () {
$app = new Application();
expect($app->getAlias(\Roots\Acorn\Application::class))->toBe('app');
expect($app->getAlias(\Roots\Acorn\PackageManifest::class))->toBe(\Illuminate\Foundation\PackageManifest::class);
});