Skip to content

Commit

Permalink
Fix #33. Optimizing Input check, Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Torsten Freyda committed Feb 24, 2024
1 parent 69601b6 commit dd47d75
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Services/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public function getServerType(): string
public function getMappingProductFields(): array
{
$mappingProductFields = array();
if($this->config->getString($this::CONFIG_PATH_MAPPING_PRODUCT_FIELDS) !== '') {
$configDefaultProductMappings = $this->config->getString($this::CONFIG_PATH_MAPPING_PRODUCT_FIELDS);
$configStr = $this->config->getString($this::CONFIG_PATH_MAPPING_PRODUCT_FIELDS);
if($configStr !== '' && str_contains(':', $configStr)) {
$configDefaultProductMappings = $configStr;
} else {
$configDefaultProductMappings = Defaults::DEFAULT_PRODUCT_MAPPINGS;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Services/SettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace MuckiSearchPlugin\Services;

use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Psr\Log\LoggerInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;

use MuckiSearchPlugin\Services\Settings as PluginSettings;

class SettingsTest extends TestCase
{
public function testGetMappingProductFieldsDefaultInput()
{
$systemConfigService = $this->createMock(SystemConfigService::class);
$systemConfigService->method('getString')->willReturnCallback(
fn () => ''
);

$instance = $this->getInstance($systemConfigService);
$mappingProductFieldsResults = $instance->getMappingProductFields();
$this->assertIsArray($mappingProductFieldsResults, 'GetMappingProductFields is not an array');
$this->assertArrayHasKey('field', $mappingProductFieldsResults[0], 'Missing field key of GetMappingProductFields method');
$this->assertArrayHasKey('type', $mappingProductFieldsResults[0], 'Missing type key of GetMappingProductFields method');
$this->assertSame('id', $mappingProductFieldsResults[0]['field'], 'Missing correct [0] field value');
$this->assertSame('keyword', $mappingProductFieldsResults[0]['type'], 'Missing correct [0] type value');
$this->assertSame('productNumber', $mappingProductFieldsResults[1]['field'], 'Missing correct [1] field value');
$this->assertSame('keyword', $mappingProductFieldsResults[1]['type'], 'Missing correct [1] type value');
$this->assertSame('translations.DEFAULT.name', $mappingProductFieldsResults[2]['field'], 'Missing correct [2] field value');
$this->assertSame('text', $mappingProductFieldsResults[2]['type'], 'Missing correct [2] type value');
$this->assertSame('translations.DEFAULT.description', $mappingProductFieldsResults[3]['field'], 'Missing correct [3] field value');
$this->assertSame('text', $mappingProductFieldsResults[3]['type'], 'Missing correct [3] type value');
$this->assertSame('cover.media.url', $mappingProductFieldsResults[4]['field'], 'Missing correct [4] field value');
$this->assertSame('text', $mappingProductFieldsResults[4]['type'], 'Missing correct [4] type value');
}

private function getInstance($systemConfigService): PluginSettings
{
return new PluginSettings($systemConfigService);
}
}

0 comments on commit dd47d75

Please sign in to comment.