diff --git a/src/Setting/Infrastructure/AbstractDatabaseSettingRepository.php b/src/Setting/Infrastructure/AbstractDatabaseSettingRepository.php index aa26633..05de4d6 100644 --- a/src/Setting/Infrastructure/AbstractDatabaseSettingRepository.php +++ b/src/Setting/Infrastructure/AbstractDatabaseSettingRepository.php @@ -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( diff --git a/src/Setting/Infrastructure/ThemeSettingRepository.php b/src/Setting/Infrastructure/ThemeSettingRepository.php index f83aa31..63acd5d 100644 --- a/src/Setting/Infrastructure/ThemeSettingRepository.php +++ b/src/Setting/Infrastructure/ThemeSettingRepository.php @@ -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 { diff --git a/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php b/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php index 0536bde..744245b 100644 --- a/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php +++ b/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php @@ -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, @@ -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); diff --git a/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php b/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php new file mode 100644 index 0000000..3a1ef8b --- /dev/null +++ b/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php @@ -0,0 +1,69 @@ +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'); + } +} diff --git a/tests/Unit/Infrastructure/ThemeSettingRepositoryTest.php b/tests/Unit/Infrastructure/GetThemeSettingRepositoryTest.php similarity index 84% rename from tests/Unit/Infrastructure/ThemeSettingRepositoryTest.php rename to tests/Unit/Infrastructure/GetThemeSettingRepositoryTest.php index 1bcda43..876b2b0 100644 --- a/tests/Unit/Infrastructure/ThemeSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/GetThemeSettingRepositoryTest.php @@ -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; @@ -16,14 +18,32 @@ 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); } @@ -31,11 +51,24 @@ public function testGetThemeSettingInteger(): void 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 diff --git a/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php b/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php index 83b9079..466cb34 100644 --- a/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php @@ -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(); diff --git a/tests/Unit/UnitTestCase.php b/tests/Unit/UnitTestCase.php index bfbefc8..d4f5864 100644 --- a/tests/Unit/UnitTestCase.php +++ b/tests/Unit/UnitTestCase.php @@ -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' + ] ) ); }