From 2a0ad3d74c8a5fa20f0697cbdd2d040c2c8315dd Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 3 Jan 2025 10:02:54 +0100 Subject: [PATCH] add get endpoint for fields, add field entity, use constants --- src/Entities/Field.php | 60 +++++++++++++++++++++++ src/Resources/Field.php | 21 +++++++- tests/integration/Resources/FieldTest.php | 38 ++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/Entities/Field.php diff --git a/src/Entities/Field.php b/src/Entities/Field.php new file mode 100644 index 0000000..96c4fee --- /dev/null +++ b/src/Entities/Field.php @@ -0,0 +1,60 @@ + + * @link https://www.oxidmodule.com + */ + +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 +{ + private ?FieldEndpoint $endpoint; + + public function __construct(array $elements = [], ?FieldEndpoint $endpoint = null) + { + $this->endpoint = $endpoint; + parent::__construct($elements); + } + + public function getId(): string + { + return $this->get(FieldEndpoint::ID); + } + + public function getName(): string + { + return $this->get(FieldEndpoint::NAME); + } + + public function setName(string $name): void + { + $this->set(FieldEndpoint::NAME, $name); + + // use persist method to send to Klicktipp + } + + /** + * @return null|bool + * @throws BaseException + */ + public function persist(): ?bool + { + return $this->endpoint?->update( + $this->getId(), + $this->getName() + ); + } +} diff --git a/src/Resources/Field.php b/src/Resources/Field.php index 84bfcd1..b6b18e6 100644 --- a/src/Resources/Field.php +++ b/src/Resources/Field.php @@ -15,12 +15,16 @@ namespace D3\KlicktippPhpClient\Resources; +use D3\KlicktippPhpClient\Entities\Field as FieldEntity; use D3\KlicktippPhpClient\Entities\FieldList; use D3\KlicktippPhpClient\Exceptions\BaseException; use GuzzleHttp\RequestOptions; class Field extends Model { + public const ID = 'id'; + public const NAME = 'name'; + /** * @throws BaseException */ @@ -34,6 +38,19 @@ class Field extends Model return new FieldList($data); } + /** + * @throws BaseException + */ + public function get(string $fieldId): FieldEntity + { + $data = $this->connection->requestAndParse( + 'GET', + 'field/'.urlencode(trim($fieldId)).'.json' + ); + + return new FieldEntity($data, $this); + } + /** * @return string - new field id * @throws BaseException @@ -46,7 +63,7 @@ class Field extends Model 'field.json', [ RequestOptions::FORM_PARAMS => [ - 'name' => trim($name), + self::NAME => trim($name), ], ] ) @@ -64,7 +81,7 @@ class Field extends Model 'field/'.urlencode(trim($fieldId)).'.json', [ RequestOptions::FORM_PARAMS => array_filter([ - 'name' => trim($name ?? ''), + self::NAME => trim($name ?? ''), ]), ] ) diff --git a/tests/integration/Resources/FieldTest.php b/tests/integration/Resources/FieldTest.php index d09abea..39dd5b1 100644 --- a/tests/integration/Resources/FieldTest.php +++ b/tests/integration/Resources/FieldTest.php @@ -15,6 +15,7 @@ namespace D3\KlicktippPhpClient\tests\integration\Resources; +use D3\KlicktippPhpClient\Entities\Field as FieldEntity; use D3\KlicktippPhpClient\Entities\FieldList; use D3\KlicktippPhpClient\Exceptions\BaseException; use D3\KlicktippPhpClient\Resources\Field; @@ -94,6 +95,43 @@ class FieldTest extends IntegrationTestCase yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; } + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\Field::get + * @dataProvider getDataProvider + */ + public function testGet(ResponseInterface $response, ?array $expected, bool $expectException = false) + { + $sut = new Field($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $return = $this->callMethod( + $sut, + 'get', + ['12514414'] + ); + + $this->assertInstanceOf(FieldEntity::class, $return); + $this->assertSame($expected, $return->toArray()); + } + + public static function getDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '{ + "'.Field::ID.'": "12514414", + "'.Field::NAME.'": "fieldName2" + }'), [ + Field::ID => "12514414", + Field::NAME => "fieldName2", + ]]; + yield 'unknown id' => [new Response(404, [], '["Kein Field mit dieser ID."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } + /** * @test * @throws ReflectionException