Skip to content

Commit fe0da11

Browse files
committed
Add more tests and fix failing ones
1 parent 2b4cb89 commit fe0da11

File tree

2 files changed

+96
-36
lines changed

2 files changed

+96
-36
lines changed

src/Services/ImportExportSystem/EntityExporter.php

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,24 @@ public function exportEntities(AbstractNamedDBElement|array $entities, array $op
102102
$groups[] = 'include_children';
103103
}
104104

105-
return $this->serializer->serialize($entities, $options['format'],
105+
return $this->serializer->serialize(
106+
$entities,
107+
$options['format'],
106108
[
107109
'groups' => $groups,
108110
'as_collection' => true,
109111
'csv_delimiter' => $options['csv_delimiter'],
110112
'xml_root_node_name' => 'PartDBExport',
111113
'partdb_export' => true,
112-
//Skip the item normalizer, so that we dont get IRIs in the output
114+
//Skip the item normalizer, so that we dont get IRIs in the output
113115
SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER => true,
114-
//Handle circular references
116+
//Handle circular references
115117
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => $this->handleCircularReference(...),
116118
]
117119
);
118120
}
119121

120-
private function handleCircularReference(object $object, string $format, array $context): string
122+
private function handleCircularReference(object $object): string
121123
{
122124
if ($object instanceof AbstractStructuralDBElement) {
123125
return $object->getFullPath("->");
@@ -127,7 +129,7 @@ private function handleCircularReference(object $object, string $format, array $
127129
return $object->__toString();
128130
}
129131

130-
throw new CircularReferenceException('Circular reference detected for object of type '.get_class($object));
132+
throw new CircularReferenceException('Circular reference detected for object of type ' . get_class($object));
131133
}
132134

133135
/**
@@ -148,7 +150,9 @@ protected function exportToExcel(array $entities, array $options): string
148150
$groups[] = 'include_children';
149151
}
150152

151-
$csvData = $this->serializer->serialize($entities, 'csv',
153+
$csvData = $this->serializer->serialize(
154+
$entities,
155+
'csv',
152156
[
153157
'groups' => $groups,
154158
'as_collection' => true,
@@ -162,18 +166,18 @@ protected function exportToExcel(array $entities, array $options): string
162166
//Convert CSV to Excel
163167
$spreadsheet = new Spreadsheet();
164168
$worksheet = $spreadsheet->getActiveSheet();
165-
169+
166170
$rows = explode("\n", $csvData);
167171
$rowIndex = 1;
168-
172+
169173
foreach ($rows as $row) {
170174
if (trim($row) === '') {
171175
continue;
172176
}
173-
174-
$columns = str_getcsv($row, $options['csv_delimiter']);
177+
178+
$columns = str_getcsv($row, $options['csv_delimiter'], '"', '\\');
175179
$colIndex = 1;
176-
180+
177181
foreach ($columns as $column) {
178182
$cellCoordinate = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex) . $rowIndex;
179183
$worksheet->setCellValue($cellCoordinate, $column);
@@ -183,17 +187,13 @@ protected function exportToExcel(array $entities, array $options): string
183187
}
184188

185189
//Save to memory stream
186-
if ($options['format'] === 'xlsx') {
187-
$writer = new Xlsx($spreadsheet);
188-
} else {
189-
$writer = new Xls($spreadsheet);
190-
}
191-
190+
$writer = $options['format'] === 'xlsx' ? new Xlsx($spreadsheet) : new Xls($spreadsheet);
191+
192192
ob_start();
193193
$writer->save('php://output');
194194
$content = ob_get_contents();
195195
ob_end_clean();
196-
196+
197197
return $content;
198198
}
199199

@@ -231,25 +231,15 @@ public function exportEntityFromRequest(AbstractNamedDBElement|array $entities,
231231

232232
//Determine the content type for the response
233233

234-
//Plain text should work for all types
235-
$content_type = 'text/plain';
236-
237234
//Try to use better content types based on the format
238235
$format = $options['format'];
239-
switch ($format) {
240-
case 'xml':
241-
$content_type = 'application/xml';
242-
break;
243-
case 'json':
244-
$content_type = 'application/json';
245-
break;
246-
case 'xlsx':
247-
$content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
248-
break;
249-
case 'xls':
250-
$content_type = 'application/vnd.ms-excel';
251-
break;
252-
}
236+
$content_type = match ($format) {
237+
'xml' => 'application/xml',
238+
'json' => 'application/json',
239+
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
240+
'xls' => 'application/vnd.ms-excel',
241+
default => 'text/plain',
242+
};
253243
$response->headers->set('Content-Type', $content_type);
254244

255245
//If view option is not specified, then download the file.
@@ -267,7 +257,7 @@ public function exportEntityFromRequest(AbstractNamedDBElement|array $entities,
267257

268258
$level = $options['level'];
269259

270-
$filename = 'export_'.$entity_name.'_'.$level.'.'.$format;
260+
$filename = "export_{$entity_name}_{$level}.{$format}";
271261

272262
//Sanitize the filename
273263
$filename = FilenameSanatizer::sanitizeFilename($filename);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
7+
*
8+
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as published
12+
* by the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
namespace App\Tests\Services\Parts;
24+
25+
use App\Entity\Parts\Part;
26+
use App\Services\Parts\PartsTableActionHandler;
27+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
28+
use Symfony\Component\HttpFoundation\RedirectResponse;
29+
30+
class PartsTableActionHandlerTest extends WebTestCase
31+
{
32+
private PartsTableActionHandler $service;
33+
34+
protected function setUp(): void
35+
{
36+
self::bootKernel();
37+
$this->service = self::getContainer()->get(PartsTableActionHandler::class);
38+
}
39+
40+
public function testExportActionsRedirectToExportController(): void
41+
{
42+
// Mock a Part entity with required properties
43+
$part = $this->createMock(Part::class);
44+
$part->method('getId')->willReturn(1);
45+
$part->method('getName')->willReturn('Test Part');
46+
47+
$selected_parts = [$part];
48+
49+
// Test each export format, focusing on our new xlsx format
50+
$formats = ['json', 'csv', 'xml', 'yaml', 'xlsx'];
51+
52+
foreach ($formats as $format) {
53+
$action = "export_{$format}";
54+
$result = $this->service->handleAction($action, $selected_parts, 1, '/test');
55+
56+
$this->assertInstanceOf(RedirectResponse::class, $result);
57+
$this->assertStringContainsString('parts/export', $result->getTargetUrl());
58+
$this->assertStringContainsString("format={$format}", $result->getTargetUrl());
59+
}
60+
}
61+
62+
public function testIdStringToArray(): void
63+
{
64+
// This test would require actual Part entities in the database
65+
// For now, we just test the method exists and handles empty strings
66+
$result = $this->service->idStringToArray('');
67+
$this->assertIsArray($result);
68+
$this->assertEmpty($result);
69+
}
70+
}

0 commit comments

Comments
 (0)