From 5b71d68179ea6d1f1254ba57ae6ffb8007eec5d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 21 Sep 2025 23:07:45 +0200 Subject: [PATCH] Added tests for new DTO objects --- .../BulkInfoProviderImportController.php | 6 +- .../DTOs/BulkSearchResponseDTO.php | 14 +++-- .../DTOs/BulkSearchFieldMappingDTOTest.php | 63 +++++++++++++++++++ .../DTOs/BulkSearchPartResultsDTOTest.php | 63 +++++++++++++++++++ .../DTOs/BulkSearchResponseDTOTest.php | 47 ++++++++++++++ 5 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php create mode 100644 tests/Services/InfoProviderSystem/DTOs/BulkSearchPartResultsDTOTest.php diff --git a/src/Controller/BulkInfoProviderImportController.php b/src/Controller/BulkInfoProviderImportController.php index b71cc745..2d3dd7f6 100644 --- a/src/Controller/BulkInfoProviderImportController.php +++ b/src/Controller/BulkInfoProviderImportController.php @@ -101,7 +101,7 @@ class BulkInfoProviderImportController extends AbstractController return $job; } - private function updatePartSearchResults(BulkInfoProviderImportJob $job, int $partId, ?BulkSearchPartResultsDTO $newResults): void + private function updatePartSearchResults(BulkInfoProviderImportJob $job, ?BulkSearchPartResultsDTO $newResults): void { if ($newResults === null) { return; @@ -111,7 +111,7 @@ class BulkInfoProviderImportController extends AbstractController $allResults = $job->getSearchResults($this->entityManager); // Find and update the results for this specific part - $allResults = $allResults->replaceResultsForPart($partId, $newResults); + $allResults = $allResults->replaceResultsForPart($newResults); // Save updated results back to job $job->setSearchResults($allResults); @@ -482,7 +482,7 @@ class BulkInfoProviderImportController extends AbstractController } // Update the job's search results for this specific part efficiently - $this->updatePartSearchResults($job, $partId, $searchResultsDto[0] ?? null); + $this->updatePartSearchResults($job, $searchResultsDto[0] ?? null); // Prefetch details if requested if ($prefetchDetails && $searchResultsDto !== null) { diff --git a/src/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTO.php b/src/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTO.php index 2f3f858b..58e9e240 100644 --- a/src/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTO.php @@ -41,21 +41,27 @@ readonly class BulkSearchResponseDTO implements \ArrayAccess, \IteratorAggregate /** * Replaces the search results for a specific part, and returns a new instance. - * @param Part|int $part + * The part to replaced, is identified by the part property of the new_results parameter. + * The original instance remains unchanged. * @param BulkSearchPartResultsDTO $new_results * @return BulkSearchResponseDTO */ - public function replaceResultsForPart(Part|int $part, BulkSearchPartResultsDTO $new_results): self + public function replaceResultsForPart(BulkSearchPartResultsDTO $new_results): self { $array = $this->partResults; + $replaced = false; foreach ($array as $index => $partResult) { - if (($part instanceof Part && $partResult->part->getId() === $part->getId()) || - ($partResult->part->getId() === $part)) { + if ($partResult->part === $new_results->part) { $array[$index] = $new_results; + $replaced = true; break; } } + if (!$replaced) { + throw new \InvalidArgumentException("Part not found in existing results."); + } + return new self($array); } diff --git a/tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php b/tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php new file mode 100644 index 00000000..a2101938 --- /dev/null +++ b/tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php @@ -0,0 +1,63 @@ +. + */ + +namespace App\Tests\Services\InfoProviderSystem\DTOs; + +use App\Services\InfoProviderSystem\DTOs\BulkSearchFieldMappingDTO; +use PHPUnit\Framework\TestCase; + +class BulkSearchFieldMappingDTOTest extends TestCase +{ + + public function testIsSupplierPartNumberField(): void + { + $fieldMapping = new BulkSearchFieldMappingDTO(field: 'reichelt_spn', providers: ['provider1'], priority: 1); + $this->assertTrue($fieldMapping->isSupplierPartNumberField()); + + $fieldMapping = new BulkSearchFieldMappingDTO(field: 'partNumber', providers: ['provider1'], priority: 1); + $this->assertFalse($fieldMapping->isSupplierPartNumberField()); + } + + public function testToSerializableArray(): void + { + $fieldMapping = new BulkSearchFieldMappingDTO(field: 'test', providers: ['provider1', 'provider2'], priority: 3); + $array = $fieldMapping->toSerializableArray(); + $this->assertIsArray($array); + $this->assertSame([ + 'field' => 'test', + 'providers' => ['provider1', 'provider2'], + 'priority' => 3, + ], $array); + } + + public function testFromSerializableArray(): void + { + $data = [ + 'field' => 'test', + 'providers' => ['provider1', 'provider2'], + 'priority' => 3, + ]; + $fieldMapping = BulkSearchFieldMappingDTO::fromSerializableArray($data); + $this->assertInstanceOf(BulkSearchFieldMappingDTO::class, $fieldMapping); + $this->assertSame('test', $fieldMapping->field); + $this->assertSame(['provider1', 'provider2'], $fieldMapping->providers); + $this->assertSame(3, $fieldMapping->priority); + } +} diff --git a/tests/Services/InfoProviderSystem/DTOs/BulkSearchPartResultsDTOTest.php b/tests/Services/InfoProviderSystem/DTOs/BulkSearchPartResultsDTOTest.php new file mode 100644 index 00000000..09fa4973 --- /dev/null +++ b/tests/Services/InfoProviderSystem/DTOs/BulkSearchPartResultsDTOTest.php @@ -0,0 +1,63 @@ +. + */ + +namespace App\Tests\Services\InfoProviderSystem\DTOs; + +use App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultsDTO; +use PHPUnit\Framework\TestCase; + +class BulkSearchPartResultsDTOTest extends TestCase +{ + + public function testHasErrors(): void + { + $test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []); + $this->assertFalse($test->hasErrors()); + $test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], ['error1']); + $this->assertTrue($test->hasErrors()); + } + + public function testGetErrorCount(): void + { + $test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []); + $this->assertCount(0, $test->errors); + $test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], ['error1', 'error2']); + $this->assertCount(2, $test->errors); + } + + public function testHasResults(): void + { + $test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []); + $this->assertFalse($test->hasResults()); + $test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [ $this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class) ], []); + $this->assertTrue($test->hasResults()); + } + + public function testGetResultCount(): void + { + $test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []); + $this->assertCount(0, $test->searchResults); + $test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [ + $this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class), + $this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class) + ], []); + $this->assertCount(2, $test->searchResults); + } +} diff --git a/tests/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTOTest.php b/tests/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTOTest.php index a76c9748..b4dc0dea 100644 --- a/tests/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTOTest.php +++ b/tests/Services/InfoProviderSystem/DTOs/BulkSearchResponseDTOTest.php @@ -206,6 +206,53 @@ class BulkSearchResponseDTOTest extends KernelTestCase $deserialized = BulkSearchResponseDTO::fromSerializableRepresentation($input, $this->entityManager); $this->assertEquals($this->dummy, $deserialized); + } + public function testMerge(): void + { + $merged = BulkSearchResponseDTO::merge($this->dummy, $this->dummyEmpty); + $this->assertCount(1, $merged->partResults); + + $merged = BulkSearchResponseDTO::merge($this->dummyEmpty, $this->dummyEmpty); + $this->assertCount(0, $merged->partResults); + + $merged = BulkSearchResponseDTO::merge($this->dummy, $this->dummy, $this->dummy); + $this->assertCount(3, $merged->partResults); + } + + public function testReplaceResultsForPart(): void + { + $newPartResults = new BulkSearchPartResultsDTO( + part: $this->entityManager->find(Part::class, 1), + searchResults: [ + new BulkSearchPartResultDTO( + searchResult: new SearchResultDTO(provider_key: "new", provider_id: "new", name: "New Part", description: "A new part"), + sourceField: "mpn", sourceKeyword: "new", priority: 1 + ) + ], + errors: ['New Error'] + ); + + $replaced = $this->dummy->replaceResultsForPart($newPartResults); + $this->assertCount(1, $replaced->partResults); + $this->assertSame($newPartResults, $replaced->partResults[0]); + } + + public function testReplaceResultsForPartNotExisting(): void + { + $newPartResults = new BulkSearchPartResultsDTO( + part: $this->entityManager->find(Part::class, 1), + searchResults: [ + new BulkSearchPartResultDTO( + searchResult: new SearchResultDTO(provider_key: "new", provider_id: "new", name: "New Part", description: "A new part"), + sourceField: "mpn", sourceKeyword: "new", priority: 1 + ) + ], + errors: ['New Error'] + ); + + $this->expectException(\InvalidArgumentException::class); + + $replaced = $this->dummyEmpty->replaceResultsForPart($newPartResults); } }