Skip to content

Commit

Permalink
OXDEV-7557 Cleanup theme setting getter tests
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 14, 2023
1 parent d41c4f4 commit f1a3a3b
Showing 1 changed file with 121 additions and 81 deletions.
202 changes: 121 additions & 81 deletions tests/Unit/Service/ThemeSettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Service;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\BooleanSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\FloatSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\IntegerSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\StringSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\InvalidCollection;
Expand All @@ -15,110 +18,125 @@ class ThemeSettingServiceTest extends UnitTestCase
{
public function testGetThemeSettingInteger(): void
{
$serviceIntegerSetting = $this->getIntegerSetting();

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repository->expects($this->once())
->method('getInteger')
->willReturn(123);

$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('integerSetting');
$integerSetting = $settingService->getIntegerSetting($nameID, 'awesomeTheme');
$themeId = 'awesomeTheme';

$repositoryResult = 123;
$sut = $this->getSut(
themeSettingRepository: $this->getRepositorySettingGetterMock(
'getInteger',
$nameID,
$themeId,
$repositoryResult
)
);

$this->assertEquals($serviceIntegerSetting, $integerSetting);
$this->assertEquals(
new IntegerSetting($nameID, $repositoryResult),
$sut->getIntegerSetting($nameID, $themeId)
);
}

public function testGetThemeSettingFloat(): void
{
$serviceFloatSetting = $this->getFloatSetting();

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repository->expects($this->once())
->method('getFloat')
->willReturn(1.23);

$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('floatSetting');
$floatSetting = $settingService->getFloatSetting($nameID, 'awesomeTheme');
$themeId = 'awesomeTheme';

$repositoryResult = 1.23;
$sut = $this->getSut(
themeSettingRepository: $this->getRepositorySettingGetterMock(
'getFloat',
$nameID,
$themeId,
$repositoryResult
)
);

$this->assertEquals($serviceFloatSetting, $floatSetting);
$this->assertEquals(
new FloatSetting($nameID, $repositoryResult),
$sut->getFloatSetting($nameID, $themeId)
);
}

public function testGetThemeSettingBoolean(): void
{
$serviceBooleanSetting = $this->getNegativeBooleanSetting();

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repository->expects($this->once())
->method('getBoolean')
->willReturn(false);

$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('booleanSetting');
$booleanSetting = $settingService->getBooleanSetting($nameID, 'awesomeTheme');
$themeId = 'awesomeTheme';

$this->assertEquals($serviceBooleanSetting, $booleanSetting);
$repositoryResult = false;
$sut = $this->getSut(
themeSettingRepository: $this->getRepositorySettingGetterMock(
'getBoolean',
$nameID,
$themeId,
$repositoryResult
)
);

$this->assertEquals(
new BooleanSetting($nameID, $repositoryResult),
$sut->getBooleanSetting($nameID, $themeId)
);
}

public function testGetThemeSettingString(): void
{
$serviceStringSetting = $this->getStringSetting();

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repository->expects($this->once())
->method('getString')
->willReturn('default');

$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('stringSetting');
$stringSetting = $settingService->getStringSetting($nameID, 'awesomeTheme');
$themeId = 'awesomeTheme';

$repositoryResult = 'default';
$sut = $this->getSut(
themeSettingRepository: $this->getRepositorySettingGetterMock(
'getString',
$nameID,
$themeId,
$repositoryResult
)
);

$this->assertEquals($serviceStringSetting, $stringSetting);
$this->assertEquals(
new StringSetting($nameID, $repositoryResult),
$sut->getStringSetting($nameID, $themeId)
);
}

public function testGetThemeSettingSelect(): void
{
$serviceSelectSetting = $this->getSelectSetting();

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repository->expects($this->once())
->method('getSelect')
->willReturn('select');

$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('selectSetting');
$selectSetting = $settingService->getSelectSetting($nameID, 'awesomeTheme');
$themeId = 'awesomeTheme';

$this->assertEquals($serviceSelectSetting, $selectSetting);
$repositoryResult = 'select';
$sut = $this->getSut(
themeSettingRepository: $this->getRepositorySettingGetterMock(
'getSelect',
$nameID,
$themeId,
$repositoryResult
)
);

$this->assertEquals(
new StringSetting($nameID, $repositoryResult),
$sut->getSelectSetting($nameID, $themeId)
);
}

public function testGetThemeSettingCollection(): void
{
$nameID = new ID('arraySetting');
$themeId = 'awesomeTheme';

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repositoryResult = ['nice', 'values'];
$repository->expects($this->once())
->method('getCollection')
->with($nameID, $themeId)
->willReturn($repositoryResult);

$jsonService = $this->createMock(JsonServiceInterface::class);
$collectionEncodingResult = 'someEncodedResult';
$jsonService->method('jsonEncodeArray')
->with($repositoryResult)
->willReturn($collectionEncodingResult);

$sut = $this->getSut(
themeSettingRepository: $repository,
jsonService: $jsonService,
themeSettingRepository: $this->getRepositorySettingGetterMock(
'getCollection',
$nameID,
$themeId,
$repositoryResult
),
jsonService: $this->getJsonEncodeServiceMock($repositoryResult, $collectionEncodingResult),
);

$this->assertEquals(
Expand All @@ -127,27 +145,22 @@ public function testGetThemeSettingCollection(): void
);
}

public function testGetThemeSettingAssocCollection(): void
public function testGetThemeSettingAssocCollectionGetsAndEncodesRepositoryResult(): void
{
$nameID = new ID('aarraySetting');
$themeId = 'awesomeTheme';

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repositoryResult = ['first' => '10', 'second' => '20', 'third' => '50'];
$repository->expects($this->once())
->method('getAssocCollection')
->with($nameID, $themeId)
->willReturn($repositoryResult);

$jsonService = $this->createMock(JsonServiceInterface::class);
$collectionEncodingResult = 'someEncodedResult';
$jsonService->method('jsonEncodeArray')
->with($repositoryResult)
->willReturn($collectionEncodingResult);

$sut = $this->getSut(
themeSettingRepository: $repository,
jsonService: $jsonService
themeSettingRepository: $this->getRepositorySettingGetterMock(
'getAssocCollection',
$nameID,
$themeId,
$repositoryResult
),
jsonService: $this->getJsonEncodeServiceMock($repositoryResult, $collectionEncodingResult)
);

$this->assertEquals(
Expand All @@ -156,6 +169,31 @@ public function testGetThemeSettingAssocCollection(): void
);
}

private function getRepositorySettingGetterMock(
string $repositoryMethod,
ID $nameID,
string $themeId,
$repositoryResult
): ThemeSettingRepositoryInterface {
$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repository->expects($this->once())
->method($repositoryMethod)
->with($nameID, $themeId)
->willReturn($repositoryResult);
return $repository;
}

private function getJsonEncodeServiceMock(
array $repositoryResult,
string $collectionEncodingResult
): JsonServiceInterface {
$jsonService = $this->createMock(JsonServiceInterface::class);
$jsonService->method('jsonEncodeArray')
->with($repositoryResult)
->willReturn($collectionEncodingResult);
return $jsonService;
}

public function testListThemeSettings(): void
{
$themeId = 'awesomeTheme';
Expand Down Expand Up @@ -281,7 +319,9 @@ public function getSut(
?JsonServiceInterface $jsonService = null,
): ThemeSettingService {
return new ThemeSettingService(
themeSettingRepository: $themeSettingRepository ?? $this->createStub(ThemeSettingRepositoryInterface::class),
themeSettingRepository: $themeSettingRepository ?? $this->createStub(
ThemeSettingRepositoryInterface::class
),
jsonService: $jsonService ?? $this->createStub(JsonServiceInterface::class)
);
}
Expand Down

0 comments on commit f1a3a3b

Please sign in to comment.