Skip to content

Commit

Permalink
OXDEV-7456: Restructure tests for getting and setting integer-setting
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid committed Nov 14, 2023
1 parent 33c67e5 commit 99f5a62
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected function saveSettingValue(ID $name, string $themeId, string $value): v

$affectedRows = $queryBuilder->execute();
if ($affectedRows === 0) {
throw new NotFound('No configurations found for ' . $themeId);
throw new NotFound('Configuration not found for ' . $themeId);
}

$this->eventDispatcher->dispatch(
Expand Down
2 changes: 1 addition & 1 deletion src/Setting/Infrastructure/ThemeSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use TheCodingMachine\GraphQLite\Types\ID;
use UnexpectedValueException;

final class ThemeSettingRepository extends AbstractDatabaseSettingRepository implements ThemeSettingRepositoryInterface
class ThemeSettingRepository extends AbstractDatabaseSettingRepository implements ThemeSettingRepositoryInterface
{
public function getInteger(ID $name, string $themeId): int
{
Expand Down
17 changes: 7 additions & 10 deletions tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testSaveAndGetIntegerSetting(): void
$basicContext = $this->get(BasicContextInterface::class);
/** @var QueryBuilderFactoryInterface $queryBuilderFactory */
$queryBuilderFactory = $this->get(QueryBuilderFactoryInterface::class);
$shopSettingEncoder = new DecoratedShopSettingEncoder();
$shopSettingEncoder = $this->get(ShopSettingEncoderInterface::class);

$repository = new ThemeSettingRepository(
$basicContext,
Expand Down Expand Up @@ -69,21 +69,18 @@ public function testSaveAndGetIntegerSetting(): void
$integerResult = $repository->getInteger(new ID('coolIntSetting'), 'awesomeTheme');

$this->assertSame(124, $integerResult);
$this->assertSame(1, $shopSettingEncoder->getEncodeCounter());
}

public function testSaveNotExistingSetting(): void
{
$basicContext = $this->get(BasicContextInterface::class);
$eventDispatcher = $this->get(EventDispatcherInterface::class);
$queryBuilderFactory = $this->get(QueryBuilderFactoryInterface::class);
$shopSettingEncoder = $this->get(ShopSettingEncoderInterface::class);

$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects($this->never())
->method('dispatch');
$repository = new ThemeSettingRepository(
$basicContext,
$this->get(BasicContextInterface::class),
$eventDispatcher,
$queryBuilderFactory,
$shopSettingEncoder
$this->get(QueryBuilderFactoryInterface::class),
$this->get(ShopSettingEncoderInterface::class)
);

$this->expectException(NotFound::class);
Expand Down
69 changes: 69 additions & 0 deletions tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

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

use OxidEsales\EshopCommunity\Internal\Framework\Config\Utility\ShopSettingEncoderInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
use OxidEsales\GraphQL\Base\Exception\NotFound;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ThemeSettingRepository;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use TheCodingMachine\GraphQLite\Types\ID;

class ChangeThemeSettingRepositoryTest extends UnitTestCase
{
public function testChangeThemeSettingInteger(): void
{
$nameID = new ID('integerSetting');
$settingEncoder = $this->createMock(ShopSettingEncoderInterface::class);
$settingEncoder->expects($this->once())
->method('encode')
->with(FieldType::NUMBER, 123)
->willReturn(123);

$repository = $this->getMockBuilder(ThemeSettingRepository::class)
->setConstructorArgs([
$this->createMock(BasicContextInterface::class),
$this->createMock(EventDispatcherInterface::class),
$this->createMock(QueryBuilderFactoryInterface::class),
$settingEncoder
])
->onlyMethods(['saveSettingValue'])
->getMock();
$repository->expects($this->once())
->method('saveSettingValue')
->with($nameID, 'awesomeTheme', '123');

$repository->saveIntegerSetting($nameID, 123, 'awesomeTheme');
}

public function testChangeNoThemeSettingInteger(): void
{
$nameID = new ID('NotExistingSetting');
$settingEncoder = $this->createMock(ShopSettingEncoderInterface::class);
$settingEncoder->expects($this->once())
->method('encode')
->with(FieldType::NUMBER, 123)
->willReturn(123);
$repository = $this->getMockBuilder(ThemeSettingRepository::class)
->setConstructorArgs([
$this->createMock(BasicContextInterface::class),
$this->createMock(EventDispatcherInterface::class),
$this->createMock(QueryBuilderFactoryInterface::class),
$settingEncoder
])
->onlyMethods(['saveSettingValue'])
->getMock();
$repository->expects($this->once())
->method('saveSettingValue')
->with($nameID, 'awesomeTheme', '123')
->willThrowException(new NotFound());

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The integer setting "' . $nameID->val() . '" doesn\'t exist');

$repository->saveIntegerSetting($nameID, 123, 'awesomeTheme');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Doctrine\DBAL\ForwardCompatibility\Result;
use Doctrine\DBAL\Query\QueryBuilder;
use OxidEsales\EshopCommunity\Internal\Framework\Config\Utility\ShopSettingEncoder;
use OxidEsales\EshopCommunity\Internal\Framework\Config\Utility\ShopSettingEncoderInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
use OxidEsales\GraphQL\Base\Exception\NotFound;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ThemeSettingRepository;
Expand All @@ -16,26 +18,57 @@
use TheCodingMachine\GraphQLite\Types\ID;
use UnexpectedValueException;

class ThemeSettingRepositoryTest extends UnitTestCase
class GetThemeSettingRepositoryTest extends UnitTestCase
{
public function testGetThemeSettingInteger(): void
{
$nameID = new ID('integerSetting');
$repository = $this->getFetchOneThemeSettingRepoInstance('123');

$integer = $repository->getInteger($nameID, 'awesomeModule');
$settingEncoder = $this->createMock(ShopSettingEncoderInterface::class);
$settingEncoder->expects($this->once())
->method('decode')
->with(FieldType::NUMBER, '123')
->willReturn('123');

$repository = $this->getMockBuilder(ThemeSettingRepository::class)
->setConstructorArgs([
$this->createMock(BasicContextInterface::class),
$this->createMock(EventDispatcherInterface::class),
$this->createMock(QueryBuilderFactoryInterface::class),
$settingEncoder
])
->onlyMethods(['getSettingValue'])
->getMock();
$repository->expects($this->once())
->method('getSettingValue')
->with($nameID, FieldType::NUMBER, 'awesomeTheme')
->willReturn('123');

$integer = $repository->getInteger($nameID, 'awesomeTheme');

$this->assertEquals(123, $integer);
}

public function testGetNoThemeSettingInteger(): void
{
$nameID = new ID('NotExistingSetting');
$repository = $this->getFetchOneThemeSettingRepoInstance(false);
$repository = $this->getMockBuilder(ThemeSettingRepository::class)
->setConstructorArgs([
$this->createMock(BasicContextInterface::class),
$this->createMock(EventDispatcherInterface::class),
$this->createMock(QueryBuilderFactoryInterface::class),
$this->createMock(ShopSettingEncoderInterface::class)
])
->onlyMethods(['getSettingValue'])
->getMock();
$repository->expects($this->once())
->method('getSettingValue')
->with($nameID, FieldType::NUMBER, 'awesomeTheme')
->willThrowException(new NotFound());

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The queried name couldn\'t be found as an integer configuration');
$repository->getInteger($nameID, 'awesomeModule');

$repository->getInteger($nameID, 'awesomeTheme');
}

public function testGetThemeSettingInvalidInteger(): void
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Infrastructure/ShopSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ private function getFetchOneShopSettingRepoInstance(string|bool $qbReturnValue):
*/
private function getShopSettingRepository(
MockObject|QueryBuilderFactoryInterface $queryBuilderFactory
): ShopSettingRepository {
): ShopSettingRepository
{
$basicContextMock = $this->getBasicContextMock(1);
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$shopSettingEncoder = new ShopSettingEncoder();
Expand Down
6 changes: 5 additions & 1 deletion tests/Unit/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ protected function getAssocCollectionSetting(): StringSetting
return new StringSetting(
new ID('aarraySetting'),
json_encode(
['first' => '10', 'second' => '20', 'third' => '50']
[
'first' => '10',
'second' => '20',
'third' => '50'
]
)
);
}
Expand Down

0 comments on commit 99f5a62

Please sign in to comment.