From 7ebf6f984403277a3d201f5417bab7655a8774bd Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 7 Jan 2025 14:04:35 +0100 Subject: [PATCH] assert right return types in Field entity --- src/Entities/Field.php | 11 +++--- tests/unit/Entities/FieldTest.php | 57 +++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/Entities/Field.php b/src/Entities/Field.php index a28999c..f96bb99 100644 --- a/src/Entities/Field.php +++ b/src/Entities/Field.php @@ -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 diff --git a/tests/unit/Entities/FieldTest.php b/tests/unit/Entities/FieldTest.php index d74aec0..6d44a96 100644 --- a/tests/unit/Entities/FieldTest.php +++ b/tests/unit/Entities/FieldTest.php @@ -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'];