mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-06 02:59:29 +00:00
Added tests for new DTO objects
This commit is contained in:
parent
b94e28a961
commit
5b71d68179
5 changed files with 186 additions and 7 deletions
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2025 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\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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2025 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\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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue