ldc-boutique-cnc-sable/tests/Unit/App/Services/EmployeeExportServiceTest.php

67 lines
2 KiB
PHP
Raw Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Models\Employee;
use App\Services\EmployeeExportService;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\Matricule;
use Maatwebsite\Excel\Facades\Excel;
use Tests\TestCase;
uses(TestCase::class);
covers(EmployeeExportService::class);
afterEach(function () {
\Mockery::close();
});
describe('EmployeeExportService', function () {
it('exports employees successfully', function () {
// GIVEN
$service = new EmployeeExportService();
$employee = new Employee();
$employee->matricule = new Matricule('1234567');
$employee->nom = 'Doe';
$employee->prenom = 'John';
$employee->site = 'Paris';
$employee->code = new Code('ABCDEFGH');
Excel::shouldReceive('store')
->once()
->with(\Mockery::on(function ($exportClass) use ($employee) {
expect($exportClass->headings())->toBe(['Matricule', 'Nom', 'Prenom', 'Site', 'Code']);
$mapped = $exportClass->map($employee);
expect($mapped)->toBe(['1234567', 'Doe', 'John', 'Paris', $employee->code]);
expect($exportClass->getCsvSettings())->toBe([
'delimiter' => ';',
'output_encoding' => 'UTF-8',
]);
expect($exportClass->collection()->first())->toBe($employee);
return true;
}), 'export.csv', 'local')
->andReturn(true);
// WHEN
$result = $service->export('local', 'export.csv', [$employee]);
// THEN
expect($result)->toBeTrue();
});
it('throws exception if not an employee', function () {
// GIVEN
$service = new EmployeeExportService();
// WHEN & THEN
expect(fn () => $service->export('local', 'export.csv', [new \stdClass()]))
->toThrow(\InvalidArgumentException::class, 'employeeToExport as to be an array of ' . Employee::class);
});
});