Skip to content

Commit de607f1

Browse files
feat(laravel): Adding tests for boolean cast
1 parent 35e6ea8 commit de607f1

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/Laravel/Tests/GraphQlTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Illuminate\Foundation\Testing\RefreshDatabase;
2020
use Orchestra\Testbench\Concerns\WithWorkbench;
2121
use Orchestra\Testbench\TestCase;
22+
use Symfony\Component\Uid\Ulid;
2223
use Workbench\Database\Factories\AuthorFactory;
2324
use Workbench\Database\Factories\BookFactory;
2425

@@ -66,4 +67,37 @@ public function testGetBooksWithPaginationAndOrder(): void
6667
$this->assertCount(3, $data['data']['books']['edges']);
6768
$this->assertArrayNotHasKey('errors', $data);
6869
}
70+
public function testCreateBook(): void
71+
{
72+
/** @var \Workbench\App\Models\Author $author */
73+
$author = AuthorFactory::new()->create();
74+
$response = $this->postJson('/api/graphql', [
75+
'query' => '
76+
mutation createBook($book: createBookInput!){
77+
createBook(input: $book){
78+
book{
79+
name
80+
isAvailable
81+
}
82+
}
83+
}
84+
',
85+
'variables' => [
86+
'book' => [
87+
'name' => fake()->name(),
88+
'author' => 'api/authors/'.$author->id,
89+
'isbn' => fake()->isbn13(),
90+
'isAvailable' => rand(0,1) === 1,
91+
]
92+
]
93+
], ['accept' => ['application/json']]);
94+
$response->assertStatus(200);
95+
$data = $response->json();
96+
$this->assertArrayNotHasKey('errors', $data);
97+
$this->assertArrayHasKey('data', $data);
98+
$this->assertArrayHasKey('createBook', $data['data']);
99+
$this->assertArrayHasKey('book', $data['data']['createBook']);
100+
$this->assertArrayHasKey('isAvailable', $data['data']['createBook']['book']);
101+
$this->assertIsBool($data['data']['createBook']['book']['isAvailable']);
102+
}
69103
}

src/Laravel/workbench/app/Models/Book.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ApiPlatform\Metadata\Delete;
2424
use ApiPlatform\Metadata\Get;
2525
use ApiPlatform\Metadata\GetCollection;
26+
use ApiPlatform\Metadata\GraphQl\Mutation;
2627
use ApiPlatform\Metadata\GraphQl\Query;
2728
use ApiPlatform\Metadata\GraphQl\QueryCollection;
2829
use ApiPlatform\Metadata\Patch;
@@ -55,6 +56,7 @@
5556
new QueryParameter(key: 'order[:property]', filter: OrderFilter::class),
5657
],
5758
),
59+
new Mutation(name: 'create'),
5860
]
5961
)]
6062
#[QueryParameter(key: 'isbn', filter: PartialSearchFilter::class, constraints: 'min:2')]
@@ -74,8 +76,11 @@ class Book extends Model
7476
use HasFactory;
7577
use HasUlids;
7678

77-
protected $visible = ['name', 'author', 'isbn', 'publication_date'];
78-
protected $fillable = ['name'];
79+
protected $visible = ['name', 'author', 'isbn', 'publication_date', 'is_available'];
80+
protected $fillable = ['name', 'is_available'];
81+
protected $casts = [
82+
'is_available' => 'boolean',
83+
];
7984

8085
public function author(): BelongsTo
8186
{

src/Laravel/workbench/database/factories/BookFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function definition(): array
3737
'author_id' => AuthorFactory::new(),
3838
'isbn' => fake()->isbn13(),
3939
'publication_date' => fake()->optional()->date(),
40+
'is_available' => rand(0,1) === 1,
4041
'internal_note' => fake()->text(),
4142
];
4243
}

src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function up(): void
3232
$table->string('name');
3333
$table->string('isbn');
3434
$table->date('publication_date')->nullable();
35+
$table->boolean('is_available')->default(true);
3536
$table->text('internal_note')->nullable();
3637
$table->integer('author_id')->unsigned();
3738
$table->foreign('author_id')->references('id')->on('authors');

0 commit comments

Comments
 (0)