From 63530475cfc0f41ce8ef311d1dca8da0c1b5fc53 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 3 Jan 2025 09:42:12 +0100 Subject: [PATCH] add create, update and delete endpoints for fields --- src/Resources/Field.php | 51 +++++++++++++ tests/integration/Resources/FieldTest.php | 93 +++++++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/src/Resources/Field.php b/src/Resources/Field.php index 8d7aa07..84bfcd1 100644 --- a/src/Resources/Field.php +++ b/src/Resources/Field.php @@ -17,6 +17,7 @@ namespace D3\KlicktippPhpClient\Resources; use D3\KlicktippPhpClient\Entities\FieldList; use D3\KlicktippPhpClient\Exceptions\BaseException; +use GuzzleHttp\RequestOptions; class Field extends Model { @@ -32,4 +33,54 @@ class Field extends Model return new FieldList($data); } + + /** + * @return string - new field id + * @throws BaseException + */ + public function create(string $name): string + { + return current( + $this->connection->requestAndParse( + 'POST', + 'field.json', + [ + RequestOptions::FORM_PARAMS => [ + 'name' => trim($name), + ], + ] + ) + ); + } + + /** + * @throws BaseException + */ + public function update(string $fieldId, ?string $name = null): bool + { + return (bool) current( + $this->connection->requestAndParse( + 'PUT', + 'field/'.urlencode(trim($fieldId)).'.json', + [ + RequestOptions::FORM_PARAMS => array_filter([ + 'name' => trim($name ?? ''), + ]), + ] + ) + ); + } + + /** + * @throws BaseException + */ + public function delete(string $fieldId): bool + { + return (bool) current( + $this->connection->requestAndParse( + 'DELETE', + 'field/'.urlencode(trim($fieldId)).'.json' + ) + ); + } } diff --git a/tests/integration/Resources/FieldTest.php b/tests/integration/Resources/FieldTest.php index 8ec712a..d09abea 100644 --- a/tests/integration/Resources/FieldTest.php +++ b/tests/integration/Resources/FieldTest.php @@ -93,4 +93,97 @@ class FieldTest extends IntegrationTestCase yield 'wrong request type' => [new Response(406, [], '["Bei der Erstellung des Objekt ist ein Fehler aufgetreten."]'), null, true]; yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; } + + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\Field::create + * @dataProvider createDataProvider + */ + public function testCreate(ResponseInterface $response, ?string $expected, bool $expectException = false) + { + $sut = new Field($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'create', + ['newFieldName'] + ) + ); + } + + public static function createDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '[12494414]'), '12494414']; + yield 'missing or empty field name' => [new Response(406, [], '["Field konnte nicht erstellt werden."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\Field::update + * @dataProvider updateDataProvider + */ + public function testUpdate(ResponseInterface $response, ?bool $expected, bool $expectException = false) + { + $sut = new Field($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'update', + ['12494414', 'fieldName'] + ) + ); + } + + public static function updateDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '[true]'), true]; + yield 'unknown field' => [new Response(404, [], '["Kein Tag mit dieser ID."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\Field::delete + * @dataProvider deleteDataProvider + */ + public function testDelete(ResponseInterface $response, ?bool $expected, bool $expectException = false) + { + $sut = new Field($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'delete', + ['12494414'] + ) + ); + } + + public static function deleteDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '[true]'), true]; + yield 'unknown field' => [new Response(404, [], '["Kein Field mit dieser ID."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } }