From 36e1fcfbed28d2004eb49d7a9278cbd9b262a5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Wed, 3 Dec 2025 22:22:21 +0100 Subject: [PATCH] Fixed bulk provider imports, issue described in #869 This makes PR #1110 obsolete --- .../DTOs/BulkSearchFieldMappingDTO.php | 21 +++++++++++++++++-- .../DTOs/BulkSearchFieldMappingDTOTest.php | 9 ++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTO.php b/src/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTO.php index 50b7f4cf..47d8ac69 100644 --- a/src/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTO.php @@ -22,24 +22,41 @@ declare(strict_types=1); namespace App\Services\InfoProviderSystem\DTOs; +use App\Services\InfoProviderSystem\Providers\InfoProviderInterface; + /** * Represents a mapping between a part field and the info providers that should search in that field. */ readonly class BulkSearchFieldMappingDTO { + /** @var string[] $providers Array of provider keys to search with (e.g., ['digikey', 'farnell']) */ + public array $providers; + /** * @param string $field The field to search in (e.g., 'mpn', 'name', or supplier-specific fields like 'digikey_spn') - * @param string[] $providers Array of provider keys to search with (e.g., ['digikey', 'farnell']) + * @param string[]|InfoProviderInterface[] $providers Array of provider keys to search with (e.g., ['digikey', 'farnell']) * @param int $priority Priority for this field mapping (1-10, lower numbers = higher priority) */ public function __construct( public string $field, - public array $providers, + array $providers = [], public int $priority = 1 ) { if ($priority < 1 || $priority > 10) { throw new \InvalidArgumentException('Priority must be between 1 and 10'); } + + //Ensure that providers are provided as keys + foreach ($providers as &$provider) { + if ($provider instanceof InfoProviderInterface) { + $provider = $provider->getProviderKey(); + } + if (!is_string($provider)) { + throw new \InvalidArgumentException('Providers must be provided as strings or InfoProviderInterface instances'); + } + } + unset($provider); + $this->providers = $providers; } /** diff --git a/tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php b/tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php index a2101938..e300e2bf 100644 --- a/tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php +++ b/tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php @@ -26,6 +26,15 @@ use PHPUnit\Framework\TestCase; class BulkSearchFieldMappingDTOTest extends TestCase { + public function testProviderInstanceNormalization(): void + { + $mockProvider = $this->createMock(\App\Services\InfoProviderSystem\Providers\InfoProviderInterface::class); + $mockProvider->method('getProviderKey')->willReturn('mock_provider'); + + $fieldMapping = new BulkSearchFieldMappingDTO(field: 'mpn', providers: ['provider1', $mockProvider], priority: 5); + $this->assertSame(['provider1', 'mock_provider'], $fieldMapping->providers); + } + public function testIsSupplierPartNumberField(): void { $fieldMapping = new BulkSearchFieldMappingDTO(field: 'reichelt_spn', providers: ['provider1'], priority: 1);