engelsystem/tests/Unit/Controllers/Admin/LocationsControllerTest.php

225 lines
8.2 KiB
PHP
Raw Permalink Normal View History

2023-02-26 11:27:41 +01:00
<?php
declare(strict_types=1);
namespace Engelsystem\Test\Unit\Controllers\Admin;
2023-10-15 19:25:55 +02:00
use Engelsystem\Controllers\Admin\LocationsController;
2023-02-26 11:27:41 +01:00
use Engelsystem\Events\EventDispatcher;
use Engelsystem\Helpers\Carbon;
use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Redirector;
use Engelsystem\Http\Validation\Validator;
use Engelsystem\Models\AngelType;
2023-10-15 19:25:55 +02:00
use Engelsystem\Models\Location;
2023-02-26 11:27:41 +01:00
use Engelsystem\Models\Shifts\NeededAngelType;
use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftEntry;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\Controllers\ControllerTest;
use PHPUnit\Framework\MockObject\MockObject;
2023-10-15 19:25:55 +02:00
class LocationsControllerTest extends ControllerTest
2023-02-26 11:27:41 +01:00
{
protected Redirector|MockObject $redirect;
/**
2023-10-15 19:25:55 +02:00
* @covers \Engelsystem\Controllers\Admin\LocationsController::__construct
* @covers \Engelsystem\Controllers\Admin\LocationsController::index
2023-02-26 11:27:41 +01:00
*/
public function testIndex(): void
{
2023-10-15 19:25:55 +02:00
/** @var LocationsController $controller */
$controller = $this->app->make(LocationsController::class);
Location::factory(5)->create();
2023-02-26 11:27:41 +01:00
$this->response->expects($this->once())
->method('withView')
->willReturnCallback(function (string $view, array $data) {
2023-10-15 19:25:55 +02:00
$this->assertEquals('admin/locations/index', $view);
2023-02-26 11:27:41 +01:00
$this->assertTrue($data['is_index'] ?? false);
2023-10-15 19:25:55 +02:00
$this->assertCount(5, $data['locations'] ?? []);
2023-02-26 11:27:41 +01:00
return $this->response;
});
$controller->index();
}
/**
2023-10-15 19:25:55 +02:00
* @covers \Engelsystem\Controllers\Admin\LocationsController::edit
* @covers \Engelsystem\Controllers\Admin\LocationsController::showEdit
2023-02-26 11:27:41 +01:00
*/
public function testEdit(): void
{
2023-10-15 19:25:55 +02:00
/** @var LocationsController $controller */
$controller = $this->app->make(LocationsController::class);
/** @var Location $location */
$location = Location::factory()->create();
2023-02-26 11:27:41 +01:00
$angelTypes = AngelType::factory(3)->create();
2023-10-15 19:25:55 +02:00
(new NeededAngelType([
'location_id' => $location->id,
'angel_type_id' => $angelTypes[0]->id,
'count' => 3,
]))->save();
2023-02-26 11:27:41 +01:00
$this->response->expects($this->once())
->method('withView')
2023-10-15 19:25:55 +02:00
->willReturnCallback(function (string $view, array $data) use ($location) {
$this->assertEquals('admin/locations/edit', $view);
$this->assertEquals($location->id, $data['location']?->id);
2023-02-26 11:27:41 +01:00
$this->assertCount(3, $data['angel_types'] ?? []);
$this->assertCount(1, $data['needed_angel_types'] ?? []);
return $this->response;
});
2023-10-15 19:25:55 +02:00
$this->request = $this->request->withAttribute('location_id', 1);
2023-02-26 11:27:41 +01:00
$controller->edit($this->request);
}
/**
2023-10-15 19:25:55 +02:00
* @covers \Engelsystem\Controllers\Admin\LocationsController::edit
* @covers \Engelsystem\Controllers\Admin\LocationsController::showEdit
2023-02-26 11:27:41 +01:00
*/
public function testEditNew(): void
{
2023-10-15 19:25:55 +02:00
/** @var LocationsController $controller */
$controller = $this->app->make(LocationsController::class);
2023-02-26 11:27:41 +01:00
AngelType::factory(3)->create();
$this->response->expects($this->once())
->method('withView')
->willReturnCallback(function (string $view, array $data) {
2023-10-15 19:25:55 +02:00
$this->assertEquals('admin/locations/edit', $view);
$this->assertEmpty($data['location'] ?? []);
2023-02-26 11:27:41 +01:00
$this->assertCount(3, $data['angel_types'] ?? []);
$this->assertEmpty($data['needed_angel_types'] ?? []);
return $this->response;
});
$controller->edit($this->request);
}
/**
2023-10-15 19:25:55 +02:00
* @covers \Engelsystem\Controllers\Admin\LocationsController::save
2023-02-26 11:27:41 +01:00
*/
public function testSave(): void
{
2023-10-15 19:25:55 +02:00
/** @var LocationsController $controller */
$controller = $this->app->make(LocationsController::class);
2023-02-26 11:27:41 +01:00
$controller->setValidator(new Validator());
AngelType::factory(3)->create();
2023-10-15 19:25:55 +02:00
$this->setExpects($this->redirect, 'to', ['/admin/locations']);
2023-02-26 11:27:41 +01:00
$this->request = $this->request->withParsedBody([
2023-10-13 11:53:13 +02:00
'name' => 'Testlocation',
2023-02-26 11:27:41 +01:00
'description' => 'Something',
'dect' => 'DECTNR',
'map_url' => 'https://osm.url/#map=h/x/y',
'angel_type_1' => '0',
'angel_type_2' => '3',
]);
$controller->save($this->request);
2023-10-13 11:53:13 +02:00
$this->assertTrue($this->log->hasInfoThatContains('Updated location'));
2023-10-15 19:25:55 +02:00
$this->assertHasNotification('location.edit.success');
$this->assertCount(1, Location::whereName('Testlocation')->get());
2023-02-26 11:27:41 +01:00
2023-10-15 19:25:55 +02:00
$neededAngelType = NeededAngelType::whereLocationId(1)
2023-02-26 11:27:41 +01:00
->where('angel_type_id', 2)
->where('count', 3)
->get();
$this->assertCount(1, $neededAngelType);
}
/**
2023-10-15 19:25:55 +02:00
* @covers \Engelsystem\Controllers\Admin\LocationsController::save
2023-02-26 11:27:41 +01:00
*/
public function testSaveUniqueName(): void
{
2023-10-15 19:25:55 +02:00
/** @var LocationsController $controller */
$controller = $this->app->make(LocationsController::class);
2023-02-26 11:27:41 +01:00
$controller->setValidator(new Validator());
2023-10-15 19:25:55 +02:00
Location::factory()->create(['name' => 'Testlocation']);
2023-02-26 11:27:41 +01:00
$this->request = $this->request->withParsedBody([
2023-10-13 11:53:13 +02:00
'name' => 'Testlocation',
2023-02-26 11:27:41 +01:00
]);
$this->expectException(ValidationException::class);
$controller->save($this->request);
}
/**
2023-10-15 19:25:55 +02:00
* @covers \Engelsystem\Controllers\Admin\LocationsController::save
* @covers \Engelsystem\Controllers\Admin\LocationsController::delete
2023-02-26 11:27:41 +01:00
*/
public function testSaveDelete(): void
{
2023-10-15 19:25:55 +02:00
/** @var LocationsController $controller */
$controller = $this->app->make(LocationsController::class);
2023-02-26 11:27:41 +01:00
$controller->setValidator(new Validator());
2023-10-15 19:25:55 +02:00
/** @var Location $location */
$location = Location::factory()->create();
2023-02-26 11:27:41 +01:00
$this->request = $this->request->withParsedBody([
'id' => '1',
'delete' => '1',
]);
$controller->save($this->request);
2023-10-15 19:25:55 +02:00
$this->assertEmpty(Location::find($location->id));
2023-02-26 11:27:41 +01:00
}
/**
2023-10-15 19:25:55 +02:00
* @covers \Engelsystem\Controllers\Admin\LocationsController::delete
2023-02-26 11:27:41 +01:00
*/
public function testDelete(): void
{
/** @var EventDispatcher|MockObject $dispatcher */
$dispatcher = $this->createMock(EventDispatcher::class);
$this->app->instance('events.dispatcher', $dispatcher);
2023-10-15 19:25:55 +02:00
/** @var LocationsController $controller */
$controller = $this->app->make(LocationsController::class);
2023-02-26 11:27:41 +01:00
$controller->setValidator(new Validator());
2023-10-15 19:25:55 +02:00
/** @var Location $location */
$location = Location::factory()->create();
2023-02-26 11:27:41 +01:00
/** @var Shift $shift */
2023-10-15 19:25:55 +02:00
$shift = Shift::factory()->create(['location_id' => $location->id, 'start' => Carbon::create()->subHour()]);
2023-02-26 11:27:41 +01:00
/** @var User $user */
$user = User::factory()->create(['name' => 'foo', 'email' => 'lorem@ipsum']);
/** @var ShiftEntry $shiftEntry */
ShiftEntry::factory()->create(['shift_id' => $shift->id, 'user_id' => $user->id]);
2023-10-15 19:25:55 +02:00
$this->setExpects($this->redirect, 'to', ['/admin/locations'], $this->response);
2023-02-26 11:27:41 +01:00
$dispatcher->expects($this->once())
->method('dispatch')
2023-10-15 19:25:55 +02:00
->willReturnCallback(function (string $event, array $data) use ($location, $user) {
$this->assertEquals('shift.deleting', $event);
$this->assertEquals($location->id, $data['shift']->location->id);
$this->assertEquals($user->id, $data['shift']->shiftEntries[0]->user->id);
2023-02-26 11:27:41 +01:00
return [];
});
$this->request = $this->request->withParsedBody(['id' => 1, 'delete' => '1']);
$controller->delete($this->request);
2023-10-15 19:25:55 +02:00
$this->assertNull(Location::find($location->id));
2023-10-13 11:53:13 +02:00
$this->assertTrue($this->log->hasInfoThatContains('Deleted location'));
2023-10-15 19:25:55 +02:00
$this->assertHasNotification('location.delete.success');
2023-02-26 11:27:41 +01:00
}
public function setUp(): void
{
parent::setUp();
$this->redirect = $this->createMock(Redirector::class);
$this->app->instance(Redirector::class, $this->redirect);
}
}