Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(delete-person): move logic to PersonRequestBuilder #100

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [Move generated Doc-Files to `out`-Folder](https://github.com/5pm-HDH/churchtools-api/pull/89)
- [Create UpdatableMode-Interface for type safety](https://github.com/5pm-HDH/churchtools-api/pull/93)
- [Status-Code handling and Exception-handling](https://github.com/5pm-HDH/churchtools-api/pull/99)
- [Refactor delete person](https://github.com/5pm-HDH/churchtools-api/pull/100)

### Fixed

Expand Down
32 changes: 17 additions & 15 deletions src/Requests/AbstractRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ public function get(): array
return $this->getModelClass()::createModelsFromArray($data);
}

/**
* Update object's data in ChurchTools by the given object ID.
* @param string $objectId
* @param array $data Key-Value pair with attributes
*/
protected function updateData(string $objectId, array $data): void
{
$url = $this->getApiEndpoint() . '/' . $objectId;

$client = CTClient::getClient();
$client->patch($url, ['json' => $data]);
}

/**
* Send Update-Request for given Model. Only update Attributes that are given with the updateAttributes-Parameter.
*
Expand Down Expand Up @@ -104,10 +91,25 @@ protected function updateDataForModel(UpdatableModel $model, string $modelId, ar
$this->updateData($modelId, $updateAttributes);
}


/**
* Update object's data in ChurchTools by the given object ID.
* @param string $objectId
* @param array $data Key-Value pair with attributes
*/
protected function updateData(string $objectId, array $data): void
{
$url = $this->getApiEndpoint() . '/' . $objectId;

$client = CTClient::getClient();
$client->patch($url, ['json' => $data]);
}

/**
* Delete the object in ChurchTools by the given ID.
* Delete the object in ChurchTools by given Id.
* @param string $modelId
*/
public function delete(string $modelId): void
public function deleteData(string $modelId): void
{
$url = $this->getApiEndpoint() . '/' . $modelId;

Expand Down
9 changes: 1 addition & 8 deletions src/Requests/PersonRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace CTApi\Requests;

use CTApi\Exceptions\CTModelException;
use CTApi\Models\Person;

class PersonRequest
Expand Down Expand Up @@ -58,12 +57,6 @@ public static function update(Person $person, array $attributesToUpdate = []): v
*/
public static function delete(Person $person): void
{
$id = $person->getId();

if (is_null($id)) {
throw new CTModelException("ID of Person cannot be null.");
}

(new PersonRequestBuilder())->delete($id);
(new PersonRequestBuilder())->delete($person);
}
}
11 changes: 11 additions & 0 deletions src/Requests/PersonRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public function update(Person $person, array $attributesToUpdate = []): void
}
}

public function delete(Person $person): void
{
$id = $person->getId();

if (is_null($id)) {
throw new CTModelException("ID of Person cannot be null.");
}

$this->deleteData($id);
}

protected function getApiEndpoint(): string
{
return '/api/persons';
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/HttpMock/CTClientMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function patch($uri, array $options = []): ResponseInterface
return CTResponse::createEmpty();
}

public function delete($uri, array $options = []): ResponseInterface
{
$this->addMethodCall("DELETE", $uri, $options);
return CTResponse::createEmpty();
}

protected function convertGETRequestToResponse($uri, $options): ResponseInterface
{
$responseData = HttpMockDataResolver::resolveEndpoint($uri, $options);
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/Requests/PersonRequestBuilderDeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php


namespace Tests\Unit\Requests;


use CTApi\CTClient;
use CTApi\Exceptions\CTModelException;
use CTApi\Models\Person;
use CTApi\Requests\PersonRequest;
use Tests\Unit\TestCaseHttpMocked;

class PersonRequestBuilderDeleteTest extends TestCaseHttpMocked
{
public function testDelete()
{
$person = Person::createModelFromData([
'id' => '777',
'firstName' => 'Jane',
'lastName' => 'Mustermann',
'birthName' => 'Doe',
]);

PersonRequest::delete($person);

$client = CTClient::getClient();
$this->assertRequestCallExists("DELETE", "/api/persons/777");
}

public function testDeleteWithoutId()
{
$this->expectException(CTModelException::class);
$person = new Person();
PersonRequest::delete($person);
}
}