Skip to content

Commit 1054b9a

Browse files
authored
test(laravel): call factories, debug is now false by default (#6702)
1 parent 3b948b3 commit 1054b9a

File tree

11 files changed

+142
-7
lines changed

11 files changed

+142
-7
lines changed

src/GraphQl/Type/TypeConverter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,14 @@ private function getResourceType(Type $type, bool $input, Operation $rootOperati
168168
try {
169169
$operation = $resourceMetadataCollection->getOperation($operationName);
170170
} catch (OperationNotFoundException) {
171-
$operation = $resourceMetadataCollection->getOperation($isCollection ? 'collection_query' : 'item_query');
171+
try {
172+
$operation = $resourceMetadataCollection->getOperation($isCollection ? 'collection_query' : 'item_query');
173+
} catch (OperationNotFoundException) {
174+
throw new OperationNotFoundException(\sprintf('A GraphQl operation named "%s" should exist on the type "%s" as we reference this type in another query.', $isCollection ? 'collection_query' : 'item_query', $resourceClass));
175+
}
172176
}
173177
if (!$operation instanceof Operation) {
174-
throw new OperationNotFoundException();
178+
throw new OperationNotFoundException(\sprintf('A GraphQl operation named "%s" should exist on the type "%s" as we reference this type in another query.', $operationName, $resourceClass));
175179
}
176180

177181
return $this->typeBuilder->getResourceObjectType($resourceMetadataCollection, $operation, $propertyMetadata, [

src/Laravel/Tests/AuthTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,35 @@
1414
namespace ApiPlatform\Laravel\Tests;
1515

1616
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
17+
use Illuminate\Config\Repository;
18+
use Illuminate\Foundation\Application;
1719
use Illuminate\Foundation\Testing\RefreshDatabase;
1820
use Orchestra\Testbench\Concerns\WithWorkbench;
1921
use Orchestra\Testbench\TestCase;
22+
use Workbench\Database\Factories\UserFactory;
2023

2124
class AuthTest extends TestCase
2225
{
2326
use ApiTestAssertionsTrait;
2427
use RefreshDatabase;
2528
use WithWorkbench;
2629

30+
/**
31+
* @param Application $app
32+
*/
33+
protected function defineEnvironment($app): void
34+
{
35+
tap($app['config'], function (Repository $config): void {
36+
$config->set('api-platform.graphql.enabled', true);
37+
$config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
38+
});
39+
}
40+
41+
protected function afterRefreshingDatabase(): void
42+
{
43+
UserFactory::new()->create();
44+
}
45+
2746
public function testGetCollection(): void
2847
{
2948
$response = $this->get('/api/vaults', ['accept' => ['application/ld+json']]);
@@ -44,7 +63,7 @@ public function testAuthenticatedPolicy(): void
4463
{
4564
$response = $this->post('/tokens/create');
4665
$token = $response->json()['token'];
47-
$response = $this->post('/api/vaults', [], ['accept' => ['application/ld+json'], 'content-type' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
66+
$response = $this->postJson('/api/vaults', [], ['accept' => ['application/ld+json'], 'content-type' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
4867
$response->assertStatus(403);
4968
}
5069

src/Laravel/Tests/EloquentTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
use Illuminate\Foundation\Testing\RefreshDatabase;
1818
use Orchestra\Testbench\Concerns\WithWorkbench;
1919
use Orchestra\Testbench\TestCase;
20+
use Workbench\Database\Factories\AuthorFactory;
21+
use Workbench\Database\Factories\BookFactory;
22+
use Workbench\Database\Factories\WithAccessorFactory;
2023

2124
class EloquentTest extends TestCase
2225
{
@@ -26,6 +29,8 @@ class EloquentTest extends TestCase
2629

2730
public function testSearchFilter(): void
2831
{
32+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
33+
2934
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
3035
$book = $response->json()['member'][0];
3136

@@ -35,18 +40,24 @@ public function testSearchFilter(): void
3540

3641
public function testValidateSearchFilter(): void
3742
{
43+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
44+
3845
$response = $this->get('/api/books?isbn=a', ['Accept' => ['application/ld+json']]);
3946
$this->assertSame($response->json()['detail'], 'The isbn field must be at least 2 characters.');
4047
}
4148

4249
public function testSearchFilterRelation(): void
4350
{
51+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
52+
4453
$response = $this->get('/api/books?author=1', ['Accept' => ['application/ld+json']]);
4554
$this->assertSame($response->json()['member'][0]['author'], '/api/authors/1');
4655
}
4756

4857
public function testPropertyFilter(): void
4958
{
59+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
60+
5061
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
5162
$book = $response->json()['member'][0];
5263

@@ -60,6 +71,8 @@ public function testPropertyFilter(): void
6071

6172
public function testPartialSearchFilter(): void
6273
{
74+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
75+
6376
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
6477
$book = $response->json()['member'][0];
6578

@@ -76,6 +89,8 @@ public function testPartialSearchFilter(): void
7689

7790
public function testDateFilterEqual(): void
7891
{
92+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
93+
7994
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
8095
$book = $response->json()['member'][0];
8196
$updated = $this->patchJson(
@@ -93,6 +108,8 @@ public function testDateFilterEqual(): void
93108

94109
public function testDateFilterIncludeNull(): void
95110
{
111+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
112+
96113
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
97114
$book = $response->json()['member'][0];
98115
$updated = $this->patchJson(
@@ -110,6 +127,8 @@ public function testDateFilterIncludeNull(): void
110127

111128
public function testDateFilterExcludeNull(): void
112129
{
130+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
131+
113132
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
114133
$book = $response->json()['member'][0];
115134
$updated = $this->patchJson(
@@ -127,6 +146,8 @@ public function testDateFilterExcludeNull(): void
127146

128147
public function testDateFilterGreaterThan(): void
129148
{
149+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
150+
130151
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
131152
$bookBefore = $response->json()['member'][0];
132153
$updated = $this->patchJson(
@@ -155,9 +176,10 @@ public function testDateFilterGreaterThan(): void
155176

156177
public function testDateFilterLowerThanEqual(): void
157178
{
179+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
158180
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
159181
$bookBefore = $response->json()['member'][0];
160-
$updated = $this->patchJson(
182+
$this->patchJson(
161183
$bookBefore['@id'],
162184
['publicationDate' => '0001-02-18 00:00:00'],
163185
[
@@ -184,6 +206,7 @@ public function testDateFilterLowerThanEqual(): void
184206

185207
public function testDateFilterBetween(): void
186208
{
209+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
187210
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
188211
$book = $response->json()['member'][0];
189212
$updated = $this->patchJson(
@@ -223,6 +246,7 @@ public function testDateFilterBetween(): void
223246

224247
public function testSearchFilterWithPropertyPlaceholder(): void
225248
{
249+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
226250
$response = $this->get('/api/authors', ['Accept' => ['application/ld+json']])->json();
227251
$author = $response['member'][0];
228252

@@ -235,12 +259,14 @@ public function testSearchFilterWithPropertyPlaceholder(): void
235259

236260
public function testOrderFilterWithPropertyPlaceholder(): void
237261
{
262+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
238263
$res = $this->get('/api/authors?order[id]=desc', ['Accept' => ['application/ld+json']])->json();
239264
$this->assertSame($res['member'][0]['id'], 10);
240265
}
241266

242267
public function testOrFilter(): void
243268
{
269+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
244270
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']])->json()['member'];
245271
$book = $response[0];
246272
$book2 = $response[1];
@@ -251,6 +277,7 @@ public function testOrFilter(): void
251277

252278
public function testRangeLowerThanFilter(): void
253279
{
280+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
254281
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
255282
$bookBefore = $response->json()['member'][0];
256283
$this->patchJson(
@@ -279,6 +306,7 @@ public function testRangeLowerThanFilter(): void
279306

280307
public function testRangeLowerThanEqualFilter(): void
281308
{
309+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
282310
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
283311
$bookBefore = $response->json()['member'][0];
284312
$this->patchJson(
@@ -308,6 +336,7 @@ public function testRangeLowerThanEqualFilter(): void
308336

309337
public function testRangeGreaterThanFilter(): void
310338
{
339+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
311340
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
312341
$bookBefore = $response->json()['member'][0];
313342
$updated = $this->patchJson(
@@ -336,6 +365,7 @@ public function testRangeGreaterThanFilter(): void
336365

337366
public function testRangeGreaterThanEqualFilter(): void
338367
{
368+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
339369
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
340370
$bookBefore = $response->json()['member'][0];
341371
$updated = $this->patchJson(
@@ -365,12 +395,14 @@ public function testRangeGreaterThanEqualFilter(): void
365395

366396
public function testWrongOrderFilter(): void
367397
{
398+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
368399
$res = $this->get('/api/authors?order[name]=something', ['Accept' => ['application/ld+json']]);
369400
$this->assertEquals($res->getStatusCode(), 422);
370401
}
371402

372403
public function testWithAccessor(): void
373404
{
405+
WithAccessorFactory::new()->create();
374406
$res = $this->get('/api/with_accessors/1', ['Accept' => ['application/ld+json']]);
375407
$this->assertArraySubset(['name' => 'test'], $res->json());
376408
}

src/Laravel/Tests/GraphQlAuthTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,30 @@
2020
use Orchestra\Testbench\Attributes\DefineEnvironment;
2121
use Orchestra\Testbench\Concerns\WithWorkbench;
2222
use Orchestra\Testbench\TestCase;
23+
use Workbench\Database\Factories\AuthorFactory;
24+
use Workbench\Database\Factories\BookFactory;
25+
use Workbench\Database\Factories\UserFactory;
26+
use Workbench\Database\Factories\VaultFactory;
2327

2428
class GraphQlAuthTest extends TestCase
2529
{
2630
use ApiTestAssertionsTrait;
2731
use RefreshDatabase;
2832
use WithWorkbench;
2933

34+
protected function afterRefreshingDatabase(): void
35+
{
36+
UserFactory::new()->create();
37+
}
38+
3039
/**
3140
* @param Application $app
3241
*/
3342
protected function defineEnvironment($app): void
3443
{
3544
tap($app['config'], function (Repository $config): void {
3645
$config->set('api-platform.routes.middleware', ['auth:sanctum']);
46+
$config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
3747
$config->set('api-platform.graphql.enabled', true);
3848
});
3949
}
@@ -46,6 +56,7 @@ public function testUnauthenticated(): void
4656

4757
public function testAuthenticated(): void
4858
{
59+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
4960
$response = $this->post('/tokens/create');
5061
$token = $response->json()['token'];
5162
$response = $this->get('/api/graphql', ['accept' => ['text/html'], 'authorization' => 'Bearer '.$token]);
@@ -64,6 +75,7 @@ public function testAuthenticated(): void
6475

6576
public function testPolicy(): void
6677
{
78+
VaultFactory::new()->count(10)->create();
6779
$response = $this->post('/tokens/create');
6880
$token = $response->json()['token'];
6981
$response = $this->postJson('/api/graphql', ['query' => 'mutation {
@@ -86,13 +98,15 @@ protected function useProductionMode($app): void
8698
tap($app['config'], function (Repository $config): void {
8799
$config->set('api-platform.routes.middleware', ['auth:sanctum']);
88100
$config->set('api-platform.graphql.enabled', true);
101+
$config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
89102
$config->set('app.debug', false);
90103
});
91104
}
92105

93106
#[DefineEnvironment('useProductionMode')]
94107
public function testProductionError(): void
95108
{
109+
VaultFactory::new()->count(10)->create();
96110
$response = $this->post('/tokens/create');
97111
$token = $response->json()['token'];
98112
$response = $this->postJson('/api/graphql', ['query' => 'mutation {

src/Laravel/Tests/GraphQlTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
namespace ApiPlatform\Laravel\Tests;
1515

1616
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
17-
use Illuminate\Contracts\Config\Repository;
17+
use Illuminate\Config\Repository;
1818
use Illuminate\Foundation\Application;
1919
use Illuminate\Foundation\Testing\RefreshDatabase;
2020
use Orchestra\Testbench\Concerns\WithWorkbench;
2121
use Orchestra\Testbench\TestCase;
22+
use Workbench\Database\Factories\AuthorFactory;
23+
use Workbench\Database\Factories\BookFactory;
2224

2325
class GraphQlTest extends TestCase
2426
{
@@ -38,6 +40,7 @@ protected function defineEnvironment($app): void
3840

3941
public function testGetBooks(): void
4042
{
43+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
4144
$response = $this->postJson('/api/graphql', ['query' => '{books { edges { node {id, name, publicationDate, author {id, name }}}}}'], ['accept' => ['application/json']]);
4245
$response->assertStatus(200);
4346
$data = $response->json();

src/Laravel/Tests/HalTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Orchestra\Testbench\Concerns\WithWorkbench;
2121
use Orchestra\Testbench\TestCase;
2222
use Workbench\App\Models\Book;
23+
use Workbench\Database\Factories\AuthorFactory;
24+
use Workbench\Database\Factories\BookFactory;
2325

2426
class HalTest extends TestCase
2527
{
@@ -35,6 +37,7 @@ protected function defineEnvironment($app): void
3537
tap($app['config'], function (Repository $config): void {
3638
$config->set('api-platform.formats', ['jsonhal' => ['application/hal+json']]);
3739
$config->set('api-platform.docs_formats', ['jsonhal' => ['application/hal+json']]);
40+
$config->set('app.debug', true);
3841
});
3942
}
4043

@@ -61,6 +64,7 @@ public function testGetEntrypoint(): void
6164

6265
public function testGetCollection(): void
6366
{
67+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
6468
$response = $this->get('/api/books', ['accept' => 'application/hal+json']);
6569
$response->assertStatus(200);
6670
$response->assertHeader('content-type', 'application/hal+json; charset=utf-8');
@@ -79,6 +83,7 @@ public function testGetCollection(): void
7983

8084
public function testGetBook(): void
8185
{
86+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
8287
$book = Book::first();
8388
$iri = $this->getIriFromResource($book);
8489
$response = $this->get($iri, ['accept' => ['application/hal+json']]);
@@ -103,6 +108,7 @@ public function testGetBook(): void
103108

104109
public function testDeleteBook(): void
105110
{
111+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
106112
$book = Book::first();
107113
$iri = $this->getIriFromResource($book);
108114
$response = $this->delete($iri, headers: ['accept' => 'application/hal+json']);

0 commit comments

Comments
 (0)