104 lines
3.5 KiB
PHP
104 lines
3.5 KiB
PHP
<?php
|
|
|
|
use App\Services\PriceListService;
|
|
use App\Services\PricelistFileProcessService;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
use Tests\TestCase;
|
|
|
|
uses(TestCase::class);
|
|
|
|
/**
|
|
* A 3. sor fejlécei alapján épített oszlop-leképezés meghívása (a metódus privát).
|
|
*
|
|
* @return array{map: array<int, int>, errors: array<int, array>, warnings: array<int, array>}
|
|
*/
|
|
function invokeBuildColumnMap(Worksheet $worksheet): array
|
|
{
|
|
$service = app(PricelistFileProcessService::class);
|
|
$method = new ReflectionMethod($service, 'buildColumnMap');
|
|
$method->setAccessible(true);
|
|
|
|
return $method->invoke($service, $worksheet);
|
|
}
|
|
|
|
/**
|
|
* @param array<int, int> $order a mezők (EXPECTED_HEADERS index) sorrendje az oszlopokban
|
|
*/
|
|
function makeWorksheetWithHeaderOrder(array $order): Worksheet
|
|
{
|
|
$expected = PriceListService::EXPECTED_HEADERS;
|
|
|
|
$worksheet = (new Spreadsheet())->getActiveSheet();
|
|
$worksheet->setCellValue('A1', 'Árlista érvénybelépési időpontja:');
|
|
$worksheet->setCellValue('A2', 'Beszállító megnevezése:');
|
|
|
|
$col = 1;
|
|
foreach ($order as $fieldIndex) {
|
|
// 3. sor: fejléc, 4. sor: figyelmen kívül hagyandó, 5. sor: adat
|
|
$worksheet->setCellValueByColumnAndRow($col, 3, $expected[$fieldIndex]);
|
|
$worksheet->setCellValueByColumnAndRow($col, 4, 'egyedi megnevezés');
|
|
$worksheet->setCellValueByColumnAndRow($col, 5, 'VAL_' . $fieldIndex);
|
|
$col++;
|
|
}
|
|
|
|
return $worksheet;
|
|
}
|
|
|
|
test('a fejlécek alapján tetszőleges oszlopsorrend helyesen leképeződik', function () {
|
|
$expected = PriceListService::EXPECTED_HEADERS;
|
|
// Fordított oszlopsorrend
|
|
$order = array_reverse(array_keys($expected));
|
|
|
|
$worksheet = makeWorksheetWithHeaderOrder($order);
|
|
$result = invokeBuildColumnMap($worksheet);
|
|
|
|
expect($result['errors'])->toBe([]);
|
|
|
|
foreach ($expected as $fieldIndex => $label) {
|
|
$mappedColumn = $result['map'][$fieldIndex] ?? null;
|
|
expect($mappedColumn)->not->toBeNull();
|
|
|
|
$value = $worksheet->getCellByColumnAndRow($mappedColumn, 5)->getValue();
|
|
expect($value)->toBe('VAL_' . $fieldIndex);
|
|
}
|
|
});
|
|
|
|
test('a fejléc-egyeztetés kis/nagybetű- és szeparátor-toleráns', function () {
|
|
$expected = PriceListService::EXPECTED_HEADERS;
|
|
|
|
$worksheet = (new Spreadsheet())->getActiveSheet();
|
|
$col = 1;
|
|
foreach ($expected as $fieldIndex => $label) {
|
|
// Kisbetűsítés + extra szóközök, hogy a normalizálást teszteljük
|
|
$worksheet->setCellValueByColumnAndRow($col, 3, ' ' . mb_strtolower($label) . ' ');
|
|
$col++;
|
|
}
|
|
|
|
$result = invokeBuildColumnMap($worksheet);
|
|
|
|
expect($result['errors'])->toBe([]);
|
|
expect($result['map'])->toHaveCount(count($expected));
|
|
});
|
|
|
|
test('hiányzó kötelező fejléc blokkoló hibát ad', function () {
|
|
$expected = PriceListService::EXPECTED_HEADERS;
|
|
|
|
$worksheet = (new Spreadsheet())->getActiveSheet();
|
|
$col = 1;
|
|
foreach ($expected as $fieldIndex => $label) {
|
|
if ($fieldIndex === 0) {
|
|
// A szállítói cikkszám fejlécét szándékosan kihagyjuk
|
|
continue;
|
|
}
|
|
$worksheet->setCellValueByColumnAndRow($col, 3, $label);
|
|
$col++;
|
|
}
|
|
|
|
$result = invokeBuildColumnMap($worksheet);
|
|
|
|
$hasBlocker = collect($result['errors'])->contains(fn ($error) => ($error['severity'] ?? '') === 'blocker');
|
|
expect($hasBlocker)->toBeTrue();
|
|
expect($result['map'])->not->toHaveKey(0);
|
|
});
|