165 lines
4.5 KiB
PHP
165 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Dto\PickupExceptionDto;
|
|
use App\Models\Customer;
|
|
use App\Models\PickupException;
|
|
use App\Models\User;
|
|
use App\Policies\Backend\PickupExceptionPolicy;
|
|
use Carbon\Carbon;
|
|
use Domain\Enums\RepeatFrequency;
|
|
use Domain\Enums\UserRole;
|
|
|
|
describe('PickupExceptionPolicy', function () {
|
|
test('viewAny returns false for customer', function () {
|
|
// GIVEN
|
|
$customer = new Customer();
|
|
$policy = new PickupExceptionPolicy();
|
|
|
|
// WHEN
|
|
$result = $policy->viewAny($customer);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
test('viewAny returns true for admin user', function () {
|
|
// GIVEN
|
|
$user = new User();
|
|
$user->role = UserRole::Admin;
|
|
$policy = new PickupExceptionPolicy();
|
|
|
|
// WHEN
|
|
$result = $policy->viewAny($user);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
test('view returns false for customer', function () {
|
|
// GIVEN
|
|
$customer = new Customer();
|
|
$policy = new PickupExceptionPolicy();
|
|
$modelInstance = new PickupException();
|
|
|
|
// WHEN
|
|
$result = $policy->view($customer, $modelInstance);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
test('view returns true for admin user', function () {
|
|
// GIVEN
|
|
$user = new User();
|
|
$user->role = UserRole::Admin;
|
|
$policy = new PickupExceptionPolicy();
|
|
$modelInstance = new PickupException();
|
|
|
|
// WHEN
|
|
$result = $policy->view($user, $modelInstance);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
test('create returns false for customer', function () {
|
|
// GIVEN
|
|
$customer = new Customer();
|
|
$policy = new PickupExceptionPolicy();
|
|
|
|
// WHEN
|
|
$result = $policy->create($customer);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
test('create returns true for admin user', function () {
|
|
// GIVEN
|
|
$user = new User();
|
|
$user->role = UserRole::Admin;
|
|
$policy = new PickupExceptionPolicy();
|
|
|
|
// WHEN
|
|
$result = $policy->create($user);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
test('update returns false for customer', function () {
|
|
// GIVEN
|
|
$customer = new Customer();
|
|
$policy = new PickupExceptionPolicy();
|
|
$modelInstance = new PickupException();
|
|
|
|
// WHEN
|
|
$result = $policy->update($customer, $modelInstance);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
test('update returns true for admin user', function () {
|
|
// GIVEN
|
|
$user = new User();
|
|
$user->role = UserRole::Admin;
|
|
$policy = new PickupExceptionPolicy();
|
|
$modelInstance = new PickupException();
|
|
|
|
// WHEN
|
|
$result = $policy->update($user, $modelInstance);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
test('delete returns false for customer', function () {
|
|
// GIVEN
|
|
$customer = new Customer();
|
|
$policy = new PickupExceptionPolicy();
|
|
$modelInstance = new PickupException();
|
|
|
|
// WHEN
|
|
$result = $policy->delete($customer, $modelInstance);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
|
|
test('delete returns true for admin user', function () {
|
|
// GIVEN
|
|
$user = new User();
|
|
$user->role = UserRole::Admin;
|
|
$policy = new PickupExceptionPolicy();
|
|
$modelInstance = new PickupException();
|
|
|
|
// WHEN
|
|
$result = $policy->delete($user, $modelInstance);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
test('policy methods work with PickupExceptionDto', function () {
|
|
// GIVEN
|
|
$user = new User();
|
|
$user->role = UserRole::Admin;
|
|
$policy = new PickupExceptionPolicy();
|
|
$dto = new PickupExceptionDto(
|
|
id: '018f6c40-1234-7123-8123-123456789012',
|
|
date: Carbon::parse('2026-05-01'),
|
|
label: '1er Mai',
|
|
repeatable: true,
|
|
repeat_frequency: RepeatFrequency::YEARLY,
|
|
auto_updated: false,
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect($policy->view($user, $dto))->toBeTrue()
|
|
->and($policy->update($user, $dto))->toBeTrue()
|
|
->and($policy->delete($user, $dto))->toBeTrue();
|
|
});
|
|
});
|