From 839ffd63466a14119c84e2def13811600beaace5 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sun, 29 Dec 2024 23:33:09 +0100 Subject: [PATCH] add tag tests --- src/Resources/Tag.php | 66 ++++---- tests/integration/Resources/TagTest.php | 200 ++++++++++++++++++++++++ 2 files changed, 236 insertions(+), 30 deletions(-) create mode 100644 tests/integration/Resources/TagTest.php diff --git a/src/Resources/Tag.php b/src/Resources/Tag.php index 6636e4a..a25c5a5 100644 --- a/src/Resources/Tag.php +++ b/src/Resources/Tag.php @@ -18,77 +18,83 @@ namespace D3\KlicktippPhpClient\Resources; use D3\KlicktippPhpClient\Entities\Tag as TagEntity; use D3\KlicktippPhpClient\Entities\TagList; use D3\KlicktippPhpClient\Exceptions\BaseException; -use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\RequestOptions; class Tag extends Model { /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function index(): TagList { $data = $this->connection->requestAndParse( 'GET', - 'tag' + 'tag.json' ); return new TagList($data); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function get(string $tagId): TagEntity { $data = $this->connection->requestAndParse( 'GET', - 'tag/'.urlencode(trim($tagId)) + 'tag/'.urlencode(trim($tagId)).'.json' ); return new TagEntity($data); } /** - * @throws BaseException|GuzzleException + * @return string - new tag id + * @throws BaseException */ - public function create(string $name): array + public function create(string $name): string { - return $this->connection->requestAndParse( - 'POST', - 'tag/', - [ - RequestOptions::FORM_PARAMS => [ - 'name' => trim($name), - ], - ] + return current( + $this->connection->requestAndParse( + 'POST', + 'tag.json', + [ + RequestOptions::FORM_PARAMS => [ + 'name' => trim($name), + ], + ] + ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ - public function update(string $tagId, string $newName): array + public function update(string $tagId, string $newName): bool { - return $this->connection->requestAndParse( - 'PUT', - 'tag/'.urlencode(trim($tagId)), - [ - RequestOptions::FORM_PARAMS => [ - 'name' => trim($newName), - ], - ] + return (bool) current( + $this->connection->requestAndParse( + 'PUT', + 'tag/'.urlencode(trim($tagId)).'.json', + [ + RequestOptions::FORM_PARAMS => [ + 'name' => trim($newName), + ], + ] + ) ); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ - public function delete(string $tagId): array + public function delete(string $tagId): bool { - return $this->connection->requestAndParse( - 'DELETE', - 'tag/'.urlencode(trim($tagId)) + return (bool) current( + $this->connection->requestAndParse( + 'DELETE', + 'tag/'.urlencode(trim($tagId)).'.json' + ) ); } } diff --git a/tests/integration/Resources/TagTest.php b/tests/integration/Resources/TagTest.php new file mode 100644 index 0000000..5164596 --- /dev/null +++ b/tests/integration/Resources/TagTest.php @@ -0,0 +1,200 @@ + + * @link https://www.oxidmodule.com + */ + +namespace D3\KlicktippPhpClient\tests\integration\Resources; + +use D3\KlicktippPhpClient\Entities\Tag as TagEntity; +use D3\KlicktippPhpClient\Entities\TagList; +use D3\KlicktippPhpClient\Exceptions\BaseException; +use D3\KlicktippPhpClient\Resources\Tag; +use D3\KlicktippPhpClient\tests\integration\IntegrationTestCase; +use Generator; +use GuzzleHttp\Psr7\Response; +use Psr\Http\Message\ResponseInterface; +use ReflectionException; + +/** + * @coversNothing + */ +class TagTest extends IntegrationTestCase +{ + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\Tag::index + * @dataProvider indexDataProvider + */ + public function testIndex(ResponseInterface $response, ?TagList $expected, bool $expectException = false) + { + $sut = new Tag($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'index', + ) + ); + } + + public static function indexDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '{ + "12514414": "tagName2", + "12514413": "tagName" + }'), new TagList([ + "12514414" => "tagName2", + "12514413" => "tagName", + ])]; + yield 'wrong request type' => [new Response(405, [], '"No route found for \"DELETE https:\/\/api-internal.klicktipp.com\/api\/tag.json\": Method Not Allowed (Allow: GET, POST)"'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\Tag::get + * @dataProvider getDataProvider + */ + public function testGet(ResponseInterface $response, ?TagEntity $expected, bool $expectException = false) + { + $sut = new Tag($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'get', + ['12514414'] + ) + ); + } + + public static function getDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '{ + "tagid": "12514414", + "name": "tagName2", + "text": "" + }'), new TagEntity([ + "tagid" => "12514414", + "name" => "tagName2", + "text" => "", + ])]; + yield 'unknown id' => [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\Tag::create + * @dataProvider createDataProvider + */ + public function testCreate(ResponseInterface $response, ?string $expected, bool $expectException = false) + { + $sut = new Tag($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'create', + ['newTagName'] + ) + ); + } + + public static function createDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '[12494414]'), '12494414']; + yield 'missing or empty tag name' => [new Response(406, [], '["Tag konnte nicht erstellt werden."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\Tag::update + * @dataProvider updateDataProvider + */ + public function testUpdate(ResponseInterface $response, ?bool $expected, bool $expectException = false) + { + $sut = new Tag($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'update', + ['12494414', 'tagName'] + ) + ); + } + + public static function updateDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '[true]'), true]; + yield 'unknown tag' => [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\Tag::delete + * @dataProvider deleteDataProvider + */ + public function testDelete(ResponseInterface $response, ?bool $expected, bool $expectException = false) + { + $sut = new Tag($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 tag' => [new Response(404, [], '["Kein Tag mit dieser ID."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } +}