mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-06 02:59:29 +00:00
Implement excel based import/export
This commit is contained in:
parent
c5751b2aa6
commit
facfb37383
7 changed files with 690 additions and 12 deletions
|
|
@ -26,6 +26,7 @@ use App\Entity\Parts\Category;
|
|||
use App\Services\ImportExportSystem\EntityExporter;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
|
||||
class EntityExporterTest extends WebTestCase
|
||||
{
|
||||
|
|
@ -76,7 +77,40 @@ class EntityExporterTest extends WebTestCase
|
|||
|
||||
$this->assertSame('application/json', $response->headers->get('Content-Type'));
|
||||
$this->assertNotEmpty($response->headers->get('Content-Disposition'));
|
||||
}
|
||||
|
||||
public function testExportToExcel(): void
|
||||
{
|
||||
$entities = $this->getEntities();
|
||||
|
||||
$xlsxData = $this->service->exportEntities($entities, ['format' => 'xlsx', 'level' => 'simple']);
|
||||
$this->assertNotEmpty($xlsxData);
|
||||
|
||||
$tempFile = tempnam(sys_get_temp_dir(), 'test_export') . '.xlsx';
|
||||
file_put_contents($tempFile, $xlsxData);
|
||||
|
||||
$spreadsheet = IOFactory::load($tempFile);
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$this->assertSame('name', $worksheet->getCell('A1')->getValue());
|
||||
$this->assertSame('full_name', $worksheet->getCell('B1')->getValue());
|
||||
|
||||
$this->assertSame('Enitity 1', $worksheet->getCell('A2')->getValue());
|
||||
$this->assertSame('Enitity 1', $worksheet->getCell('B2')->getValue());
|
||||
|
||||
unlink($tempFile);
|
||||
}
|
||||
|
||||
public function testExportExcelFromRequest(): void
|
||||
{
|
||||
$entities = $this->getEntities();
|
||||
|
||||
$request = new Request();
|
||||
$request->request->set('format', 'xlsx');
|
||||
$request->request->set('level', 'simple');
|
||||
$response = $this->service->exportEntityFromRequest($entities, $request);
|
||||
|
||||
$this->assertSame('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type'));
|
||||
$this->assertStringContainsString('export_Category_simple.xlsx', $response->headers->get('Content-Disposition'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,9 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||
|
||||
#[Group('DB')]
|
||||
class EntityImporterTest extends WebTestCase
|
||||
|
|
@ -207,6 +210,10 @@ EOT;
|
|||
yield ['json', 'json'];
|
||||
yield ['yaml', 'yml'];
|
||||
yield ['yaml', 'YAML'];
|
||||
yield ['xlsx', 'xlsx'];
|
||||
yield ['xlsx', 'XLSX'];
|
||||
yield ['xls', 'xls'];
|
||||
yield ['xls', 'XLS'];
|
||||
}
|
||||
|
||||
#[DataProvider('formatDataProvider')]
|
||||
|
|
@ -342,4 +349,41 @@ EOT;
|
|||
$this->assertSame($category, $results[0]->getCategory());
|
||||
$this->assertSame('test,test2', $results[0]->getTags());
|
||||
}
|
||||
|
||||
public function testImportExcelFileProjects(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$worksheet->setCellValue('A1', 'name');
|
||||
$worksheet->setCellValue('B1', 'comment');
|
||||
$worksheet->setCellValue('A2', 'Test Excel 1');
|
||||
$worksheet->setCellValue('B2', 'Test Excel 1 notes');
|
||||
$worksheet->setCellValue('A3', 'Test Excel 2');
|
||||
$worksheet->setCellValue('B3', 'Test Excel 2 notes');
|
||||
|
||||
$tempFile = tempnam(sys_get_temp_dir(), 'test_excel') . '.xlsx';
|
||||
$writer = new Xlsx($spreadsheet);
|
||||
$writer->save($tempFile);
|
||||
|
||||
$file = new File($tempFile);
|
||||
|
||||
$errors = [];
|
||||
$results = $this->service->importFile($file, [
|
||||
'class' => Project::class,
|
||||
'format' => 'xlsx',
|
||||
'csv_delimiter' => ';',
|
||||
], $errors);
|
||||
|
||||
$this->assertCount(2, $results);
|
||||
$this->assertEmpty($errors);
|
||||
$this->assertContainsOnlyInstancesOf(Project::class, $results);
|
||||
|
||||
$this->assertSame('Test Excel 1', $results[0]->getName());
|
||||
$this->assertSame('Test Excel 1 notes', $results[0]->getComment());
|
||||
$this->assertSame('Test Excel 2', $results[1]->getName());
|
||||
$this->assertSame('Test Excel 2 notes', $results[1]->getComment());
|
||||
|
||||
unlink($tempFile);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue