ldc-cocotte/tests/Integration/WithApp/Directories/EloquentCustomerDirectoryTest.php
2026-10-27 09:58:12 +01:00

170 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
use App\Directories\EloquentCustomerDirectory;
use App\Models\Customer;
use App\Models\Employee;
use Carbon\CarbonImmutable;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Matricule;
use Ramsey\Uuid\Uuid;
describe('EloquentCustomerDirectory', function () {
it('finds a customer by uuid', function () {
// GIVEN
$directory = new EloquentCustomerDirectory();
$customer = Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => Uuid::uuid4()->toString(),
'email' => new Email('test@example.com'),
]);
// WHEN
$result = $directory->find(Uuid::fromString($customer->id));
// THEN
expect($result)->not->toBeNull();
expect($result->id)->toBe($customer->id);
});
it('returns null if customer not found', function () {
// GIVEN
$directory = new EloquentCustomerDirectory();
// WHEN
$result = $directory->find(Uuid::uuid7());
// THEN
expect($result)->toBeNull();
});
it('gets the number of salaries with account in date range', function () {
// GIVEN
$directory = new EloquentCustomerDirectory();
$customer1 = Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => Uuid::uuid4()->toString(),
'email' => new Email('test1@example.com'),
'created_at' => CarbonImmutable::now()->subDays(5),
]);
Employee::forceCreate([
'id' => 10,
'customer_id' => $customer1->id,
'matricule' => new Matricule('1000010'),
'code' => new Code('C1111112'),
'nom' => 'Doe',
'prenom' => 'John',
'etablissement' => 'E',
'site' => 'S',
'service' => 'S',
'created_at' => CarbonImmutable::now()->subDays(5),
'updated_at' => CarbonImmutable::now()->subDays(5),
]);
$customer2 = Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => Uuid::uuid4()->toString(),
'email' => new Email('test2@example.com'),
'created_at' => CarbonImmutable::now()->subDays(15),
]);
Employee::forceCreate([
'id' => 20,
'customer_id' => $customer2->id,
'matricule' => new Matricule('1000020'),
'code' => new Code('C1111113'),
'nom' => 'Doe',
'prenom' => 'John',
'etablissement' => 'E',
'site' => 'S',
'service' => 'S',
'created_at' => CarbonImmutable::now()->subDays(15),
'updated_at' => CarbonImmutable::now()->subDays(15),
]);
// Customer without employee
Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => Uuid::uuid4()->toString(),
'email' => new Email('test3@example.com'),
'created_at' => CarbonImmutable::now()->subDays(5),
]);
// WHEN
$start = CarbonImmutable::now()->subDays(10);
$end = CarbonImmutable::now();
$count = $directory->getNumberSalariesWithAccount($start, $end);
// THEN
expect($count)->toBeGreaterThanOrEqual(1);
});
it('gets total registered salaries', function () {
// GIVEN
$directory = new EloquentCustomerDirectory();
$customer1 = Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => Uuid::uuid4()->toString(),
'email' => new Email('test1t@example.com'),
]);
Employee::forceCreate([
'id' => 30,
'customer_id' => $customer1->id,
'matricule' => new Matricule('1000030'),
'code' => new Code('C1111114'),
'nom' => 'Doe',
'prenom' => 'John',
'etablissement' => 'E',
'site' => 'S',
'service' => 'S',
'created_at' => CarbonImmutable::now(),
'updated_at' => CarbonImmutable::now(),
]);
$customer2 = Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => Uuid::uuid4()->toString(),
'email' => new Email('test2t@example.com'),
]);
// no employee for customer2
// WHEN
$count = $directory->getTotalRegisteredSalaries();
// THEN
expect($count)->toBeGreaterThanOrEqual(1);
});
it('gets a customer by email', function () {
// GIVEN
$directory = new EloquentCustomerDirectory();
$email = new Email('testfind@example.com');
$customer = Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => Uuid::uuid4()->toString(),
'email' => $email,
]);
// WHEN
$result = $directory->getCustomerByEmail($email);
// THEN
expect($result)->not->toBeNull();
expect($result->id)->toBe($customer->id);
});
it('returns null if customer by email not found', function () {
// GIVEN
$directory = new EloquentCustomerDirectory();
$email = new Email('notfound@example.com');
// WHEN
$result = $directory->getCustomerByEmail($email);
// THEN
expect($result)->toBeNull();
});
});