refactor(bo): encapsulate pickup exception filter in IndexShopRequest, align VOs and DTOs
This commit is contained in:
parent
b2c1b6eb1d
commit
9f1b4242b9
16 changed files with 396 additions and 106 deletions
|
|
@ -11,14 +11,13 @@ use Webmozart\Assert\Assert;
|
|||
final readonly class UpdateShopPickupExceptionsInput implements ActionInput
|
||||
{
|
||||
/**
|
||||
* @param array<int|string, array{replacement_date: string|null, order_deadline_at: string|null}> $syncData
|
||||
* @param array<array-key, array{replacement_date: string|null, order_deadline_at: string|null}> $syncData
|
||||
*/
|
||||
public function __construct(
|
||||
public Shop $shop,
|
||||
public array $syncData,
|
||||
) {
|
||||
foreach ($this->syncData as $key => $item) {
|
||||
Assert::true(is_int($key) || is_string($key), 'Sync key must be an integer or string.');
|
||||
foreach ($this->syncData as $item) {
|
||||
Assert::isArray($item, 'Each sync item must be an array.');
|
||||
Assert::keyExists($item, 'replacement_date', 'Sync item must contain replacement_date key.');
|
||||
Assert::keyExists($item, 'order_deadline_at', 'Sync item must contain order_deadline_at key.');
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace App\Dto;
|
||||
|
||||
use App\Models\PickupException;
|
||||
use App\Models\ShopPickupExceptionPivot;
|
||||
use Carbon\CarbonInterface;
|
||||
use Domain\Enums\RepeatFrequency;
|
||||
use Illuminate\Contracts\Routing\UrlRoutable;
|
||||
|
|
@ -29,10 +30,12 @@ final readonly class PickupExceptionDto implements Arrayable, UrlRoutable
|
|||
public static function fromModel(PickupException $pickupException): self
|
||||
{
|
||||
$pivot = null;
|
||||
if ($pickupException->pivot !== null) {
|
||||
$pivot = new ShopPickupExceptionPivotDto(
|
||||
replacement_date: $pickupException->pivot->replacement_date,
|
||||
order_deadline_at: $pickupException->pivot->order_deadline_at,
|
||||
if ($pickupException->relationLoaded('pivot') && $pickupException->getRelation('pivot') instanceof ShopPickupExceptionPivot) {
|
||||
/** @var ShopPickupExceptionPivot $pivotModel */
|
||||
$pivotModel = $pickupException->getRelation('pivot');
|
||||
$pivot = new ShopPickupExceptionPivotDto(
|
||||
replacement_date: $pivotModel->replacement_date,
|
||||
order_deadline_at: $pivotModel->order_deadline_at,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ use App\Actions\Shops\UpdateShopPickupExceptionsInput;
|
|||
use App\Contracts\Executor;
|
||||
use App\Dto\ShopDto;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Bo\Shops\IndexShopRequest;
|
||||
use App\Http\Requests\Bo\Shops\StoreShopRequest;
|
||||
use App\Http\Requests\Bo\Shops\UpdateShopPickupExceptionsRequest;
|
||||
use App\Http\Requests\Bo\Shops\UpdateShopRequest;
|
||||
|
|
@ -23,11 +24,8 @@ use App\Queries\Shops\GetPaginatedShopsQuery;
|
|||
use App\Queries\Shops\GetPaginatedShopsQueryInput;
|
||||
use App\Queries\Shops\GetShopPickupExceptionsQuery;
|
||||
use App\Queries\Shops\GetShopPickupExceptionsQueryInput;
|
||||
use Domain\Models\ValueObjects\Coordinates;
|
||||
use Domain\Models\ValueObjects\PhoneNumber;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ShopsController extends Controller
|
||||
|
|
@ -37,26 +35,15 @@ class ShopsController extends Controller
|
|||
) {
|
||||
}
|
||||
|
||||
public function index(Request $request, GetPaginatedShopsQuery $query): View
|
||||
public function index(IndexShopRequest $request, GetPaginatedShopsQuery $query): View
|
||||
{
|
||||
$this->authorize('viewAny', Shop::class);
|
||||
|
||||
if ($request->has('pickup-exception')) {
|
||||
$request->session()->forget('shops.filter.pickup-exception');
|
||||
$request->session()->put(
|
||||
'shops.filter.pickup-exception',
|
||||
$request->input('pickup-exception')
|
||||
);
|
||||
}
|
||||
|
||||
$pickupException = $request->session()->get('shops.filter.pickup-exception');
|
||||
$pickupExceptionId = $pickupException !== null ? (string) $pickupException : null;
|
||||
|
||||
$output = $this->executor->execute(
|
||||
$query,
|
||||
new GetPaginatedShopsQueryInput(
|
||||
pagination: $request->pagination(),
|
||||
pickupExceptionId: $pickupExceptionId,
|
||||
pickupExceptionId: $request->toPickupExceptionId(),
|
||||
),
|
||||
);
|
||||
|
||||
|
|
@ -77,12 +64,8 @@ class ShopsController extends Controller
|
|||
{
|
||||
$this->authorize('create', Shop::class);
|
||||
|
||||
$coordinates = Coordinates::tryFrom(
|
||||
$request->input('latitude') !== null ? (string) $request->input('latitude') : null,
|
||||
$request->input('longitude') !== null ? (string) $request->input('longitude') : null,
|
||||
);
|
||||
|
||||
$phone = PhoneNumber::tryFrom($request->input('phone') !== null ? (string) $request->input('phone') : null);
|
||||
$coordinates = $request->toCoordinates();
|
||||
$phone = $request->toPhoneNumber();
|
||||
|
||||
$shop = $this->executor->execute(
|
||||
$action,
|
||||
|
|
@ -116,12 +99,8 @@ class ShopsController extends Controller
|
|||
{
|
||||
$this->authorize('update', $shop);
|
||||
|
||||
$coordinates = Coordinates::tryFrom(
|
||||
$request->input('latitude') !== null ? (string) $request->input('latitude') : null,
|
||||
$request->input('longitude') !== null ? (string) $request->input('longitude') : null,
|
||||
);
|
||||
|
||||
$phone = PhoneNumber::tryFrom($request->input('phone') !== null ? (string) $request->input('phone') : null);
|
||||
$coordinates = $request->toCoordinates();
|
||||
$phone = $request->toPhoneNumber();
|
||||
|
||||
$this->executor->execute(
|
||||
$action,
|
||||
|
|
@ -175,8 +154,11 @@ class ShopsController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
public function pickupExceptionsUpdate(UpdateShopPickupExceptionsRequest $request, Shop $shop, UpdateShopPickupExceptions $action): RedirectResponse
|
||||
{
|
||||
public function pickupExceptionsUpdate(
|
||||
UpdateShopPickupExceptionsRequest $request,
|
||||
Shop $shop,
|
||||
UpdateShopPickupExceptions $action
|
||||
): RedirectResponse {
|
||||
$this->authorize('pickupExceptionUpdate', $shop);
|
||||
|
||||
$pickupExceptions = $request->array('pickup-exceptions');
|
||||
|
|
|
|||
68
app/Http/Requests/Bo/Shops/IndexShopRequest.php
Normal file
68
app/Http/Requests/Bo/Shops/IndexShopRequest.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Bo\Shops;
|
||||
|
||||
use Domain\Models\ValueObjects\Entities\PickupExceptionId;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Infrastructure\Adapters\Services\AppAdminAuthService;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\Helpers\UuidHelper;
|
||||
|
||||
class IndexShopRequest extends FormRequest
|
||||
{
|
||||
public const string SESSION_KEY = 'shops.filter.pickup-exception';
|
||||
|
||||
public function __construct(
|
||||
private readonly AppAdminAuthService $authService
|
||||
) {
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->authService->check();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('pickup-exception')) {
|
||||
$filterValue = $this->string('pickup-exception');
|
||||
|
||||
if ($filterValue->isEmpty()) {
|
||||
$this->session()->forget(self::SESSION_KEY);
|
||||
} else {
|
||||
$this->session()->put(self::SESSION_KEY, (string) $filterValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function toPickupExceptionId(): ?PickupExceptionId
|
||||
{
|
||||
$raw = $this->session()->get(self::SESSION_KEY);
|
||||
if ($raw === null || ! is_string($raw) || $raw === '' || ! Uuid::isValid($raw)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$uuid = Uuid::fromString($raw);
|
||||
if (! UuidHelper::isV7($uuid)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new PickupExceptionId($uuid);
|
||||
}
|
||||
|
||||
public function getPickupExceptionId(): ?PickupExceptionId
|
||||
{
|
||||
return $this->toPickupExceptionId();
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'pickup-exception' => ['sometimes', 'nullable', 'string', 'uuid'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -4,9 +4,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Http\Requests\Bo\Shops;
|
||||
|
||||
use Domain\Models\ValueObjects\Coordinates;
|
||||
use Domain\Models\ValueObjects\PhoneNumber as DomainPhoneNumber;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Infrastructure\Adapters\Services\AppAdminAuthService;
|
||||
use Propaganistas\LaravelPhone\PhoneNumber;
|
||||
use Propaganistas\LaravelPhone\PhoneNumber as LaravelPhoneNumber;
|
||||
|
||||
class StoreShopRequest extends FormRequest
|
||||
{
|
||||
|
|
@ -20,6 +22,44 @@ class StoreShopRequest extends FormRequest
|
|||
return $this->authService->check();
|
||||
}
|
||||
|
||||
public function toCoordinates(): ?Coordinates
|
||||
{
|
||||
/** @var float|null $latitude */
|
||||
$latitude = $this->whenHas(
|
||||
'latitude',
|
||||
fn ($value) => is_numeric($value) ? (float) $value : null,
|
||||
fn () => null,
|
||||
);
|
||||
/** @var float|null $longitude */
|
||||
$longitude = $this->whenHas(
|
||||
'longitude',
|
||||
fn ($value) => is_numeric($value) ? (float) $value : null,
|
||||
fn () => null,
|
||||
);
|
||||
|
||||
if ($latitude !== null && $longitude !== null) {
|
||||
return new Coordinates($latitude, $longitude);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function toPhoneNumber(): ?DomainPhoneNumber
|
||||
{
|
||||
$phone = $this->input('phone');
|
||||
|
||||
if ($phone !== null && $phone !== '') {
|
||||
/** @var LaravelPhoneNumber $phone */
|
||||
$phone = $phone instanceof LaravelPhoneNumber
|
||||
? $phone
|
||||
: new LaravelPhoneNumber((string) $phone, 'FR');
|
||||
|
||||
return new DomainPhoneNumber($phone->formatRFC3966());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
|
|
@ -47,7 +87,7 @@ class StoreShopRequest extends FormRequest
|
|||
protected function passedValidation()
|
||||
{
|
||||
if ($this->filled('phone')) {
|
||||
$inputValue = new PhoneNumber(
|
||||
$inputValue = new LaravelPhoneNumber(
|
||||
$this->string('phone')->value(),
|
||||
'FR'
|
||||
);
|
||||
|
|
|
|||
|
|
@ -34,10 +34,10 @@ final readonly class GetPaginatedShopsQuery implements Query
|
|||
|
||||
$query = Shop::query();
|
||||
|
||||
if ($input->pickupExceptionId !== null && $input->pickupExceptionId !== '') {
|
||||
if ($input->pickupExceptionId !== null) {
|
||||
$query->whereHas(
|
||||
'pickupExceptions',
|
||||
fn (Builder $q) => $q->where('id', $input->pickupExceptionId)
|
||||
fn (Builder $q) => $q->where('id', (string) $input->pickupExceptionId)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,13 +5,14 @@ declare(strict_types=1);
|
|||
namespace App\Queries\Shops;
|
||||
|
||||
use App\Contracts\QueryInput;
|
||||
use Domain\Models\ValueObjects\Entities\PickupExceptionId;
|
||||
use Domain\Models\ValueObjects\Pagination;
|
||||
|
||||
final readonly class GetPaginatedShopsQueryInput implements QueryInput
|
||||
{
|
||||
public function __construct(
|
||||
public Pagination $pagination,
|
||||
public ?string $pickupExceptionId = null,
|
||||
public ?PickupExceptionId $pickupExceptionId = null,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,16 +17,4 @@ final readonly class Coordinates
|
|||
Assert::greaterThanEq($this->longitude, -180.0, 'Longitude must be between -180 and 180 degrees.');
|
||||
Assert::lessThanEq($this->longitude, 180.0, 'Longitude must be between -180 and 180 degrees.');
|
||||
}
|
||||
|
||||
public static function tryFrom(?string $latitude, ?string $longitude): ?self
|
||||
{
|
||||
if ($latitude === null || $longitude === null || trim($latitude) === '' || trim($longitude) === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
Assert::numeric($latitude, 'Latitude must be numeric.');
|
||||
Assert::numeric($longitude, 'Longitude must be numeric.');
|
||||
|
||||
return new self((float) $latitude, (float) $longitude);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,16 +15,7 @@ final readonly class PhoneNumber implements Stringable
|
|||
Assert::stringNotEmpty($trimmed, 'Phone number cannot be empty.');
|
||||
Assert::minLength($trimmed, 4, 'Phone number must have at least 4 characters.');
|
||||
Assert::maxLength($trimmed, 30, 'Phone number cannot exceed 30 characters.');
|
||||
Assert::regex($trimmed, '/^\+?[0-9\s\-\.\(\)]+$/', 'Phone number format is invalid.');
|
||||
}
|
||||
|
||||
public static function tryFrom(?string $value): ?self
|
||||
{
|
||||
if ($value === null || trim($value) === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new self($value);
|
||||
Assert::regex($trimmed, '/^(tel:)?\+?[0-9\s\-\.\(\)]+$/', 'Phone number format is invalid.');
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Http\Requests\Bo\Shops\IndexShopRequest;
|
||||
use App\Models\PickupException;
|
||||
use App\Models\Shop;
|
||||
use App\Models\User;
|
||||
use Domain\Enums\UserRole;
|
||||
|
|
@ -25,6 +27,36 @@ describe('ShopsController', function () {
|
|||
$response->assertViewIs('bo.shops.index');
|
||||
});
|
||||
|
||||
it('can filter shops by pickup-exception and persist in session', function () {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$shop1 = Shop::factory()->create(['name' => 'Shop With Exception']);
|
||||
$shop2 = Shop::factory()->create(['name' => 'Shop Without Exception']);
|
||||
$exception = PickupException::factory()->create();
|
||||
$shop1->pickupExceptions()->attach($exception->id);
|
||||
|
||||
// WHEN
|
||||
$response = $this->get('/bo/shops?pickup-exception=' . $exception->id);
|
||||
|
||||
// THEN
|
||||
expect($response->status())->toBe(200);
|
||||
$response->assertViewIs('bo.shops.index');
|
||||
$response->assertSessionHas(IndexShopRequest::SESSION_KEY, $exception->id);
|
||||
});
|
||||
|
||||
it('clears pickup-exception filter from session when empty string is passed', function () {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
session([IndexShopRequest::SESSION_KEY => 'some-id']);
|
||||
|
||||
// WHEN
|
||||
$response = $this->get('/bo/shops?pickup-exception=');
|
||||
|
||||
// THEN
|
||||
expect($response->status())->toBe(200);
|
||||
$response->assertSessionMissing(IndexShopRequest::SESSION_KEY);
|
||||
});
|
||||
|
||||
it('can access create', function () {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// WHEN
|
||||
|
|
|
|||
|
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Http\Requests\Bo\Shops\IndexShopRequest;
|
||||
use Domain\Models\ValueObjects\Entities\PickupExceptionId;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Infrastructure\Adapters\Services\AppAdminAuthService;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
use function Pest\Laravel\mock;
|
||||
|
||||
describe('IndexShopRequest', function () {
|
||||
it('authorizes via auth service', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$authService->shouldReceive('check')->once()->andReturn(true);
|
||||
$request = new IndexShopRequest($authService);
|
||||
|
||||
// WHEN
|
||||
$request->initialize();
|
||||
|
||||
// THEN
|
||||
expect($request->authorize())->toBeTrue();
|
||||
});
|
||||
|
||||
it('returns rules', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$request = new IndexShopRequest($authService);
|
||||
|
||||
// WHEN
|
||||
$request->initialize();
|
||||
|
||||
// THEN
|
||||
expect($request->rules())->toBeArray()
|
||||
->and($request->rules())->toHaveKey('pickup-exception');
|
||||
});
|
||||
|
||||
it('prepares for validation and updates session', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$session = mock(Session::class);
|
||||
$uuid = Uuid::uuid7()->toString();
|
||||
|
||||
$session->shouldReceive('put')
|
||||
->once()
|
||||
->with(IndexShopRequest::SESSION_KEY, $uuid);
|
||||
|
||||
$request = new IndexShopRequest($authService);
|
||||
$request->initialize(query: ['pickup-exception' => $uuid]);
|
||||
$request->setLaravelSession($session);
|
||||
|
||||
$method = new \ReflectionMethod($request, 'prepareForValidation');
|
||||
$method->setAccessible(true);
|
||||
|
||||
// WHEN
|
||||
$method->invoke($request);
|
||||
|
||||
// THEN
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
it('prepares for validation and forgets session when pickup-exception is empty', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$session = mock(Session::class);
|
||||
|
||||
$session->shouldReceive('forget')
|
||||
->once()
|
||||
->with(IndexShopRequest::SESSION_KEY);
|
||||
|
||||
$request = new IndexShopRequest($authService);
|
||||
$request->initialize(query: ['pickup-exception' => '']);
|
||||
$request->setLaravelSession($session);
|
||||
|
||||
$method = new \ReflectionMethod($request, 'prepareForValidation');
|
||||
$method->setAccessible(true);
|
||||
|
||||
// WHEN
|
||||
$method->invoke($request);
|
||||
|
||||
// THEN
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
it('getPickupExceptionId returns PickupExceptionId from session', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$session = mock(Session::class);
|
||||
$uuid = Uuid::uuid7();
|
||||
|
||||
$session->shouldReceive('get')
|
||||
->once()
|
||||
->with(IndexShopRequest::SESSION_KEY)
|
||||
->andReturn($uuid->toString());
|
||||
|
||||
$request = new IndexShopRequest($authService);
|
||||
$request->initialize();
|
||||
$request->setLaravelSession($session);
|
||||
|
||||
// WHEN
|
||||
$id = $request->getPickupExceptionId();
|
||||
|
||||
// THEN
|
||||
expect($id)->toBeInstanceOf(PickupExceptionId::class)
|
||||
->and((string) $id)->toBe($uuid->toString());
|
||||
});
|
||||
|
||||
it('getPickupExceptionId returns null when session is empty or invalid', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$session = mock(Session::class);
|
||||
|
||||
$session->shouldReceive('get')
|
||||
->twice()
|
||||
->with(IndexShopRequest::SESSION_KEY)
|
||||
->andReturn(null);
|
||||
|
||||
$request = new IndexShopRequest($authService);
|
||||
$request->initialize();
|
||||
$request->setLaravelSession($session);
|
||||
|
||||
// WHEN
|
||||
$id = $request->getPickupExceptionId();
|
||||
|
||||
// THEN
|
||||
expect($id)->toBeNull()
|
||||
->and($request->toPickupExceptionId())->toBeNull();
|
||||
});
|
||||
|
||||
it('toPickupExceptionId returns PickupExceptionId from valid session', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$session = mock(Session::class);
|
||||
$uuid = Uuid::uuid7();
|
||||
|
||||
$session->shouldReceive('get')
|
||||
->once()
|
||||
->with(IndexShopRequest::SESSION_KEY)
|
||||
->andReturn($uuid->toString());
|
||||
|
||||
$request = new IndexShopRequest($authService);
|
||||
$request->initialize();
|
||||
$request->setLaravelSession($session);
|
||||
|
||||
// WHEN
|
||||
$id = $request->toPickupExceptionId();
|
||||
|
||||
// THEN
|
||||
expect($id)->toBeInstanceOf(PickupExceptionId::class)
|
||||
->and((string) $id)->toBe($uuid->toString());
|
||||
});
|
||||
});
|
||||
|
|
@ -60,18 +60,32 @@ describe('StoreShopRequest', function () {
|
|||
expect(true)->toBeTrue(); // just making sure it runs without exception
|
||||
});
|
||||
|
||||
it('passes validation', function () {
|
||||
it('converts to coordinates', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$request = new StoreShopRequest($authService);
|
||||
$request->initialize();
|
||||
$method = new \ReflectionMethod($request, 'passedValidation');
|
||||
$method->setAccessible(true);
|
||||
$request->initialize(query: ['latitude' => '48.8566', 'longitude' => '2.3522']);
|
||||
|
||||
// WHEN
|
||||
$method->invoke($request);
|
||||
$coords = $request->toCoordinates();
|
||||
|
||||
// THEN
|
||||
expect(true)->toBeTrue();
|
||||
expect($coords)->not->toBeNull()
|
||||
->and($coords?->latitude)->toBe(48.8566)
|
||||
->and($coords?->longitude)->toBe(2.3522);
|
||||
});
|
||||
|
||||
it('converts to phone number', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$request = new StoreShopRequest($authService);
|
||||
$request->initialize(query: ['phone' => '+33102030405']);
|
||||
|
||||
// WHEN
|
||||
$phone = $request->toPhoneNumber();
|
||||
|
||||
// THEN
|
||||
expect($phone)->not->toBeNull()
|
||||
->and((string) $phone)->toBe('tel:+33-1-02-03-04-05');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
48
tests/Integration/WithApp/App/Dto/PickupExceptionDtoTest.php
Normal file
48
tests/Integration/WithApp/App/Dto/PickupExceptionDtoTest.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Dto\PickupExceptionDto;
|
||||
use App\Models\PickupException;
|
||||
use App\Models\Shop;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
describe('PickupExceptionDto Integration', function () {
|
||||
test('fromModel works without pivot loaded', function () {
|
||||
// GIVEN
|
||||
$exception = PickupException::factory()->create();
|
||||
|
||||
// WHEN
|
||||
$dto = PickupExceptionDto::fromModel($exception);
|
||||
|
||||
// THEN
|
||||
expect($dto->id)->toBe($exception->id)
|
||||
->and($dto->label)->toBe($exception->label)
|
||||
->and($dto->pivot)->toBeNull();
|
||||
});
|
||||
|
||||
test('fromModel works with pivot loaded from shop relationship', function () {
|
||||
// GIVEN
|
||||
$shop = Shop::factory()->create();
|
||||
$exception = PickupException::factory()->create();
|
||||
|
||||
$shop->pickupExceptions()->attach($exception->id, [
|
||||
'replacement_date' => '2026-05-02',
|
||||
'order_deadline_at' => '2026-05-01 18:00:00',
|
||||
]);
|
||||
|
||||
$loadedShop = Shop::query()->with('pickupExceptions')->find($shop->id);
|
||||
$loadedException = $loadedShop?->pickupExceptions->first();
|
||||
|
||||
// WHEN
|
||||
$dto = PickupExceptionDto::fromModel($loadedException);
|
||||
|
||||
// THEN
|
||||
expect($dto->id)->toBe($exception->id)
|
||||
->and($dto->pivot)->not->toBeNull()
|
||||
->and($dto->pivot?->replacement_date?->format('Y-m-d'))->toBe('2026-05-02')
|
||||
->and($dto->pivot?->order_deadline_at?->format('Y-m-d H:i:s'))->toBe('2026-05-01 18:00:00');
|
||||
});
|
||||
});
|
||||
|
|
@ -52,7 +52,7 @@ describe('GetPaginatedShopsQuery', function () {
|
|||
$query = new GetPaginatedShopsQuery($dataTableService);
|
||||
$input = new GetPaginatedShopsQueryInput(
|
||||
pagination: Pagination::ofPage(1, 10),
|
||||
pickupExceptionId: $exception->id,
|
||||
pickupExceptionId: $exception->getId(),
|
||||
);
|
||||
|
||||
// WHEN
|
||||
|
|
|
|||
|
|
@ -35,27 +35,4 @@ describe('Coordinates Value Object', function () {
|
|||
expect(fn () => new Coordinates(0.0, -181.0))
|
||||
->toThrow(\InvalidArgumentException::class, 'Longitude must be between -180 and 180 degrees.');
|
||||
});
|
||||
|
||||
test('it creates instance using tryFrom with valid numeric strings', function () {
|
||||
// GIVEN & WHEN
|
||||
$coords = Coordinates::tryFrom('45.764043', '4.835659');
|
||||
|
||||
// THEN
|
||||
expect($coords)->not->toBeNull()
|
||||
->and($coords?->latitude)->toBe(45.764043)
|
||||
->and($coords?->longitude)->toBe(4.835659);
|
||||
});
|
||||
|
||||
test('tryFrom returns null when any coordinate is null or empty', function () {
|
||||
// GIVEN & WHEN & THEN
|
||||
expect(Coordinates::tryFrom(null, '4.83'))->toBeNull()
|
||||
->and(Coordinates::tryFrom('45.76', null))->toBeNull()
|
||||
->and(Coordinates::tryFrom('', ''))->toBeNull();
|
||||
});
|
||||
|
||||
test('tryFrom throws when coordinate is not numeric', function () {
|
||||
// GIVEN & WHEN & THEN
|
||||
expect(fn () => Coordinates::tryFrom('invalid', '4.83'))
|
||||
->toThrow(\InvalidArgumentException::class, 'Latitude must be numeric.');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -41,22 +41,15 @@ describe('PhoneNumber Value Object', function () {
|
|||
->toThrow(\InvalidArgumentException::class, 'Phone number format is invalid.');
|
||||
});
|
||||
|
||||
test('tryFrom returns null when value is null or empty', function () {
|
||||
// GIVEN & WHEN & THEN
|
||||
expect(PhoneNumber::tryFrom(null))->toBeNull()
|
||||
->and(PhoneNumber::tryFrom(''))->toBeNull()
|
||||
->and(PhoneNumber::tryFrom(' '))->toBeNull();
|
||||
});
|
||||
|
||||
test('tryFrom returns PhoneNumber when value is valid', function () {
|
||||
test('it can be instantiated with RFC3966 tel: format', function () {
|
||||
// GIVEN
|
||||
$raw = '+33102030405';
|
||||
$raw = 'tel:+33-1-02-03-04-05';
|
||||
|
||||
// WHEN
|
||||
$phone = PhoneNumber::tryFrom($raw);
|
||||
$phone = new PhoneNumber($raw);
|
||||
|
||||
// THEN
|
||||
expect($phone)->not->toBeNull()
|
||||
->and($phone?->value)->toBe('+33102030405');
|
||||
expect($phone->value)->toBe('tel:+33-1-02-03-04-05')
|
||||
->and((string) $phone)->toBe('tel:+33-1-02-03-04-05');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue