Skip to content

Commit 5b71d68

Browse files
committed
Added tests for new DTO objects
1 parent b94e28a commit 5b71d68

File tree

5 files changed

+186
-7
lines changed

5 files changed

+186
-7
lines changed

src/Controller/BulkInfoProviderImportController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private function validateJobAccess(int $jobId): ?BulkInfoProviderImportJob
101101
return $job;
102102
}
103103

104-
private function updatePartSearchResults(BulkInfoProviderImportJob $job, int $partId, ?BulkSearchPartResultsDTO $newResults): void
104+
private function updatePartSearchResults(BulkInfoProviderImportJob $job, ?BulkSearchPartResultsDTO $newResults): void
105105
{
106106
if ($newResults === null) {
107107
return;
@@ -111,7 +111,7 @@ private function updatePartSearchResults(BulkInfoProviderImportJob $job, int $pa
111111
$allResults = $job->getSearchResults($this->entityManager);
112112

113113
// Find and update the results for this specific part
114-
$allResults = $allResults->replaceResultsForPart($partId, $newResults);
114+
$allResults = $allResults->replaceResultsForPart($newResults);
115115

116116
// Save updated results back to job
117117
$job->setSearchResults($allResults);
@@ -482,7 +482,7 @@ public function researchPart(int $jobId, int $partId): JsonResponse
482482
}
483483

484484
// Update the job's search results for this specific part efficiently
485-
$this->updatePartSearchResults($job, $partId, $searchResultsDto[0] ?? null);
485+
$this->updatePartSearchResults($job, $searchResultsDto[0] ?? null);
486486

487487
// Prefetch details if requested
488488
if ($prefetchDetails && $searchResultsDto !== null) {

src/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTO.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,27 @@ public function __construct(
4141

4242
/**
4343
* Replaces the search results for a specific part, and returns a new instance.
44-
* @param Part|int $part
44+
* The part to replaced, is identified by the part property of the new_results parameter.
45+
* The original instance remains unchanged.
4546
* @param BulkSearchPartResultsDTO $new_results
4647
* @return BulkSearchResponseDTO
4748
*/
48-
public function replaceResultsForPart(Part|int $part, BulkSearchPartResultsDTO $new_results): self
49+
public function replaceResultsForPart(BulkSearchPartResultsDTO $new_results): self
4950
{
5051
$array = $this->partResults;
52+
$replaced = false;
5153
foreach ($array as $index => $partResult) {
52-
if (($part instanceof Part && $partResult->part->getId() === $part->getId()) ||
53-
($partResult->part->getId() === $part)) {
54+
if ($partResult->part === $new_results->part) {
5455
$array[$index] = $new_results;
56+
$replaced = true;
5557
break;
5658
}
5759
}
5860

61+
if (!$replaced) {
62+
throw new \InvalidArgumentException("Part not found in existing results.");
63+
}
64+
5965
return new self($array);
6066
}
6167

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
namespace App\Tests\Services\InfoProviderSystem\DTOs;
22+
23+
use App\Services\InfoProviderSystem\DTOs\BulkSearchFieldMappingDTO;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class BulkSearchFieldMappingDTOTest extends TestCase
27+
{
28+
29+
public function testIsSupplierPartNumberField(): void
30+
{
31+
$fieldMapping = new BulkSearchFieldMappingDTO(field: 'reichelt_spn', providers: ['provider1'], priority: 1);
32+
$this->assertTrue($fieldMapping->isSupplierPartNumberField());
33+
34+
$fieldMapping = new BulkSearchFieldMappingDTO(field: 'partNumber', providers: ['provider1'], priority: 1);
35+
$this->assertFalse($fieldMapping->isSupplierPartNumberField());
36+
}
37+
38+
public function testToSerializableArray(): void
39+
{
40+
$fieldMapping = new BulkSearchFieldMappingDTO(field: 'test', providers: ['provider1', 'provider2'], priority: 3);
41+
$array = $fieldMapping->toSerializableArray();
42+
$this->assertIsArray($array);
43+
$this->assertSame([
44+
'field' => 'test',
45+
'providers' => ['provider1', 'provider2'],
46+
'priority' => 3,
47+
], $array);
48+
}
49+
50+
public function testFromSerializableArray(): void
51+
{
52+
$data = [
53+
'field' => 'test',
54+
'providers' => ['provider1', 'provider2'],
55+
'priority' => 3,
56+
];
57+
$fieldMapping = BulkSearchFieldMappingDTO::fromSerializableArray($data);
58+
$this->assertInstanceOf(BulkSearchFieldMappingDTO::class, $fieldMapping);
59+
$this->assertSame('test', $fieldMapping->field);
60+
$this->assertSame(['provider1', 'provider2'], $fieldMapping->providers);
61+
$this->assertSame(3, $fieldMapping->priority);
62+
}
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
namespace App\Tests\Services\InfoProviderSystem\DTOs;
22+
23+
use App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultsDTO;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class BulkSearchPartResultsDTOTest extends TestCase
27+
{
28+
29+
public function testHasErrors(): void
30+
{
31+
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []);
32+
$this->assertFalse($test->hasErrors());
33+
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], ['error1']);
34+
$this->assertTrue($test->hasErrors());
35+
}
36+
37+
public function testGetErrorCount(): void
38+
{
39+
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []);
40+
$this->assertCount(0, $test->errors);
41+
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], ['error1', 'error2']);
42+
$this->assertCount(2, $test->errors);
43+
}
44+
45+
public function testHasResults(): void
46+
{
47+
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []);
48+
$this->assertFalse($test->hasResults());
49+
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [ $this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class) ], []);
50+
$this->assertTrue($test->hasResults());
51+
}
52+
53+
public function testGetResultCount(): void
54+
{
55+
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []);
56+
$this->assertCount(0, $test->searchResults);
57+
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [
58+
$this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class),
59+
$this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class)
60+
], []);
61+
$this->assertCount(2, $test->searchResults);
62+
}
63+
}

tests/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTOTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,53 @@ public function testFromSerializableRepresentation(): void
206206

207207
$deserialized = BulkSearchResponseDTO::fromSerializableRepresentation($input, $this->entityManager);
208208
$this->assertEquals($this->dummy, $deserialized);
209+
}
210+
211+
public function testMerge(): void
212+
{
213+
$merged = BulkSearchResponseDTO::merge($this->dummy, $this->dummyEmpty);
214+
$this->assertCount(1, $merged->partResults);
215+
216+
$merged = BulkSearchResponseDTO::merge($this->dummyEmpty, $this->dummyEmpty);
217+
$this->assertCount(0, $merged->partResults);
218+
219+
$merged = BulkSearchResponseDTO::merge($this->dummy, $this->dummy, $this->dummy);
220+
$this->assertCount(3, $merged->partResults);
221+
}
222+
223+
public function testReplaceResultsForPart(): void
224+
{
225+
$newPartResults = new BulkSearchPartResultsDTO(
226+
part: $this->entityManager->find(Part::class, 1),
227+
searchResults: [
228+
new BulkSearchPartResultDTO(
229+
searchResult: new SearchResultDTO(provider_key: "new", provider_id: "new", name: "New Part", description: "A new part"),
230+
sourceField: "mpn", sourceKeyword: "new", priority: 1
231+
)
232+
],
233+
errors: ['New Error']
234+
);
235+
236+
$replaced = $this->dummy->replaceResultsForPart($newPartResults);
237+
$this->assertCount(1, $replaced->partResults);
238+
$this->assertSame($newPartResults, $replaced->partResults[0]);
239+
}
240+
241+
public function testReplaceResultsForPartNotExisting(): void
242+
{
243+
$newPartResults = new BulkSearchPartResultsDTO(
244+
part: $this->entityManager->find(Part::class, 1),
245+
searchResults: [
246+
new BulkSearchPartResultDTO(
247+
searchResult: new SearchResultDTO(provider_key: "new", provider_id: "new", name: "New Part", description: "A new part"),
248+
sourceField: "mpn", sourceKeyword: "new", priority: 1
249+
)
250+
],
251+
errors: ['New Error']
252+
);
253+
254+
$this->expectException(\InvalidArgumentException::class);
209255

256+
$replaced = $this->dummyEmpty->replaceResultsForPart($newPartResults);
210257
}
211258
}

0 commit comments

Comments
 (0)