Skip to content

Commit

Permalink
OXDEV-8215: Refactor the tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid committed Jul 30, 2024
1 parent b0b2051 commit 3f2c331
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 78 deletions.
34 changes: 7 additions & 27 deletions tests/Unit/Module/Controller/ModuleListControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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\DataType\ModuleFiltersInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleListServiceInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
Expand All @@ -23,39 +22,20 @@ class ModuleListControllerTest extends UnitTestCase
{
public function testModulesListWithFilters(): void
{
$filtersMock = $this->createMock(ModuleFilters::class);
$moduleMock1 = $this->createMock(ModuleDataType::class);
$moduleMock2 = $this->createMock(ModuleDataType::class);
$filteredModules = [$moduleMock1, $moduleMock2];
$filtersStub = $this->createStub(ModuleFiltersInterface::class);
$moduleStub1 = $this->createStub(ModuleDataType::class);
$moduleStub2 = $this->createStub(ModuleDataType::class);
$filteredModules = [$moduleStub1, $moduleStub2];

$moduleListServiceMock = $this->createMock(ModuleListServiceInterface::class);
$moduleListServiceMock
$moduleListServiceMock->expects($this->once())
->method('getModuleList')
->with($filtersMock)
->with($filtersStub)
->willReturn($filteredModules);

$sut = new ModuleListController($moduleListServiceMock);
$actualModules = $sut->modulesList($filtersMock);
$actualModules = $sut->modulesList($filtersStub);

$this->assertSame($filteredModules, $actualModules);
}

public function testModulesListWithoutFilters(): void
{
$filtersMock = $this->createMock(ModuleFilters::class);
$moduleMock1 = $this->createMock(ModuleDataType::class);
$moduleMock2 = $this->createMock(ModuleDataType::class);
$expectedModules = [$moduleMock1, $moduleMock2];

$moduleListServiceMock = $this->createMock(ModuleListServiceInterface::class);
$moduleListServiceMock
->method('getModuleList')
->with($filtersMock)
->willReturn($expectedModules);

$sut = new ModuleListController($moduleListServiceMock);
$actualModules = $sut->modulesList($filtersMock);

$this->assertSame($expectedModules, $actualModules);
}
}
61 changes: 41 additions & 20 deletions tests/Unit/Module/DataType/ModuleFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class ModuleFiltersTest extends TestCase
/** @dataProvider moduleByTitleDataProvider */
public function testFilterModuleByTitle(
bool $expectedResult,
bool $enableFilters,
): void {
$title = uniqid();
$stringFilterMock = $this->createMock(StringFilter::class);
Expand All @@ -32,66 +31,88 @@ public function testFilterModuleByTitle(
$moduleMock = $this->createMock(ModuleDataType::class);
$moduleMock->expects($this->once())->method('getTitle')->willReturn($title);

$moduleFilters = ($enableFilters) ? new ModuleFilters(titleFilter: $stringFilterMock) : new ModuleFilters();
$this->assertEquals($expectedResult, $moduleFilters->filterModuleByTitle($moduleMock));
$themeFilters = new ModuleFilters(titleFilter: $stringFilterMock);
$this->assertEquals($expectedResult, $themeFilters->filterModuleByTitle($moduleMock));
}

public static function moduleByTitleDataProvider(): \Generator
{
yield "filter module by title matches" => [
'expectedResult' => true,
'enableFilters' => true
];

yield "filter module by title do not matches" => [
'expectedResult' => false,
'enableFilters' => true
];
}

yield "filter module by title no filters" => [
'expectedTitle' => 'test module 1',
'expectedResult' => true,
'enableFilters' => false,
];
public function testModuleFiltersWithoutFilter(): void
{
$themeMock = $this->createStub(ModuleDataType::class);

$themeFilters = new ModuleFilters();
$this->assertTrue($themeFilters->filterModuleByTitle($themeMock));
$this->assertTrue($themeFilters->filterModuleByStatus($themeMock));
}

/** @dataProvider moduleByStatusDataProvider */
public function testFilterModuleByStatus(
bool $expectedStatus,
bool $filterStatus,
bool $actualStatus,
bool $expectedResult
): void {
$mockBoolFilter = $this->createMock(BoolFilter::class);
$mockBoolFilter->method('equals')->willReturn($expectedStatus);
$mockBoolFilter->expects($this->exactly(2))->method('equals')->willReturn($filterStatus);
$moduleFilterList = new ModuleFilters(activeFilter: $mockBoolFilter);

$mockModuleDataType = $this->createMock(ModuleDataType::class);
$mockModuleDataType->method('isActive')->willReturn($actualStatus);
$mockModuleDataType->expects($this->once())->method('isActive')->willReturn($actualStatus);

$this->assertSame($expectedResult, $moduleFilterList->filterModuleByStatus($mockModuleDataType));
}

public static function moduleByStatusDataProvider(): \Generator
{
yield "filter module by providing same module status" => [
'expectedStatus' => true,
yield "filter module by true with same module status" => [
'filterStatus' => true,
'actualStatus' => true,
'expectedResult' => true
];

yield "filter module by providing different module status" => [
'expectedStatus' => 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->createMock(BoolFilter::class);
$boolFilter = $this->createStub(BoolFilter::class);

$moduleFilterListSpy = ModuleFilters::createModuleFilters($stringFilter, $boolFilter);
$this->assertInstanceOf(ModuleFilters::class, $moduleFilterListSpy);
$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);
}
}
38 changes: 9 additions & 29 deletions tests/Unit/Module/Service/ModuleListServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,31 @@ class ModuleListServiceTest extends UnitTestCase
{
public function testGetModuleListWithFilters(): void
{
$filtersMock = $this->createMock(ModuleFiltersInterface::class);
$moduleMock1 = $this->createMock(ModuleDataType::class);
$moduleMock2 = $this->createMock(ModuleDataType::class);
$filteredModules = [$moduleMock1];
$filtersStub = $this->createStub(ModuleFiltersInterface::class);
$moduleStub1 = $this->createStub(ModuleDataType::class);
$moduleStub2 = $this->createStub(ModuleDataType::class);
$filteredModules = [$moduleStub1];

$moduleListInfrastructureMock = $this->createMock(ModuleListInfrastructureInterface::class);
$moduleListInfrastructureMock->method('getModuleList')
->willReturn([$moduleMock1, $moduleMock2]);
$moduleListInfrastructureMock->expects($this->once())->method('getModuleList')
->willReturn([$moduleStub1, $moduleStub2]);

$moduleFilterServiceMock = $this->createMock(ModuleFilterServiceInterface::class);

$moduleFilterServiceMock
$moduleFilterServiceMock->expects($this->once())
->method('filterModules')
->with([$moduleMock1, $moduleMock2], $filtersMock)
->with([$moduleStub1, $moduleStub2], $filtersStub)
->willReturn($filteredModules);

$sut = $this->getSut(
moduleListInfrastructureMock: $moduleListInfrastructureMock,
moduleFilterServiceMock: $moduleFilterServiceMock
);

$actualModules = $sut->getModuleList($filtersMock);
$actualModules = $sut->getModuleList($filtersStub);
$this->assertSame($filteredModules, $actualModules);
}

public function testGetModuleListNoModules(): void
{
$filtersMock = $this->createMock(ModuleFiltersInterface::class);
$moduleListInfrastructureMock = $this->createMock(ModuleListInfrastructureInterface::class);
$moduleFilterServiceMock = $this->createMock(ModuleFilterServiceInterface::class);

$moduleFilterServiceMock
->method('filterModules')
->with([], $filtersMock)
->willReturn([]);

$sut = $this->getSut(
moduleListInfrastructureMock: $moduleListInfrastructureMock,
moduleFilterServiceMock: $moduleFilterServiceMock
);

$actualModules = $sut->getModuleList($filtersMock);
$this->assertSame([], $actualModules);
}

public function getSut(
?ModuleListInfrastructureInterface $moduleListInfrastructureMock = null,
?ModuleFilterServiceInterface $moduleFilterServiceMock = null
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Theme/DataType/ThemeFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function testFilterThemeByTitle(

public static function themeByTitleDataProvider(): \Generator
{
yield "filter theme by titles matches" => [
yield "filter theme by title match" => [
'expectedResult' => true,
];

yield "filter theme by titles do not matches" => [
yield "filter theme by title do not match" => [
'expectedResult' => false,
];
}
Expand Down

0 comments on commit 3f2c331

Please sign in to comment.