Skip to content

Commit

Permalink
OXDEV-7557 Cleanup and simplify Shop and Theme controller tests
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 16, 2023
1 parent 39f715a commit b52882d
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 407 deletions.
9 changes: 6 additions & 3 deletions tests/Unit/Controller/ModuleSettingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\StringSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ModuleSettingServiceInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
use PHPUnit\Framework\TestCase;
use TheCodingMachine\GraphQLite\Types\ID;

class ModuleSettingControllerTest extends UnitTestCase
/**
* @covers \OxidEsales\GraphQL\ConfigurationAccess\Setting\Controller\ModuleSettingController
*/
class ModuleSettingControllerTest extends TestCase
{
/** @dataProvider proxyTestDataProvider */
public function testControllerProxiesTheDataToServiceAndReturnsItsResult(
public function testControllerProxiesParametersToServiceAndReturnsItsResult(
string $controllerMethod,
string $serviceMethod,
array $params,
Expand Down
205 changes: 81 additions & 124 deletions tests/Unit/Controller/ShopSettingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,140 +3,97 @@
namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Controller;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\Controller\ShopSettingController;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\BooleanSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\FloatSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\IntegerSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\SettingType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\StringSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ShopSettingServiceInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
use PHPUnit\Framework\TestCase;
use TheCodingMachine\GraphQLite\Types\ID;

class ShopSettingControllerTest extends UnitTestCase
/**
* @covers \OxidEsales\GraphQL\ConfigurationAccess\Setting\Controller\ShopSettingController
*/
class ShopSettingControllerTest extends TestCase
{
public function testGetShopSettingInteger(): void
{
$serviceIntegerSetting = $this->getIntegerSetting();

$settingService = $this->createMock(ShopSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getIntegerSetting')
->willReturn($serviceIntegerSetting);

$settingController = new ShopSettingController($settingService);

$nameID = new ID('integerSetting');
$integerSetting = $settingController->getShopSettingInteger($nameID);

$this->assertSame($serviceIntegerSetting, $integerSetting);
}

public function testGetShopSettingFloat(): void
{
$serviceFloatSetting = $this->getFloatSetting();

$settingService = $this->createMock(ShopSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getFloatSetting')
->willReturn($serviceFloatSetting);

$settingController = new ShopSettingController($settingService);

$nameID = new ID('floatSetting');
$floatSetting = $settingController->getShopSettingFloat($nameID);

$this->assertSame($serviceFloatSetting, $floatSetting);
}

public function testGetShopSettingBoolean(): void
{
$serviceBooleanSetting = $this->getNegativeBooleanSetting();

/** @dataProvider proxyTestDataProvider */
public function testControllerProxiesParametersToServiceAndReturnsItsResult(
string $controllerMethod,
string $serviceMethod,
array $params,
$expectedValue
): void {
$settingService = $this->createMock(ShopSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getBooleanSetting')
->willReturn($serviceBooleanSetting);
$settingService->expects($this->once())->method($serviceMethod)->with(...$params)->willReturn($expectedValue);

$settingController = new ShopSettingController($settingService);

$nameID = new ID('booleanSetting');
$booleanSetting = $settingController->getShopSettingBoolean($nameID);

$this->assertSame($serviceBooleanSetting, $booleanSetting);
}

public function testGetShopSettingString(): void
{
$serviceStringSetting = $this->getStringSetting();

$settingService = $this->createMock(ShopSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getStringSetting')
->willReturn($serviceStringSetting);

$settingController = new ShopSettingController($settingService);

$nameID = new ID('stringSetting');
$stringSetting = $settingController->getShopSettingString($nameID);

$this->assertSame($serviceStringSetting, $stringSetting);
$this->assertSame($expectedValue, $settingController->$controllerMethod(...$params));
}

public function testGetShopSettingSelect(): void
public function proxyTestDataProvider(): \Generator
{
$serviceSelectSetting = $this->getSelectSetting();

$settingService = $this->createMock(ShopSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getSelectSetting')
->willReturn($serviceSelectSetting);

$settingController = new ShopSettingController($settingService);

$nameID = new ID('selectSetting');
$selectSetting = $settingController->getShopSettingSelect($nameID);

$this->assertSame($serviceSelectSetting, $selectSetting);
}

public function testGetShopSettingCollection(): void
{
$serviceCollectionSetting = $this->getCollectionSetting();

$settingService = $this->createMock(ShopSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getCollectionSetting')
->willReturn($serviceCollectionSetting);

$settingController = new ShopSettingController($settingService);

$nameID = new ID('arraySetting');
$collectionSetting = $settingController->getShopSettingCollection($nameID);

$this->assertSame($serviceCollectionSetting, $collectionSetting);
}

public function testGetShopSettingAssocCollection(): void
{
$serviceAssocCollectionSetting = $this->getAssocCollectionSetting();

$settingService = $this->createMock(ShopSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getAssocCollectionSetting')
->willReturn($serviceAssocCollectionSetting);

$settingController = new ShopSettingController($settingService);

$nameID = new ID('aarraySetting');
$assocCollectionSetting = $settingController->getShopSettingAssocCollection($nameID);

$this->assertSame($serviceAssocCollectionSetting, $assocCollectionSetting);
}

public function testListShopSettings(): void
{
$serviceSettingsList = $this->getSettingTypeList();
$settingService = $this->createMock(ShopSettingServiceInterface::class);
$settingService->expects($this->once())
->method('getSettingsList')
->willReturn($serviceSettingsList);

$sut = new ShopSettingController($settingService);
$this->assertSame($serviceSettingsList, $sut->getShopSettingsList());
$settingNameID = new ID('settingName');

yield 'getter integer' => [
'controllerMethod' => 'getShopSettingInteger',
'serviceMethod' => 'getIntegerSetting',
'params' => [$settingNameID],
'expectedValue' => new IntegerSetting($settingNameID, 123)
];

yield 'getter float' => [
'controllerMethod' => 'getShopSettingFloat',
'serviceMethod' => 'getFloatSetting',
'params' => [$settingNameID],
'expectedValue' => new FloatSetting($settingNameID, 1.23)
];

yield 'getter bool' => [
'controllerMethod' => 'getShopSettingBoolean',
'serviceMethod' => 'getBooleanSetting',
'params' => [$settingNameID],
'expectedValue' => new BooleanSetting($settingNameID, false)
];

yield 'getter string' => [
'controllerMethod' => 'getShopSettingString',
'serviceMethod' => 'getStringSetting',
'params' => [$settingNameID],
'expectedValue' => new StringSetting($settingNameID, 'default')
];

yield 'getter select' => [
'controllerMethod' => 'getShopSettingSelect',
'serviceMethod' => 'getSelectSetting',
'params' => [$settingNameID],
'expectedValue' => new StringSetting($settingNameID, 'some select setting value')
];

yield 'getter collection' => [
'controllerMethod' => 'getShopSettingCollection',
'serviceMethod' => 'getCollectionSetting',
'params' => [$settingNameID],
'expectedValue' => new StringSetting($settingNameID, 'some collection string example')
];

yield 'getter associative collection' => [
'controllerMethod' => 'getShopSettingAssocCollection',
'serviceMethod' => 'getAssocCollectionSetting',
'params' => [$settingNameID],
'expectedValue' => new StringSetting($settingNameID, 'some associative collection string example')
];

yield 'setting list getter' => [
'controllerMethod' => 'getShopSettingsList',
'serviceMethod' => 'getSettingsList',
'params' => [],
'expectedValue' => [
new SettingType($settingNameID, FieldType::NUMBER),
new SettingType($settingNameID, FieldType::NUMBER),
]
];
}
}
Loading

0 comments on commit b52882d

Please sign in to comment.