diff --git a/src/Setting/Infrastructure/AbstractDatabaseSettingRepository.php b/src/Setting/Infrastructure/AbstractDatabaseSettingRepository.php index 507881c..6d74298 100644 --- a/src/Setting/Infrastructure/AbstractDatabaseSettingRepository.php +++ b/src/Setting/Infrastructure/AbstractDatabaseSettingRepository.php @@ -15,8 +15,6 @@ */ abstract class AbstractDatabaseSettingRepository { - private QueryBuilder $queryBuilder; - public function __construct( private BasicContextInterface $basicContext, private EventDispatcherInterface $eventDispatcher, @@ -69,7 +67,8 @@ protected function getSettingTypes(string $theme = ''): array $themeCondition = (!empty($theme)) ? 'theme:'.$theme : ''; $shopId = $this->basicContext->getCurrentShopId(); - $this->queryBuilder->select('c.oxvarname') + $queryBuilder = $this->queryBuilderFactory->create(); + $queryBuilder->select('c.oxvarname') ->addSelect('c.oxvartype') ->from('oxconfig', 'c') ->where('c.oxmodule = :module') @@ -78,7 +77,7 @@ protected function getSettingTypes(string $theme = ''): array ':module' => $themeCondition, ':shopId' => $shopId ]); - $result = $this->queryBuilder->execute(); + $result = $queryBuilder->execute(); $value = $result->fetchAllKeyValue(); $notFoundLocation = (!empty($theme)) ? 'theme: "'.$theme.'"' : 'shopID: "'.$shopId.'"'; diff --git a/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php b/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php index 79210e6..6c3b1f6 100644 --- a/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/ShopSettingRepositoryTest.php @@ -22,7 +22,7 @@ public function testGetShopSettingInteger(): void $nameID = new ID('integerSetting'); - $repository = $this->getShopSettingRepoInstance('123'); + $repository = $this->getFetchOneShopSettingRepoInstance('123'); $integer = $repository->getInteger($nameID); @@ -33,7 +33,7 @@ public function testGetNoShopSettingInteger(): void { $nameID = new ID('NotExistingSetting'); - $repository = $this->getShopSettingRepoInstance(False); + $repository = $this->getFetchOneShopSettingRepoInstance(False); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as an integer configuration'); @@ -44,7 +44,7 @@ public function testGetShopSettingInvalidInteger(): void { $nameID = new ID('floatSetting'); - $repository = $this->getShopSettingRepoInstance('1.23'); + $repository = $this->getFetchOneShopSettingRepoInstance('1.23'); $this->expectException(UnexpectedValueException::class); $this->expectExceptionMessage('The queried configuration was found as a float, not an integer'); @@ -56,7 +56,7 @@ public function testGetShopSettingFloat(): void $nameID = new ID('floatSetting'); - $repository = $this->getShopSettingRepoInstance('1.23'); + $repository = $this->getFetchOneShopSettingRepoInstance('1.23'); $float = $repository->getFloat($nameID); @@ -67,7 +67,7 @@ public function testGetNoShopSettingFloat(): void { $nameID = new ID('NotExistingSetting'); - $repository = $this->getShopSettingRepoInstance(False); + $repository = $this->getFetchOneShopSettingRepoInstance(False); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a float configuration'); @@ -78,7 +78,7 @@ public function testGetShopSettingInvalidFloat(): void { $nameID = new ID('intSetting'); - $repository = $this->getShopSettingRepoInstance('123'); + $repository = $this->getFetchOneShopSettingRepoInstance('123'); $this->expectException(UnexpectedValueException::class); $this->expectExceptionMessage('The queried configuration was found as an integer, not a float'); @@ -90,7 +90,7 @@ public function testGetShopSettingBooleanNegativ(): void $nameID = new ID('booleanSetting'); - $repository = $this->getShopSettingRepoInstance(''); + $repository = $this->getFetchOneShopSettingRepoInstance(''); $boolean = $repository->getBoolean($nameID); @@ -102,7 +102,7 @@ public function testGetShopSettingBooleanPositiv(): void $nameID = new ID('booleanSetting'); - $repository = $this->getShopSettingRepoInstance('1'); + $repository = $this->getFetchOneShopSettingRepoInstance('1'); $boolean = $repository->getBoolean($nameID); @@ -113,7 +113,7 @@ public function testGetNoShopSettingBoolean(): void { $nameID = new ID('NotExistingSetting'); - $repository = $this->getShopSettingRepoInstance(False); + $repository = $this->getFetchOneShopSettingRepoInstance(False); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a boolean configuration'); @@ -125,7 +125,7 @@ public function testGetShopSettingString(): void $nameID = new ID('stringSetting'); - $repository = $this->getShopSettingRepoInstance('default'); + $repository = $this->getFetchOneShopSettingRepoInstance('default'); $string = $repository->getString($nameID); @@ -136,7 +136,7 @@ public function testGetNoShopSettingString(): void { $nameID = new ID('NotExistingSetting'); - $repository = $this->getShopSettingRepoInstance(False); + $repository = $this->getFetchOneShopSettingRepoInstance(False); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a string configuration'); @@ -148,7 +148,7 @@ public function testGetShopSettingSelect(): void $nameID = new ID('selectSetting'); - $repository = $this->getShopSettingRepoInstance('select'); + $repository = $this->getFetchOneShopSettingRepoInstance('select'); $select = $repository->getSelect($nameID); @@ -159,7 +159,7 @@ public function testGetNoShopSettingSelect(): void { $nameID = new ID('NotExistingSetting'); - $repository = $this->getShopSettingRepoInstance(False); + $repository = $this->getFetchOneShopSettingRepoInstance(False); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a select configuration'); @@ -171,7 +171,7 @@ public function testGetShopSettingCollection(): void $nameID = new ID('arraySetting'); - $repository = $this->getShopSettingRepoInstance('a:2:{i:0;s:4:"nice";i:1;s:6:"values";}', 1); + $repository = $this->getFetchOneShopSettingRepoInstance('a:2:{i:0;s:4:"nice";i:1;s:6:"values";}', 1); $collection = $repository->getCollection($nameID); @@ -182,7 +182,7 @@ public function testGetNoShopSettingCollection(): void { $nameID = new ID('NotExistingSetting'); - $repository = $this->getShopSettingRepoInstance(False); + $repository = $this->getFetchOneShopSettingRepoInstance(False); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a collection configuration'); @@ -195,7 +195,7 @@ public function testGetShopSettingAssocCollection(): void $serializeArrayString = 'a:3:{s:5:"first";s:2:"10";s:6:"second";s:2:"20";s:5:"third";s:2:"50";}'; - $repository = $this->getShopSettingRepoInstance($serializeArrayString); + $repository = $this->getFetchOneShopSettingRepoInstance($serializeArrayString); $assocCollection = $repository->getAssocCollection($nameID); @@ -206,7 +206,7 @@ public function testGetNoShopSettingAssocCollection(): void { $nameID = new ID('NotExistingSetting'); - $repository = $this->getShopSettingRepoInstance(False); + $repository = $this->getFetchOneShopSettingRepoInstance(False); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as an associative collection configuration'); @@ -256,27 +256,10 @@ public function getQueryBuilderFactoryMock(Result|MockObject $result): QueryBuil return $queryBuilderFactory; } - public function getBasicContextMock(int $shopId = 1): BasicContextInterface|MockObject + private function getFetchOneShopSettingRepoInstance(string|bool $qbReturnValue): ShopSettingRepositoryInterface { - $basicContext = $this->createMock(BasicContextInterface::class); - $basicContext->expects($this->once()) - ->method('getCurrentShopId') - ->willReturn($shopId); - - return $basicContext; - } - - private function getShopSettingRepoInstance(string|bool $qbReturnValue, int $shopId = 1): ShopSettingRepositoryInterface - { - $queryBuilderFactory = $this->getQueryBuilderFactoryMock($qbReturnValue); - $eventDispatcher = $this->createMock(EventDispatcherInterface::class); - $basicContext = $this->getBasicContextMock($shopId); - - return new ShopSettingRepository( - $basicContext, - $eventDispatcher, - $queryBuilderFactory - ); + $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock($qbReturnValue); + return $this->getShopSettingRepository($queryBuilderFactory); } /** diff --git a/tests/Unit/Infrastructure/ThemeSettingRepositoryTest.php b/tests/Unit/Infrastructure/ThemeSettingRepositoryTest.php index 3b56c3e..6c8a930 100644 --- a/tests/Unit/Infrastructure/ThemeSettingRepositoryTest.php +++ b/tests/Unit/Infrastructure/ThemeSettingRepositoryTest.php @@ -20,10 +20,7 @@ class ThemeSettingRepositoryTest extends UnitTestCase public function testGetThemeSettingInteger(): void { $nameID = new ID('integerSetting'); - - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock('123'); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance('123'); $integer = $repository->getInteger($nameID, 'awesomeModule'); @@ -33,9 +30,7 @@ public function testGetThemeSettingInteger(): void public function testGetNoThemeSettingInteger(): void { $nameID = new ID('NotExistingSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock(False); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance(false); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as an integer configuration'); @@ -45,9 +40,7 @@ public function testGetNoThemeSettingInteger(): void public function testGetThemeSettingInvalidInteger(): void { $nameID = new ID('floatSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock('1.23'); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance('1.23'); $this->expectException(UnexpectedValueException::class); $this->expectExceptionMessage('The queried configuration was found as a float, not an integer'); @@ -57,9 +50,7 @@ public function testGetThemeSettingFloat(): void { $nameID = new ID('floatSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock('1.23'); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance('1.23'); $float = $repository->getFloat($nameID, 'awesomeModule'); @@ -69,9 +60,7 @@ public function testGetThemeSettingFloat(): void public function testGetNoThemeSettingFloat(): void { $nameID = new ID('NotExistingSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock(False); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance(false); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a float configuration'); @@ -81,9 +70,7 @@ public function testGetNoThemeSettingFloat(): void public function testGetThemeSettingInvalidFloat(): void { $nameID = new ID('intSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock('123'); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance('123'); $this->expectException(UnexpectedValueException::class); $this->expectExceptionMessage('The queried configuration was found as an integer, not a float'); @@ -93,10 +80,7 @@ public function testGetThemeSettingInvalidFloat(): void public function testGetThemeSettingBooleanNegative(): void { $nameID = new ID('booleanSetting'); - - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock(''); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance(''); $boolean = $repository->getBoolean($nameID, 'awesomeModule'); @@ -106,10 +90,7 @@ public function testGetThemeSettingBooleanNegative(): void public function testGetThemeSettingBooleanPositive(): void { $nameID = new ID('booleanSetting'); - - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock('1'); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance('1'); $boolean = $repository->getBoolean($nameID, 'awesomeModule'); @@ -119,9 +100,7 @@ public function testGetThemeSettingBooleanPositive(): void public function testGetNoThemeSettingBoolean(): void { $nameID = new ID('NotExistingSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock(False); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance(false); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a boolean configuration'); @@ -131,10 +110,7 @@ public function testGetNoThemeSettingBoolean(): void public function testGetThemeSettingString(): void { $nameID = new ID('stringSetting'); - - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock('default'); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance('default'); $string = $repository->getString($nameID, 'awesomeModule'); @@ -144,9 +120,7 @@ public function testGetThemeSettingString(): void public function testGetNoThemeSettingString(): void { $nameID = new ID('NotExistingSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock(False); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance(false); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a string configuration'); @@ -156,10 +130,7 @@ public function testGetNoThemeSettingString(): void public function testGetThemeSettingSelect(): void { $nameID = new ID('selectSetting'); - - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock('select'); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance('select'); $select = $repository->getSelect($nameID, 'awesomeModule'); @@ -169,9 +140,7 @@ public function testGetThemeSettingSelect(): void public function testGetNoThemeSettingSelect(): void { $nameID = new ID('NotExistingSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock(False); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance(false); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a select configuration'); @@ -181,10 +150,7 @@ public function testGetNoThemeSettingSelect(): void public function testGetThemeSettingCollection(): void { $nameID = new ID('arraySetting'); - - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock('a:2:{i:0;s:4:"nice";i:1;s:6:"values";}'); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance('a:2:{i:0;s:4:"nice";i:1;s:6:"values";}'); $collection = $repository->getCollection($nameID, 'awesomeModule'); @@ -194,9 +160,7 @@ public function testGetThemeSettingCollection(): void public function testGetNoThemeSettingCollection(): void { $nameID = new ID('NotExistingSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock(False); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance(false); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as a collection configuration'); @@ -208,9 +172,7 @@ public function testGetThemeSettingAssocCollection(): void $nameID = new ID('aarraySetting'); $serializeArrayString = 'a:3:{s:5:"first";s:2:"10";s:6:"second";s:2:"20";s:5:"third";s:2:"50";}'; - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock($serializeArrayString); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance($serializeArrayString); $assocCollection = $repository->getAssocCollection($nameID, 'awesomeModule'); @@ -220,9 +182,7 @@ public function testGetThemeSettingAssocCollection(): void public function testGetNoThemeSettingAssocCollection(): void { $nameID = new ID('NotExistingSetting'); - $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock(False); - - $repository = $this->getThemeSettingRepository($queryBuilderFactory); + $repository = $this->getFetchOneThemeSettingRepoInstance(false); $this->expectException(NotFound::class); $this->expectExceptionMessage('The queried name couldn\'t be found as an associative collection configuration'); @@ -301,19 +261,13 @@ private function getThemeSettingRepository( MockObject|QueryBuilderFactoryInterface $queryBuilderFactory, ): ThemeSettingRepository { $basicContext = $this->getBasicContextMock(); - return new ThemeSettingRepository($queryBuilderFactory, $basicContext); + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); + return new ThemeSettingRepository($basicContext, $eventDispatcher, $queryBuilderFactory); } - private function getThemeSettingRepoInstance(string|bool $qbReturnedValue, int $shopId = 1): ThemeSettingRepositoryInterface + private function getFetchOneThemeSettingRepoInstance(string|bool $qbReturnedValue): ThemeSettingRepositoryInterface { - $queryBuilderFactory = $this->getQueryBuilderFactoryMock($qbReturnedValue); - $eventDispatcher = $this->createMock(EventDispatcherInterface::class); - $basicContext = $this->getBasicContextMock($shopId); - - return new ThemeSettingRepository( - $basicContext, - $eventDispatcher, - $queryBuilderFactory - ); + $queryBuilderFactory = $this->getFetchOneQueryBuilderFactoryMock($qbReturnedValue); + return $this->getThemeSettingRepository($queryBuilderFactory); } }