From 0a58c8077b92cd4ec42cd595e07ffaded22b3132 Mon Sep 17 00:00:00 2001 From: marcelmanzel Date: Fri, 17 Nov 2023 11:43:58 +0100 Subject: [PATCH] OXDEV-7456: Add mutation to change theme float setting --- .../Infrastructure/ThemeSettingRepository.php | 8 +- .../ThemeSettingRepositoryTest.php | 137 +++++++++++++----- .../ChangeThemeSettingRepositoryTest.php | 113 +++++++++++---- 3 files changed, 194 insertions(+), 64 deletions(-) diff --git a/src/Setting/Infrastructure/ThemeSettingRepository.php b/src/Setting/Infrastructure/ThemeSettingRepository.php index 95e9d80..1056e5a 100644 --- a/src/Setting/Infrastructure/ThemeSettingRepository.php +++ b/src/Setting/Infrastructure/ThemeSettingRepository.php @@ -132,7 +132,13 @@ public function saveIntegerSetting(ID $name, int $value, string $themeId): void public function saveFloatSetting(ID $name, float $value, string $themeId): void { - // TODO: Implement saveFloatSetting() method. + $value = $this->shopSettingEncoder->encode(FieldType::NUMBER, $value); + + try { + $this->saveSettingValue($name, $themeId, (string)$value); + } catch (NotFound $e) { + $this->throwSetterNotFoundException('float', $name->val()); + } } public function saveBooleanSetting(ID $name, bool $value, string $themeId): void diff --git a/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php b/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php index 744245b..651cb45 100644 --- a/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php +++ b/tests/Integration/Infrastructure/ThemeSettingRepositoryTest.php @@ -24,27 +24,100 @@ class ThemeSettingRepositoryTest extends IntegrationTestCase { public function testSaveAndGetIntegerSetting(): void { - $configurationChangedEvent = new ThemeSettingChangedEvent( - "coolIntSetting", - 1, - 'awesomeTheme' + $name = 'coolIntSetting'; + $eventDispatcher = $this->getEventDispatcherMock($name); + /** @var QueryBuilderFactoryInterface $queryBuilderFactory */ + $queryBuilderFactory = $this->get(QueryBuilderFactoryInterface::class); + + $repository = $this->getSut( + eventDispatcher: $eventDispatcher, + queryBuilderFactory: $queryBuilderFactory + ); + + $this->createThemeSetting( + $queryBuilderFactory, + $name, + FieldType::NUMBER, + 123 ); + + $repository->saveIntegerSetting(new ID($name), 124, 'awesomeTheme'); + $integerResult = $repository->getInteger(new ID($name), 'awesomeTheme'); + + $this->assertSame(124, $integerResult); + } + + public function testSaveNotExistingIntegerSetting(): void + { $eventDispatcher = $this->createMock(EventDispatcherInterface::class); - $eventDispatcher->expects($this->once()) - ->method('dispatch') - ->with($configurationChangedEvent); - $basicContext = $this->get(BasicContextInterface::class); + $eventDispatcher->expects($this->never()) + ->method('dispatch'); + $repository = $this->getSut(eventDispatcher: $eventDispatcher); + + $this->expectException(NotFound::class); + $this->expectExceptionMessage('The integer setting "notExistingSetting" doesn\'t exist'); + $repository->saveIntegerSetting(new ID('notExistingSetting'), 1234, 'awesomeTheme'); + } + + public function testSaveAndGetFloatSetting(): void + { + $name = "coolFloatSetting"; + $eventDispatcher = $this->getEventDispatcherMock("coolFloatSetting"); /** @var QueryBuilderFactoryInterface $queryBuilderFactory */ $queryBuilderFactory = $this->get(QueryBuilderFactoryInterface::class); - $shopSettingEncoder = $this->get(ShopSettingEncoderInterface::class); - $repository = new ThemeSettingRepository( - $basicContext, - $eventDispatcher, + $repository = $this->getSut( + eventDispatcher: $eventDispatcher, + queryBuilderFactory: $queryBuilderFactory + ); + + $this->createThemeSetting( $queryBuilderFactory, - $shopSettingEncoder + $name, + FieldType::NUMBER, + 1.23 + ); + + $repository->saveFloatSetting(new ID($name), 1.24, 'awesomeTheme'); + $floatResult = $repository->getFloat(new ID($name), 'awesomeTheme'); + + $this->assertSame(1.24, $floatResult); + } + + public function testSaveNotExistingFloatSetting(): void + { + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $eventDispatcher->expects($this->never()) + ->method('dispatch'); + $repository = $this->getSut(eventDispatcher: $eventDispatcher); + + $this->expectException(NotFound::class); + $this->expectExceptionMessage('The integer setting "notExistingSetting" doesn\'t exist'); + $repository->saveIntegerSetting(new ID('notExistingSetting'), 1234, 'awesomeTheme'); + } + + private function getSut( + ?BasicContextInterface $basicContext = null, + ?EventDispatcherInterface $eventDispatcher = null, + ?QueryBuilderFactoryInterface $queryBuilderFactory = null, + ?ShopSettingEncoderInterface $shopSettingEncoder = null + ): ThemeSettingRepository + { + return new ThemeSettingRepository( + $basicContext ?? $this->get(BasicContextInterface::class), + $eventDispatcher ?? $this->get(EventDispatcherInterface::class), + $queryBuilderFactory ?? $this->get(QueryBuilderFactoryInterface::class), + $shopSettingEncoder ?? $this->get(ShopSettingEncoderInterface::class) ); + } + private function createThemeSetting( + QueryBuilderFactoryInterface $queryBuilderFactory, + string $name, + string $fieldType, + mixed $value + ): void + { $uniqueId = uniqid(); $queryBuilder = $queryBuilderFactory->create(); $queryBuilder @@ -59,32 +132,28 @@ public function testSaveAndGetIntegerSetting(): void 'oxid' => $uniqueId, 'oxshopid' => 1, 'oxmodule' => 'theme:awesomeTheme', - 'oxvarname' => 'coolIntSetting', - 'oxvartype' => FieldType::NUMBER, - 'oxvarvalue' => 123 + 'oxvarname' => $name, + 'oxvartype' => $fieldType, + 'oxvarvalue' => $value ]); $queryBuilder->execute(); - - $repository->saveIntegerSetting(new ID('coolIntSetting'), 124, 'awesomeTheme'); - $integerResult = $repository->getInteger(new ID('coolIntSetting'), 'awesomeTheme'); - - $this->assertSame(124, $integerResult); } - public function testSaveNotExistingSetting(): void + /** + * @param string $name + * @return \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface|(EventDispatcherInterface&\PHPUnit\Framework\MockObject\MockObject) + */ + public function getEventDispatcherMock(string $name): \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface { - $eventDispatcher = $this->createMock(EventDispatcherInterface::class); - $eventDispatcher->expects($this->never()) - ->method('dispatch'); - $repository = new ThemeSettingRepository( - $this->get(BasicContextInterface::class), - $eventDispatcher, - $this->get(QueryBuilderFactoryInterface::class), - $this->get(ShopSettingEncoderInterface::class) + $configurationChangedEvent = new ThemeSettingChangedEvent( + $name, + 1, + 'awesomeTheme' ); - - $this->expectException(NotFound::class); - $this->expectExceptionMessage('The integer setting "notExistingSetting" doesn\'t exist'); - $repository->saveIntegerSetting(new ID('notExistingSetting'), 1234, 'awesomeTheme'); + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $eventDispatcher->expects($this->once()) + ->method('dispatch') + ->with($configurationChangedEvent); + return $eventDispatcher; } } diff --git a/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php b/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php index 4a77e3e..cb04a30 100644 --- a/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/ChangeThemeSettingRepositoryTest.php @@ -9,6 +9,7 @@ use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType; use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ThemeSettingRepository; use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use TheCodingMachine\GraphQLite\Types\ID; @@ -17,21 +18,13 @@ 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(); + $settingEncoder = $this->getShopSettingEncoderMock( + FieldType::NUMBER, + 123, + 123 + ); + $repository = $this->getSut(settingEncoder: $settingEncoder); $repository->expects($this->once()) ->method('saveSettingValue') ->with($nameID, 'awesomeTheme', '123'); @@ -42,20 +35,13 @@ public function testChangeThemeSettingInteger(): void 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(); + + $settingEncoder = $this->getShopSettingEncoderMock( + FieldType::NUMBER, + 123, + 123 + ); + $repository = $this->getSut(settingEncoder: $settingEncoder); $repository->expects($this->once()) ->method('saveSettingValue') ->with($nameID, 'awesomeTheme', '123') @@ -63,7 +49,76 @@ public function testChangeNoThemeSettingInteger(): void $this->expectException(NotFound::class); $this->expectExceptionMessage('The integer setting "' . $nameID->val() . '" doesn\'t exist'); - $repository->saveIntegerSetting($nameID, 123, 'awesomeTheme'); } + + public function testChangeThemeSettingFloat(): void + { + $nameID = new ID('floatSetting'); + + $settingEncoder = $this->getShopSettingEncoderMock( + FieldType::NUMBER, + 1.23, + 1.23 + ); + $repository = $this->getSut(settingEncoder: $settingEncoder); + $repository->expects($this->once()) + ->method('saveSettingValue') + ->with($nameID, 'awesomeTheme', '1.23'); + + $repository->saveFloatSetting($nameID, 1.23, 'awesomeTheme'); + } + + public function testChangeNoThemeSettingFloat(): void + { + $nameID = new ID('NotExistingSetting'); + + $settingEncoder = $this->getShopSettingEncoderMock( + FieldType::NUMBER, + 1.23, + 1.23 + ); + $repository = $this->getSut(settingEncoder: $settingEncoder); + $repository->expects($this->once()) + ->method('saveSettingValue') + ->with($nameID, 'awesomeTheme', '1.23') + ->willThrowException(new NotFound()); + + $this->expectException(NotFound::class); + $this->expectExceptionMessage('The float setting "' . $nameID->val() . '" doesn\'t exist'); + + $repository->saveFloatSetting($nameID, 1.23, 'awesomeTheme'); + } + + public function getShopSettingEncoderMock( + string $fieldType, + mixed $decodedValue, + mixed $encodedValue + ): ShopSettingEncoderInterface|MockObject + { + $settingEncoder = $this->createMock(ShopSettingEncoderInterface::class); + $settingEncoder->expects($this->once()) + ->method('encode') + ->with($fieldType, $decodedValue) + ->willReturn($encodedValue); + return $settingEncoder; + } + + public function getSut( + ?BasicContextInterface $basicContext = null, + ?EventDispatcherInterface $eventDispatcher = null, + ?QueryBuilderFactoryInterface $queryBuilderFactory = null, + ?ShopSettingEncoderInterface $settingEncoder = null + ): MockObject|ThemeSettingRepository { + $repository = $this->getMockBuilder(ThemeSettingRepository::class) + ->setConstructorArgs([ + $basicContext ?? $this->createMock(BasicContextInterface::class), + $eventDispatcher ?? $this->createMock(EventDispatcherInterface::class), + $queryBuilderFactory ?? $this->createMock(QueryBuilderFactoryInterface::class), + $settingEncoder ?? $this->createMock(ShopSettingEncoderInterface::class) + ]) + ->onlyMethods(['saveSettingValue']) + ->getMock(); + return $repository; + } }