-
-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathTestCase.php
234 lines (203 loc) · 7.79 KB
/
TestCase.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
<?php declare(strict_types=1);
namespace Tests;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use GraphQL\Error\DebugFlag;
use GraphQL\Type\Schema;
use Illuminate\Console\Application as ConsoleApplication;
use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Redis\RedisServiceProvider;
use Laravel\Scout\ScoutServiceProvider as LaravelScoutServiceProvider;
use Nuwave\Lighthouse\Async\AsyncServiceProvider;
use Nuwave\Lighthouse\Auth\AuthServiceProvider as LighthouseAuthServiceProvider;
use Nuwave\Lighthouse\Bind\BindServiceProvider;
use Nuwave\Lighthouse\Cache\CacheServiceProvider;
use Nuwave\Lighthouse\CacheControl\CacheControlServiceProvider;
use Nuwave\Lighthouse\GlobalId\GlobalIdServiceProvider;
use Nuwave\Lighthouse\LighthouseServiceProvider;
use Nuwave\Lighthouse\OrderBy\OrderByServiceProvider;
use Nuwave\Lighthouse\Pagination\PaginationServiceProvider;
use Nuwave\Lighthouse\Schema\SchemaBuilder;
use Nuwave\Lighthouse\Scout\ScoutServiceProvider as LighthouseScoutServiceProvider;
use Nuwave\Lighthouse\SoftDeletes\SoftDeletesServiceProvider;
use Nuwave\Lighthouse\Testing\MakesGraphQLRequests;
use Nuwave\Lighthouse\Testing\MocksResolvers;
use Nuwave\Lighthouse\Testing\TestingServiceProvider;
use Nuwave\Lighthouse\Testing\UsesTestSchema;
use Nuwave\Lighthouse\Validation\ValidationServiceProvider;
use Orchestra\Testbench\TestCase as TestbenchTestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Utils\Policies\AuthServiceProvider;
abstract class TestCase extends TestbenchTestCase
{
use ArraySubsetAsserts;
use MakesGraphQLRequests;
use MocksResolvers;
use UsesTestSchema;
/**
* Set when not in setUp.
*
* @var \Illuminate\Foundation\Application
*/
protected $app;
/** A dummy query type definition that is added to tests by default. */
public const PLACEHOLDER_QUERY = /** @lang GraphQL */ <<<'GRAPHQL'
type Query {
foo: Int
}
GRAPHQL;
protected function setUp(): void
{
parent::setUp();
// This default is only valid for testing Lighthouse itself and thus
// is not defined in the reusable test trait.
$this->schema ??= self::PLACEHOLDER_QUERY;
$this->setUpTestSchema();
// Using qualifyTestResolver() requires instantiation of the test class through the container.
// https://laravel.com/docs/container#binding-primitives
$this->app->when(static::class)
->needs('$name')
->give('TestName');
}
/** @return array<class-string<\Illuminate\Support\ServiceProvider>> */
protected function getPackageProviders($app): array
{
return [
AuthServiceProvider::class,
LaravelScoutServiceProvider::class,
RedisServiceProvider::class,
// Lighthouse's own
LighthouseServiceProvider::class,
AsyncServiceProvider::class,
LighthouseAuthServiceProvider::class,
BindServiceProvider::class,
CacheServiceProvider::class,
CacheControlServiceProvider::class,
GlobalIdServiceProvider::class,
LighthouseScoutServiceProvider::class,
OrderByServiceProvider::class,
PaginationServiceProvider::class,
SoftDeletesServiceProvider::class,
TestingServiceProvider::class,
ValidationServiceProvider::class,
];
}
protected function getEnvironmentSetUp($app): void
{
$config = $app->make(ConfigRepository::class);
$config->set('lighthouse.namespaces', [
'models' => [
'Tests\\Utils\\Models',
'Tests\\Utils\\ModelsSecondary',
],
'queries' => [
'Tests\\Utils\\Queries',
'Tests\\Utils\\QueriesSecondary',
],
'mutations' => [
'Tests\\Utils\\Mutations',
'Tests\\Utils\\MutationsSecondary',
],
'subscriptions' => [
'Tests\\Utils\\Subscriptions',
],
'types' => [
'Tests\\Utils\\Types',
],
'interfaces' => [
'Tests\\Utils\\Interfaces',
'Tests\\Utils\\InterfacesSecondary',
],
'scalars' => [
'Tests\\Utils\\Scalars',
'Tests\\Utils\\ScalarsSecondary',
],
'unions' => [
'Tests\\Utils\\Unions',
'Tests\\Utils\\UnionsSecondary',
],
'directives' => [
'Tests\\Utils\\Directives',
],
'validators' => [
'Tests\\Utils\\Validators',
],
]);
$config->set('app.debug', true);
$config->set(
'lighthouse.debug',
DebugFlag::INCLUDE_DEBUG_MESSAGE
| DebugFlag::INCLUDE_TRACE
// | Debug::RETHROW_INTERNAL_EXCEPTIONS
| DebugFlag::RETHROW_UNSAFE_EXCEPTIONS,
);
$config->set('lighthouse.guards', null);
$config->set('lighthouse.subscriptions', [
'version' => 1,
'storage' => 'array',
'broadcaster' => 'log',
]);
$config->set('broadcasting.connections.pusher', [
'driver' => 'pusher',
'key' => 'foo',
'secret' => 'bar',
'app_id' => 'baz',
]);
$config->set('database.redis.default', [
'url' => env('LIGHTHOUSE_TEST_REDIS_URL'),
'host' => env('LIGHTHOUSE_TEST_REDIS_HOST', 'redis'),
'password' => env('LIGHTHOUSE_TEST_REDIS_PASSWORD'),
'port' => env('LIGHTHOUSE_TEST_REDIS_PORT', '6379'),
'database' => env('LIGHTHOUSE_TEST_REDIS_DB', '0'),
]);
$config->set('database.redis.options', [
'prefix' => 'lighthouse-test-',
]);
$config->set('pennant.default', 'array');
// Defaults to "algolia", which is not needed in our test setup
$config->set('scout.driver', null);
$config->set('lighthouse.federation', [
'entities_resolver_namespace' => 'Tests\\Utils\\Entities',
]);
$config->set('lighthouse.schema_cache.enable', false);
}
/**
* Rethrow all errors that are not handled by GraphQL.
*
* This makes debugging the tests much simpler as Exceptions
* are fully dumped to the console when making requests.
*/
protected function resolveApplicationExceptionHandler($app): void
{
$app->singleton(ExceptionHandler::class, static fn (): ThrowingExceptionHandler => new ThrowingExceptionHandler());
}
/** Build an executable schema from a SDL string, adding on a default Query type. */
protected function buildSchemaWithPlaceholderQuery(string $schema): Schema
{
return $this->buildSchema(
$schema . self::PLACEHOLDER_QUERY,
);
}
/** Build an executable schema from an SDL string. */
protected function buildSchema(string $schema): Schema
{
$this->schema = $schema;
$schemaBuilder = $this->app->make(SchemaBuilder::class);
return $schemaBuilder->schema();
}
/** Get a fully qualified reference to a method that is defined on the test class. */
protected static function qualifyTestResolver(string $method): string
{
$escapedClass = addslashes(static::class);
return "{$escapedClass}@{$method}";
}
protected function commandTester(Command $command): CommandTester
{
$command->setLaravel($this->app);
$command->setApplication($this->app->make(ConsoleApplication::class, [
'version' => $this->app->version(),
]));
return new CommandTester($command);
}
}