From 2e38b55167fcc218c88d6642af9614e5c86f9003 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 31 Jul 2023 20:49:17 +0200 Subject: [PATCH] API: Added API value tests --- .../Api/AngelTypeControllerTest.php | 5 ++- .../Controllers/Api/NewsControllerTest.php | 6 ++- .../Controllers/Api/RoomsControllerTest.php | 5 ++- .../Controllers/Api/ShiftsControllerTest.php | 41 ++++++++++++++++--- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/tests/Unit/Controllers/Api/AngelTypeControllerTest.php b/tests/Unit/Controllers/Api/AngelTypeControllerTest.php index b6db952b..e3dc57a6 100644 --- a/tests/Unit/Controllers/Api/AngelTypeControllerTest.php +++ b/tests/Unit/Controllers/Api/AngelTypeControllerTest.php @@ -16,7 +16,7 @@ class AngelTypeControllerTest extends ApiBaseControllerTest public function testIndex(): void { $this->initDatabase(); - AngelType::factory(3)->create(); + $items = AngelType::factory(3)->create(); $controller = new AngelTypeController(new Response(), $this->url); @@ -29,5 +29,8 @@ class AngelTypeControllerTest extends ApiBaseControllerTest $data = json_decode($response->getContent(), true); $this->assertArrayHasKey('data', $data); $this->assertCount(3, $data['data']); + $this->assertCount(1, collect($data['data'])->filter(function ($item) use ($items) { + return $item['name'] == $items->first()->getAttribute('name'); + })); } } diff --git a/tests/Unit/Controllers/Api/NewsControllerTest.php b/tests/Unit/Controllers/Api/NewsControllerTest.php index 1cd67e6b..f85a8e51 100644 --- a/tests/Unit/Controllers/Api/NewsControllerTest.php +++ b/tests/Unit/Controllers/Api/NewsControllerTest.php @@ -15,7 +15,7 @@ class NewsControllerTest extends ApiBaseControllerTest */ public function testIndex(): void { - News::factory(3)->create(); + $items = News::factory(3)->create(); $controller = new NewsController(new Response(), $this->url); @@ -28,5 +28,9 @@ class NewsControllerTest extends ApiBaseControllerTest $data = json_decode($response->getContent(), true); $this->assertArrayHasKey('data', $data); $this->assertCount(3, $data['data']); + + $this->assertCount(1, collect($data['data'])->filter(function ($item) use ($items) { + return $item['title'] == $items->first()->getAttribute('title'); + })); } } diff --git a/tests/Unit/Controllers/Api/RoomsControllerTest.php b/tests/Unit/Controllers/Api/RoomsControllerTest.php index bf20ece8..af0375dd 100644 --- a/tests/Unit/Controllers/Api/RoomsControllerTest.php +++ b/tests/Unit/Controllers/Api/RoomsControllerTest.php @@ -16,7 +16,7 @@ class RoomsControllerTest extends ApiBaseControllerTest public function testIndex(): void { $this->initDatabase(); - Room::factory(3)->create(); + $items = Room::factory(3)->create(); $controller = new RoomsController(new Response(), $this->url); @@ -29,5 +29,8 @@ class RoomsControllerTest extends ApiBaseControllerTest $data = json_decode($response->getContent(), true); $this->assertArrayHasKey('data', $data); $this->assertCount(3, $data['data']); + $this->assertCount(1, collect($data['data'])->filter(function ($item) use ($items) { + return $item['name'] == $items->first()->getAttribute('name'); + })); } } diff --git a/tests/Unit/Controllers/Api/ShiftsControllerTest.php b/tests/Unit/Controllers/Api/ShiftsControllerTest.php index 238b5a70..0c980eaf 100644 --- a/tests/Unit/Controllers/Api/ShiftsControllerTest.php +++ b/tests/Unit/Controllers/Api/ShiftsControllerTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Engelsystem\Test\Unit\Controllers\Api; use Engelsystem\Controllers\Api\ShiftsController; +use Engelsystem\Helpers\Carbon; use Engelsystem\Http\Request; use Engelsystem\Http\Response; use Engelsystem\Models\Room; @@ -14,7 +15,6 @@ use Engelsystem\Models\Shifts\ShiftEntry; use Engelsystem\Models\User\Contact; use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\User; -use Illuminate\Database\Eloquent\Collection; class ShiftsControllerTest extends ApiBaseControllerTest { @@ -30,10 +30,14 @@ class ShiftsControllerTest extends ApiBaseControllerTest $room = Room::factory()->create(); // Shifts - /** @var Collection|Shift[] $shifts */ - $shifts = Shift::factory(2) - ->create(['room_id' => $room->id]); - $shiftA = $shifts[0]; + /** @var Shift $shiftA */ + $shiftA = Shift::factory(1) + ->create(['room_id' => $room->id, 'start' => Carbon::now()->subHour()]) + ->first(); + /** @var Shift $shiftB */ + $shiftB = Shift::factory(1) + ->create(['room_id' => $room->id, 'start' => Carbon::now()->addHour()]) + ->first(); // "Empty" entry to be skipped NeededAngelType::factory(1)->create(['room_id' => null, 'shift_id' => $shiftA->id, 'count' => 0]); @@ -87,5 +91,32 @@ class ShiftsControllerTest extends ApiBaseControllerTest $data = json_decode($response->getContent(), true); $this->assertArrayHasKey('data', $data); $this->assertCount(2, $data['data']); + + // First shift + $shiftAData = $data['data'][0]; + $this->assertEquals($shiftA->title, $shiftAData['title'], 'Title is equal'); + $this->assertEquals($room->id, $shiftAData['room']['id'], 'Same room'); + $this->assertEquals($shiftA->shiftType->id, $shiftAData['shift_type']['id'], 'Shift type equals'); + $this->assertCount(4, $shiftAData['entries']); + // Has users + $entriesA = collect($shiftAData['entries'])->sortBy('type.id'); + $entry = $entriesA[0]; + $this->assertCount(2, $entry['users']); + $this->assertEquals(5, $entry['needs']); + $user = $entry['users'][0]; + $this->assertEquals('/users?action=view&user_id=' . $user['id'], $user['url']); + $this->assertCount(0, $entriesA[1]['users']); + $this->assertCount(1, $entriesA[2]['users']); + $this->assertCount(1, $entriesA[3]['users']); + + // Second (empty) shift + $shiftBData = $data['data'][1]; + $this->assertEquals($shiftB->title, $shiftBData['title'], 'Title is equal'); + $this->assertEquals($room->id, $shiftBData['room']['id'], 'Same room'); + $this->assertEquals($shiftB->shiftType->id, $shiftBData['shift_type']['id'], 'Shift type equals'); + $this->assertCount(2, $shiftBData['entries']); + // No users + $entriesB = collect($shiftBData['entries'])->sortBy('type.id'); + $this->assertCount(0, $entriesB[0]['users']); } }