add create, update and delete endpoints for fields

This commit is contained in:
Daniel Seifert 2025-01-03 09:42:12 +01:00
bovenliggende cc3814bbcd
commit 63530475cf
2 gewijzigde bestanden met toevoegingen van 144 en 0 verwijderingen

Bestand weergeven

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

Bestand weergeven

@ -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];
}
}