Skip to content

Commit

Permalink
Merge pull request #92 from alma/feature/ecom-1313-php-client-call-al…
Browse files Browse the repository at this point in the history
…ma-api-cancel-subscription-410-treatment

Php client call alma api cancel subscription 410 treatment
  • Loading branch information
Benjamin-Freoua-Alma authored Feb 21, 2024
2 parents 47a9de1 + 19835c7 commit e44d0c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Endpoints/Insurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Alma\API\Entities\Insurance\Contract;
use Alma\API\Entities\Insurance\File;
use Alma\API\Entities\Insurance\Subscription;
use Alma\API\Exceptions\InsuranceCancelPendingException;
use Alma\API\Exceptions\MissingKeyException;
use Alma\API\Exceptions\ParametersException;
use Alma\API\Exceptions\RequestException;
Expand Down Expand Up @@ -317,6 +318,7 @@ public function addCustomerSessionToRequest($request, $customerSessionId = null,
/**
* @param string $subscriptionId
* @return void
* @throws InsuranceCancelPendingException
* @throws ParametersException
* @throws RequestError
* @throws RequestException
Expand All @@ -329,6 +331,9 @@ public function cancelSubscription($subscriptionId)
$request = $this->request(self::INSURANCE_PATH . 'subscriptions/' . $subscriptionId . '/void');
$response = $request->post();

if ($response->responseCode === 410) {
throw new InsuranceCancelPendingException('Subscription can not be cancelled at this time');
}
if ($response->isError()) {
throw new RequestException($response->errorMessage, $request, $response);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/InsuranceCancelPendingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Alma\API\Exceptions;


class InsuranceCancelPendingException extends AlmaException
{
}
28 changes: 27 additions & 1 deletion tests/Unit/Legacy/Endpoints/InsuranceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Alma\API\Entities\Insurance\File;
use Alma\API\Entities\Insurance\Subscriber;
use Alma\API\Entities\Insurance\Subscription;
use Alma\API\Exceptions\InsuranceCancelPendingException;
use Alma\API\Exceptions\MissingKeyException;
use Alma\API\Exceptions\ParametersException;
use Alma\API\Exceptions\RequestException;
Expand Down Expand Up @@ -897,14 +898,16 @@ public function testSendCustomerCartCallApiPostCustomerCartWithCmsReferencesArra

/**
* @return void
* @throws InsuranceCancelPendingException
* @throws ParametersException
* @throws RequestError
* @throws RequestException
*/
public function testCancelSubscriptionCallRequestWithSubscriptionArrayPayloadAndThrowExceptionForResponseUpperThan400()
public function testCancelSubscriptionCallRequestWithSubscriptionArrayPayloadAndThrowExceptionForResponseUpperThan400ButNo410()
{
$this->expectException(RequestException::class);
$subscriptionCancelPayload = 'subscriptionId1';

$this->responseMock->shouldReceive('isError')->once()->andReturn(true);
$this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock);
$this->insuranceMock->shouldReceive('request')
Expand All @@ -914,6 +917,26 @@ public function testCancelSubscriptionCallRequestWithSubscriptionArrayPayloadAnd
$this->insuranceMock->cancelSubscription($subscriptionCancelPayload);
}

/**
* @return void
* @throws InsuranceCancelPendingException
* @throws ParametersException
* @throws RequestError
* @throws RequestException
*/
public function testCancelSubscriptionCallRequestWithSubscriptionArrayPayloadAndThrowInsuranceCancelPendingExceptionForResponse410()
{
$this->expectException(InsuranceCancelPendingException::class);
$subscriptionCancelPayload = 'subscriptionId1';
$this->responseMock->responseCode = 410;
$this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock);
$this->insuranceMock->shouldReceive('request')
->with(self::INSURANCE_SUBSCRIPTIONS_PATH . '/subscriptionId1/void')
->once()
->andReturn($this->requestObject);
$this->insuranceMock->cancelSubscription($subscriptionCancelPayload);
}

/**
* @dataProvider cancelSubscriptionErrorPayloadDataProvider
* @param $payload
Expand All @@ -926,6 +949,9 @@ public function testCheckSubscriptionIdFormatThrowParamsErrorForBadPayload($payl
$this->insuranceMock->checkSubscriptionIdFormat($payload);
}

/**
* @return array
*/
public function cancelSubscriptionErrorPayloadDataProvider()
{
return [
Expand Down

0 comments on commit e44d0c4

Please sign in to comment.