Files
kupshop/bundles/KupShop/KupShopBundle/Tests/SettingsTest.php
2025-08-02 16:30:27 +02:00

121 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\KupShopBundle\Tests;
class SettingsTest extends \DatabaseTestCase
{
private \Settings $settings;
protected function setUp(): void
{
parent::setUp();
$this->settings = \Settings::getDefault();
}
/** @dataProvider data_testReadEmulatedOrderNotInStore */
public function testReadEmulatedOrderNotInStore(string $orderAvailability, string $expectedOrderNotInStore): void
{
$this->settings->order_availability = $orderAvailability;
$this->assertEquals($orderAvailability, $this->settings->order_availability);
$this->assertEquals($expectedOrderNotInStore, $this->settings->order_not_in_store);
}
/** @dataProvider data_testWriteEmulatedOrderNotInStore */
public function testWriteEmulatedOrderNotInStore(string $orderNotInStore, string $expectedOrderAvailability, bool $withProductSuppliersModule = true): void
{
$withProductSuppliersModule ? $this->addModule('products_suppliers') : $this->removeModule('products_suppliers');
$this->settings->order_not_in_store = $orderNotInStore;
$this->assertEquals($orderNotInStore, $this->settings->order_not_in_store);
$this->assertEquals($expectedOrderAvailability, $this->settings->order_availability);
}
/** @dataProvider data_testRead */
public function testRead(string $key, ?string $subkey, mixed $expected, bool $useArrayAccess = false): void
{
$this->assertEquals($expected, $this->readSettings($key, $subkey, $useArrayAccess));
}
/** @dataProvider data_testReadWrite */
public function testReadWrite(string $key, ?string $subkey, mixed $value, bool $useArrayAccess = false): void
{
$this->writeSettings($key, $subkey, $value, $useArrayAccess);
$this->assertEquals($value, $this->readSettings($key, $subkey, $useArrayAccess));
}
public function data_testReadEmulatedOrderNotInStore(): iterable
{
yield 'Set order_availability to `all`, so order_not_in_store should be `Y`' => [\Settings::ORDER_AVAILABILITY_ALL, 'Y'];
yield 'Set order_availability to `in_store`, so order_not_in_store should be `N`' => [\Settings::ORDER_AVAILABILITY_IN_STORE, 'N'];
yield 'Set order_availability to `in_store_or_suppliers_store`, so order_not_in_store should be `N`' => [\Settings::ORDER_AVAILABILITY_IN_STORE_OR_SUPPLIER_STORE, 'N'];
}
public function data_testWriteEmulatedOrderNotInStore(): iterable
{
yield 'Disable order_not_in_store with enabled products_suppliers module' => ['N', \Settings::ORDER_AVAILABILITY_IN_STORE_OR_SUPPLIER_STORE];
yield 'Disable order_not_in_store with disabled products_suppliers module' => ['N', \Settings::ORDER_AVAILABILITY_IN_STORE, false];
yield 'Allow order_not_in_store' => ['Y', \Settings::ORDER_AVAILABILITY_ALL];
}
public function data_testRead(): iterable
{
yield ['nesmysl', null, null];
yield ['order_availability', null, \Settings::ORDER_AVAILABILITY_ALL];
yield ['order_availability', null, \Settings::ORDER_AVAILABILITY_ALL, true];
yield ['order_not_in_store', null, 'Y'];
yield ['order_not_in_store', null, 'Y', true];
yield ['cdn', null, ['active' => 'N']];
yield ['cdn', null, ['active' => 'N'], true];
}
public function data_testReadWrite(): iterable
{
yield ['testkey1', null, null];
yield ['testkey2', null, ['test' => 'value'], true];
yield ['testkey3', 'key', 'value'];
yield ['testkey3', 'key2', 'value2', true];
}
private function writeSettings(string $key, ?string $subkey, mixed $value, bool $useArrayAccess = false): void
{
if ($useArrayAccess) {
if ($subkey !== null) {
$this->settings[$key][$subkey] = $value;
} else {
$this->settings[$key] = $value;
}
return;
}
if ($subkey !== null) {
$this->settings->{$key}[$subkey] = $value;
} else {
$this->settings->{$key} = $value;
}
}
private function readSettings(string $key, ?string $subkey, bool $useArrayAccess = false): mixed
{
if ($useArrayAccess) {
if ($subkey !== null) {
return $this->settings[$key][$subkey] ?? null;
}
return $this->settings[$key] ?? null;
}
if ($subkey !== null) {
return $this->settings->{$key}[$subkey] ?? null;
}
return $this->settings->{$key} ?? null;
}
}