Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Php client call alma api cancel subscription 410 treatment #92

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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