ldc-cocotte/tests/Unit/Domain/Services/ShopperServiceTest.php
2026-10-27 09:58:12 +01:00

275 lines
8.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Services;
use Carbon\CarbonImmutable;
use Domain\Models\Entities\Shop;
use Domain\Models\Entities\Shopper;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\ShopId;
use Domain\Models\ValueObjects\Entities\ShopperId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Ports\Repositories\ShopperRepository;
use Domain\Ports\Repositories\ShopRepository;
use Domain\Services\ShopperService;
use Mockery;
use Ramsey\Uuid\Uuid;
describe('ShopperService', function (): void {
it('gets shopper for id', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$id = new ShopperId(Uuid::uuid7());
$shopper = new Shopper(
$id,
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
'Etab',
'SiteA',
'ServiceA'
);
$shopperRepo->shouldReceive('find')->once()->with($id)->andReturn($shopper);
// WHEN
$result = $service->getShopperFor($id);
// THEN
expect($result)->toBe($shopper);
});
it('resolves shop for employee based on exception shop', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$shopper = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
'Etab',
'SiteA',
'ServiceA'
);
$shop = new Shop(
new ShopId(Uuid::uuid7()),
'Shop',
'Addr',
'00000',
'City'
);
$shopRepo->shouldReceive('findExceptionShopFor')->once()->with($shopper->matricule)->andReturn($shop);
// WHEN
$result = $service->resolveShopForEmployee($shopper);
// THEN
expect($result)->toBe($shop);
});
it('resolves shop for employee based on site and service', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$shopper = new Shopper(
isset($id) ? $id : new ShopperId(Uuid::uuid7()),
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
'Etab',
'SiteA',
'ServiceA'
);
$shop = new Shop(new ShopId(Uuid::uuid7()), 'Shop', 'Addr', '00000', 'City');
$shopRepo->shouldReceive('findExceptionShopFor')->once()->with($shopper->matricule)->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with('SiteA', 'ServiceA')->andReturn($shop);
// WHEN
$result = $service->resolveShopForEmployee($shopper);
// THEN
expect($result)->toBe($shop);
});
it('resolves shop for employee based on site only', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$shopper = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
'Etab',
'SiteA',
'ServiceA'
);
$shop = new Shop(new ShopId(Uuid::uuid7()), 'Shop', 'Addr', '00000', 'City');
$shopRepo->shouldReceive('findExceptionShopFor')->once()->with($shopper->matricule)->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with('SiteA', 'ServiceA')->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with('SiteA', null)->andReturn($shop);
// WHEN
$result = $service->resolveShopForEmployee($shopper);
// THEN
expect($result)->toBe($shop);
});
it('resolves shop for employee using default fallback', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$shopper = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
'Etab',
'SiteA',
'ServiceA'
);
$shop = new Shop(new ShopId(Uuid::uuid7()), 'Shop', 'Addr', '00000', 'City');
$shopRepo->shouldReceive('findExceptionShopFor')->once()->with($shopper->matricule)->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with('SiteA', 'ServiceA')->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with('SiteA', null)->andReturn(null);
$shopRepo->shouldReceive('getDefaultShop')->once()->andReturn($shop);
// WHEN
$result = $service->resolveShopForEmployee($shopper);
// THEN
expect($result)->toBe($shop);
});
it('returns null when shopper is not found for id', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$id = new ShopperId(Uuid::uuid7());
$shopperRepo->shouldReceive('find')->once()->with($id)->andReturn(null);
// WHEN
$result = $service->getShopFor($id);
// THEN
expect($result)->toBeNull();
});
it('gets shop when shopper is found for id', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$id = new ShopperId(Uuid::uuid7());
$shopper = new Shopper(
$id,
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
'Etab',
'SiteA',
'ServiceA'
);
$shop = new Shop(
new ShopId(Uuid::uuid7()),
'Shop',
'Addr',
'00000',
'City'
);
$shopperRepo->shouldReceive('find')->once()->with($id)->andReturn($shopper);
$shopRepo->shouldReceive('findExceptionShopFor')->once()->with($shopper->matricule)->andReturn($shop);
// WHEN
$result = $service->getShopFor($id);
// THEN
expect($result)->toBe($shop);
});
it('returns false when customer not found checking app pass orders', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$id = new ShopperId(Uuid::uuid7());
$shopperRepo->shouldReceive('find')->once()->with($id)->andReturn(null);
// WHEN
$result = $service->canCustomerPassOrdersApp($id);
// THEN
expect($result)->toBeFalse();
});
it('returns customer active status for pass orders app', function (): void {
// GIVEN
$shopRepo = Mockery::mock(ShopRepository::class);
$shopperRepo = Mockery::mock(ShopperRepository::class);
$service = new ShopperService($shopRepo, $shopperRepo);
$id = new ShopperId(Uuid::uuid7());
$shopper = new Shopper(
$id,
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
'Etab',
'SiteA',
'ServiceA'
);
$shopperRepo->shouldReceive('find')->once()->with($id)->andReturn($shopper);
// WHEN
$result = $service->canCustomerPassOrdersApp($id);
// THEN
expect($result)->toBeTrue();
});
});