Commit 5bf0263f authored by matthieu-rolland's avatar matthieu-rolland
Browse files

improve tests

parent 6ac48680
......@@ -27,6 +27,7 @@
namespace Tests\Integration\Adapter;
use Doctrine\ORM\EntityManager;
use Generator;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use PrestaShopBundle\Entity\Shop;
......@@ -60,134 +61,207 @@ class ConfigurationTest extends KernelTestCase
* @param array $setParams
* @param array $getParams
* @param $expectedResult
* @param bool $isLastTest
*
* @dataProvider getProvider
*/
public function testGet(array $setParams, array $getParams, $expectedResult, bool $isLastTest): void
public function testGet(array $setParams, array $getParams, $expectedResult): void
{
foreach ($setParams as $values) {
$this->configuration->set($values['key'], $values['value'], $values['shopConstraint']);
}
$result = $this->configuration->get($getParams['key'], $getParams['default'], $getParams['shopConstraint']);
$this->assertEquals($expectedResult, $result);
$this->setAndGetValuesForTesting($setParams, $getParams, $expectedResult);
}
if ($isLastTest) {
$this->cleanDb();
}
/**
* @param array $setParams
* @param array $getParams
* @param $expectedResult
*
* @dataProvider getWithStrictParameterProvider
*/
public function testGetWithSrictParameter(array $setParams, array $getParams, $expectedResult)
{
$this->setAndGetValuesForTesting($setParams, $getParams, $expectedResult);
}
/**
* @return array[]
* @return Generator
*/
public function getProvider(): array
public function getProvider(): Generator
{
return [
// simple case: get an all shop config value
// simple case: get an all shop config value
yield [
[
[
[
'key' => 'key_test_1',
'value' => 'value_test_1',
'shopConstraint' => ShopConstraint::allShops(),
],
],
[
'key' => 'key_test_1',
'default' => false,
'value' => 'value_test_1',
'shopConstraint' => ShopConstraint::allShops(),
],
'value_test_1',
false,
],
// set and get a value for shop 2
[
[
[
'key' => 'key_test_1',
'value' => 'value_test_1',
'shopConstraint' => ShopConstraint::shop(2),
],
],
[
'key' => 'key_test_1',
'default' => false,
'shopConstraint' => ShopConstraint::shop(2),
],
'value_test_1',
false,
'key' => 'key_test_1',
'default' => false,
'shopConstraint' => ShopConstraint::allShops(),
],
// try to get a non existing value for shop 2, get default value instead
'value_test_1',
];
// simple case: get a group shop config value
yield [
[
[],
[
'key' => 'does_not_exist',
'default' => 'default_value',
'key' => 'key_test_2',
'value' => 'value_test_2',
'shopConstraint' => ShopConstraint::shop(2),
],
'default_value',
false,
],
// try to get a non existing value for group 1, get default value instead
[
[],
'key' => 'key_test_2',
'default' => false,
'shopConstraint' => ShopConstraint::shop(2),
],
'value_test_2',
];
// simple case: get a single shop config value
yield [
[
[
'key' => 'does_not_exist',
'default' => 'default_value',
'key' => 'key_test_3',
'value' => 'value_test_3',
'shopConstraint' => ShopConstraint::shopGroup(1),
],
'default_value',
false,
],
// get value for shop 2, inherited from parent group shop
[
'key' => 'key_test_3',
'default' => false,
'shopConstraint' => ShopConstraint::shopGroup(1),
],
'value_test_3',
];
// try to get a non existing value for all shop, get default value instead
yield [
[],
[
'key' => 'does_not_exist',
'default' => 'default_value_all_shop',
'shopConstraint' => ShopConstraint::allShops(),
],
'default_value_all_shop',
];
// try to get a non existing value for group shop, get default value instead
yield [
[],
[
'key' => 'does_not_exist',
'default' => 'default_value',
'shopConstraint' => ShopConstraint::shopGroup(1),
],
'default_value',
];
// try to get a non existing value for single shop, get default value instead
yield [
[],
[
'key' => 'does_not_exist',
'default' => 'default_value',
'shopConstraint' => ShopConstraint::shop(2),
],
'default_value',
];
// get value for a group shop, inherited from all shop
yield [
[
[
[
'key' => 'parent_group_key_2',
'value' => 'parent_group_value',
'shopConstraint' => ShopConstraint::shopGroup(1),
],
],
[
'key' => 'parent_group_key_2',
'default' => false,
'shopConstraint' => ShopConstraint::shop(2),
'key' => 'all_shop_key_1',
'value' => 'all_shop_value_1',
'shopConstraint' => ShopConstraint::allShops(),
],
'parent_group_value',
false,
],
// try getting a non existing value for shop 2, with is strict = true => should not inherit from parent group
[
'key' => 'all_shop_key_1',
'default' => false,
'shopConstraint' => ShopConstraint::shopGroup(1),
],
'all_shop_value_1',
];
// get value for shop 2, inherited from parent group shop
yield [
[
[
[
'key' => 'parent_group_key_3',
'value' => 'parent_group_value',
'shopConstraint' => ShopConstraint::shopGroup(1),
],
'key' => 'parent_group_key_1',
'value' => 'parent_group_value',
'shopConstraint' => ShopConstraint::shopGroup(1),
],
],
[
'key' => 'parent_group_key_1',
'default' => false,
'shopConstraint' => ShopConstraint::shop(2),
],
'parent_group_value',
];
}
/**
* @return Generator
*/
public function getWithStrictParameterProvider(): Generator
{
// try getting a non existing value for a aingle shop, with is strict = true => should not inherit from parent group
yield [
[
[
'key' => 'parent_group_key_3',
'default' => false,
'shopConstraint' => ShopConstraint::shop(2, true),
'key' => 'parent_group_key_2',
'value' => 'parent_group_value_2',
'shopConstraint' => ShopConstraint::shopGroup(1),
],
null,
false,
],
// try getting a non existing value for all shop, with is strict = true => should return null
[
[],
'key' => 'parent_group_key_2',
'default' => false,
'shopConstraint' => ShopConstraint::shop(2, true),
],
null,
];
// try getting a non existing value for a group, with is strict = true => should not inherit from all shop
yield [
[
[
'key' => 'does_not_exist',
'default' => false,
'shopConstraint' => ShopConstraint::allShops(true),
'key' => 'all_shop_key_1',
'value' => 'all_shop_value_1',
'shopConstraint' => ShopConstraint::allShops(),
],
null,
true,
],
[
'key' => 'all_shop_key_1',
'default' => false,
'shopConstraint' => ShopConstraint::shopGroup(1, true),
],
null,
];
// try getting a non existing value for all shop, with is strict = true => should return null
yield [
[],
[
'key' => 'does_not_exist',
'default' => false,
'shopConstraint' => ShopConstraint::allShops(true),
],
null,
];
}
/**
* @param array $setParams
* @param array $getParams
* @param $expectedResult
*/
private function setAndGetValuesForTesting(array $setParams, array $getParams, $expectedResult): void
{
foreach ($setParams as $values) {
$this->configuration->set($values['key'], $values['value'], $values['shopConstraint']);
}
$result = $this->configuration->get($getParams['key'], $getParams['default'], $getParams['shopConstraint']);
$this->assertEquals($expectedResult, $result);
}
private function initMultistore(): void
{
// we want to execute this only once for the whole class
......@@ -215,16 +289,4 @@ class ConfigurationTest extends KernelTestCase
$this->configuration->set('CONFIGURATION_INTEGRATION_TEST_FLAG', 1);
}
}
public function cleanDb(): void
{
//remove newly created shop
$shop = $this->entityManager->getRepository(Shop::class)->findOneBy(['name' => 'test_shop_2']);
$this->entityManager->remove($shop);
$this->entityManager->flush();
// remove multistore feature enabling configuration
$this->configuration->set('PS_MULTISHOP_FEATURE_ACTIVE', 0);
// remove flag
$this->configuration->remove('CONFIGURATION_INTEGRATION_TEST_FLAG');
}
}
......@@ -26,6 +26,7 @@
namespace Tests\TestCase;
use Context;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
......@@ -40,7 +41,7 @@ abstract class ContextStateTestCase extends TestCase
*/
protected function createContextMock(array $contextFields)
{
$contextMock = $this->getMockBuilder(\Context::class)
$contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment