mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-06 11:09:29 +00:00
Add more tests and fix failing ones
This commit is contained in:
parent
1fb137e89f
commit
78885ec3c5
2 changed files with 96 additions and 36 deletions
|
|
@ -102,22 +102,24 @@ class EntityExporter
|
||||||
$groups[] = 'include_children';
|
$groups[] = 'include_children';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->serializer->serialize($entities, $options['format'],
|
return $this->serializer->serialize(
|
||||||
|
$entities,
|
||||||
|
$options['format'],
|
||||||
[
|
[
|
||||||
'groups' => $groups,
|
'groups' => $groups,
|
||||||
'as_collection' => true,
|
'as_collection' => true,
|
||||||
'csv_delimiter' => $options['csv_delimiter'],
|
'csv_delimiter' => $options['csv_delimiter'],
|
||||||
'xml_root_node_name' => 'PartDBExport',
|
'xml_root_node_name' => 'PartDBExport',
|
||||||
'partdb_export' => true,
|
'partdb_export' => true,
|
||||||
//Skip the item normalizer, so that we dont get IRIs in the output
|
//Skip the item normalizer, so that we dont get IRIs in the output
|
||||||
SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER => true,
|
SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER => true,
|
||||||
//Handle circular references
|
//Handle circular references
|
||||||
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => $this->handleCircularReference(...),
|
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => $this->handleCircularReference(...),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function handleCircularReference(object $object, string $format, array $context): string
|
private function handleCircularReference(object $object): string
|
||||||
{
|
{
|
||||||
if ($object instanceof AbstractStructuralDBElement) {
|
if ($object instanceof AbstractStructuralDBElement) {
|
||||||
return $object->getFullPath("->");
|
return $object->getFullPath("->");
|
||||||
|
|
@ -127,7 +129,7 @@ class EntityExporter
|
||||||
return $object->__toString();
|
return $object->__toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CircularReferenceException('Circular reference detected for object of type '.get_class($object));
|
throw new CircularReferenceException('Circular reference detected for object of type ' . get_class($object));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -148,7 +150,9 @@ class EntityExporter
|
||||||
$groups[] = 'include_children';
|
$groups[] = 'include_children';
|
||||||
}
|
}
|
||||||
|
|
||||||
$csvData = $this->serializer->serialize($entities, 'csv',
|
$csvData = $this->serializer->serialize(
|
||||||
|
$entities,
|
||||||
|
'csv',
|
||||||
[
|
[
|
||||||
'groups' => $groups,
|
'groups' => $groups,
|
||||||
'as_collection' => true,
|
'as_collection' => true,
|
||||||
|
|
@ -162,18 +166,18 @@ class EntityExporter
|
||||||
//Convert CSV to Excel
|
//Convert CSV to Excel
|
||||||
$spreadsheet = new Spreadsheet();
|
$spreadsheet = new Spreadsheet();
|
||||||
$worksheet = $spreadsheet->getActiveSheet();
|
$worksheet = $spreadsheet->getActiveSheet();
|
||||||
|
|
||||||
$rows = explode("\n", $csvData);
|
$rows = explode("\n", $csvData);
|
||||||
$rowIndex = 1;
|
$rowIndex = 1;
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
if (trim($row) === '') {
|
if (trim($row) === '') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$columns = str_getcsv($row, $options['csv_delimiter']);
|
$columns = str_getcsv($row, $options['csv_delimiter'], '"', '\\');
|
||||||
$colIndex = 1;
|
$colIndex = 1;
|
||||||
|
|
||||||
foreach ($columns as $column) {
|
foreach ($columns as $column) {
|
||||||
$cellCoordinate = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex) . $rowIndex;
|
$cellCoordinate = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex) . $rowIndex;
|
||||||
$worksheet->setCellValue($cellCoordinate, $column);
|
$worksheet->setCellValue($cellCoordinate, $column);
|
||||||
|
|
@ -183,17 +187,13 @@ class EntityExporter
|
||||||
}
|
}
|
||||||
|
|
||||||
//Save to memory stream
|
//Save to memory stream
|
||||||
if ($options['format'] === 'xlsx') {
|
$writer = $options['format'] === 'xlsx' ? new Xlsx($spreadsheet) : new Xls($spreadsheet);
|
||||||
$writer = new Xlsx($spreadsheet);
|
|
||||||
} else {
|
|
||||||
$writer = new Xls($spreadsheet);
|
|
||||||
}
|
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
$writer->save('php://output');
|
$writer->save('php://output');
|
||||||
$content = ob_get_contents();
|
$content = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -231,25 +231,15 @@ class EntityExporter
|
||||||
|
|
||||||
//Determine the content type for the response
|
//Determine the content type for the response
|
||||||
|
|
||||||
//Plain text should work for all types
|
|
||||||
$content_type = 'text/plain';
|
|
||||||
|
|
||||||
//Try to use better content types based on the format
|
//Try to use better content types based on the format
|
||||||
$format = $options['format'];
|
$format = $options['format'];
|
||||||
switch ($format) {
|
$content_type = match ($format) {
|
||||||
case 'xml':
|
'xml' => 'application/xml',
|
||||||
$content_type = 'application/xml';
|
'json' => 'application/json',
|
||||||
break;
|
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||||
case 'json':
|
'xls' => 'application/vnd.ms-excel',
|
||||||
$content_type = 'application/json';
|
default => 'text/plain',
|
||||||
break;
|
};
|
||||||
case 'xlsx':
|
|
||||||
$content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
||||||
break;
|
|
||||||
case 'xls':
|
|
||||||
$content_type = 'application/vnd.ms-excel';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$response->headers->set('Content-Type', $content_type);
|
$response->headers->set('Content-Type', $content_type);
|
||||||
|
|
||||||
//If view option is not specified, then download the file.
|
//If view option is not specified, then download the file.
|
||||||
|
|
@ -267,7 +257,7 @@ class EntityExporter
|
||||||
|
|
||||||
$level = $options['level'];
|
$level = $options['level'];
|
||||||
|
|
||||||
$filename = 'export_'.$entity_name.'_'.$level.'.'.$format;
|
$filename = "export_{$entity_name}_{$level}.{$format}";
|
||||||
|
|
||||||
//Sanitize the filename
|
//Sanitize the filename
|
||||||
$filename = FilenameSanatizer::sanitizeFilename($filename);
|
$filename = FilenameSanatizer::sanitizeFilename($filename);
|
||||||
|
|
|
||||||
70
tests/Services/Parts/PartsTableActionHandlerTest.php
Normal file
70
tests/Services/Parts/PartsTableActionHandlerTest.php
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
|
* by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
namespace App\Tests\Services\Parts;
|
||||||
|
|
||||||
|
use App\Entity\Parts\Part;
|
||||||
|
use App\Services\Parts\PartsTableActionHandler;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||||
|
|
||||||
|
class PartsTableActionHandlerTest extends WebTestCase
|
||||||
|
{
|
||||||
|
private PartsTableActionHandler $service;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
self::bootKernel();
|
||||||
|
$this->service = self::getContainer()->get(PartsTableActionHandler::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExportActionsRedirectToExportController(): void
|
||||||
|
{
|
||||||
|
// Mock a Part entity with required properties
|
||||||
|
$part = $this->createMock(Part::class);
|
||||||
|
$part->method('getId')->willReturn(1);
|
||||||
|
$part->method('getName')->willReturn('Test Part');
|
||||||
|
|
||||||
|
$selected_parts = [$part];
|
||||||
|
|
||||||
|
// Test each export format, focusing on our new xlsx format
|
||||||
|
$formats = ['json', 'csv', 'xml', 'yaml', 'xlsx'];
|
||||||
|
|
||||||
|
foreach ($formats as $format) {
|
||||||
|
$action = "export_{$format}";
|
||||||
|
$result = $this->service->handleAction($action, $selected_parts, 1, '/test');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(RedirectResponse::class, $result);
|
||||||
|
$this->assertStringContainsString('parts/export', $result->getTargetUrl());
|
||||||
|
$this->assertStringContainsString("format={$format}", $result->getTargetUrl());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIdStringToArray(): void
|
||||||
|
{
|
||||||
|
// This test would require actual Part entities in the database
|
||||||
|
// For now, we just test the method exists and handles empty strings
|
||||||
|
$result = $this->service->idStringToArray('');
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertEmpty($result);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue