diff --git a/src/Suite/Api/AC/Program.php b/src/Suite/Api/AC/Program.php index 5eccf83..469d75b 100644 --- a/src/Suite/Api/AC/Program.php +++ b/src/Suite/Api/AC/Program.php @@ -8,6 +8,9 @@ class Program { + const CALLBACK_STATUS_DONE = 'done'; + const CALLBACK_STATUS_CANCELED = 'canceled'; + /* @var Client */ private $apiClient; @@ -21,45 +24,34 @@ public function __construct(Client $apiClient, EndPoints $endPoints) $this->endPoints = $endPoints; } - public function programCallbackWithUserId(int $customerId, string $triggerId, int $userId) - { - $this->programCallback($customerId, $triggerId, $userId, null); - } - - public function programCallbackWithListId(int $customerId, string $triggerId, int $listId) - { - $this->programCallback($customerId, $triggerId, null, $listId); - } - - public function programCallbackCancel(int $customerId, string $triggerId) - { - try - { - $this->apiClient->delete($this->endPoints->programCallbackUrl($customerId, $triggerId), []); - } - catch (Error $ex) - { - $this->throwRequestFailedException($ex); - } - } - - private function programCallback(int $customerId, string $triggerId, $userId, $listId) + private function programCallback(int $customerId, string $triggerId, $userId, $listId, string $status = self::CALLBACK_STATUS_DONE) { try { $this->apiClient->post($this->endPoints->programCallbackUrl($customerId, $triggerId),[ 'user_id' => $userId, 'list_id' => $listId, + 'status' => $status, ]); } catch (Error $ex) { - $this->throwRequestFailedException($ex); + throw new RequestFailed('Program callback failed: ' . $ex->getMessage(), $ex->getCode(), $ex); } } - private function throwRequestFailedException($ex) + public function programCallbackWithUserId(int $customerId, string $triggerId, int $userId) + { + $this->programCallback($customerId, $triggerId, $userId, null); + } + + public function programCallbackWithListId(int $customerId, string $triggerId, int $listId) + { + $this->programCallback($customerId, $triggerId, null, $listId); + } + + public function programCallbackCancel(int $customerId, string $triggerId) { - throw new RequestFailed('Program callback failed: ' . $ex->getMessage(), $ex->getCode(), $ex); + $this->programCallback($customerId, $triggerId, null, null, Program::CALLBACK_STATUS_CANCELED); } } diff --git a/test/unit/Suite/Api/AC/ProgramTest.php b/test/unit/Suite/Api/AC/ProgramTest.php index 5d1c137..32a9096 100644 --- a/test/unit/Suite/Api/AC/ProgramTest.php +++ b/test/unit/Suite/Api/AC/ProgramTest.php @@ -33,6 +33,7 @@ public function programCallbackWithUserId_Perfect_Perfect() [ 'user_id' => self::USER_ID, 'list_id' => null, + 'status' => Program::CALLBACK_STATUS_DONE, ] ); @@ -59,6 +60,7 @@ public function programCallbackWithListId_Perfect_Perfect() [ 'user_id' => null, 'list_id' => self::LIST_ID, + 'status' => Program::CALLBACK_STATUS_DONE ] ); @@ -81,14 +83,20 @@ public function programCallbackWithListId_postThrowsError_ThrowsRequestFailExcep */ public function programCallbackCancel_Perfect_Perfect() { - $this->expectApiCallSuccess([], 'delete'); + $this->expectApiCallSuccess( + [ + 'user_id' => 0, + 'list_id' => null, + 'status' => Program::CALLBACK_STATUS_CANCELED + ] + ); $this->program->programCallbackCancel($this->customerId, self::TRIGGER_ID); } - private function expectApiCallSuccess(array $postParams, string $httpVerb = 'post') + private function expectApiCallSuccess(array $postParams) { - $this->apiClient->expects($this->once())->method($httpVerb)->with( + $this->apiClient->expects($this->once())->method('post')->with( $this->endPoints->programCallbackUrl($this->customerId, self::TRIGGER_ID), $postParams )->willReturn($this->apiSuccess()); }