Skip to content

Commit

Permalink
OXDEV-7573 Refactor new tests to one data provider
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 4f3a287 commit 7eff591
Showing 1 changed file with 155 additions and 144 deletions.
299 changes: 155 additions & 144 deletions tests/Unit/Infrastructure/ShopSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

class ShopSettingRepositoryTest extends UnitTestCase
{
/** @dataProvider possibleIntegerValuesDataProvider */
public function testGetShopSettingInteger($possibleValue, $expectedResult): void
/** @dataProvider possibleGetterValuesDataProvider */
public function testGetShopSetting($method, $type, $possibleValue, $expectedResult): void
{
$settingName = 'settingName';
$shopId = 3;
Expand All @@ -35,7 +35,7 @@ public function testGetShopSettingInteger($possibleValue, $expectedResult): void
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => FieldType::NUMBER,
'getType' => $type,
'getValue' => $possibleValue
])
);
Expand All @@ -48,48 +48,165 @@ public function testGetShopSettingInteger($possibleValue, $expectedResult): void
shopSettingDao: $shopSettingDaoStub
);

$this->assertSame($expectedResult, $sut->getInteger($settingName));
$this->assertSame($expectedResult, $sut->$method($settingName));
}

public function possibleIntegerValuesDataProvider(): \Generator
public function possibleGetterValuesDataProvider(): \Generator
{
yield ['possibleValue' => 123, 'expectedResult' => 123];
yield ['possibleValue' => '123', 'expectedResult' => 123];
}

/** @dataProvider possibleFloatValuesDataProvider */
public function testGetShopSettingFloat($possibleValue, $expectedResult): void
{
$settingName = 'settingName';
$shopId = 3;

$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')
->with($settingName, $shopId)
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => FieldType::NUMBER,
'getValue' => $possibleValue
])
);
yield 'int regular' => [
'method' => 'getInteger',
'type' => FieldType::NUMBER,
'possibleValue' => 123,
'expectedResult' => 123
];
yield 'int in string' => [
'method' => 'getInteger',
'type' => FieldType::NUMBER,
'possibleValue' => '123',
'expectedResult' => 123
];

$basicContext = $this->createStub(BasicContextInterface::class);
$basicContext->method('getCurrentShopId')->willReturn($shopId);
yield 'float regular' => [
'method' => 'getFloat',
'type' => FieldType::NUMBER,
'possibleValue' => 123.2,
'expectedResult' => 123.2
];
yield 'float from integer' => [
'method' => 'getFloat',
'type' => FieldType::NUMBER,
'possibleValue' => 123,
'expectedResult' => 123.0
];
yield 'float from string' => [
'method' => 'getFloat',
'type' => FieldType::NUMBER,
'possibleValue' => '123',
'expectedResult' => 123.0
];

$sut = $this->getSut(
basicContext: $basicContext,
shopSettingDao: $shopSettingDaoStub
);
yield [
'method' => 'getBoolean',
'type' => FieldType::BOOLEAN,
'possibleValue' => 1,
'expectedResult' => true
];
yield [
'method' => 'getBoolean',
'type' => FieldType::BOOLEAN,
'possibleValue' => '1',
'expectedResult' => true
];
yield [
'method' => 'getBoolean',
'type' => FieldType::BOOLEAN,
'possibleValue' => true,
'expectedResult' => true
];
yield [
'method' => 'getBoolean',
'type' => FieldType::BOOLEAN,
'possibleValue' => 'anything',
'expectedResult' => true
];
yield [
'method' => 'getBoolean',
'type' => FieldType::BOOLEAN,
'possibleValue' => null,
'expectedResult' => false
];
yield [
'method' => 'getBoolean',
'type' => FieldType::BOOLEAN,
'possibleValue' => 0,
'expectedResult' => false
];
yield [
'method' => 'getBoolean',
'type' => FieldType::BOOLEAN,
'possibleValue' => '0',
'expectedResult' => false
];
yield [
'method' => 'getBoolean',
'type' => FieldType::BOOLEAN,
'possibleValue' => false,
'expectedResult' => false
];

$this->assertSame($expectedResult, $sut->getFloat($settingName));
}
yield [
'method' => 'getString',
'type' => FieldType::STRING,
'possibleValue' => 1,
'expectedResult' => '1'
];
yield [
'method' => 'getString',
'type' => FieldType::STRING,
'possibleValue' => '1',
'expectedResult' => '1'
];
yield [
'method' => 'getString',
'type' => FieldType::STRING,
'possibleValue' => 'regular',
'expectedResult' => 'regular'
];
yield [
'method' => 'getString',
'type' => FieldType::STRING,
'possibleValue' => '0',
'expectedResult' => '0'
];
yield [
'method' => 'getString',
'type' => FieldType::STRING,
'possibleValue' => null,
'expectedResult' => ''
];
yield [
'method' => 'getString',
'type' => FieldType::STRING,
'possibleValue' => '',
'expectedResult' => ''
];

