add tests for Configuration class

This commit is contained in:
Daniel Seifert 2023-01-06 23:42:51 +01:00
parent f7418e06b0
commit 26a6223442
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
2 changed files with 591 additions and 17 deletions

View File

@ -18,6 +18,7 @@ namespace D3\Linkmobility4OXID\Application\Model;
use Assert\Assert;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
class Configuration
@ -44,10 +45,10 @@ class Configuration
public function getApiToken(): string
{
/** @var string $token */
$token = Registry::getConfig()->getConfigParam(self::GENERAL_APITOKEN);
$token = $this->getConfig()->getConfigParam(self::GENERAL_APITOKEN);
Assert::that($token)->string();
$token = trim($token);
Assert::that($token)->string()->notEmpty();
Assert::that($token)->notEmpty();
return $token;
}
@ -57,7 +58,7 @@ class Configuration
*/
public function getTestMode(): bool
{
return (bool) Registry::getConfig()->getConfigParam(self::GENERAL_DEBUG);
return (bool) $this->getConfig()->getConfigParam(self::GENERAL_DEBUG);
}
/**
@ -66,7 +67,8 @@ class Configuration
public function getSmsSenderNumber(): ?string
{
/** @var string $number */
$number = Registry::getConfig()->getConfigParam(self::SMS_SENDERNR);
$number = $this->getConfig()->getConfigParam(self::SMS_SENDERNR);
Assert::that($number)->string();
$number = trim($number);
return strlen($number) ? $number : null;
@ -78,7 +80,10 @@ class Configuration
public function getSmsSenderCountry(): ?string
{
/** @var string $country */
$country = Registry::getConfig()->getConfigParam(self::SMS_SENDERCOUNTRY);
$country = $this->getConfig()->getConfigParam(self::SMS_SENDERCOUNTRY);
Assert::that($country)->string();
$country = trim($country);
$country = strlen($country) ? strtoupper($country) : null;
@ -93,18 +98,18 @@ class Configuration
public function getOrderRecipientFields(): array
{
/** @var string[] $customFields */
$customFields = Registry::getConfig()->getConfigParam(self::ORDER_RECFIELDS) ?: [];
$customFields = $this->getConfig()->getConfigParam(self::ORDER_RECFIELDS) ?: [];
Assert::that($customFields)->isArray();
array_walk(
$customFields,
[$this, 'checkFieldExists'],
[self::ARGS_CHECKKEYS => true, self::ARGS_CHECKCLASS => Order::class]
);
$customFields = array_filter($customFields);
Assert::that($customFields)->isArray();
return $customFields;
// remove all false values and trim keys
return array_filter($this->sanitizeKeys($customFields));
}
/**
@ -113,17 +118,33 @@ class Configuration
public function getUserRecipientFields(): array
{
/** @var string[] $customFields */
$customFields = Registry::getConfig()->getConfigParam(self::USER_RECFIELDS) ?: [];
$customFields = $this->getConfig()->getConfigParam(self::USER_RECFIELDS) ?: [];
Assert::that($customFields)->isArray();
array_walk(
$customFields,
[$this, 'checkFieldExists'],
[self::ARGS_CHECKKEYS => false, self::ARGS_CHECKCLASS => User::class]
);
$customFields = array_filter($customFields);
Assert::that($customFields)->isArray();
// remove all false values
return array_filter($customFields);
}
/**
* @param array $customFields
* @return array
*/
public function sanitizeKeys(array $customFields): array
{
foreach ($customFields as $key => $value) {
$sanitizedKey = trim($key);
if ($key !== $sanitizedKey) {
$customFields[$sanitizedKey] = $value;
unset($customFields[$key]);
}
}
return $customFields;
}
@ -138,6 +159,7 @@ class Configuration
{
$checkCountryFieldName = $args[self::ARGS_CHECKKEYS] ? trim($checkCountryFieldName) : $checkCountryFieldName;
$checkPhoneFieldName = trim($checkPhoneFieldName);
$allFieldNames = oxNew($args[self::ARGS_CHECKCLASS])->getFieldNames() ?: [];
array_walk($allFieldNames, function (&$value) {
@ -155,7 +177,7 @@ class Configuration
*/
public function sendOrderFinishedMessage(): bool
{
return (bool) Registry::getConfig()->getConfigParam(self::SENDBY_ORDERED);
return (bool) $this->getConfig()->getConfigParam(self::SENDBY_ORDERED);
}
/**
@ -163,7 +185,7 @@ class Configuration
*/
public function sendOrderSendedNowMessage(): bool
{
return (bool) Registry::getConfig()->getConfigParam(self::SENDBY_SENDEDNOW);
return (bool) $this->getConfig()->getConfigParam(self::SENDBY_SENDEDNOW);
}
/**
@ -171,6 +193,16 @@ class Configuration
*/
public function sendOrderCanceledMessage(): bool
{
return (bool) Registry::getConfig()->getConfigParam(self::SENDBY_CANCELED);
return (bool) $this->getConfig()->getConfigParam(self::SENDBY_CANCELED);
}
/**
* @return Config
*/
protected function getConfig(): Config
{
/** @var Config $config */
$config = d3GetOxidDIC()->get('d3ox.linkmobility.'.Config::class);
return $config;
}
}

View File

@ -0,0 +1,542 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\Linkmobility4OXID\tests\unit\Application\Model;
use Assert\InvalidArgumentException;
use D3\Linkmobility4OXID\Application\Model\Configuration;
use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase;
use D3\TestingTools\Development\CanAccessRestricted;
use OxidEsales\Eshop\Core\Config;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class ConfigurationTest extends LMUnitTestCase
{
use CanAccessRestricted;
/**
* @test
* @return void
* @throws ReflectionException
* @dataProvider canGetApiTokenDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::getApiToken
*/
public function canGetApiToken($savedToken, $expected, $expectException)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($savedToken) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::GENERAL_APITOKEN:
return $savedToken;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
if ($expectException) {
$this->expectException(InvalidArgumentException::class);
}
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'getApiToken'
)
);
}
/**
* @return array[]
*/
public function canGetApiTokenDataProvider(): array
{
return [
'token ok' => [' apiTokenFixture ', 'apiTokenFixture', false],
'array token' => [['apiTokenFixture'], 'aptTokenFixture', true],
'null token' => [null, 'aptTokenFixture', true],
'empty token' => ['', 'aptTokenFixture', true],
];
}
/**
* @test
* @param $testMode
* @return void
* @throws ReflectionException
* @dataProvider trueFalseDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::getTestMode
*/
public function canGetTestMode($testMode, $expected)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($testMode) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::GENERAL_DEBUG:
return $testMode;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'getTestMode'
)
);
}
/**
* @test
* @param $config
* @param $expected
* @param $expectException
* @return void
* @throws ReflectionException
* @dataProvider canGetSmsSenderNumberDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::getSmsSenderNumber
*/
public function canGetSmsSenderNumber($config, $expected, $expectException)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($config) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::SMS_SENDERNR:
return $config;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
if ($expectException) {
$this->expectException(InvalidArgumentException::class);
}
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'getSmsSenderNumber'
)
);
}
/**
* @return array[]
*/
public function canGetSmsSenderNumberDataProvider(): array
{
return [
'number ok' => [' 0123456789 ', '0123456789', false],
'array number' => [['0123456789'], '0123456789', true],
'null number' => [null, null, true],
'empty number' => ['', null, false],
];
}
/**
* @test
* @param $config
* @param $expected
* @param $expectException
* @return void
* @throws ReflectionException
* @dataProvider canGetSmsSenderCountryDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::getSmsSenderCountry
*/
public function canGetSmsSenderCountry($config, $expected, $expectException)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($config) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::SMS_SENDERCOUNTRY:
return $config;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
if ($expectException) {
$this->expectException(InvalidArgumentException::class);
}
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'getSmsSenderCountry'
)
);
}
/**
* @return array[]
*/
public function canGetSmsSenderCountryDataProvider(): array
{
return [
'country ok' => [' at ', 'AT', false],
'country to short' => [' D ', 'D', true],
'country to long' => [' FRA ', 'FRA', true],
'array country' => [['DE'], 'DE', true],
'null country' => [null, null, true],
'empty number' => ['', null, false],
];
}
/**
* @test
* @param $config
* @param $expected
* @param $expectException
* @return void
* @throws ReflectionException
* @dataProvider canGetOrderRecipientFieldsDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::getOrderRecipientFields
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::checkFieldExists
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::sanitizeKeys
*/
public function canGetOrderRecipientFields($config, $expected, $expectException)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($config) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::ORDER_RECFIELDS:
return $config;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
if ($expectException) {
$this->expectException(InvalidArgumentException::class);
}
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'getOrderRecipientFields'
)
);
}
/**
* @return array[]
*/
public function canGetOrderRecipientFieldsDataProvider(): array
{
return [
'string fields' => ['oxbillfon', '', true],
'array fields exist' => [[' oxbillcountryid ' => ' oxbillfon ', ' oxdelcountryid ' => ' oxdelfon '], ['oxbillcountryid' => 'oxbillfon', 'oxdelcountryid' => 'oxdelfon'], false],
'array fields not exist'=> [[' d3custfield1 ' => ' oxbillfon ', ' oxdelcountryid ' => ' d3custfield2 '], [], false],
'empty number' => [[], [], false],
];
}
/**
* @test
* @param $config
* @param $expected
* @param $expectException
* @return void
* @throws ReflectionException
* @dataProvider canGetUserRecipientFieldsDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::getUserRecipientFields
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::checkFieldExists
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::sanitizeKeys
*/
public function canGetUserRecipientFields($config, $expected, $expectException)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($config) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::USER_RECFIELDS:
return $config;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
if ($expectException) {
$this->expectException(InvalidArgumentException::class);
}
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'getUserRecipientFields'
)
);
}
/**
* @return array[]
*/
public function canGetUserRecipientFieldsDataProvider(): array
{
return [
'string fields' => ['oxfon', '', true],
'array fields exist' => [[' oxfon ', ' oxmobfon '], ['oxfon', 'oxmobfon'], false],
'array fields not exist'=> [[' d3custfield1 ', ' d3custfield2 '], [], false],
'empty number' => [[], [], false],
];
}
/**
* @test
* @param $config
* @param $expected
* @return void
* @throws ReflectionException
* @dataProvider trueFalseDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::sendOrderFinishedMessage
*/
public function canGetSendOrderFinishedMessage($config, $expected)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($config) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::SENDBY_ORDERED:
return $config;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'sendOrderFinishedMessage'
)
);
}
/**
* @test
* @param $config
* @param $expected
* @return void
* @throws ReflectionException
* @dataProvider trueFalseDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::sendOrderSendedNowMessage
*/
public function canGetSendOrderSendedNowMessage($config, $expected)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($config) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::SENDBY_SENDEDNOW:
return $config;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'sendOrderSendedNowMessage'
)
);
}
/**
* @test
* @param $config
* @param $expected
* @return void
* @throws ReflectionException
* @dataProvider trueFalseDataProvider
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::sendOrderCanceledMessage
*/
public function canGetSendOrderCanceledMessage($config, $expected)
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->method('getConfigParam')->willReturnCallback(
function () use ($config) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case Configuration::SENDBY_CANCELED:
return $config;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getConfig'])
->getMock();
$sut->method('getConfig')->willReturn($configMock);
$this->assertSame(
$expected,
$this->callMethod(
$sut,
'sendOrderCanceledMessage'
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Application\Model\Configuration::getConfig
*/
public function canGetConfig()
{
/** @var Configuration|MockObject $sut */
$sut = $this->getMockBuilder(Configuration::class)
->getMock();
$this->assertInstanceOf(
Config::class,
$this->callMethod(
$sut,
'getConfig'
)
);
}
/**
* @return array
*/
public function trueFalseDataProvider(): array
{
return [
'true value' => [true, true],
'false value' => [false, false],
'one value' => [1, true],
'zero value' => [0, false]
];
}
}