API: Added API value tests

This commit is contained in:
Igor Scheller 2023-07-31 20:49:17 +02:00 committed by Michael Weimann
parent 1a250dc250
commit 2e38b55167
4 changed files with 49 additions and 8 deletions

View File

@ -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');
}));
}
}

View File

@ -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');
}));
}
}

View File

@ -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');
}));
}
}

View File

@ -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']);
}
}