public function possibleFloatValuesDataProvider(): \Generator
{
yield ['possibleValue' => 123.2, 'expectedResult' => 123.2];
yield ['possibleValue' => 123, 'expectedResult' => 123.0];
yield ['possibleValue' => '123', 'expectedResult' => 123.0];
yield [
'method' => 'getSelect',
'type' => FieldType::SELECT,
'possibleValue' => 1,
'expectedResult' => '1'
];
yield [
'method' => 'getSelect',
'type' => FieldType::SELECT,
'possibleValue' => '1',
'expectedResult' => '1'
];
yield [
'method' => 'getSelect',
'type' => FieldType::SELECT,
'possibleValue' => 'regular',
'expectedResult' => 'regular'
];
yield [
'method' => 'getSelect',
'type' => FieldType::SELECT,
'possibleValue' => '0',
'expectedResult' => '0'
];
yield [
'method' => 'getSelect',
'type' => FieldType::SELECT,
'possibleValue' => null,
'expectedResult' => ''
];
yield [
'method' => 'getSelect',
'type' => FieldType::SELECT,
'possibleValue' => '',
'expectedResult' => ''
];
}

/** @dataProvider wrongSettingsDataProvider */
Expand Down Expand Up @@ -188,112 +305,6 @@ public function wrongSettingsDataProvider(): \Generator
];
}

/** @dataProvider possibleBoolValuesDataProvider */
public function testGetShopSettingBoolean($possibleValue, $expectedResult): void
{
$settingName = 'settingName';
$shopId = 3;

$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')
->with($settingName, $shopId)
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => FieldType::BOOLEAN,
'getValue' => $possibleValue
])
);

$basicContext = $this->createStub(BasicContextInterface::class);
$basicContext->method('getCurrentShopId')->willReturn($shopId);

$sut = $this->getSut(
basicContext: $basicContext,
shopSettingDao: $shopSettingDaoStub
);

$this->assertSame($expectedResult, $sut->getBoolean($settingName));
}

public function possibleBoolValuesDataProvider(): \Generator
{
yield ['possibleValue' => 1, 'expectedResult' => true];
yield ['possibleValue' => '1', 'expectedResult' => true];
yield ['possibleValue' => true, 'expectedResult' => true];
yield ['possibleValue' => 'anything', 'expectedResult' => true];
yield ['possibleValue' => null, 'expectedResult' => false];
yield ['possibleValue' => 0, 'expectedResult' => false];
yield ['possibleValue' => '0', 'expectedResult' => false];
yield ['possibleValue' => false, 'expectedResult' => false];
}

/** @dataProvider possibleStringValuesDataProvider */
public function testGetShopSettingString($possibleValue, $expectedResult): void
{
$settingName = 'settingName';
$shopId = 3;

$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')
->with($settingName, $shopId)
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => FieldType::STRING,
'getValue' => $possibleValue
])
);

$basicContext = $this->createStub(BasicContextInterface::class);
$basicContext->method('getCurrentShopId')->willReturn($shopId);

$sut = $this->getSut(
basicContext: $basicContext,
shopSettingDao: $shopSettingDaoStub
);

$this->assertSame($expectedResult, $sut->getString($settingName));
}

public function possibleStringValuesDataProvider(): \Generator
{
yield ['possibleValue' => 1, 'expectedResult' => '1'];
yield ['possibleValue' => '1', 'expectedResult' => '1'];
yield ['possibleValue' => 'regular', 'expectedResult' => 'regular'];
yield ['possibleValue' => '0', 'expectedResult' => '0'];
yield ['possibleValue' => null, 'expectedResult' => ''];
yield ['possibleValue' => '', 'expectedResult' => ''];
}

/** @dataProvider possibleStringValuesDataProvider */
public function testGetShopSettingSelect($possibleValue, $expectedResult): void
{
$settingName = 'settingName';
$shopId = 3;

$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')
->with($settingName, $shopId)
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => FieldType::SELECT,
'getValue' => $possibleValue
])
);

$basicContext = $this->createStub(BasicContextInterface::class);
$basicContext->method('getCurrentShopId')->willReturn($shopId);

$sut = $this->getSut(
basicContext: $basicContext,
shopSettingDao: $shopSettingDaoStub
);

$this->assertSame($expectedResult, $sut->getSelect($settingName));
}

public function testGetShopSettingCollection(): void
{
$nameID = new ID('arraySetting');
Expand Down

0 comments on commit 7eff591

Please sign in to comment.