ldc-cocotte/tests/Unit/Domain/Services/ShopperServiceTest.php

290 lines
9.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Services;
use Carbon\Carbon;
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\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Domain\Ports\Repositories\ShopperRepository;
use Domain\Ports\Repositories\ShopRepository;
use Domain\Services\ShopperService;
use Mockery;
use Shared\Helpers\UuidHelper;
beforeEach(function (): void {
$now = CarbonImmutable::parse('2026-06-15 10:00:00', 'Europe/Paris');
CarbonImmutable::setTestNow($now);
Carbon::setTestNow($now);
});
afterEach(function (): void {
CarbonImmutable::setTestNow();
Carbon::setTestNow();
});
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(UuidHelper::v7());
$shopper = new Shopper(
$id,
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITEA'),
new ShopperServiceCode('101')
);
$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(UuidHelper::v7()),
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITEA'),
new ShopperServiceCode('101')
);
$shop = new Shop(
new ShopId(UuidHelper::v7()),
'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(
new ShopperId(UuidHelper::v7()),
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITEA'),
new ShopperServiceCode('101')
);
$shop = new Shop(new ShopId(UuidHelper::v7()), 'Shop', 'Addr', '00000', 'City');
$shopRepo->shouldReceive('findExceptionShopFor')->once()->with($shopper->matricule)->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with($shopper->site, $shopper->service)->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(UuidHelper::v7()),
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITEA'),
new ShopperServiceCode('101')
);
$shop = new Shop(new ShopId(UuidHelper::v7()), 'Shop', 'Addr', '00000', 'City');
$shopRepo->shouldReceive('findExceptionShopFor')->once()->with($shopper->matricule)->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with($shopper->site, $shopper->service)->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with($shopper->site, 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(UuidHelper::v7()),
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITEA'),
new ShopperServiceCode('101')
);
$shop = new Shop(new ShopId(UuidHelper::v7()), 'Shop', 'Addr', '00000', 'City');
$shopRepo->shouldReceive('findExceptionShopFor')->once()->with($shopper->matricule)->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with($shopper->site, $shopper->service)->andReturn(null);
$shopRepo->shouldReceive('findShopForSiteAndService')->once()->with($shopper->site, 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(UuidHelper::v7());
$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(UuidHelper::v7());
$shopper = new Shopper(
$id,
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITEA'),
new ShopperServiceCode('101')
);
$shop = new Shop(
new ShopId(UuidHelper::v7()),
'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(UuidHelper::v7());
$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(UuidHelper::v7());
$shopper = new Shopper(
$id,
new Email('test@test.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDay(),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITEA'),
new ShopperServiceCode('101')
);
$shopperRepo->shouldReceive('find')->once()->with($id)->andReturn($shopper);
// WHEN
$result = $service->canCustomerPassOrdersApp($id);
// THEN
expect($result)->toBeTrue();
});
});