66 lines
2 KiB
PHP
66 lines
2 KiB
PHP
<?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);
|
|
});
|
|
});
|