2023-07-23 13:52:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Api;
|
|
|
|
|
|
|
|
use Engelsystem\Controllers\Api\ShiftsController;
|
2023-07-31 20:49:17 +02:00
|
|
|
use Engelsystem\Helpers\Carbon;
|
2023-07-23 13:52:33 +02:00
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
use Engelsystem\Http\Response;
|
2023-11-03 19:03:31 +01:00
|
|
|
use Engelsystem\Models\Location;
|
2023-07-29 21:37:46 +02:00
|
|
|
use Engelsystem\Models\Shifts\NeededAngelType;
|
2023-07-23 13:52:33 +02:00
|
|
|
use Engelsystem\Models\Shifts\Shift;
|
|
|
|
use Engelsystem\Models\Shifts\ShiftEntry;
|
|
|
|
use Engelsystem\Models\User\Contact;
|
|
|
|
use Engelsystem\Models\User\PersonalData;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
|
|
|
|
class ShiftsControllerTest extends ApiBaseControllerTest
|
|
|
|
{
|
|
|
|
/**
|
2023-11-03 19:03:31 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Api\ShiftsController::entriesByLocation
|
2023-07-29 21:37:46 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Api\ShiftsController::getNeededAngelTypes
|
2023-07-23 13:52:33 +02:00
|
|
|
*/
|
2023-11-03 19:03:31 +01:00
|
|
|
public function testEntriesByLocation(): void
|
2023-07-23 13:52:33 +02:00
|
|
|
{
|
|
|
|
$this->initDatabase();
|
|
|
|
|
2023-11-03 19:03:31 +01:00
|
|
|
/** @var Location $location */
|
|
|
|
$location = Location::factory()->create();
|
2023-07-23 13:52:33 +02:00
|
|
|
|
2023-07-29 21:37:46 +02:00
|
|
|
// Shifts
|
2023-07-31 20:49:17 +02:00
|
|
|
/** @var Shift $shiftA */
|
|
|
|
$shiftA = Shift::factory(1)
|
2023-11-03 19:03:31 +01:00
|
|
|
->create(['location_id' => $location->id, 'start' => Carbon::now()->subHour()])
|
2023-07-31 20:49:17 +02:00
|
|
|
->first();
|
|
|
|
/** @var Shift $shiftB */
|
|
|
|
$shiftB = Shift::factory(1)
|
2023-11-03 19:03:31 +01:00
|
|
|
->create(['location_id' => $location->id, 'start' => Carbon::now()->addHour()])
|
2023-07-31 20:49:17 +02:00
|
|
|
->first();
|
2023-07-29 21:37:46 +02:00
|
|
|
|
|
|
|
// "Empty" entry to be skipped
|
2023-11-03 19:03:31 +01:00
|
|
|
NeededAngelType::factory(1)->create(['location_id' => null, 'shift_id' => $shiftA->id, 'count' => 0]);
|
2023-07-29 21:37:46 +02:00
|
|
|
|
|
|
|
// Needed entry by shift
|
|
|
|
/** @var NeededAngelType $byShift */
|
|
|
|
$byShift = NeededAngelType::factory(2)
|
2023-11-03 19:03:31 +01:00
|
|
|
->create(['location_id' => null, 'shift_id' => $shiftA->id, 'count' => 2])
|
2023-07-29 21:37:46 +02:00
|
|
|
->first();
|
|
|
|
|
2023-11-03 19:03:31 +01:00
|
|
|
// Needed entry by location
|
|
|
|
/** @var NeededAngelType $byLocation */
|
|
|
|
$byLocation = NeededAngelType::factory(1)
|
|
|
|
->create(['location_id' => $location->id, 'shift_id' => null, 'count' => 3])
|
2023-07-29 21:37:46 +02:00
|
|
|
->first();
|
|
|
|
|
|
|
|
// Added by both
|
|
|
|
NeededAngelType::factory(1)
|
|
|
|
->create([
|
2023-11-03 19:03:31 +01:00
|
|
|
'location_id' => $location->id,
|
|
|
|
'shift_id' => null,
|
|
|
|
'angel_type_id' => $byShift->angel_type_id,
|
|
|
|
'count' => 3,
|
2023-07-29 21:37:46 +02:00
|
|
|
])
|
|
|
|
->first();
|
|
|
|
|
|
|
|
// By shift
|
|
|
|
ShiftEntry::factory(2)->create(['shift_id' => $shiftA->id, 'angel_type_id' => $byShift->angel_type_id]);
|
|
|
|
|
2023-11-03 19:03:31 +01:00
|
|
|
// By location
|
|
|
|
ShiftEntry::factory(1)->create(['shift_id' => $shiftA->id, 'angel_type_id' => $byLocation->angel_type_id]);
|
2023-07-29 21:37:46 +02:00
|
|
|
|
2023-11-03 19:03:31 +01:00
|
|
|
// Additional (not required by shift nor location)
|
2023-07-29 21:37:46 +02:00
|
|
|
ShiftEntry::factory(1)->create(['shift_id' => $shiftA->id]);
|
2023-07-23 13:52:33 +02:00
|
|
|
|
|
|
|
foreach (User::all() as $user) {
|
|
|
|
// Generate user data
|
|
|
|
/** @var User $user */
|
|
|
|
PersonalData::factory()->create(['user_id' => $user->id]);
|
|
|
|
Contact::factory()->create(['user_id' => $user->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = new Request();
|
2023-11-03 19:03:31 +01:00
|
|
|
$request = $request->withAttribute('location_id', $location->id);
|
2023-07-23 13:52:33 +02:00
|
|
|
|
2023-07-27 14:34:21 +02:00
|
|
|
$controller = new ShiftsController(new Response(), $this->url);
|
2023-07-23 13:52:33 +02:00
|
|
|
|
2023-11-03 19:03:31 +01:00
|
|
|
$response = $controller->entriesByLocation($request);
|
|
|
|
$this->validateApiResponse('/locations/{id}/shifts', 'get', $response);
|
2023-07-23 13:52:33 +02:00
|
|
|
|
|
|
|
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
|
|
|
|
$this->assertJson($response->getContent());
|
|
|
|
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
$this->assertArrayHasKey('data', $data);
|
2023-07-29 21:37:46 +02:00
|
|
|
$this->assertCount(2, $data['data']);
|
2023-07-31 20:49:17 +02:00
|
|
|
|
|
|
|
// First shift
|
|
|
|
$shiftAData = $data['data'][0];
|
|
|
|
$this->assertEquals($shiftA->title, $shiftAData['title'], 'Title is equal');
|
2023-11-03 19:03:31 +01:00
|
|
|
$this->assertEquals($location->id, $shiftAData['location']['id'], 'Same location');
|
2023-07-31 20:49:17 +02:00
|
|
|
$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');
|
2023-11-03 19:03:31 +01:00
|
|
|
$this->assertEquals($location->id, $shiftBData['location']['id'], 'Same location');
|
2023-07-31 20:49:17 +02:00
|
|
|
$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']);
|
2023-07-23 13:52:33 +02:00
|
|
|
}
|
|
|
|
}
|