diff --git a/src/Module/Controller/ModuleListController.php b/src/Module/Controller/ModuleListController.php index a471fc5..d4c1143 100644 --- a/src/Module/Controller/ModuleListController.php +++ b/src/Module/Controller/ModuleListController.php @@ -10,8 +10,9 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Controller; use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleFilters; use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleListServiceInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFilters; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; use TheCodingMachine\GraphQLite\Annotations\Logged; use TheCodingMachine\GraphQLite\Annotations\Query; use TheCodingMachine\GraphQLite\Annotations\Right; @@ -30,8 +31,8 @@ public function __construct( #[Query] #[Logged] #[Right('LIST_MODULES')] - public function modulesList(?ModuleFilters $filters = null): array + public function modulesList(?ComponentFiltersInterface $filters): array { - return $this->moduleListService->getModuleList($filters ?? new ModuleFilters()); + return $this->moduleListService->getModuleList($filters ?? new ComponentFilters()); } } diff --git a/src/Module/DataType/ModuleDataType.php b/src/Module/DataType/ModuleDataType.php index 6371bd8..f3162f9 100644 --- a/src/Module/DataType/ModuleDataType.php +++ b/src/Module/DataType/ModuleDataType.php @@ -9,47 +9,25 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\DataType; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\AbstractComponentDataType; use TheCodingMachine\GraphQLite\Annotations\Field; use TheCodingMachine\GraphQLite\Annotations\Type; #[Type] -final class ModuleDataType implements ModuleDataTypeInterface +final class ModuleDataType extends AbstractComponentDataType implements ModuleDataTypeInterface { public function __construct( - private readonly string $id, - private readonly string $version, - private readonly string $title, - private readonly string $description, + string $id, + string $title, + string $version, + string $description, + bool $active, private readonly ?string $thumbnail, private readonly ?string $author, private readonly ?string $url, - private readonly ?string $email, - private readonly bool $active + private readonly ?string $email ) { - } - - #[Field] - public function getId(): string - { - return $this->id; - } - - #[Field] - public function getTitle(): string - { - return $this->title; - } - - #[Field] - public function getVersion(): string - { - return $this->version; - } - - #[Field] - public function getDescription(): string - { - return $this->description; + parent::__construct($id, $title, $version, $description, $active); } #[Field] diff --git a/src/Module/DataType/ModuleDataTypeFactory.php b/src/Module/DataType/ModuleDataTypeFactory.php index 3d786a9..25e816e 100644 --- a/src/Module/DataType/ModuleDataTypeFactory.php +++ b/src/Module/DataType/ModuleDataTypeFactory.php @@ -19,7 +19,7 @@ public function __construct( ) { } - public function createFromCoreModule( + public function createFromModuleConfiguration( ModuleConfiguration $moduleConfig ): ModuleDataTypeInterface { $titlesData = $moduleConfig->getTitle(); @@ -36,14 +36,14 @@ public function createFromCoreModule( return new ModuleDataType( id: $moduleConfig->getId(), - version: $moduleConfig->getVersion(), title: $translatedTitle, + version: $moduleConfig->getVersion(), description: $translatedDescription, + active: $moduleConfig->isActivated(), thumbnail: $moduleConfig->getThumbnail(), author: $moduleConfig->getAuthor(), url: $moduleConfig->getUrl(), - email: $moduleConfig->getEmail(), - active: $moduleConfig->isActivated(), + email: $moduleConfig->getEmail() ); } } diff --git a/src/Module/DataType/ModuleDataTypeFactoryInterface.php b/src/Module/DataType/ModuleDataTypeFactoryInterface.php index 996409d..3c0d9f4 100644 --- a/src/Module/DataType/ModuleDataTypeFactoryInterface.php +++ b/src/Module/DataType/ModuleDataTypeFactoryInterface.php @@ -11,5 +11,5 @@ interface ModuleDataTypeFactoryInterface { - public function createFromCoreModule(ModuleConfiguration $moduleConfig): ModuleDataTypeInterface; + public function createFromModuleConfiguration(ModuleConfiguration $moduleConfig): ModuleDataTypeInterface; } diff --git a/src/Module/DataType/ModuleDataTypeInterface.php b/src/Module/DataType/ModuleDataTypeInterface.php index 6d33d35..77faac3 100644 --- a/src/Module/DataType/ModuleDataTypeInterface.php +++ b/src/Module/DataType/ModuleDataTypeInterface.php @@ -7,16 +7,10 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\DataType; -interface ModuleDataTypeInterface -{ - public function getId(): string; - - public function getTitle(): string; - - public function getVersion(): string; - - public function getDescription(): string; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentDataTypeInterface; +interface ModuleDataTypeInterface extends ComponentDataTypeInterface +{ public function getThumbnail(): ?string; public function getAuthor(): ?string; diff --git a/src/Module/DataType/ModuleFiltersInterface.php b/src/Module/DataType/ModuleFiltersInterface.php deleted file mode 100644 index 8c91651..0000000 --- a/src/Module/DataType/ModuleFiltersInterface.php +++ /dev/null @@ -1,21 +0,0 @@ -shopConfigurationDaoBridge->get(); $moduleConfigurations = $shopConfiguration->getModuleConfigurations(); diff --git a/src/Module/Infrastructure/ModuleListInfrastructureInterface.php b/src/Module/Infrastructure/ModuleListInfrastructureInterface.php index 3eef730..8855519 100644 --- a/src/Module/Infrastructure/ModuleListInfrastructureInterface.php +++ b/src/Module/Infrastructure/ModuleListInfrastructureInterface.php @@ -16,5 +16,5 @@ interface ModuleListInfrastructureInterface * @return array * @throws ModulesNotFoundException */ - public function getModuleList(): array; + public function getModuleConfigurations(): array; } diff --git a/src/Module/Service/ModuleFilterService.php b/src/Module/Service/ModuleFilterService.php deleted file mode 100644 index c8432eb..0000000 --- a/src/Module/Service/ModuleFilterService.php +++ /dev/null @@ -1,24 +0,0 @@ -filterModuleByTitle(module: $module) - && $filterList->filterModuleByStatus(module: $module); - }); - } -} diff --git a/src/Module/Service/ModuleFilterServiceInterface.php b/src/Module/Service/ModuleFilterServiceInterface.php deleted file mode 100644 index ba0184d..0000000 --- a/src/Module/Service/ModuleFilterServiceInterface.php +++ /dev/null @@ -1,21 +0,0 @@ - $modules - * @param ModuleFiltersInterface $filterList - * @return array - */ - public function filterModules(array $modules, ModuleFiltersInterface $filterList): array; -} diff --git a/src/Module/Service/ModuleListService.php b/src/Module/Service/ModuleListService.php index f65b5a4..9fb5b46 100644 --- a/src/Module/Service/ModuleListService.php +++ b/src/Module/Service/ModuleListService.php @@ -10,25 +10,27 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Service; use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeFactoryInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleFiltersInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure\ModuleListInfrastructureInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\ComponentFilterServiceInterface; final class ModuleListService implements ModuleListServiceInterface { public function __construct( private readonly ModuleListInfrastructureInterface $moduleListInfrastructure, - private readonly ModuleFilterServiceInterface $moduleFilterService, + private readonly ComponentFilterServiceInterface $componentFilterService, private readonly ModuleDataTypeFactoryInterface $moduleDataTypeFactory ) { } - public function getModuleList(ModuleFiltersInterface $filters): array + public function getModuleList(ComponentFiltersInterface $filters): array { $modulesArray = []; - $moduleConfigurations = $this->moduleListInfrastructure->getModuleList(); + $moduleConfigurations = $this->moduleListInfrastructure->getModuleConfigurations(); foreach ($moduleConfigurations as $moduleConfig) { - $modulesArray[] = $this->moduleDataTypeFactory->createFromCoreModule($moduleConfig); + $modulesArray[] = $this->moduleDataTypeFactory->createFromModuleConfiguration($moduleConfig); } - return $this->moduleFilterService->filterModules($modulesArray, $filters); + + return $this->componentFilterService->filterComponents($modulesArray, $filters); } } diff --git a/src/Module/Service/ModuleListServiceInterface.php b/src/Module/Service/ModuleListServiceInterface.php index a2705f1..826fbfa 100644 --- a/src/Module/Service/ModuleListServiceInterface.php +++ b/src/Module/Service/ModuleListServiceInterface.php @@ -7,13 +7,13 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Service; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleFiltersInterface; interface ModuleListServiceInterface { /** * @return array */ - public function getModuleList(ModuleFiltersInterface $filters): array; + public function getModuleList(ComponentFiltersInterface $filters): array; } diff --git a/src/Shared/DataType/AbstractComponentDataType.php b/src/Shared/DataType/AbstractComponentDataType.php new file mode 100644 index 0000000..bfa9f23 --- /dev/null +++ b/src/Shared/DataType/AbstractComponentDataType.php @@ -0,0 +1,56 @@ +id; + } + + #[Field] + public function getTitle(): string + { + return $this->title; + } + + #[Field] + public function getVersion(): string + { + return $this->version; + } + + #[Field] + public function getDescription(): string + { + return $this->description; + } + + #[Field] + public function isActive(): bool + { + return $this->active; + } +} diff --git a/src/Shared/DataType/ComponentDataTypeInterface.php b/src/Shared/DataType/ComponentDataTypeInterface.php new file mode 100644 index 0000000..bc902e3 --- /dev/null +++ b/src/Shared/DataType/ComponentDataTypeInterface.php @@ -0,0 +1,16 @@ +titleFilter; if ($titleFilter !== null) { - return $titleFilter->matches($module->getTitle()); + return $titleFilter->matches($title); } return true; } - public function filterModuleByStatus(ModuleDataTypeInterface $module): bool + private function filterComponentByStatus(bool $status): bool { $statusFilter = $this->activeFilter; - if ($statusFilter !== null && $statusFilter->equals() !== null) { - if ($module->isActive() !== $statusFilter->equals()) { - return false; - } + if ($statusFilter !== null && $status !== $statusFilter->equals()) { + return false; } + return true; } + public function filterComponent(ComponentDataTypeInterface $component): bool + { + return $this->filterComponentByTitle($component->getTitle()) + && $this->filterComponentByStatus($component->isActive()); + } + /** - * @Factory(name="ModuleFilters", default=true) + * @Factory(name="ComponentFilters", default=true) */ - public static function createModuleFilters( + public static function createComponentFilters( ?StringFilter $title = null, ?BoolFilter $active = null ): self { diff --git a/src/Shared/DataType/ComponentFiltersInterface.php b/src/Shared/DataType/ComponentFiltersInterface.php new file mode 100644 index 0000000..7499993 --- /dev/null +++ b/src/Shared/DataType/ComponentFiltersInterface.php @@ -0,0 +1,8 @@ +filterComponent($module); + }); + } +} diff --git a/src/Shared/Service/ComponentFilterServiceInterface.php b/src/Shared/Service/ComponentFilterServiceInterface.php new file mode 100644 index 0000000..f597c3e --- /dev/null +++ b/src/Shared/Service/ComponentFilterServiceInterface.php @@ -0,0 +1,17 @@ + $components + * + * @return array + */ + public function filterComponents(array $components, ComponentFiltersInterface $filterList): array; +} diff --git a/src/Theme/Controller/ThemeListController.php b/src/Theme/Controller/ThemeListController.php index fc21dcb..a22c05a 100644 --- a/src/Theme/Controller/ThemeListController.php +++ b/src/Theme/Controller/ThemeListController.php @@ -9,11 +9,12 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Theme\Controller; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFilters; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Service\ThemeListServiceInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; use TheCodingMachine\GraphQLite\Annotations\Logged; use TheCodingMachine\GraphQLite\Annotations\Query; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeFilters; use TheCodingMachine\GraphQLite\Annotations\Right; final class ThemeListController @@ -25,14 +26,13 @@ public function __construct( /** * Query of Configuration Access Module - * @param ThemeFilters|null $filters - * @return ThemeDataType[] + * @return ThemeDataTypeInterface[] */ #[Query] #[Logged] #[Right('LIST_THEMES')] - public function themesList(?ThemeFilters $filters): array + public function themesList(?ComponentFiltersInterface $filters): array { - return $this->themeListService->getThemeList($filters ?? new ThemeFilters()); + return $this->themeListService->getThemeList($filters ?? new ComponentFilters()); } } diff --git a/src/Theme/DataType/ThemeDataType.php b/src/Theme/DataType/ThemeDataType.php index 961e04c..77f37c6 100644 --- a/src/Theme/DataType/ThemeDataType.php +++ b/src/Theme/DataType/ThemeDataType.php @@ -9,48 +9,19 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType; -use TheCodingMachine\GraphQLite\Annotations\Field; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\AbstractComponentDataType; use TheCodingMachine\GraphQLite\Annotations\Type; #[Type] -class ThemeDataType +class ThemeDataType extends AbstractComponentDataType implements ThemeDataTypeInterface { public function __construct( - private readonly string $title, - private readonly string $identifier, - private readonly string $version, - private readonly string $description, - private readonly bool $active + string $id, + string $title, + string $version, + string $description, + bool $active ) { - } - - #[Field] - public function getTitle(): string - { - return $this->title; - } - - #[Field] - public function getIdentifier(): string - { - return $this->identifier; - } - - #[Field] - public function getVersion(): string - { - return $this->version; - } - - #[Field] - public function getDescription(): string - { - return $this->description; - } - - #[Field] - public function isActive(): bool - { - return $this->active; + parent::__construct($id, $title, $version, $description, $active); } } diff --git a/src/Theme/DataType/ThemeDataTypeFactory.php b/src/Theme/DataType/ThemeDataTypeFactory.php index d0c14ad..8809fbf 100644 --- a/src/Theme/DataType/ThemeDataTypeFactory.php +++ b/src/Theme/DataType/ThemeDataTypeFactory.php @@ -17,8 +17,8 @@ public function createFromCoreTheme( Theme $theme ): ThemeDataType { return new ThemeDataType( + id: $theme->getInfo('id'), title: $theme->getInfo('title'), - identifier: $theme->getInfo('id'), version: $theme->getInfo('version'), description: $theme->getInfo('description'), active: $theme->getInfo('active') diff --git a/src/Theme/DataType/ThemeDataTypeFactoryInterface.php b/src/Theme/DataType/ThemeDataTypeFactoryInterface.php index 1d47724..0c2c118 100644 --- a/src/Theme/DataType/ThemeDataTypeFactoryInterface.php +++ b/src/Theme/DataType/ThemeDataTypeFactoryInterface.php @@ -11,5 +11,5 @@ interface ThemeDataTypeFactoryInterface { - public function createFromCoreTheme(Theme $theme): ThemeDataType; + public function createFromCoreTheme(Theme $theme): ThemeDataTypeInterface; } diff --git a/src/Theme/DataType/ThemeDataTypeInterface.php b/src/Theme/DataType/ThemeDataTypeInterface.php new file mode 100644 index 0000000..e557603 --- /dev/null +++ b/src/Theme/DataType/ThemeDataTypeInterface.php @@ -0,0 +1,9 @@ +titleFilter; - if ($titleFilter !== null) { - return $titleFilter->matches($theme->getTitle()); - } - - return true; - } - - public function filterThemeByStatus(ThemeDataType $theme): bool - { - $statusFilter = $this->activeFilter; - if ($statusFilter !== null && $statusFilter->equals() !== null) { - if ($theme->isActive() !== $statusFilter->equals()) { - return false; - } - } - return true; - } - - /** - * @Factory(name="ThemeFilters", default=true) - */ - public static function createThemeFilters( - ?StringFilter $title, - ?BoolFilter $active - ): self { - return new self(titleFilter: $title, activeFilter: $active); - } -} diff --git a/src/Theme/DataType/ThemeFiltersInterface.php b/src/Theme/DataType/ThemeFiltersInterface.php deleted file mode 100644 index 666b6cf..0000000 --- a/src/Theme/DataType/ThemeFiltersInterface.php +++ /dev/null @@ -1,21 +0,0 @@ -filterThemeByTitle($theme) && $filterList->filterThemeByStatus($theme); - }); - } -} diff --git a/src/Theme/Service/ThemeFilterServiceInterface.php b/src/Theme/Service/ThemeFilterServiceInterface.php deleted file mode 100644 index 12fbcf7..0000000 --- a/src/Theme/Service/ThemeFilterServiceInterface.php +++ /dev/null @@ -1,21 +0,0 @@ - $themes - * @param ThemeFiltersInterface $filterList - * @return array - */ - public function filterThemes(array $themes, ThemeFiltersInterface $filterList): array; -} diff --git a/src/Theme/Service/ThemeListService.php b/src/Theme/Service/ThemeListService.php index 6682ffa..711cdd8 100644 --- a/src/Theme/Service/ThemeListService.php +++ b/src/Theme/Service/ThemeListService.php @@ -9,25 +9,21 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Theme\Service; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\ComponentFilterServiceInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeFiltersInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructureInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; -use OxidEsales\Eshop\Core\Theme; final class ThemeListService implements ThemeListServiceInterface { public function __construct( private readonly ThemeListInfrastructureInterface $themeListInfrastructure, - private readonly ThemeFilterServiceInterface $themeFilterService, + private readonly ComponentFilterServiceInterface $componentFilterService, private readonly ThemeDataTypeFactoryInterface $themeDataTypeFactory ) { } - /** - * @return ThemeDataType[] - */ - public function getThemeList(ThemeFiltersInterface $filters): array + public function getThemeList(ComponentFiltersInterface $filters): array { $themesArray = []; $themesList = $this->themeListInfrastructure->getThemes(); @@ -36,6 +32,6 @@ public function getThemeList(ThemeFiltersInterface $filters): array $themesArray[] = $this->themeDataTypeFactory->createFromCoreTheme(theme: $theme); } - return $this->themeFilterService->filterThemes($themesArray, $filters); + return $this->componentFilterService->filterComponents($themesArray, $filters); } } diff --git a/src/Theme/Service/ThemeListServiceInterface.php b/src/Theme/Service/ThemeListServiceInterface.php index faf6be2..ffa2eda 100644 --- a/src/Theme/Service/ThemeListServiceInterface.php +++ b/src/Theme/Service/ThemeListServiceInterface.php @@ -7,13 +7,13 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Theme\Service; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeFiltersInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeInterface; interface ThemeListServiceInterface { /** - * @return ThemeDataType[] + * @return ThemeDataTypeInterface[] */ - public function getThemeList(ThemeFiltersInterface $filters): array; + public function getThemeList(ComponentFiltersInterface $filters): array; } diff --git a/tests/Integration/Infrastructure/ModuleListInfrastructureTest.php b/tests/Integration/Infrastructure/ModuleListInfrastructureTest.php index b4c5e35..423f01b 100644 --- a/tests/Integration/Infrastructure/ModuleListInfrastructureTest.php +++ b/tests/Integration/Infrastructure/ModuleListInfrastructureTest.php @@ -38,7 +38,7 @@ public function testGetModuleList() $sut = new ModuleListInfrastructure( $shopConfigurationDaoBridge ); - $modulesList = $sut->getModuleList(); + $modulesList = $sut->getModuleConfigurations(); $this->assertEquals([ $moduleConfiguration1->getId() => $moduleConfiguration1, $moduleConfiguration2->getId() => $moduleConfiguration2 diff --git a/tests/Integration/Infrastructure/ThemeListInfrastructureTest.php b/tests/Integration/Infrastructure/ThemeListInfrastructureTest.php index 18b4312..c42f600 100644 --- a/tests/Integration/Infrastructure/ThemeListInfrastructureTest.php +++ b/tests/Integration/Infrastructure/ThemeListInfrastructureTest.php @@ -9,14 +9,10 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Integration\Infrastructure; +use OxidEsales\Eshop\Core\Theme; use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\Exception\ThemesNotFound; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\CoreThemeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructure; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructureInterface; -use OxidEsales\Eshop\Core\Theme; /** * @covers \OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructure @@ -32,7 +28,7 @@ public function testGetThemesValidData(): void $this->assertIsArray($themesArray); foreach ($themesArray as $theme) { - $this->assertInstanceOf(ThemeDataType::class, $theme); + $this->assertInstanceOf(Theme::class, $theme); } } diff --git a/tests/Unit/Module/Controller/ModuleListControllerTest.php b/tests/Unit/Module/Controller/ModuleListControllerTest.php index 861f148..a757896 100644 --- a/tests/Unit/Module/Controller/ModuleListControllerTest.php +++ b/tests/Unit/Module/Controller/ModuleListControllerTest.php @@ -10,8 +10,9 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Module\Controller; use OxidEsales\GraphQL\ConfigurationAccess\Module\Controller\ModuleListController; -use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleFilters; use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleListServiceInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFilters; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase; use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeInterface; @@ -22,7 +23,7 @@ class ModuleListControllerTest extends UnitTestCase { public function testModulesListWithFilters(): void { - $filtersStub = $this->createStub(ModuleFilters::class); + $filtersStub = $this->createStub(ComponentFiltersInterface::class); $moduleStub1 = $this->createStub(ModuleDataTypeInterface::class); $moduleStub2 = $this->createStub(ModuleDataTypeInterface::class); $filteredModules = [$moduleStub1, $moduleStub2]; @@ -38,4 +39,20 @@ public function testModulesListWithFilters(): void $this->assertSame($filteredModules, $actualModules); } + + public function testModulesListWithoutFilters(): void + { + $moduleStub = $this->createStub(ModuleDataTypeInterface::class); + $componentFilters = new ComponentFilters(); + + $moduleListServiceSpy = $this->createMock(ModuleListServiceInterface::class); + $moduleListServiceSpy->method('getModuleList') + ->with($componentFilters) + ->willReturn([$moduleStub]); + + $sut = new ModuleListController($moduleListServiceSpy); + $resultModuleList = $sut->modulesList(null); + + $this->assertSame($resultModuleList, [$moduleStub]); + } } diff --git a/tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php b/tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php index 65525a7..91f119b 100644 --- a/tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php +++ b/tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php @@ -61,7 +61,7 @@ public function testCreateFromCoreModule() ]); $moduleDataTypeFactory = new ModuleDataTypeFactory($languageServiceMock); - $moduleDataType = $moduleDataTypeFactory->createFromCoreModule(moduleConfig: $moduleConfigMock); + $moduleDataType = $moduleDataTypeFactory->createFromModuleConfiguration(moduleConfig: $moduleConfigMock); $this->assertSame($expectedId, $moduleDataType->getId()); $this->assertSame($expectedVersion, $moduleDataType->getVersion()); diff --git a/tests/Unit/Module/DataType/ModuleDataTypeTest.php b/tests/Unit/Module/DataType/ModuleDataTypeTest.php index c89e5bd..5019b23 100644 --- a/tests/Unit/Module/DataType/ModuleDataTypeTest.php +++ b/tests/Unit/Module/DataType/ModuleDataTypeTest.php @@ -10,6 +10,7 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Module\DataType; use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataType; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase; /** @@ -19,10 +20,6 @@ class ModuleDataTypeTest extends UnitTestCase { public function testModuleDataType(): void { - $title = uniqid(); - $id = uniqid(); - $version = uniqid(); - $description = uniqid(); $thumbnail = uniqid(); $author = uniqid(); $url = uniqid(); @@ -30,21 +27,18 @@ public function testModuleDataType(): void $active = (bool)random_int(0, 1); $moduleDataType = new ModuleDataType( - id: $id, - version: $version, - title: $title, - description: $description, + id: uniqid(), + title: uniqid(), + version: uniqid(), + description: uniqid(), + active: $active, thumbnail: $thumbnail, author: $author, url: $url, - email: $email, - active: $active + email: $email ); - $this->assertSame($id, $moduleDataType->getId()); - $this->assertSame($version, $moduleDataType->getVersion()); - $this->assertSame($title, $moduleDataType->getTitle()); - $this->assertSame($description, $moduleDataType->getDescription()); + $this->assertInstanceOf(ComponentDataTypeInterface::class, $moduleDataType); $this->assertSame($thumbnail, $moduleDataType->getThumbnail()); $this->assertSame($author, $moduleDataType->getAuthor()); $this->assertSame($url, $moduleDataType->getUrl()); diff --git a/tests/Unit/Module/DataType/ModuleFiltersTest.php b/tests/Unit/Module/DataType/ModuleFiltersTest.php deleted file mode 100644 index dac37bf..0000000 --- a/tests/Unit/Module/DataType/ModuleFiltersTest.php +++ /dev/null @@ -1,118 +0,0 @@ -createMock(StringFilter::class); - $stringFilterMock->expects($this->once())->method('matches')->with($title)->willReturn($expectedResult); - - $moduleMock = $this->createMock(ModuleDataTypeInterface::class); - $moduleMock->expects($this->once())->method('getTitle')->willReturn($title); - - $themeFilters = new ModuleFilters(titleFilter: $stringFilterMock); - $this->assertEquals($expectedResult, $themeFilters->filterModuleByTitle($moduleMock)); - } - - public static function moduleByTitleDataProvider(): \Generator - { - yield "filter module by title matches" => [ - 'expectedResult' => true, - ]; - - yield "filter module by title do not matches" => [ - 'expectedResult' => false, - ]; - } - - public function testModuleFiltersWithoutFilter(): void - { - $themeMock = $this->createStub(ModuleDataTypeInterface::class); - - $themeFilters = new ModuleFilters(); - $this->assertTrue($themeFilters->filterModuleByTitle($themeMock)); - $this->assertTrue($themeFilters->filterModuleByStatus($themeMock)); - } - - /** @dataProvider moduleByStatusDataProvider */ - public function testFilterModuleByStatus( - bool $filterStatus, - bool $actualStatus, - bool $expectedResult - ): void { - $mockBoolFilter = $this->createMock(BoolFilter::class); - $mockBoolFilter->expects($this->exactly(2))->method('equals')->willReturn($filterStatus); - $moduleFilterList = new ModuleFilters(activeFilter: $mockBoolFilter); - - $mockModuleDataType = $this->createMock(ModuleDataTypeInterface::class); - $mockModuleDataType->expects($this->once())->method('isActive')->willReturn($actualStatus); - - $this->assertSame($expectedResult, $moduleFilterList->filterModuleByStatus($mockModuleDataType)); - } - - public static function moduleByStatusDataProvider(): \Generator - { - yield "filter module by true with same module status" => [ - 'filterStatus' => true, - 'actualStatus' => true, - 'expectedResult' => true - ]; - - yield "filter module by false with same module status" => [ - 'filterStatus' => false, - 'actualStatus' => false, - 'expectedResult' => true - ]; - - yield "filter module by true with different module status" => [ - 'filterStatus' => true, - 'actualStatus' => false, - 'expectedResult' => false - ]; - - yield "filter module by false with different theme status" => [ - 'filterStatus' => false, - 'actualStatus' => true, - 'expectedResult' => false - ]; - } - - public function testCreateModuleFilterList(): void - { - $stringFilter = $this->createMock(StringFilter::class); - $boolFilter = $this->createStub(BoolFilter::class); - - $expectedModuleFilters = new ModuleFilters(titleFilter: $stringFilter, activeFilter: $boolFilter); - - $moduleFilters = ModuleFilters::createModuleFilters($stringFilter, $boolFilter); - $this->assertEquals($expectedModuleFilters, $moduleFilters); - } - - public function testCreateModuleFilterListWithNull(): void - { - $expectedThemeFilters = new ModuleFilters(); - $themeFilters = ModuleFilters::createModuleFilters(null, null); - $this->assertEquals($expectedThemeFilters, $themeFilters); - } -} diff --git a/tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php b/tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php index 5f3d6e6..f7d3a83 100644 --- a/tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php +++ b/tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php @@ -41,7 +41,7 @@ public function testGetModuleList() shopConfigurationDaoBridgeMock: $shopConfigurationDaoBridgeMock ); - $result = $sut->getModuleList(); + $result = $sut->getModuleConfigurations(); $this->assertSame([$moduleConfigurationMock], $result); } @@ -59,7 +59,7 @@ public function testGetModuleListThrowsException() shopConfigurationDaoBridgeMock: $shopConfigurationDaoBridgeMock ); - $sut->getModuleList(); + $sut->getModuleConfigurations(); } public function getSut( diff --git a/tests/Unit/Module/Service/ModuleFilterServiceTest.php b/tests/Unit/Module/Service/ModuleFilterServiceTest.php deleted file mode 100644 index 4f315d7..0000000 --- a/tests/Unit/Module/Service/ModuleFilterServiceTest.php +++ /dev/null @@ -1,75 +0,0 @@ -createMock(ModuleFiltersInterface::class); - $moduleFiltersMock->method('filterModuleByTitle') - ->willReturnCallback(function (ModuleDataType $module) { - return str_contains($module->getTitle(), 'Test'); - }); - $moduleFiltersMock->method('filterModuleByStatus') - ->willReturnCallback(function (ModuleDataType $module) { - return $module->isActive(); - }); - - $moduleFilterService = new ModuleFilterService(); - $modulesAfterFilter = $moduleFilterService->filterModules($modulesList, $moduleFiltersMock); - - $this->assertSame([$module1], $modulesAfterFilter); - } -} diff --git a/tests/Unit/Module/Service/ModuleListServiceTest.php b/tests/Unit/Module/Service/ModuleListServiceTest.php index 88bf8c4..19e4865 100644 --- a/tests/Unit/Module/Service/ModuleListServiceTest.php +++ b/tests/Unit/Module/Service/ModuleListServiceTest.php @@ -9,14 +9,12 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Module\Service; -use OxidEsales\EshopCommunity\Internal\Framework\Module\MetaData\Dao\ModuleConfigurationDaoInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeFactoryInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleFiltersInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleFilterServiceInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleListService; use OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure\ModuleListInfrastructureInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataType; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\ComponentFilterServiceInterface; use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; @@ -27,51 +25,40 @@ class ModuleListServiceTest extends UnitTestCase { public function testGetModuleListWithFilters(): void { - $filtersStub = $this->createStub(ModuleFiltersInterface::class); - + $filtersStub = $this->createStub(ComponentFiltersInterface::class); $moduleStub1 = $this->createStub(ModuleDataTypeInterface::class); $moduleStub2 = $this->createStub(ModuleDataTypeInterface::class); $filteredModules = [$moduleStub1]; - $moduleConfigStub1 = $this->createMock(ModuleConfiguration::class); - $moduleConfigStub2 = $this->createMock(ModuleConfiguration::class); - $modules = [$moduleConfigStub1, $moduleConfigStub2]; + $moduleConfigStub1 = $this->createStub(ModuleConfiguration::class); + $moduleConfigStub2 = $this->createStub(ModuleConfiguration::class); + $modulesConfigurations = [$moduleConfigStub1, $moduleConfigStub2]; $moduleListInfrastructureMock = $this->createMock(ModuleListInfrastructureInterface::class); - $moduleListInfrastructureMock->expects($this->once())->method('getModuleList') - ->willReturn($modules); - - $moduleFilterServiceMock = $this->createMock(ModuleFilterServiceInterface::class); + $moduleListInfrastructureMock->method('getModuleConfigurations') + ->willReturn($modulesConfigurations); - $moduleFilterServiceMock->expects($this->once()) - ->method('filterModules') + $componentFilterServiceMock = $this->createMock(ComponentFilterServiceInterface::class); + $componentFilterServiceMock->method('filterComponents') ->with([$moduleStub1, $moduleStub2], $filtersStub) ->willReturn($filteredModules); $moduleDataTypeFactoryMock = $this->createMock(ModuleDataTypeFactoryInterface::class); $moduleDataTypeFactoryMock - ->method('createFromCoreModule') - ->willReturnOnConsecutiveCalls($moduleStub1, $moduleStub2); + ->expects($this->exactly(2)) + ->method('createFromModuleConfiguration') + ->willReturnMap([ + [$moduleConfigStub1, $moduleStub1], + [$moduleConfigStub2, $moduleStub2] + ]); - $sut = $this->getSut( - moduleListInfrastructureMock: $moduleListInfrastructureMock, - moduleFilterServiceMock: $moduleFilterServiceMock, - moduleDataTypeFactoryMock: $moduleDataTypeFactoryMock + $sut = new ModuleListService( + moduleListInfrastructure: $moduleListInfrastructureMock, + componentFilterService: $componentFilterServiceMock, + moduleDataTypeFactory: $moduleDataTypeFactoryMock ); $actualModules = $sut->getModuleList($filtersStub); $this->assertSame($filteredModules, $actualModules); } - - public function getSut( - ?ModuleListInfrastructureInterface $moduleListInfrastructureMock = null, - ?ModuleFilterServiceInterface $moduleFilterServiceMock = null, - ?ModuleDataTypeFactoryInterface $moduleDataTypeFactoryMock = null - ): ModuleListService { - return new ModuleListService( - $moduleListInfrastructureMock ?? $this->createMock(ModuleListInfrastructureInterface::class), - $moduleFilterServiceMock ?? $this->createMock(ModuleFilterServiceInterface::class), - $moduleDataTypeFactoryMock ?? $this->createMock(ModuleDataTypeFactoryInterface::class) - ); - } } diff --git a/tests/Unit/Shared/DataType/AbstractComponentDataTypeTest.php b/tests/Unit/Shared/DataType/AbstractComponentDataTypeTest.php new file mode 100644 index 0000000..f37930c --- /dev/null +++ b/tests/Unit/Shared/DataType/AbstractComponentDataTypeTest.php @@ -0,0 +1,37 @@ +assertSame($id, $component->getId()); + $this->assertSame($title, $component->getTitle()); + $this->assertSame($version, $component->getVersion()); + $this->assertSame($description, $component->getDescription()); + $this->assertSame($isActive, $component->isActive()); + } +} diff --git a/tests/Unit/Shared/DataType/ComponentFiltersTest.php b/tests/Unit/Shared/DataType/ComponentFiltersTest.php new file mode 100644 index 0000000..6a6905a --- /dev/null +++ b/tests/Unit/Shared/DataType/ComponentFiltersTest.php @@ -0,0 +1,125 @@ +createMock(StringFilter::class); + $stringFilterMock->method('matches')->with($title)->willReturn($expectedResult); + + $componentFilters = new ComponentFilters(titleFilter: $stringFilterMock); + $filterComponentByTitleMethod = $this->getComponentFiltersMethod('filterComponentByTitle'); + + $this->assertEquals($expectedResult, $filterComponentByTitleMethod->invoke($componentFilters, $title)); + } + + public static function componentByTitleDataProvider(): \Generator + { + yield "filter component by title matches" => [ + 'expectedResult' => true, + ]; + + yield "filter component by title do not matches" => [ + 'expectedResult' => false, + ]; + } + + public function testComponentFiltersWithoutFilter(): void + { + $themeFilters = new ComponentFilters(); + $filterComponentByTitleMethod = $this->getComponentFiltersMethod('filterComponentByTitle'); + $filterComponentByStatusMethod = $this->getComponentFiltersMethod('filterComponentByStatus'); + + $this->assertTrue($filterComponentByTitleMethod->invoke($themeFilters, uniqid())); + $this->assertTrue($filterComponentByStatusMethod->invoke($themeFilters, (bool)random_int(0, 1))); + } + + /** @dataProvider componentByStatusDataProvider */ + public function testFilterComponentByStatus( + bool $filterStatus, + bool $actualStatus, + bool $expectedResult + ): void { + $boolFilterStub = $this->createConfiguredStub(BoolFilter::class, [ + 'equals' => $filterStatus + ]); + + $componentFilters = new ComponentFilters(activeFilter: $boolFilterStub); + $filterComponentByStatusMethod = $this->getComponentFiltersMethod('filterComponentByStatus'); + $this->assertEquals($expectedResult, $filterComponentByStatusMethod->invoke($componentFilters, $actualStatus)); + } + + public static function componentByStatusDataProvider(): \Generator + { + yield "filter component by true with same module status" => [ + 'filterStatus' => true, + 'actualStatus' => true, + 'expectedResult' => true + ]; + + yield "filter component by false with same module status" => [ + 'filterStatus' => false, + 'actualStatus' => false, + 'expectedResult' => true + ]; + + yield "filter component by true with different module status" => [ + 'filterStatus' => true, + 'actualStatus' => false, + 'expectedResult' => false + ]; + + yield "filter component by false with different theme status" => [ + 'filterStatus' => false, + 'actualStatus' => true, + 'expectedResult' => false + ]; + } + + public function testCreateModuleFilterList(): void + { + $stringFilter = $this->createStub(StringFilter::class); + $boolFilter = $this->createStub(BoolFilter::class); + + $expectedComponentFilters = new ComponentFilters(titleFilter: $stringFilter, activeFilter: $boolFilter); + + $componentFilters = ComponentFilters::createComponentFilters($stringFilter, $boolFilter); + $this->assertEquals($expectedComponentFilters, $componentFilters); + } + + public function testCreateModuleFilterListWithNull(): void + { + $expectedComponentFilters = new ComponentFilters(); + $componentFilters = ComponentFilters::createComponentFilters(null, null); + $this->assertEquals($expectedComponentFilters, $componentFilters); + } + + private static function getComponentFiltersMethod($name) + { + $class = new ReflectionClass(ComponentFilters::class); + $method = $class->getMethod($name); + return $method; + } +} diff --git a/tests/Unit/Shared/Service/ComponentFilterServiceTest.php b/tests/Unit/Shared/Service/ComponentFilterServiceTest.php new file mode 100644 index 0000000..124e8c8 --- /dev/null +++ b/tests/Unit/Shared/Service/ComponentFilterServiceTest.php @@ -0,0 +1,68 @@ +createMock(ComponentFiltersInterface::class); + $componentFiltersMock->method('filterComponent') + ->willReturnCallback(function (ComponentDataTypeInterface $component) use (&$filterResults) { + return array_shift($filterResults); + }); + + $themeFilterService = new ComponentFilterService(); + $componentListResult = $themeFilterService->filterComponents($componentList, $componentFiltersMock); + $this->assertSame($expectedComponentListResult, array_values($componentListResult)); + } + + public static function componentFilterResultProvider(): \Generator + { + $theme1 = self::createStub(ComponentDataTypeInterface::class); + $theme2 = self::createStub(ComponentDataTypeInterface::class); + + yield "filter with filter results are both false" => [ + 'componentList' => [$theme1, $theme2], + 'filterResults' => [false, false], + 'expectedComponentListResult' => [] + ]; + + yield "filter with only first filter result is true" => [ + 'componentList' => [$theme1, $theme2], + 'filterResults' => [true, false], + 'expectedComponentListResult' => [$theme1] + ]; + + yield "filter with only second filter result is true" => [ + 'componentList' => [$theme1, $theme2], + 'filterResults' => [false, true], + 'expectedComponentListResult' => [$theme2] + ]; + + yield "filter with both filter results are true" => [ + 'componentList' => [$theme1, $theme2], + 'filterResults' => [true, true], + 'expectedComponentListResult' => [$theme1, $theme2] + ]; + } +} diff --git a/tests/Unit/Theme/Controller/ThemeListControllerTest.php b/tests/Unit/Theme/Controller/ThemeListControllerTest.php index 20c044c..4d3a75b 100644 --- a/tests/Unit/Theme/Controller/ThemeListControllerTest.php +++ b/tests/Unit/Theme/Controller/ThemeListControllerTest.php @@ -9,11 +9,10 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Theme\Controller; -use OxidEsales\GraphQL\Base\DataType\Filter\BoolFilter; -use OxidEsales\GraphQL\Base\DataType\Filter\StringFilter; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFilters; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Controller\ThemeListController; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeFilters; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Service\ThemeListServiceInterface; use PHPUnit\Framework\TestCase; @@ -24,11 +23,8 @@ class ThemeListControllerTest extends TestCase { public function testThemesListWithFilter(): void { - $theme = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), true); - $themeFilters = new ThemeFilters( - titleFilter: new StringFilter(contains: $theme->getTitle()), - activeFilter: new BoolFilter(equals: true) - ); + $theme = $this->createStub(ThemeDataTypeInterface::class); + $themeFilters = $this->createStub(ComponentFiltersInterface::class); $themeListServiceMock = $this->createMock(ThemeListServiceInterface::class); $themeListServiceMock->expects($this->once()) @@ -39,13 +35,13 @@ public function testThemesListWithFilter(): void $themeListController = new ThemeListController($themeListServiceMock); $resultedThemeList = $themeListController->themesList($themeFilters); - $this->assertSame($resultedThemeList, [$theme]); + $this->assertSame([$theme], $resultedThemeList); } public function testThemesListWithoutFilter(): void { - $theme = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), true); - $themeFilters = new ThemeFilters(); + $theme = $this->createStub(ThemeDataTypeInterface::class); + $themeFilters = new ComponentFilters(); $themeListServiceMock = $this->createMock(ThemeListServiceInterface::class); $themeListServiceMock->expects($this->once()) diff --git a/tests/Unit/Theme/DataType/ThemeDataTypeFactoryTest.php b/tests/Unit/Theme/DataType/ThemeDataTypeFactoryTest.php index 217632e..011e35c 100644 --- a/tests/Unit/Theme/DataType/ThemeDataTypeFactoryTest.php +++ b/tests/Unit/Theme/DataType/ThemeDataTypeFactoryTest.php @@ -10,8 +10,8 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Theme\DataType; use OxidEsales\Eshop\Core\Theme; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactory; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; use PHPUnit\Framework\TestCase; /** @@ -24,15 +24,15 @@ public function testCreateThemeDataType(): void $themeMock = $this->createMock(Theme::class); $expectedTitle = uniqid(); - $expectedIdentifier = uniqid(); + $expectedId = uniqid(); $expectedVersion = uniqid(); $expectedDescription = uniqid(); $expectedActive = true; - $themeMock->expects($this->exactly(5))->method('getInfo') + $themeMock->method('getInfo') ->willReturnMap([ ['title', $expectedTitle], - ['id', $expectedIdentifier], + ['id', $expectedId], ['version', $expectedVersion], ['description', $expectedDescription], ['active', $expectedActive], @@ -43,7 +43,7 @@ public function testCreateThemeDataType(): void $this->assertInstanceOf(ThemeDataType::class, $themeDataType); $this->assertEquals($expectedTitle, $themeDataType->getTitle()); - $this->assertEquals($expectedIdentifier, $themeDataType->getIdentifier()); + $this->assertEquals($expectedId, $themeDataType->getId()); $this->assertEquals($expectedVersion, $themeDataType->getVersion()); $this->assertEquals($expectedDescription, $themeDataType->getDescription()); $this->assertTrue($themeDataType->isActive()); diff --git a/tests/Unit/Theme/DataType/ThemeDataTypeTest.php b/tests/Unit/Theme/DataType/ThemeDataTypeTest.php index 0b9425b..efc5cc3 100644 --- a/tests/Unit/Theme/DataType/ThemeDataTypeTest.php +++ b/tests/Unit/Theme/DataType/ThemeDataTypeTest.php @@ -9,26 +9,28 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Theme\DataType; -use PHPUnit\Framework\TestCase; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; +use PHPUnit\Framework\TestCase; /** - * @covers \OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType + * @covers \OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\AbstractComponentDataType */ class ThemeDataTypeTest extends TestCase { public function testThemeDataType(): void { $name = uniqid(); - $identifier = uniqid(); + $id = uniqid(); $version = uniqid(); $description = uniqid(); $active = true; - $sut = new ThemeDataType($name, $identifier, $version, $description, $active); + $sut = new ThemeDataType($id, $name, $version, $description, $active); + $this->assertInstanceOf(ComponentDataTypeInterface::class, $sut); $this->assertSame($name, $sut->getTitle()); - $this->assertSame($identifier, $sut->getIdentifier()); + $this->assertSame($id, $sut->getId()); $this->assertSame($version, $sut->getVersion()); $this->assertSame($description, $sut->getDescription()); $this->assertSame($active, $sut->isActive()); diff --git a/tests/Unit/Theme/DataType/ThemeFiltersTest.php b/tests/Unit/Theme/DataType/ThemeFiltersTest.php deleted file mode 100644 index 97d0065..0000000 --- a/tests/Unit/Theme/DataType/ThemeFiltersTest.php +++ /dev/null @@ -1,118 +0,0 @@ -createMock(StringFilter::class); - $stringFilterMock->expects($this->once())->method('matches')->with($title)->willReturn($expectedResult); - - $themeMock = $this->createMock(ThemeDataType::class); - $themeMock->expects($this->once())->method('getTitle')->willReturn($title); - - $themeFilters = new ThemeFilters(titleFilter: $stringFilterMock); - $this->assertEquals($expectedResult, $themeFilters->filterThemeByTitle($themeMock)); - } - - public static function themeByTitleDataProvider(): \Generator - { - yield "filter theme by title match" => [ - 'expectedResult' => true, - ]; - - yield "filter theme by title do not match" => [ - 'expectedResult' => false, - ]; - } - - public function testThemeFiltersWithoutFilter(): void - { - $themeMock = $this->createStub(ThemeDataType::class); - - $themeFilters = new ThemeFilters(); - $this->assertTrue($themeFilters->filterThemeByTitle($themeMock)); - $this->assertTrue($themeFilters->filterThemeByStatus($themeMock)); - } - - /** @dataProvider themeByStatusDataProvider */ - public function testFilterThemeByStatus( - bool $filterStatus, - bool $actualThemeStatus, - bool $expectedResult - ): void { - $mockBoolFilter = $this->createMock(BoolFilter::class); - $mockBoolFilter->expects($this->exactly(2))->method('equals')->willReturn($filterStatus); - $themeFilterList = new ThemeFilters(activeFilter: $mockBoolFilter); - - $mockThemeDataType = $this->createMock(ThemeDataType::class); - $mockThemeDataType->expects($this->once())->method('isActive')->willReturn($actualThemeStatus); - - $this->assertSame($expectedResult, $themeFilterList->filterThemeByStatus($mockThemeDataType)); - } - - public static function themeByStatusDataProvider(): \Generator - { - yield "filter theme by true with same theme status" => [ - 'filterStatus' => true, - 'actualThemeStatus' => true, - 'expectedResult' => true - ]; - - yield "filter theme by false with same theme status" => [ - 'filterStatus' => false, - 'actualThemeStatus' => false, - 'expectedResult' => true - ]; - - yield "filter theme by true with different theme status" => [ - 'filterStatus' => true, - 'actualThemeStatus' => false, - 'expectedResult' => false - ]; - - yield "filter theme by false with different theme status" => [ - 'filterStatus' => false, - 'actualThemeStatus' => true, - 'expectedResult' => false - ]; - } - - public function testCreateThemeFilterList(): void - { - $stringFilter = $this->createStub(StringFilter::class); - $boolFilter = $this->createStub(BoolFilter::class); - - $expectedThemeFilters = new ThemeFilters(titleFilter: $stringFilter, activeFilter: $boolFilter); - - $themeFilters = ThemeFilters::createThemeFilters($stringFilter, $boolFilter); - $this->assertEquals($expectedThemeFilters, $themeFilters); - } - - public function testCreateThemeFilterListWithNull(): void - { - $expectedThemeFilters = new ThemeFilters(); - $themeFilters = ThemeFilters::createThemeFilters(null, null); - $this->assertEquals($expectedThemeFilters, $themeFilters); - } -} diff --git a/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php b/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php index bb57880..ee28867 100644 --- a/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php +++ b/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php @@ -10,12 +10,9 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Theme\Infrastructure; use OxidEsales\Eshop\Core\Theme; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\CoreThemeFactoryInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Exception\ThemesNotFound; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructure; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -29,9 +26,7 @@ public function testGetThemes(): void $theme2 = $this->createStub(Theme::class); $coreThemeMock = $this->createMock(Theme::class); - $coreThemeMock->expects($this->once())->method('getList') - ->willReturn([$theme1, $theme2]); - + $coreThemeMock->method('getList')->willReturn([$theme1, $theme2]); $coreThemeFactoryMock = $this->getCoreThemeFactoryMock(returnValue: $coreThemeMock); $sut = $this->getSut(coreThemeFactory: $coreThemeFactoryMock); @@ -42,8 +37,7 @@ public function testGetThemes(): void public function testGetThemesThrowsException(): void { $coreThemeMock = $this->createMock(Theme::class); - $coreThemeMock->expects($this->once())->method('getList') - ->willReturn([]); + $coreThemeMock->method('getList')->willReturn([]); $coreThemeFactoryMock = $this->getCoreThemeFactoryMock(returnValue: $coreThemeMock); $sut = $this->getSut(coreThemeFactory: $coreThemeFactoryMock); @@ -53,18 +47,17 @@ public function testGetThemesThrowsException(): void } private function getSut( - CoreThemeFactoryInterface $coreThemeFactory = null + CoreThemeFactoryInterface $coreThemeFactory ): ThemeListInfrastructure { return new ThemeListInfrastructure( - coreThemeFactory: $coreThemeFactory ?? $this->createStub(CoreThemeFactoryInterface::class) + coreThemeFactory: $coreThemeFactory ); } private function getCoreThemeFactoryMock(Theme $returnValue): CoreThemeFactoryInterface { $coreThemeFactoryMock = $this->createMock(CoreThemeFactoryInterface::class); - $coreThemeFactoryMock->expects($this->once()) - ->method('create') + $coreThemeFactoryMock->method('create') ->willReturn($returnValue); return $coreThemeFactoryMock; diff --git a/tests/Unit/Theme/Infrastructure/ThemeSettingRepositoryGettersTest.php b/tests/Unit/Theme/Infrastructure/ThemeSettingRepositoryGettersTest.php index 1356cb3..4e01ced 100644 --- a/tests/Unit/Theme/Infrastructure/ThemeSettingRepositoryGettersTest.php +++ b/tests/Unit/Theme/Infrastructure/ThemeSettingRepositoryGettersTest.php @@ -12,9 +12,7 @@ use Doctrine\DBAL\ForwardCompatibility\Result; use Doctrine\DBAL\Query\QueryBuilder; use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Shared\Enum\FieldType; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Exception\NoSettingsFoundForThemeException; -use TheCodingMachine\GraphQLite\Types\ID; /** * @covers \OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeSettingRepository diff --git a/tests/Unit/Theme/Service/ThemeFilterServiceTest.php b/tests/Unit/Theme/Service/ThemeFilterServiceTest.php deleted file mode 100644 index 9539920..0000000 --- a/tests/Unit/Theme/Service/ThemeFilterServiceTest.php +++ /dev/null @@ -1,91 +0,0 @@ -createMock(ThemeFiltersInterface::class); - $themeFiltersMock->expects($this->exactly(count($titleFilterResults)))->method('filterThemeByTitle') - ->willReturnCallback(function (ThemeDataType $theme) use (&$titleFilterResults) { - return array_shift($titleFilterResults); - }); - $themeFiltersMock->expects($this->exactly(count($statusFilterResults)))->method('filterThemeByStatus') - ->willReturnCallback(function (ThemeDataType $theme) use (&$statusFilterResults) { - return array_shift($statusFilterResults); - }); - - $themeFilterService = new ThemeFilterService(); - $themeListResult = $themeFilterService->filterThemes($themeList, $themeFiltersMock); - $this->assertCount(count($expectedThemeListResult), $themeListResult); - foreach ($expectedThemeListResult as $theme) { - $this->assertContains($theme, $themeListResult); - } - } - - /** - * Keep in mind that the status filter check for a theme will not be done if title - * check is already false. Only if title check is true, the status check is done. - */ - public static function themeFilterResultProvider(): \Generator - { - $theme1 = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), true); - $theme2 = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), false); - - yield "filter with only second title check is true but status check is false" => [ - 'themeList' => [$theme1, $theme2], - 'titleFilterResults' => [false, true], - 'statusFilterResults' => [false], - 'expectedThemeListResult' => [] - ]; - - yield "filter with both title checks are false and status check is not done at all" => [ - 'themeList' => [$theme1, $theme2], - 'titleFilterResults' => [false, false], - 'statusFilterResults' => [], - 'expectedThemeListResult' => [] - ]; - - yield "filter with only first title and status check is true" => [ - 'themeList' => [$theme1, $theme2], - 'titleFilterResults' => [true, false], - 'statusFilterResults' => [true], - 'expectedThemeListResult' => [$theme1] - ]; - - yield "filter with only second title and status check is true" => [ - 'themeList' => [$theme1, $theme2], - 'titleFilterResults' => [false, true], - 'statusFilterResults' => [true], - 'expectedThemeListResult' => [$theme2] - ]; - - yield "filter with both title and status checks are true" => [ - 'themeList' => [$theme1, $theme2], - 'titleFilterResults' => [true, true], - 'statusFilterResults' => [true, true], - 'expectedThemeListResult' => [$theme1, $theme2] - ]; - } -} diff --git a/tests/Unit/Theme/Service/ThemeListServiceTest.php b/tests/Unit/Theme/Service/ThemeListServiceTest.php index d51c731..e729e47 100644 --- a/tests/Unit/Theme/Service/ThemeListServiceTest.php +++ b/tests/Unit/Theme/Service/ThemeListServiceTest.php @@ -9,13 +9,11 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Theme\Service; -use OxidEsales\GraphQL\Base\DataType\Filter\BoolFilter; -use OxidEsales\GraphQL\Base\DataType\Filter\StringFilter; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\ComponentFilterServiceInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeFilters; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructureInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\Service\ThemeFilterServiceInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Service\ThemeListService; use PHPUnit\Framework\TestCase; use OxidEsales\Eshop\Core\Theme; @@ -27,41 +25,39 @@ class ThemeListServiceTest extends TestCase { public function testGetThemeListWithFilters(): void { - $themes = [new Theme(), new Theme()]; - $theme1 = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), true); - $theme2 = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), false); - $themeList = [$theme1, $theme2]; - $filteredThemeList = [$theme1]; + $theme1 = $this->createStub(Theme::class); + $theme2 = $this->createStub(Theme::class); + $themes = [$theme1, $theme2]; + $themeDataType1 = $this->createStub(ThemeDataTypeInterface::class); + $themeDataType2 = $this->createStub(ThemeDataTypeInterface::class); + $themeList = [$themeDataType1, $themeDataType2]; + $filteredThemeList = [$themeDataType1]; $themeListInfrastructureMock = $this->createMock(ThemeListInfrastructureInterface::class); - $themeListInfrastructureMock->expects($this->once())->method('getThemes') - ->willReturn($themes); + $themeListInfrastructureMock->method('getThemes')->willReturn($themes); $themeDataTypeFactoryMock = $this->createMock(ThemeDataTypeFactoryInterface::class); $themeDataTypeFactoryMock ->expects($this->exactly(2)) ->method('createFromCoreTheme') ->willReturnMap([ - [$themes[0], $themeList[0]], - [$themes[1], $themeList[1]] + [$theme1, $themeDataType1], + [$theme2, $themeDataType2] ]); - $filtersList = new ThemeFilters( - titleFilter: new StringFilter(contains: uniqid()), - activeFilter: new BoolFilter(equals: (bool)rand(0, 1)) - ); + $componentFiltersStub = $this->createStub(ComponentFiltersInterface::class); - $themeFilterServiceMock = $this->createMock(ThemeFilterServiceInterface::class); - $themeFilterServiceMock->expects($this->once())->method('filterThemes') - ->with($themeList, $filtersList) + $componentFilterServiceMock = $this->createMock(ComponentFilterServiceInterface::class); + $componentFilterServiceMock->method('filterComponents') + ->with($themeList, $componentFiltersStub) ->willReturn($filteredThemeList); $themeListService = new ThemeListService( themeListInfrastructure: $themeListInfrastructureMock, - themeFilterService: $themeFilterServiceMock, + componentFilterService: $componentFilterServiceMock, themeDataTypeFactory: $themeDataTypeFactoryMock ); - $actualThemes = $themeListService->getThemeList($filtersList); + $actualThemes = $themeListService->getThemeList($componentFiltersStub); $this->assertSame($filteredThemeList, $actualThemes); } }