assert right return types in Field entity

This commit is contained in:
Daniel Seifert 2025-01-07 14:04:35 +01:00
bovenliggende 8861dc2468
commit 7ebf6f9844
2 gewijzigde bestanden met toevoegingen van 59 en 9 verwijderingen

Bestand weergeven

@ -19,9 +19,8 @@ namespace D3\KlicktippPhpClient\Entities;
use D3\KlicktippPhpClient\Exceptions\BaseException;
use D3\KlicktippPhpClient\Resources\Field as FieldEndpoint;
use Doctrine\Common\Collections\ArrayCollection;
class Field extends ArrayCollection
class Field extends Entity
{
private ?FieldEndpoint $endpoint;
@ -31,14 +30,14 @@ class Field extends ArrayCollection
parent::__construct($elements);
}
public function getId(): string
public function getId(): ?string
{
return $this->get(FieldEndpoint::ID);
return $this->getStringOrNullValue($this->get(FieldEndpoint::ID));
}
public function getName(): string
public function getName(): ?string
{
return $this->get(FieldEndpoint::NAME) ?? '';
return $this->getStringOrNullValue($this->get(FieldEndpoint::NAME));
}
public function setName(string $name): void

Bestand weergeven

@ -18,6 +18,7 @@ declare(strict_types=1);
namespace D3\KlicktippPhpClient\tests\unit\Entities;
use D3\KlicktippPhpClient\Entities\Field;
use D3\KlicktippPhpClient\Exceptions\InvalidCredentialTypeException;
use D3\KlicktippPhpClient\Resources\Field as FieldEndpoint;
use D3\KlicktippPhpClient\tests\TestCase;
use Generator;
@ -73,9 +74,9 @@ class FieldTest extends TestCase
* @throws ReflectionException
* @covers \D3\KlicktippPhpClient\Entities\Field::getId
* @covers \D3\KlicktippPhpClient\Entities\Field::getName
* @dataProvider getSomethingDataProvider
* @dataProvider getDataProvider
*/
public function testGetSomething(string $methodName, string $expectedValue): void
public function testGet(string $methodName, string $expectedValue): void
{
$this->assertSame(
$expectedValue,
@ -83,7 +84,57 @@ class FieldTest extends TestCase
);
}
public static function getSomethingDataProvider(): Generator
/**
* @test
* @throws ReflectionException
* @covers \D3\KlicktippPhpClient\Entities\Field::getId
* @covers \D3\KlicktippPhpClient\Entities\Field::getName
* @dataProvider getDataProvider
*/
public function testGetNull(string $testMethod): void
{
$nullProperties = [];
foreach (array_keys($this->entity->toArray()) as $key) {
$nullProperties[$key] = null;
}
$sut = new Field($nullProperties);
$this->assertNull(
$this->callMethod(
$sut,
$testMethod,
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\KlicktippPhpClient\Entities\Field::getId
* @covers \D3\KlicktippPhpClient\Entities\Field::getName
* @dataProvider getDataProvider
*/
public function testGetInvalid(string $testMethod): void
{
$invalidProperties = [
FieldEndpoint::ID => [],
FieldEndpoint::NAME => [],
];
$sut = new Field($invalidProperties);
$this->expectException(InvalidCredentialTypeException::class);
$this->assertNull(
$this->callMethod(
$sut,
$testMethod,
)
);
}
public static function getDataProvider(): Generator
{
yield ['getId', '155988456'];
yield ['getName', 'fieldName'];