SmartCane/laravel_app/tests/Feature/ValidateExcelStructureTest.php
2024-05-22 14:00:21 +02:00

41 lines
1.6 KiB
PHP

<?php
namespace Tests\Feature;
use Maatwebsite\Excel\Exceptions\NoTypeDetectedException;
use Maatwebsite\Excel\HeadingRowImport;
use Tests\TestCase;
class ValidateExcelStructureTest extends TestCase
{
public static function excelFileProvider()
{
//good file - correct data
yield ['/Users/guillaume/smartcane/laravel_app/storage/app/livewire-tmp/Fi8skg5XxHVWmJ4LdkzOvvyBYYVsTW-metadGVzdCAoMSkueGxzeA==-.xlsx'];
//good file - wrong data
yield ['/Users/guillaume/smartcane/laravel_app/storage/app/livewire-tmp/SosZOpFgWrPWELZQrP3tIr69d0sOjf-metad3JvbmcueGxzeA==-.xlsx'];
// wrong file
yield ['/Users/guillaume/smartcane/laravel_app/storage/app/livewire-tmp/gc4uIBw5Uli7Fk1JJDR9d7eLoPRI0x-metaWGluYXZhbmVfZGVtby5nZW9qc29u-.json'];
}
/** @test
* @dataProvider excelFileProvider
*/
public function it_can_load_an_excel_sheet(string $filename)
{
$toCheck = ["field","sub_field", "year", "season_start","season_end", "age" , "sub_area", "tonnage_ha"];
self::assertFileExists($filename);
try {
$excelArray = (new HeadingRowImport)->toArray($filename);
self::assertNotNull($excelArray);
$headers = $excelArray[0][0];
self::assertNotEmpty($headers);
self::assertEmpty(array_diff($toCheck, $headers));
}catch (NoTypeDetectedException $exception){
self::assertFileExists('/Users/guillaume/smartcane/laravel_app/storage/app/livewire-tmp/gc4uIBw5Uli7Fk1JJDR9d7eLoPRI0x-metaWGluYXZhbmVfZGVtby5nZW9qc29u-.json');
}
}
}