phpstan, phpcs, phpunit fixes

This commit is contained in:
Dennis Eichhorn 2023-01-27 22:12:09 +01:00
parent eaca64d28e
commit f696b5e7fc
15 changed files with 79 additions and 250 deletions

View File

@ -174,11 +174,6 @@
"type": "TINYINT(1)",
"null": false
},
"support_attr_value_type": {
"name": "support_attr_value_type",
"type": "INT(11)",
"null": false
},
"support_attr_value_valueStr": {
"name": "support_attr_value_valueStr",
"type": "VARCHAR(255)",
@ -202,22 +197,6 @@
"type": "DATETIME",
"null": true,
"default": null
},
"support_attr_value_lang": {
"name": "support_attr_value_lang",
"type": "VARCHAR(2)",
"null": true,
"default": null,
"foreignTable": "language",
"foreignKey": "language_639_1"
},
"support_attr_value_country": {
"name": "support_attr_value_country",
"type": "VARCHAR(2)",
"null": true,
"default": null,
"foreignTable": "country",
"foreignKey": "country_code2"
}
}
},

View File

@ -36,6 +36,7 @@ use Modules\Support\Models\TicketMapper;
use Modules\Tasks\Models\TaskMapper;
use Modules\Tasks\Models\TaskStatus;
use Modules\Tasks\Models\TaskType;
use phpOMS\Localization\BaseStringL11n;
use phpOMS\Localization\ISO639x1Enum;
use phpOMS\Message\Http\RequestStatusCode;
use phpOMS\Message\NotificationLevel;
@ -507,18 +508,18 @@ final class ApiController extends Controller
*
* @param RequestAbstract $request Request
*
* @return TicketAttributeTypeL11n
* @return BaseStringL11n
*
* @since 1.0.0
*/
private function createTicketAttributeTypeL11nFromRequest(RequestAbstract $request) : TicketAttributeTypeL11n
private function createTicketAttributeTypeL11nFromRequest(RequestAbstract $request) : BaseStringL11n
{
$attrL11n = new TicketAttributeTypeL11n();
$attrL11n->type = (int) ($request->getData('type') ?? 0);
$attrL11n = new BaseStringL11n();
$attrL11n->ref = (int) ($request->getData('type') ?? 0);
$attrL11n->setLanguage((string) (
$request->getData('language') ?? $request->getLanguage()
));
$attrL11n->title = (string) ($request->getData('title') ?? '');
$attrL11n->content = (string) ($request->getData('title') ?? '');
return $attrL11n;
}
@ -639,7 +640,7 @@ final class ApiController extends Controller
if ($attrValue->isDefault) {
$this->createModelRelation(
$request->header->account,
(int) $request->getData('attributetype'),
(int) $request->getData('type'),
$attrValue->getId(),
TicketAttributeTypeMapper::class, 'defaults', '', $request->getOrigin()
);
@ -659,28 +660,17 @@ final class ApiController extends Controller
*/
private function createTicketAttributeValueFromRequest(RequestAbstract $request) : TicketAttributeValue
{
/** @var TicketAttributeType $type */
$type = TicketAttributeTypeMapper::get()
->where('id', (int) ($request->getData('type') ?? 0))
->execute();
$attrValue = new TicketAttributeValue();
$type = (int) ($request->getData('type') ?? 0);
if ($type === AttributeValueType::_INT) {
$attrValue->valueInt = $request->getData('value', 'int');
} elseif ($type === AttributeValueType::_STRING) {
$attrValue->valueStr = $request->getData('value', 'string');
} elseif ($type === AttributeValueType::_FLOAT) {
$attrValue->valueDec = $request->getData('value', 'float');
} elseif ($type === AttributeValueType::_DATETIME) {
$attrValue->valueDat = $request->getData('value', 'DateTime');
}
$attrValue->type = $type;
$attrValue->isDefault = (bool) ($request->getData('default') ?? false);
$attrValue->setValue($request->getData('value'), $type->datatype);
if ($request->hasData('language')) {
$attrValue->setLanguage((string) ($request->getData('language') ?? $request->getLanguage()));
}
if ($request->hasData('country')) {
$attrValue->setCountry((string) ($request->getData('country') ?? $request->header->l11n->getCountry()));
if ($request->getData('title') !== null) {
$attrValue->setL11n($request->getData('title'), $request->getData('language') ?? ISO639x1Enum::_EN);
}
return $attrValue;

View File

@ -42,7 +42,7 @@ final class TicketAttributeMapper extends DataMapperFactory
/**
* Has one relation.
*
* @var array<string, array{mapper:string, external:string, by?:string, column?:string, conditional?:bool}>
* @var array<string, array{mapper:class-string, external:string, by?:string, column?:string, conditional?:bool}>
* @since 1.0.0
*/
public const OWNS_ONE = [

View File

@ -14,6 +14,7 @@ declare(strict_types=1);
namespace Modules\Support\Models;
use phpOMS\Localization\BaseStringL11n;
use phpOMS\Localization\ISO639x1Enum;
/**
@ -62,12 +63,20 @@ class TicketAttributeType implements \JsonSerializable
public bool $isRequired = false;
/**
* Datatype of the attribute
*
* @var int
* @since 1.0.0
*/
public int $datatype = AttributeValueType::_STRING;
/**
* Localization
*
* @var string | TicketAttributeTypeL11n
* @var string | BaseStringL11n
*/
protected string | TicketAttributeTypeL11n $l11n;
protected string | BaseStringL11n $l11n;
/**
* Possible default attribute values
@ -103,22 +112,23 @@ class TicketAttributeType implements \JsonSerializable
/**
* Set l11n
*
* @param string|TicketAttributeTypeL11n $l11n Tag article l11n
* @param string|BaseStringL11n $l11n Tag article l11n
* @param string $lang Language
*
* @return void
*
* @since 1.0.0
*/
public function setL11n(string | TicketAttributeTypeL11n $l11n, string $lang = ISO639x1Enum::_EN) : void
public function setL11n(string | BaseStringL11n $l11n, string $lang = ISO639x1Enum::_EN) : void
{
if ($l11n instanceof TicketAttributeTypeL11n) {
if ($l11n instanceof BaseStringL11n) {
$this->l11n = $l11n;
} elseif (isset($this->l11n) && $this->l11n instanceof TicketAttributeTypeL11n) {
$this->l11n->title = $l11n;
} elseif (isset($this->l11n) && $this->l11n instanceof BaseStringL11n) {
$this->l11n->content = $l11n;
$this->l11n->setLanguage($lang);
} else {
$this->l11n = new TicketAttributeTypeL11n();
$this->l11n->title = $l11n;
$this->l11n = new BaseStringL11n();
$this->l11n->content = $l11n;
$this->l11n->setLanguage($lang);
}
}
@ -130,7 +140,11 @@ class TicketAttributeType implements \JsonSerializable
*/
public function getL11n() : string
{
return $this->l11n instanceof TicketAttributeTypeL11n ? $this->l11n->title : $this->l11n;
if (!isset($this->l11n)) {
return '';
}
return $this->l11n instanceof BaseStringL11n ? $this->l11n->content : $this->l11n;
}
/**

View File

@ -59,7 +59,7 @@ final class TicketAttributeTypeL11nMapper extends DataMapperFactory
/**
* Model to use by the mapper.
*
* @var string
* @var class-string
* @since 1.0.0
*/
public const MODEL = BaseStringL11n::class;

View File

@ -44,7 +44,7 @@ final class TicketAttributeTypeMapper extends DataMapperFactory
/**
* Has many relation.
*
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @var array<string, array{mapper:class-string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0
*/
public const HAS_MANY = [

View File

@ -124,22 +124,26 @@ class TicketAttributeValue implements \JsonSerializable
/**
* Set value
*
* @param int|string|float|\DateTimeInterface $value Value
* @param int|string|float $value Value
* @param int $datatype Datatype
*
* @return void
*
* @since 1.0.0
*/
public function setValue($value) : void
public function setValue(mixed $value, int $datatype) : void
{
if (\is_string($value)) {
$this->valueStr = $value;
} elseif (\is_int($value)) {
$this->valueInt = $value;
} elseif (\is_float($value)) {
$this->valueDec = $value;
} elseif ($value instanceof \DateTimeInterface) {
$this->valueDat = $value;
if ($datatype === AttributeValueType::_STRING) {
$this->valueStr = (string) $value;
} elseif ($datatype === AttributeValueType::_INT
|| $datatype === AttributeValueType::_FLOAT_INT
|| $datatype === AttributeValueType::_BOOL
) {
$this->valueInt = (int) $value;
} elseif ($datatype === AttributeValueType::_FLOAT) {
$this->valueDec = (float) $value;
} elseif ($datatype === AttributeValueType::_DATETIME) {
$this->valueDat = new \DateTime((string) $value);
}
}
@ -181,6 +185,7 @@ class TicketAttributeValue implements \JsonSerializable
$this->l11n = $l11n;
} elseif (isset($this->l11n) && $this->l11n instanceof BaseStringL11n) {
$this->l11n->content = $l11n;
$this->l11n->setLanguage($lang);
} else {
$this->l11n = new BaseStringL11n();
$this->l11n->content = $l11n;

View File

@ -44,7 +44,7 @@ final class TicketAttributeValueMapper extends DataMapperFactory
/**
* Has many relation.
*
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @var array<string, array{mapper:class-string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0
*/
/*

View File

@ -43,7 +43,7 @@ final class TicketElementMapper extends DataMapperFactory
/**
* Has one relation.
*
* @var array<string, array{mapper:string, external:string, by?:string, column?:string, conditional?:bool}>
* @var array<string, array{mapper:class-string, external:string, by?:string, column?:string, conditional?:bool}>
* @since 1.0.0
*/
public const OWNS_ONE = [

View File

@ -44,7 +44,7 @@ final class TicketMapper extends DataMapperFactory
/**
* Has one relation.
*
* @var array<string, array{mapper:string, external:string, by?:string, column?:string, conditional?:bool}>
* @var array<string, array{mapper:class-string, external:string, by?:string, column?:string, conditional?:bool}>
* @since 1.0.0
*/
public const OWNS_ONE = [
@ -57,7 +57,7 @@ final class TicketMapper extends DataMapperFactory
/**
* Has many relation.
*
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @var array<string, array{mapper:class-string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0
*/
public const HAS_MANY = [
@ -79,7 +79,7 @@ final class TicketMapper extends DataMapperFactory
/**
* Belongs to.
*
* @var array<string, array{mapper:string, external:string, column?:string, by?:string}>
* @var array<string, array{mapper:class-string, external:string, column?:string, by?:string}>
* @since 1.0.0
*/
public const BELONGS_TO = [

View File

@ -70,8 +70,7 @@ trait ApiControllerAttributeTrait
$request->header->account = 1;
$request->setData('default', '1');
$request->setData('attributetype', '1');
$request->setData('type', AttributeValueType::_INT);
$request->setData('type', '1');
$request->setData('value', '1');
$request->setData('language', ISO639x1Enum::_DE);
$request->setData('country', ISO3166TwoEnum::_DEU);
@ -90,7 +89,7 @@ trait ApiControllerAttributeTrait
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('type', AttributeValueType::_STRING);
$request->setData('type', '1');
$request->setData('value', '1');
$request->setData('language', ISO639x1Enum::_DE);
$request->setData('country', ISO3166TwoEnum::_DEU);
@ -109,7 +108,7 @@ trait ApiControllerAttributeTrait
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('type', AttributeValueType::_FLOAT);
$request->setData('type', '1');
$request->setData('value', '1.1');
$request->setData('language', ISO639x1Enum::_DE);
$request->setData('country', ISO3166TwoEnum::_DEU);
@ -128,7 +127,7 @@ trait ApiControllerAttributeTrait
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('type', AttributeValueType::_DATETIME);
$request->setData('type', '1');
$request->setData('value', '2020-08-02');
$request->setData('language', ISO639x1Enum::_DE);
$request->setData('country', ISO3166TwoEnum::_DEU);

View File

@ -1,42 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\Support\tests\Models;
use Modules\Support\Models\NullTicketAttributeTypeL11n;
/**
* @internal
*/
final class NullTicketAttributeTypeL11nTest extends \PHPUnit\Framework\TestCase
{
/**
* @covers Modules\Support\Models\NullTicketAttributeTypeL11n
* @group framework
*/
public function testNull() : void
{
self::assertInstanceOf('\Modules\Support\Models\TicketAttributeTypeL11n', new NullTicketAttributeTypeL11n());
}
/**
* @covers Modules\Support\Models\NullTicketAttributeTypeL11n
* @group framework
*/
public function testId() : void
{
$null = new NullTicketAttributeTypeL11n(2);
self::assertEquals(2, $null->getId());
}
}

View File

@ -1,87 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\Support\tests\Models;
use Modules\Support\Models\TicketAttributeTypeL11n;
use phpOMS\Localization\ISO639x1Enum;
/**
* @internal
*/
final class TicketAttributeTypeL11nTest extends \PHPUnit\Framework\TestCase
{
private TicketAttributeTypeL11n $l11n;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->l11n = new TicketAttributeTypeL11n();
}
/**
* @covers Modules\Support\Models\TicketAttributeTypeL11n
* @group module
*/
public function testDefault() : void
{
self::assertEquals(0, $this->l11n->getId());
self::assertEquals('', $this->l11n->title);
self::assertEquals(0, $this->l11n->type);
self::assertEquals(ISO639x1Enum::_EN, $this->l11n->getLanguage());
}
/**
* @covers Modules\Support\Models\TicketAttributeTypeL11n
* @group module
*/
public function testNameInputOutput() : void
{
$this->l11n->title = 'TestName';
self::assertEquals('TestName', $this->l11n->title);
}
/**
* @covers Modules\Support\Models\TicketAttributeTypeL11n
* @group module
*/
public function testLanguageInputOutput() : void
{
$this->l11n->setLanguage(ISO639x1Enum::_DE);
self::assertEquals(ISO639x1Enum::_DE, $this->l11n->getLanguage());
}
/**
* @covers Modules\Support\Models\TicketAttributeTypeL11n
* @group module
*/
public function testSerialize() : void
{
$this->l11n->title = 'Title';
$this->l11n->type = 2;
$this->l11n->setLanguage(ISO639x1Enum::_DE);
self::assertEquals(
[
'id' => 0,
'title' => 'Title',
'type' => 2,
'language' => ISO639x1Enum::_DE,
],
$this->l11n->jsonSerialize()
);
}
}

View File

@ -15,7 +15,6 @@ declare(strict_types=1);
namespace Modules\Support\tests\Models;
use Modules\Support\Models\TicketAttributeType;
use Modules\Support\Models\TicketAttributeTypeL11n;
/**
* @internal
@ -50,9 +49,6 @@ final class TicketAttributeTypeTest extends \PHPUnit\Framework\TestCase
{
$this->type->setL11n('Test');
self::assertEquals('Test', $this->type->getL11n());
$this->type->setL11n(new TicketAttributeTypeL11n(0, 'NewTest'));
self::assertEquals('NewTest', $this->type->getL11n());
}
/**

View File

@ -14,9 +14,8 @@ declare(strict_types=1);
namespace Modules\Support\tests\Models;
use Modules\Support\Models\AttributeValueType;
use Modules\Support\Models\TicketAttributeValue;
use phpOMS\Localization\ISO3166TwoEnum;
use phpOMS\Localization\ISO639x1Enum;
/**
* @internal
@ -41,26 +40,7 @@ final class TicketAttributeValueTest extends \PHPUnit\Framework\TestCase
{
self::assertEquals(0, $this->value->getId());
self::assertNull($this->value->getValue());
}
/**
* @covers Modules\Support\Models\TicketAttributeValue
* @group module
*/
public function testLanguageInputOutput() : void
{
$this->value->setLanguage(ISO639x1Enum::_DE);
self::assertEquals(ISO639x1Enum::_DE, $this->value->getLanguage());
}
/**
* @covers Modules\Support\Models\TicketAttributeValue
* @group module
*/
public function testCountryInputOutput() : void
{
$this->value->setCountry(ISO3166TwoEnum::_DEU);
self::assertEquals(ISO3166TwoEnum::_DEU, $this->value->getCountry());
self::assertFalse($this->value->isDefault);
}
/**
@ -69,7 +49,7 @@ final class TicketAttributeValueTest extends \PHPUnit\Framework\TestCase
*/
public function testValueIntInputOutput() : void
{
$this->value->setValue(1);
$this->value->setValue(1, AttributeValueType::_INT);
self::assertEquals(1, $this->value->getValue());
}
@ -79,7 +59,7 @@ final class TicketAttributeValueTest extends \PHPUnit\Framework\TestCase
*/
public function testValueFloatInputOutput() : void
{
$this->value->setValue(1.1);
$this->value->setValue(1.1, AttributeValueType::_FLOAT);
self::assertEquals(1.1, $this->value->getValue());
}
@ -89,7 +69,7 @@ final class TicketAttributeValueTest extends \PHPUnit\Framework\TestCase
*/
public function testValueStringInputOutput() : void
{
$this->value->setValue('test');
$this->value->setValue('test', AttributeValueType::_STRING);
self::assertEquals('test', $this->value->getValue());
}
@ -99,7 +79,8 @@ final class TicketAttributeValueTest extends \PHPUnit\Framework\TestCase
*/
public function testValueDateInputOutput() : void
{
$this->value->setValue($dat = new \DateTime('now'));
$dat = new \DateTime('now');
$this->value->setValue('now', AttributeValueType::_DATETIME);
self::assertEquals($dat->format('Y-m-d'), $this->value->getValue()->format('Y-m-d'));
}
@ -109,23 +90,17 @@ final class TicketAttributeValueTest extends \PHPUnit\Framework\TestCase
*/
public function testSerialize() : void
{
$this->value->type = 1;
$this->value->setValue('test');
$this->value->setValue('test', AttributeValueType::_STRING);
$this->value->isDefault = true;
$this->value->setLanguage(ISO639x1Enum::_DE);
$this->value->setCountry(ISO3166TwoEnum::_DEU);
self::assertEquals(
[
'id' => 0,
'type' => 1,
'valueInt' => null,
'valueStr' => 'test',
'valueDec' => null,
'valueDat' => null,
'isDefault' => true,
'language' => ISO639x1Enum::_DE,
'country' => ISO3166TwoEnum::_DEU,
],
$this->value->jsonSerialize()
);