diff --git a/lib/Api/Contacts.php b/lib/Api/Contacts.php index 14265ea..e222255 100644 --- a/lib/Api/Contacts.php +++ b/lib/Api/Contacts.php @@ -337,6 +337,72 @@ public function subtractPoints($id, $points, array $parameters = []) return $this->makeRequest('contacts/'.$id.'/points/minus/'.$points, $parameters, 'POST'); } + /** + * Get all point group scores associated with contact. + */ + public function getPointGroupScores(int $contactId): array + { + return $this->makeRequest('contacts/'.$contactId.'/points/groups'); + } + + /** + * Get the contact score for a specified point group. + */ + public function getPointGroupScore(int $contactId, int $groupId): array + { + return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId); + } + + /** + * Increase the score of the contact point group. + * + * @param array $parameters 'eventName' and 'actionName' + */ + public function addPointGroupScore(int $contactId, int $groupId, int $points, array $parameters = []): array + { + return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/plus/'.$points, $parameters, 'POST'); + } + + /** + * Decrease the score of the contact point group. + * + * @param array $parameters 'eventName' and 'actionName' + */ + public function subtractPointGroupScore(int $contactId, int $groupId, int $points, array $parameters = []): array + { + return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/minus/'.$points, $parameters, 'POST'); + } + + /** + * Multiply the score of the contact point group. + * + * @param array $parameters 'eventName' and 'actionName' + */ + public function multiplyPointGroupScore(int $contactId, int $groupId, int $value, array $parameters = []): array + { + return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/times/'.$value, $parameters, 'POST'); + } + + /** + * Divide the score of the contact point group. + * + * @param array $parameters 'eventName' and 'actionName' + */ + public function dividePointGroupScore(int $contactId, int $groupId, int $value, array $parameters = []): array + { + return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/divide/'.$value, $parameters, 'POST'); + } + + /** + * Set the score of the contact point group. + * + * @param array $parameters 'eventName' and 'actionName' + */ + public function setPointGroupScore(int $contactId, int $groupId, int $points, array $parameters = []): array + { + return $this->makeRequest('contacts/'.$contactId.'/points/groups/'.$groupId.'/set/'.$points, $parameters, 'POST'); + } + /** * Adds Do Not Contact. * diff --git a/lib/Api/PointGroups.php b/lib/Api/PointGroups.php new file mode 100644 index 0000000..72195f0 --- /dev/null +++ b/lib/Api/PointGroups.php @@ -0,0 +1,41 @@ +assertErrors($response); } + public function testGetPointGroupScores(): void + { + $response = $this->api->create($this->testPayload); + $this->assertErrors($response); + $contact = $response[$this->api->itemName()]; + + // test empty group points list + $response = $this->api->getPointGroupScores($contact['id']); + $this->assertErrors($response); + $this->assertSame(0, $response['total']); + $this->assertIsArray($response['groupScores']); + $this->assertEmpty($response['groupScores']); + + // add score + $pointsToAdd = 5; + $pointGroupApi = $this->getContext('pointGroups'); + $response = $pointGroupApi->create(['name' => 'Group A']); + $pointGroup = $response[$pointGroupApi->itemName()]; + $response = $this->api->addPointGroupScore($contact['id'], $pointGroup['id'], $pointsToAdd); + $this->assertErrors($response); + $this->assertNotEmpty($response['groupScore'], 'Adding point group score to a contact with ID ='.$contact['id'].' was not successful'); + + // test get point group scores list + $response = $this->api->getPointGroupScores($contact['id']); + $this->assertErrors($response); + $this->assertSame(1, $response['total']); + $this->assertIsArray($response['groupScores']); + $this->assertCount(1, $response['groupScores']); + $this->assertSame(5, $response['groupScores'][0]['score']); + $this->assertSame($pointGroup['id'], $response['groupScores'][0]['group']['id']); + $this->assertSame($pointGroup['name'], $response['groupScores'][0]['group']['name']); + + $response = $this->api->delete($contact['id']); + $this->assertErrors($response); + } + + public function testAddPointGroupScore(): void + { + $pointsToAdd = 5; + $pointGroupApi = $this->getContext('pointGroups'); + $response = $pointGroupApi->create(['name' => 'Group A']); + $pointGroup = $response[$pointGroupApi->itemName()]; + + $response = $this->api->create($this->testPayload); + $this->assertErrors($response); + $contact = $response[$this->api->itemName()]; + + $response = $this->api->addPointGroupScore($contact['id'], $pointGroup['id'], $pointsToAdd); + $this->assertErrors($response); + $this->assertTrue(!empty($response['groupScore']), 'Adding point group score to a contact with ID ='.$contact['id'].' was not successful'); + + $response = $this->api->getPointGroupScore($contact['id'], $pointGroup['id']); + $this->assertErrors($response); + $this->assertSame($response['groupScore']['score'], $pointsToAdd, 'Point group score was not added accurately'); + + $response = $this->api->delete($contact['id']); + $this->assertErrors($response); + } + + public function testSubtractPointGroupScore(): void + { + $pointsToSubtract = 3; + $pointGroupApi = $this->getContext('pointGroups'); + $response = $pointGroupApi->create(['name' => 'Group B']); + $pointGroup = $response[$pointGroupApi->itemName()]; + + $response = $this->api->create($this->testPayload); + $this->assertErrors($response); + $contact = $response[$this->api->itemName()]; + + $response = $this->api->setPointGroupScore($contact['id'], $pointGroup['id'], 10); + $this->assertErrors($response); + + $response = $this->api->subtractPointGroupScore($contact['id'], $pointGroup['id'], $pointsToSubtract); + $this->assertErrors($response); + $this->assertTrue(!empty($response['groupScore']), 'Subtracting point group score from a contact with ID ='.$contact['id'].' was not successful'); + + $response = $this->api->getPointGroupScore($contact['id'], $pointGroup['id']); + $this->assertErrors($response); + $this->assertSame($response['groupScore']['score'], 10 - $pointsToSubtract, 'Point group score was not subtracted accurately'); + + $response = $this->api->delete($contact['id']); + $this->assertErrors($response); + } + + public function testMultiplyPointGroupScore(): void + { + $multiplier = 2; + $pointGroupApi = $this->getContext('pointGroups'); + $response = $pointGroupApi->create(['name' => 'Group C']); + $pointGroup = $response[$pointGroupApi->itemName()]; + + $response = $this->api->create($this->testPayload); + $this->assertErrors($response); + $contact = $response[$this->api->itemName()]; + + $response = $this->api->setPointGroupScore($contact['id'], $pointGroup['id'], 5); + $this->assertErrors($response); + + $response = $this->api->multiplyPointGroupScore($contact['id'], $pointGroup['id'], $multiplier); + $this->assertErrors($response); + $this->assertTrue(!empty($response['groupScore']), 'Multiplying point group score for a contact with ID ='.$contact['id'].' was not successful'); + + $response = $this->api->getPointGroupScore($contact['id'], $pointGroup['id']); + $this->assertErrors($response); + $this->assertSame($response['groupScore']['score'], 5 * $multiplier, 'Point group score was not multiplied accurately'); + + $response = $this->api->delete($contact['id']); + $this->assertErrors($response); + } + + public function testDividePointGroupScore(): void + { + $divisor = 4; + $pointGroupApi = $this->getContext('pointGroups'); + $response = $pointGroupApi->create(['name' => 'Group D']); + $pointGroup = $response[$pointGroupApi->itemName()]; + + $response = $this->api->create($this->testPayload); + $this->assertErrors($response); + $contact = $response[$this->api->itemName()]; + + $response = $this->api->setPointGroupScore($contact['id'], $pointGroup['id'], 20); + $this->assertErrors($response); + + $response = $this->api->dividePointGroupScore($contact['id'], $pointGroup['id'], $divisor); + $this->assertErrors($response); + $this->assertTrue(!empty($response['groupScore']), 'Dividing point group score for a contact with ID ='.$contact['id'].' was not successful'); + + $response = $this->api->getPointGroupScore($contact['id'], $pointGroup['id']); + $this->assertErrors($response); + $this->assertSame($response['groupScore']['score'], 20 / $divisor, 'Point group score was not divided accurately'); + + $response = $this->api->delete($contact['id']); + $this->assertErrors($response); + } + public function testBatchEndpoints() { $contact1 = $this->testPayload; diff --git a/tests/Api/PointGroupsTest.php b/tests/Api/PointGroupsTest.php new file mode 100644 index 0000000..50541c5 --- /dev/null +++ b/tests/Api/PointGroupsTest.php @@ -0,0 +1,50 @@ +api = $this->getContext('pointGroups'); + $this->testPayload = [ + 'name' => 'New Point Group', + 'description' => 'Description of the new point group', + ]; + } + + public function testGetList(): void + { + $this->standardTestGetList(); + } + + public function testGetListOfSpecificIds(): void + { + $this->standardTestGetListOfSpecificIds(); + } + + public function testCreateGetAndDelete(): void + { + $this->standardTestCreateGetAndDelete(); + } + + public function testEditPatch(): void + { + $editTo = [ + 'name' => 'Updated Point Group Name', + ]; + $this->standardTestEditPatch($editTo); + } + + public function testEditPut(): void + { + $this->standardTestEditPut(); + } + + public function testBatchEndpoints(): void + { + $this->standardTestBatchEndpoints(); + } +}