Simplify test trait setup (#2364) · devhammed/lighthouse@a945359 · GitHub
Skip to content

Commit a945359

Browse files
authored
Simplify test trait setup (nuwave#2364)
1 parent 720e6b2 commit a945359

22 files changed

Lines changed: 131 additions & 51 deletions

CHANGELOG.md

Lines changed: 3 additions & 1 deletion

UPGRADE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ This document provides guidance for upgrading between major versions of Lighthou
77
The configuration options often change between major versions.
88
Compare your `lighthouse.php` against the latest [default configuration](src/lighthouse.php).
99

10+
## v6 to v7
11+
12+
### Leverage automatic test trait setup
13+
14+
Methods you need to explicitly call to set up test traits were removed in favor of automatically set up test traits.
15+
Keep in mind they only work when your test class extends `Illuminate\Foundation\Testing\TestCase`.
16+
17+
- Just remove calls to `Nuwave\Lighthouse\Testing\RefreshesSchemaCache::bootRefreshesSchemaCache()`.
18+
- Replace calls to `Nuwave\Lighthouse\Testing\MakesGraphQLRequests::setUpSubscriptionEnvironment()` with ` use Nuwave\Lighthouse\Testing\TestsSubscriptions`.
19+
1020
## v5 to v6
1121

1222
### `messages` on `@rules` and `@rulesForArray`

docs/6/testing/phpunit.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ Lighthouse makes it easy to add automated tests through [PHPUnit](https://phpuni
44

55
## Setup
66

7-
Lighthouse offers some useful test helpers that make it easy to call your API
8-
from within a PHPUnit test. Just add the `MakesGraphQLRequests` trait to your test class.
7+
Lighthouse offers some useful test helpers.
8+
Keep in mind they only work when your test class extends `Illuminate\Foundation\Testing\TestCase`.
9+
10+
The `MakesGraphQLRequests` trait make it easy to call your API:
911

1012
```diff
1113
+use Nuwave\Lighthouse\Testing\MakesGraphQLRequests;
@@ -18,8 +20,8 @@ abstract class TestCase extends BaseTestCase
1820
}
1921
```
2022

21-
Enabling the schema cache speeds up your tests. To ensure the schema is fresh
22-
before running tests, add the `RefreshesSchemaCache` trait to your test class and call it during set up.
23+
Enabling the schema cache speeds up your tests.
24+
To ensure the schema is fresh before running tests, add the `RefreshesSchemaCache` trait:
2325

2426
```diff
2527
+use Nuwave\Lighthouse\Testing\RefreshesSchemaCache;
@@ -29,13 +31,19 @@ abstract class TestCase extends BaseTestCase
2931
{
3032
use CreatesApplication;
3133
+ use RefreshesSchemaCache;
34+
}
35+
```
3236

33-
protected function setUp(): void
34-
{
35-
parent::setUp();
36-
+ $this->bootRefreshesSchemaCache();
37-
+ $this->setUpSubscriptionEnvironment(); // Only if you are using subscriptions
38-
}
37+
If you want to test subscriptions, add the `TestsSubscriptions` trait:
38+
39+
```diff
40+
+use Nuwave\Lighthouse\Testing\TestsSubscriptions;
41+
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
42+
43+
abstract class TestCase extends BaseTestCase
44+
{
45+
use CreatesApplication;
46+
+ use TestsSubscriptions;
3947
}
4048
```
4149

docs/master/testing/phpunit.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ Lighthouse makes it easy to add automated tests through [PHPUnit](https://phpuni
44

55
## Setup
66

7-
Lighthouse offers some useful test helpers that make it easy to call your API
8-
from within a PHPUnit test. Just add the `MakesGraphQLRequests` trait to your test class.
7+
Lighthouse offers some useful test helpers.
8+
Keep in mind they only work when your test class extends `Illuminate\Foundation\Testing\TestCase`.
9+
10+
The `MakesGraphQLRequests` trait make it easy to call your API:
911

1012
```diff
1113
+use Nuwave\Lighthouse\Testing\MakesGraphQLRequests;
@@ -18,8 +20,8 @@ abstract class TestCase extends BaseTestCase
1820
}
1921
```
2022

21-
Enabling the schema cache speeds up your tests. To ensure the schema is fresh
22-
before running tests, add the `RefreshesSchemaCache` trait to your test class and call it during set up.
23+
Enabling the schema cache speeds up your tests.
24+
To ensure the schema is fresh before running tests, add the `RefreshesSchemaCache` trait:
2325

2426
```diff
2527
+use Nuwave\Lighthouse\Testing\RefreshesSchemaCache;
@@ -29,13 +31,19 @@ abstract class TestCase extends BaseTestCase
2931
{
3032
use CreatesApplication;
3133
+ use RefreshesSchemaCache;
34+
}
35+
```
3236

33-
protected function setUp(): void
34-
{
35-
parent::setUp();
36-
+ $this->bootRefreshesSchemaCache();
37-
+ $this->setUpSubscriptionEnvironment(); // Only if you are using subscriptions
38-
}
37+
If you want to test subscriptions, add the `TestsSubscriptions` trait:
38+
39+
```diff
40+
+use Nuwave\Lighthouse\Testing\TestsSubscriptions;
41+
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
42+
43+
abstract class TestCase extends BaseTestCase
44+
{
45+
use CreatesApplication;
46+
+ use TestsSubscriptions;
3947
}
4048
```
4149

src/Testing/MakesGraphQLRequests.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ protected function rethrowGraphQLErrors(): void
232232
$config->set('lighthouse.error_handlers', [RethrowingErrorHandler::class]);
233233
}
234234

235+
/**
236+
* @deprecated use TestsSubscriptions
237+
* TODO remove in the next major version
238+
*/
235239
protected function setUpSubscriptionEnvironment(): void
236240
{
237241
$app = Container::getInstance();

src/Testing/MakesGraphQLRequestsLumen.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ protected function rethrowGraphQLErrors(): void
253253
$config->set('lighthouse.error_handlers', [RethrowingErrorHandler::class]);
254254
}
255255

256+
/**
257+
* @deprecated use TestsSubscriptions
258+
* TODO remove in the next major version
259+
*/
256260
protected function setUpSubscriptionEnvironment(): void
257261
{
258262
$app = Container::getInstance();

src/Testing/RefreshesSchemaCache.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait RefreshesSchemaCache
2424
*/
2525
protected static string $lockFilePath = __DIR__ . '/schema-cache-refreshing';
2626

27-
protected function bootRefreshesSchemaCache(): void
27+
protected function setUpRefreshesSchemaCache(): void
2828
{
2929
if (! static::$schemaCacheWasRefreshed) {
3030
// We utilize the filesystem as shared mutable state to coordinate between processes,
@@ -48,8 +48,11 @@ protected function bootRefreshesSchemaCache(): void
4848
}
4949
}
5050

51-
protected function setUpRefreshesSchemaCache(): void
51+
/**
52+
* @deprecated TODO leverage automatic test trait setup, this method will be removed in the next major version
53+
*/
54+
protected function bootRefreshesSchemaCache(): void
5255
{
53-
$this->bootRefreshesSchemaCache();
56+
$this->setUpRefreshesSchemaCache();
5457
}
5558
}

src/Testing/TestsSubscriptions.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Nuwave\Lighthouse\Testing;
4+
5+
use Illuminate\Container\Container;
6+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
7+
use Nuwave\Lighthouse\Subscriptions\Broadcasters\LogBroadcaster;
8+
use Nuwave\Lighthouse\Subscriptions\BroadcastManager;
9+
use Nuwave\Lighthouse\Subscriptions\Contracts\Broadcaster;
10+
11+
/**
12+
* Sets up the environment for testing subscriptions.
13+
*/
14+
trait TestsSubscriptions
15+
{
16+
protected function setUpTestsSubscriptions(): void
17+
{
18+
$app = Container::getInstance();
19+
20+
$config = $app->make(ConfigRepository::class);
21+
$config->set('lighthouse.subscriptions.queue_broadcasts', false);
22+
$config->set('lighthouse.subscriptions.storage', 'array');
23+
$config->set('lighthouse.subscriptions.storage_ttl', null);
24+
25+
// binding an instance to the container, so it can be spied on
26+
$app->bind(Broadcaster::class, static fn (ConfigRepository $config): \Nuwave\Lighthouse\Subscriptions\Broadcasters\LogBroadcaster => new LogBroadcaster(
27+
$config->get('lighthouse.subscriptions.broadcasters.log'),
28+
));
29+
30+
$broadcastManager = $app->make(BroadcastManager::class);
31+
assert($broadcastManager instanceof BroadcastManager);
32+
33+
// adding a custom driver which is a spied version of log driver
34+
$broadcastManager->extend('mock', fn () => $this->spy(LogBroadcaster::class)->makePartial());
35+
36+
// set the custom driver as the default driver
37+
$config->set('lighthouse.subscriptions.broadcaster', 'mock');
38+
}
39+
}

tests/TestsSubscriptions.php renamed to tests/EnablesSubscriptionServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Nuwave\Lighthouse\Subscriptions\SubscriptionServiceProvider;
66

7-
trait TestsSubscriptions
7+
trait EnablesSubscriptionServiceProvider
88
{
99
protected function getPackageProviders($app): array
1010
{

tests/Integration/Subscriptions/Broadcast/BroadcastDBTest.php

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)