diff --git a/CHANGELOG.md b/CHANGELOG.md index 791e321e..a6c5fbab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Requests/AbstractRequestBuilder.php b/src/Requests/AbstractRequestBuilder.php index 44888230..7a0021f0 100644 --- a/src/Requests/AbstractRequestBuilder.php +++ b/src/Requests/AbstractRequestBuilder.php @@ -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. * @@ -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; diff --git a/src/Requests/PersonRequest.php b/src/Requests/PersonRequest.php index 229c3d5d..fe84edab 100644 --- a/src/Requests/PersonRequest.php +++ b/src/Requests/PersonRequest.php @@ -3,7 +3,6 @@ namespace CTApi\Requests; -use CTApi\Exceptions\CTModelException; use CTApi\Models\Person; class PersonRequest @@ -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); } } \ No newline at end of file diff --git a/src/Requests/PersonRequestBuilder.php b/src/Requests/PersonRequestBuilder.php index 4673601f..b728566c 100644 --- a/src/Requests/PersonRequestBuilder.php +++ b/src/Requests/PersonRequestBuilder.php @@ -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'; diff --git a/tests/unit/HttpMock/CTClientMock.php b/tests/unit/HttpMock/CTClientMock.php index a29fc233..17275a70 100644 --- a/tests/unit/HttpMock/CTClientMock.php +++ b/tests/unit/HttpMock/CTClientMock.php @@ -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); diff --git a/tests/unit/Requests/PersonRequestBuilderDeleteTest.php b/tests/unit/Requests/PersonRequestBuilderDeleteTest.php new file mode 100644 index 00000000..8207e219 --- /dev/null +++ b/tests/unit/Requests/PersonRequestBuilderDeleteTest.php @@ -0,0 +1,36 @@ + '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); + } +} \ No newline at end of file