-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTest.php
72 lines (61 loc) · 2.22 KB
/
GameTest.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
<?php
namespace Tests\Feature;
use App\Models\Game;
use Illuminate\Testing\Fluent\AssertableJson;
use Illuminate\Testing\TestResponse;
use Tests\TestCase;
class GameTest extends TestCase
{
public function createGame(): TestResponse
{
return $this->postJson('/api/game', ['width' => 5, 'height' => 5]);
}
public function testRequiredFieldsForCreatingGame()
{
$response = $this->postJson('/api/game');
$response->assertStatus(400);
}
public function testCreateGameWithInvalidData()
{
$response = $this->postJson('/api/game', ['width' => 3, 'height' => 6]);
$response->assertStatus(400);
}
public function testCreateGame()
{
$response = $this->createGame();
$response->assertStatus(201)->assertJson(['id' => true]);
}
public function testGetGame()
{
$response = $this->createGame();
$gameResponse = $this->getJson("/api/game/$response[id]");
$gameResponse
->assertStatus(200)
->assertJson(function (AssertableJson $json) {
return $json->whereAllType([
'id' => 'string',
'field.width' => 'integer',
'field.height' => 'integer',
'field.cells' => 'array',
'players' => 'array',
'currentPlayerId' => 'integer',
'winnerPlayerId' => 'integer'
]);
})
->assertJson(function (AssertableJson $json) use ($gameResponse) {
$countCells = count($gameResponse['field']['cells']);
$countPlayers = count($gameResponse['players']);
$cells = [];
for ($i = 0; $i < $countCells; $i++) {
$cells["field.cells.$i.color"] = 'string';
$cells["field.cells.$i.playerId"] = 'integer';
}
$players = [];
for ($i = 0; $i < $countPlayers; $i++) {
$players["players.$i.id"] = 'integer';
$players["players.$i.color"] = 'string';
}
return $json->whereAllType(array_merge($cells, $players))->etc();
});
}
}