8
0
Dieser Commit ist enthalten in:
Daniel Seifert 2024-12-29 23:33:09 +01:00
Ursprung c488e9fde0
Commit 839ffd6346
Signiert von: DanielS
GPG-Schlüssel-ID: 6A513E13AEE66170
2 geänderte Dateien mit 236 neuen und 30 gelöschten Zeilen

Datei anzeigen

@ -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'
)
);
}
}

Datei anzeigen

@ -0,0 +1,200 @@
<?php
/**
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @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];
}
}