Skip to content

Commit e9adcf4

Browse files
author
David Yell
committed
Added tests
Fixed the card search with a custom resolver to fix the query for JSON_CONTAINS
1 parent e6cadc8 commit e9adcf4

File tree

5 files changed

+107
-4
lines changed

5 files changed

+107
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\GraphQL\Resolvers;
5+
6+
use App\Models\Card;
7+
use Nuwave\Lighthouse\Execution\ResolveInfo;
8+
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
9+
10+
class CardSearchResolver
11+
{
12+
public function resolve(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
13+
{
14+
$query = Card::query();
15+
16+
if (isset($args['colors'])) {
17+
$colors = $args['colors'];
18+
$query->where(function ($q) use ($colors) {
19+
foreach ($colors as $color) {
20+
$q->orWhereRaw('JSON_CONTAINS(colors, ?)', [json_encode($color)]);
21+
}
22+
});
23+
}
24+
25+
return $query->get();
26+
}
27+
}

graphql/card.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ extend type Query {
9393
cardNameContains(name: String @like(template: "%{}%")): [Card]! @all
9494
cardSearch(
9595
name: String @like(template: "%{}%")
96-
colors: [String]! @whereJsonContains
97-
): [Card!]! @all
96+
colors: [String]!
97+
): [Card!]! @field(resolver: "App\\GraphQL\\Resolvers\\CardSearchResolver@resolve")
9898
}
9999

100100
extend type Mutation {

tests/Feature/Api/CardTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature\Api;
6+
7+
use App\Models\Card;
8+
use Illuminate\Database\Eloquent\Factories\Sequence;
9+
use Tests\TestCase;
10+
11+
class CardTest extends TestCase
12+
{
13+
public function test_can_paginate_cards(): void
14+
{
15+
Card::factory()->count(10)->create();
16+
$this->assertDatabaseCount('cards', 10);
17+
18+
$response = $this->graphQL('
19+
{
20+
cards(first:5, page: 1) {
21+
data {
22+
id
23+
name
24+
types
25+
}
26+
paginatorInfo {
27+
currentPage
28+
lastPage
29+
total
30+
}
31+
}
32+
}
33+
')->assertOk();
34+
35+
$cards = $response->json('data.cards.data');
36+
$pagination = $response->json('data.cards.paginatorInfo');
37+
38+
$this->assertCount(5, $cards);
39+
$this->assertSame(1, $pagination['currentPage']);
40+
$this->assertSame(2, $pagination['lastPage']);
41+
$this->assertSame(10, $pagination['total']);
42+
}
43+
44+
public function test_can_search_for_cards(): void
45+
{
46+
Card::factory()
47+
->count(5)
48+
->state(new Sequence(
49+
['name' => 'Dragon Whelp', 'colors' => ['R']],
50+
['name' => 'Archive Dragon', 'colors' => ['U']],
51+
['name' => 'Decadent Dragon // Expensive Taste', 'colors' => ['R']],
52+
['name' => 'Decadent Dragon // Expensive Taste', 'colors' => ['B']],
53+
['name' => 'Swordsworn Cavalier', 'colors' => ['W']],
54+
))
55+
->create();
56+
57+
$response = $this->graphQL('
58+
{
59+
cardSearch(name: "dragon", colors: ["R", "U"]) {
60+
id
61+
name
62+
type
63+
colors
64+
}
65+
}
66+
')->assertOk();
67+
68+
$cards = $response->json('data.cardSearch');
69+
70+
$this->assertCount(3, $cards);
71+
}
72+
}

tests/Feature/Livewire/SetIndexTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
class SetIndexTest extends TestCase
1212
{
13-
/** @test */
1413
public function renders_successfully()
1514
{
1615
Livewire::test(SetIndex::class)

tests/TestCase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
namespace Tests;
66

7+
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
9+
use Nuwave\Lighthouse\Testing\MakesGraphQLRequests;
10+
use Nuwave\Lighthouse\Testing\RefreshesSchemaCache;
811

912
abstract class TestCase extends BaseTestCase
1013
{
11-
//
14+
use MakesGraphQLRequests;
15+
use RefreshDatabase;
16+
use RefreshesSchemaCache;
1217
}

0 commit comments

Comments
 (0)