From 0e732ba0c71d27b9a8636b93daa1af77f59a1459 Mon Sep 17 00:00:00 2001 From: Peter Kozma Date: Mon, 12 Feb 2024 14:56:42 +0100 Subject: [PATCH] CR: code style AUT-2568 --- test/unit/Suite/Api/ContactListTest.php | 217 ++++++++++++------------ 1 file changed, 110 insertions(+), 107 deletions(-) diff --git a/test/unit/Suite/Api/ContactListTest.php b/test/unit/Suite/Api/ContactListTest.php index baa7c4c..2c8dc89 100644 --- a/test/unit/Suite/Api/ContactListTest.php +++ b/test/unit/Suite/Api/ContactListTest.php @@ -7,20 +7,9 @@ class ContactListTest extends TestCase { - /** - * @var Contactlist - */ - private $listService; - - /** - * @var int - */ - private $contactListId = 654321; - - /** - * @var string - */ - private $listName = 'list_name'; + private Contactlist $listService; + private int $contactListId = 654321; + private string $listName = 'list_name'; protected function setUp(): void { @@ -33,14 +22,15 @@ protected function setUp(): void /** * @test */ - public function createContactList_Perfect_Perfect() + public function createContactList_Perfect_Perfect(): void { $contactIds = [1, 2, 3]; - $this->apiClient->expects($this->once())->method('post') + $this->apiClient + ->method('post') ->with($this->endPoints->createContactList($this->customerId), [ - 'name' => $this->listName, - 'key_id' => 'id', - 'external_ids' => $contactIds, + 'name' => $this->listName, + 'key_id' => 'id', + 'external_ids' => $contactIds, ]) ->willReturn($this->apiSuccess(['id' => $this->contactListId])); @@ -51,13 +41,11 @@ public function createContactList_Perfect_Perfect() /** * @test */ - public function createContactList_NoContactIdsGiven_ListCreated() + public function createContactList_NoContactIdsGiven_ListCreated(): void { $contactIds = []; - $this->apiClient->expects($this->once())->method('post') - ->with($this->endPoints->createContactList($this->customerId), [ - 'name' => $this->listName, - ]) + $this->apiClient + ->method('post') ->willReturn($this->apiSuccess(['id' => $this->contactListId])); $contactListId = $this->listService->createContactList($this->customerId, $this->listName, $contactIds); @@ -67,19 +55,20 @@ public function createContactList_NoContactIdsGiven_ListCreated() /** * @test */ - public function createContactList_ApiCallFails_ExceptionThrown() + public function createContactList_ApiCallFails_ExceptionThrown(): void { - $this->apiClient->expects($this->once())->method('post')->willReturn($this->apiFailure()); + $this->apiClient + ->method('post') + ->willReturn($this->apiFailure()); $this->expectException(RequestFailed::class); $this->listService->createContactList($this->customerId, $this->listName, []); } - /** * @test */ - public function createContactList_ContactListNameIsEmptyString_SuiteAPINotCalled() + public function createContactList_ContactListNameIsEmptyString_SuiteAPINotCalled(): void { $this->apiClient->expects($this->never())->method('post'); $this->expectException(InvalidArgumentException::class); @@ -87,18 +76,19 @@ public function createContactList_ContactListNameIsEmptyString_SuiteAPINotCalled $this->listService->createContactList($this->customerId, ' '); } - /** * @test */ - public function getContactLists_Perfect_Perfect() + public function getContactLists_Perfect_Perfect(): void { - $contactLists = array( + $contactLists = [ $this->contactListData('id1', 'contact list 1'), $this->contactListData('id2', 'contact list 2'), - ); + ]; - $this->apiClient->expects($this->once())->method('get')->with($this->endPoints->contactLists($this->customerId)) + $this->apiClient + ->method('get') + ->with($this->endPoints->contactLists($this->customerId)) ->willReturn($this->apiSuccess($contactLists)); $returnedContactLists = $this->listService->getContactLists($this->customerId); @@ -106,19 +96,20 @@ public function getContactLists_Perfect_Perfect() $this->assertEquals($contactLists, $returnedContactLists); } - /** * @test */ - public function findContactListByName_Perfect_Perfect() + public function findContactListByName_Perfect_Perfect(): void { - $contactLists = array( + $contactLists = [ $this->contactListData('id1', 'contact list 1'), $this->contactListData('id2', 'contact list 2'), $this->contactListData($this->contactListId, $this->listName), - ); + ]; - $this->apiClient->expects($this->once())->method('get')->with($this->endPoints->contactLists($this->customerId)) + $this->apiClient + ->method('get') + ->with($this->endPoints->contactLists($this->customerId)) ->willReturn($this->apiSuccess($contactLists)); $contactListId = $this->listService->findContactListByName($this->customerId, $this->listName); @@ -130,7 +121,7 @@ public function findContactListByName_Perfect_Perfect() * @test * @dataProvider contactListNameProvider */ - public function findContactListByName_CasesDoNotMatch_ContactListIdStillReturned($contactListName) + public function findContactListByName_CasesDoNotMatch_ContactListIdStillReturned($contactListName): void { $contactLists = [ $this->contactListData('id1', 'contact list 1'), @@ -138,7 +129,8 @@ public function findContactListByName_CasesDoNotMatch_ContactListIdStillReturned $this->contactListData($this->contactListId, $contactListName), ]; - $this->apiClient->expects($this->once())->method('get')->with($this->endPoints->contactLists($this->customerId)) + $this->apiClient + ->method('get') ->willReturn($this->apiSuccess($contactLists)); $contactListId = $this->listService->findContactListByName($this->customerId, $contactListName); @@ -146,28 +138,29 @@ public function findContactListByName_CasesDoNotMatch_ContactListIdStillReturned $this->assertEquals($this->contactListId, $contactListId); } - public function contactListNameProvider() + public function contactListNameProvider(): array { return [ - 'upperCase' => [strtoupper($this->listName)], - 'lowerCase' => [strtolower($this->listName)], - 'spaceAfter' => [$this->listName . " "], - 'spaceBefore' => [" " . $this->listName], - 'spaceInContactListName' => [" my very best contact list "] + 'upperCase' => [strtoupper($this->listName)], + 'lowerCase' => [strtolower($this->listName)], + 'spaceAfter' => [$this->listName . " "], + 'spaceBefore' => [" " . $this->listName], + 'spaceInContactListName' => [" my very best contact list "], ]; } /** * @test */ - public function findContactListByName_ListDoesNotExist_ReturnsNull() + public function findContactListByName_ListDoesNotExist_ReturnsNull(): void { - $contactLists = array( + $contactLists = [ $this->contactListData('id1', 'contact list 1'), $this->contactListData('id2', 'contact list 2'), - ); + ]; - $this->apiClient->expects($this->once())->method('get')->with($this->endPoints->contactLists($this->customerId)) + $this->apiClient + ->method('get') ->willReturn($this->apiSuccess($contactLists)); $contactListId = $this->listService->findContactListByName($this->customerId, $this->listName); @@ -178,13 +171,19 @@ public function findContactListByName_ListDoesNotExist_ReturnsNull() /** * @test */ - public function addToContactList_Perfect_Perfect() + public function addToContactList_Perfect_Perfect(): void { $contactIds = [1, 2, 3]; - $this->apiClient->expects($this->once())->method('post')->with($this->endPoints->addToContactList($this->customerId, $this->contactListId), [ - 'key_id' => 'id', - 'external_ids' => $contactIds - ])->willReturn($this->apiSuccess()); + $this->apiClient + ->expects($this->once()) + ->method('post') + ->with( + $this->endPoints->addToContactList($this->customerId, $this->contactListId), + [ + 'key_id' => 'id', + 'external_ids' => $contactIds, + ] + )->willReturn($this->apiSuccess()); $this->listService->addToContactList($this->customerId, $this->contactListId, $contactIds); } @@ -192,13 +191,12 @@ public function addToContactList_Perfect_Perfect() /** * @test */ - public function addToContactList_ApiFailure_ThrowsException() + public function addToContactList_ApiFailure_ThrowsException(): void { $contactIds = [1, 2, 3]; - $this->apiClient->expects($this->once())->method('post')->with($this->endPoints->addToContactList($this->customerId, $this->contactListId), [ - 'key_id' => 'id', - 'external_ids' => $contactIds - ])->willReturn($this->apiFailure()); + $this->apiClient + ->method('post') + ->willReturn($this->apiFailure()); $this->expectException(RequestFailed::class); $this->listService->addToContactList($this->customerId, $this->contactListId, $contactIds); @@ -207,13 +205,18 @@ public function addToContactList_ApiFailure_ThrowsException() /** * @test */ - public function replaceContactList_Perfect_Perfect() + public function replaceContactList_Perfect_Perfect(): void { $contactIds = [1, 2, 3]; - $this->apiClient->expects($this->once())->method('post')->with($this->endPoints->replaceContactList($this->customerId, $this->contactListId), [ - 'key_id' => 'id', - 'external_ids' => $contactIds - ])->willReturn($this->apiSuccess()); + $this->apiClient + ->method('post') + ->with( + $this->endPoints->replaceContactList($this->customerId, $this->contactListId), + [ + 'key_id' => 'id', + 'external_ids' => $contactIds, + ] + )->willReturn($this->apiSuccess()); $contactListId = $this->listService->replaceContactList($this->customerId, $this->contactListId, $contactIds); $this->assertEquals($this->contactListId, $contactListId); @@ -222,13 +225,12 @@ public function replaceContactList_Perfect_Perfect() /** * @test */ - public function replaceContactList_ApiFailure_ThrowsException() + public function replaceContactList_ApiFailure_ThrowsException(): void { $contactIds = [1, 2, 3]; - $this->apiClient->expects($this->once())->method('post')->with($this->endPoints->replaceContactList($this->customerId, $this->contactListId), [ - 'key_id' => 'id', - 'external_ids' => $contactIds - ])->willReturn($this->apiFailure()); + $this->apiClient + ->method('post') + ->willReturn($this->apiFailure()); $this->expectException(RequestFailed::class); $this->listService->replaceContactList($this->customerId, $this->contactListId, $contactIds); @@ -237,12 +239,13 @@ public function replaceContactList_ApiFailure_ThrowsException() /** * @test */ - public function getContactIdsInList_Perfect_Perfect() + public function getContactIdsInList_Perfect_Perfect(): void { $response = ['value' => [1, 2, 3], 'next' => null]; - $this->apiClient->expects($this->once())->method('get')->with( - $this->endPoints->contactIdsInList($this->customerId, $this->contactListId) - )->willReturn($this->apiSuccess($response)); + $this->apiClient + ->method('get') + ->with($this->endPoints->contactIdsInList($this->customerId, $this->contactListId)) + ->willReturn($this->apiSuccess($response)); $result = $this->listService->getContactIdsInList($this->customerId, $this->contactListId); $this->assertEquals($response, $result); @@ -251,112 +254,112 @@ public function getContactIdsInList_Perfect_Perfect() /** * @test */ - public function getContactIdsInList_ApiCallFails_ExceptionThrown() + public function getContactIdsInList_ApiCallFails_ExceptionThrown(): void { - $this->apiClient->expects($this->once())->method('get')->will($this->apiFailure()); + $this->apiClient->method('get')->will($this->apiFailure()); $this->expectException(RequestFailed::class); $this->listService->getContactIdsInList($this->customerId, $this->contactListId); } - /** * @test */ - public function getContactsOfList_Perfect_Perfect() + public function getContactsOfList_Perfect_Perfect(): void { $limit = 100; $offset = 200; $chunk = [1, 2, 3]; - $this->apiClient->expects($this->once())->method('get')->with( - $this->endPoints->contactsOfList($this->customerId, $this->contactListId, $limit, $offset) - )->willReturn($this->apiSuccess($chunk)); + $this->apiClient + ->method('get') + ->with($this->endPoints->contactsOfList($this->customerId, $this->contactListId, $limit, $offset)) + ->willReturn($this->apiSuccess($chunk)); $result = $this->listService->getContactsOfList($this->customerId, $this->contactListId, $limit, $offset); $this->assertEquals($chunk, $result); } - /** * @test */ - public function getContactsOfList_NoDataInResult_EmptyArrayReturned() + public function getContactsOfList_NoDataInResult_EmptyArrayReturned(): void { $limit = 100; $offset = 200; - $this->apiClient->expects($this->once())->method('get')->with( - $this->endPoints->contactsOfList($this->customerId, $this->contactListId, $limit, $offset) - )->willReturn($this->apiSuccess()); + $this->apiClient + ->method('get') + ->with($this->endPoints->contactsOfList($this->customerId, $this->contactListId, $limit, $offset)) + ->willReturn($this->apiSuccess()); $result = $this->listService->getContactsOfList($this->customerId, $this->contactListId, $limit, $offset); $this->assertEquals([], $result); } - /** * @test */ - public function getContactsOfList_ApiCallFails_ExceptionThrown() + public function getContactsOfList_ApiCallFails_ExceptionThrown(): void { - $this->apiClient->expects($this->once())->method('get')->will($this->apiFailure()); + $this->apiClient->method('get')->will($this->apiFailure()); $this->expectException(RequestFailed::class); $this->listService->getContactsOfList($this->customerId, $this->contactListId, 100, 200); } - /** * @test */ - public function getContactListChunkIterator_Perfect_Perfect() + public function getContactListChunkIterator_Perfect_Perfect(): void { $chunkSize = 3; $iterator = $this->listService->getListChunkIterator($this->customerId, $this->contactListId, $chunkSize); $this->assertInstanceOf(\Traversable::class, $iterator); } - /** * @test */ - public function deleteContactsFromList_Perfect_Perfect() + public function deleteContactsFromList_Perfect_Perfect(): void { $contactIds = [1, 2, 3]; - $this->apiClient->expects($this->once())->method('post')->with( - $this->endPoints->deleteContactsFromList($this->customerId, $this->contactListId), ([ - 'key_id' => 'id', - 'external_ids' => $contactIds - ]) - ); + $this->apiClient + ->expects($this->once()) + ->method('post') + ->with( + $this->endPoints->deleteContactsFromList($this->customerId, $this->contactListId), + [ + 'key_id' => 'id', + 'external_ids' => $contactIds, + ] + ); $this->listService->deleteContactsFromList($this->customerId, $this->contactListId, $contactIds); } - /** * @test */ - public function deleteContactsFromList_postThrowsError_ThrowsRequestFailException() + public function deleteContactsFromList_postThrowsError_ThrowsRequestFailException(): void { - $this->apiClient->expects($this->once())->method('post')->willThrowException(new Error()); + $this->apiClient->method('post')->willThrowException(new Error()); $this->expectException(RequestFailed::class); $this->listService->deleteContactsFromList($this->customerId, $this->contactListId, []); } - public function contactListData($id, $name) + public function contactListData($id, $name): array { - return array( + return [ 'id' => $id, 'name' => $name, 'created' => '2014-11-05 12:34:56', - 'type' => 0 - ); + 'type' => 0, + ]; } - private function apiFailure() + private function apiFailure(): \PHPUnit\Framework\MockObject\Stub\Exception { return $this->throwException(new Error(self::API_FAILURE_TEXT, self::API_FAILURE_CODE)); }