-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathTestCase.php
More file actions
277 lines (216 loc) · 9.59 KB
/
TestCase.php
File metadata and controls
277 lines (216 loc) · 9.59 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
<?php
namespace Tests;
use Illuminate\Testing\Assert as IlluminateAssert;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Assert;
use Statamic\Facades\Config;
use Statamic\Facades\Site;
use Statamic\Http\Middleware\CP\AuthenticateSession;
abstract class TestCase extends \Orchestra\Testbench\TestCase
{
use WindowsHelpers;
protected $shouldFakeVersion = true;
protected $shouldPreventNavBeingBuilt = true;
protected $fakeStacheDirectory = __DIR__.'/__fixtures__/dev-null';
protected function setUp(): void
{
parent::setUp();
$this->withoutVite();
$this->withoutMiddleware(AuthenticateSession::class);
$uses = array_flip(class_uses_recursive(static::class));
if (isset($uses[PreventSavingStacheItemsToDisk::class])) {
$this->preventSavingStacheItemsToDisk();
}
if ($this->shouldFakeVersion) {
\Facades\Statamic\Version::shouldReceive('get')->andReturn('3.0.0-testing');
}
if ($this->shouldPreventNavBeingBuilt) {
\Statamic\Facades\CP\Nav::shouldReceive('build')->andReturn(collect());
\Statamic\Facades\CP\Nav::shouldReceive('clearCachedUrls');
}
$this->addGqlMacros();
}
public function tearDown(): void
{
$uses = array_flip(class_uses_recursive(static::class));
if (isset($uses[PreventSavingStacheItemsToDisk::class])) {
$this->deleteFakeStacheDirectory();
}
parent::tearDown();
}
protected function getPackageProviders($app)
{
return [
\Statamic\Providers\StatamicServiceProvider::class,
\Rebing\GraphQL\GraphQLServiceProvider::class,
\Wilderborn\Partyline\ServiceProvider::class,
\Archetype\ServiceProvider::class,
\Spatie\LaravelRay\RayServiceProvider::class,
];
}
protected function getPackageAliases($app)
{
return ['Statamic' => 'Statamic\Statamic'];
}
protected function resolveApplicationConfiguration($app)
{
parent::resolveApplicationConfiguration($app);
$configs = [
'assets', 'cp', 'forms', 'routes', 'static_caching',
'stache', 'system', 'users',
];
foreach ($configs as $config) {
$app['config']->set("statamic.$config", require (__DIR__."/../config/{$config}.php"));
}
}
protected function getEnvironmentSetUp($app)
{
$app['config']->set('auth.providers.users.driver', 'statamic');
$app['config']->set('statamic.stache.watcher', false);
$app['config']->set('statamic.users.repository', 'file');
$app['config']->set('statamic.stache.stores.users', [
'class' => \Statamic\Stache\Stores\UsersStore::class,
'directory' => __DIR__.'/__fixtures__/users',
]);
$app['config']->set('statamic.stache.stores.taxonomies.directory', __DIR__.'/__fixtures__/content/taxonomies');
$app['config']->set('statamic.stache.stores.terms.directory', __DIR__.'/__fixtures__/content/taxonomies');
$app['config']->set('statamic.stache.stores.collections.directory', __DIR__.'/__fixtures__/content/collections');
$app['config']->set('statamic.stache.stores.entries.directory', __DIR__.'/__fixtures__/content/collections');
$app['config']->set('statamic.stache.stores.navigation.directory', __DIR__.'/__fixtures__/content/navigation');
$app['config']->set('statamic.stache.stores.globals.directory', __DIR__.'/__fixtures__/content/globals');
$app['config']->set('statamic.stache.stores.global-variables.directory', __DIR__.'/__fixtures__/content/globals');
$app['config']->set('statamic.stache.stores.asset-containers.directory', __DIR__.'/__fixtures__/content/assets');
$app['config']->set('statamic.stache.stores.nav-trees.directory', __DIR__.'/__fixtures__/content/structures/navigation');
$app['config']->set('statamic.stache.stores.collection-trees.directory', __DIR__.'/__fixtures__/content/structures/collections');
$app['config']->set('statamic.stache.stores.form-submissions.directory', __DIR__.'/__fixtures__/content/submissions');
$app['config']->set('statamic.api.enabled', true);
$app['config']->set('statamic.graphql.enabled', true);
$app['config']->set('statamic.editions.pro', true);
$app['config']->set('cache.stores.outpost', [
'driver' => 'file',
'path' => storage_path('framework/cache/outpost-data'),
]);
$app['config']->set('statamic.search.indexes.default.driver', 'null');
$viewPaths = $app['config']->get('view.paths');
$viewPaths[] = __DIR__.'/__fixtures__/views/';
$app['config']->set('view.paths', $viewPaths);
// We changed the default sites setup but the tests assume defaults like the following.
// We write the file early so its ready the first time Site facade is used.
$app['files']->put(resource_path('sites.yaml'), <<<'YAML'
en:
name: English
url: http://localhost/
locale: en_US
YAML);
}
protected function setSites($sites)
{
Site::setSites($sites);
Config::set('statamic.system.multisite', Site::hasMultiple());
}
protected function setSiteValue($site, $key, $value)
{
Site::setSiteValue($site, $key, $value);
Config::set('statamic.system.multisite', Site::hasMultiple());
}
protected function assertEveryItem($items, $callback)
{
if ($items instanceof \Illuminate\Support\Collection) {
$items = $items->all();
}
$passes = 0;
foreach ($items as $item) {
if ($callback($item)) {
$passes++;
}
}
$this->assertEquals(count($items), $passes, 'Failed asserting that every item passes.');
}
protected function assertEveryItemIsInstanceOf($class, $items)
{
if ($items instanceof \Illuminate\Support\Collection) {
$items = $items->all();
}
$matches = 0;
foreach ($items as $item) {
if ($item instanceof $class) {
$matches++;
}
}
$this->assertEquals(count($items), $matches, 'Failed asserting that every item is an instance of '.$class);
}
protected function assertContainsHtml($string)
{
preg_match('/<[^<]+>/', $string, $matches);
$this->assertNotEmpty($matches, 'Failed asserting that string contains HTML.');
}
public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void
{
IlluminateAssert::assertArraySubset($subset, $array, $checkForObjectIdentity, $message);
}
// This method is unavailable on earlier versions of Laravel.
public function partialMock($abstract, ?\Closure $mock = null)
{
$mock = \Mockery::mock(...array_filter(func_get_args()))->makePartial();
$this->app->instance($abstract, $mock);
return $mock;
}
private function addGqlMacros()
{
TestResponse::macro('assertGqlOk', function () {
$this->assertOk();
$json = $this->json();
if (isset($json['errors'])) {
throw new \PHPUnit\Framework\ExpectationFailedException(
'GraphQL response contained errors',
new \SebastianBergmann\Comparator\ComparisonFailure('', '', '', json_encode($json, JSON_PRETTY_PRINT))
);
}
return $this;
});
TestResponse::macro('assertGqlUnauthorized', function () {
$this->assertOk();
$json = $this->json();
if (! isset($json['errors'])) {
throw new \PHPUnit\Framework\ExpectationFailedException(
'GraphQL response contained no errors',
new \SebastianBergmann\Comparator\ComparisonFailure('', '', json_encode(['errors' => [['message' => 'Unauthorized']]], JSON_PRETTY_PRINT), json_encode($json, JSON_PRETTY_PRINT))
);
}
Assert::assertTrue(
collect($json['errors'])->map->message->contains('Unauthorized'),
'No unauthorized error message in response'
);
return $this;
});
// Symfony 7.4.0 changed "UTF-8" to "utf-8".
// https://github.com/symfony/symfony/pull/60685
// While we continue to support lower versions, we'll do a case-insensitive check.
// This macro is essentially assertHeader but with case-insensitive value check.
TestResponse::macro('assertContentType', function (string $value) {
$headerName = 'Content-Type';
Assert::assertTrue(
$this->headers->has($headerName), "Header [{$headerName}] not present on response."
);
$actual = $this->headers->get($headerName);
if (! is_null($value)) {
Assert::assertEquals(
strtolower($value), strtolower($this->headers->get($headerName)),
"Header [{$headerName}] was found, but value [{$actual}] does not match [{$value}]."
);
}
return $this;
});
}
public function __call($name, $arguments)
{
if ($name == 'assertStringEqualsStringIgnoringLineEndings') {
return Assert::assertThat(
$arguments[1],
new StringEqualsStringIgnoringLineEndings($arguments[0]),
$arguments[2] ?? ''
);
}
throw new \BadMethodCallException("Method [$name] does not exist.");
}
}