Skip to content

Commit

Permalink
Merge pull request #91 from alma/develop
Browse files Browse the repository at this point in the history
Release 2.0.4
  • Loading branch information
Benjamin-Freoua-Alma authored Feb 16, 2024
2 parents 2c73716 + 47a9de1 commit ce024b0
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

v2.0.4
-------
* Fix json_decode in getSubscriptionDetails
* Add subscription pending_cancellation status

v2.0.3
-------
* Added order_id in subscription model
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "alma/alma-php-client",
"description": "PHP API client for the Alma payments API",
"version": "2.0.3",
"version": "2.0.4",
"type": "library",
"require": {
"php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1 || ~8.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class Client
{
const VERSION = '2.0.3';
const VERSION = '2.0.4';

const LIVE_MODE = 'live';
const TEST_MODE = 'test';
Expand Down
12 changes: 10 additions & 2 deletions src/Endpoints/Insurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Alma\API\Exceptions\MissingKeyException;
use Alma\API\Exceptions\ParametersException;
use Alma\API\Exceptions\RequestException;
use Alma\API\Exceptions\ResponseException;
use Alma\API\Lib\ArrayUtils;
use Alma\API\Lib\InsuranceValidator;
use Alma\API\RequestError;
Expand Down Expand Up @@ -144,15 +145,17 @@ public function subscription(
}

/**
* @param $subscriptionIds
* @return array json_decode in response constructor
* @throws RequestException
* @throws ParametersException
* @throws RequestError
* @throws RequestException
* @throws ResponseException
*/
public function getSubscription($subscriptionIds)
{
$this->insuranceValidator->checkSubscriptionIds($subscriptionIds);
$response = $this->request(
$response = $this->request(
self::INSURANCE_PATH . 'subscriptions'
)->setQueryParams(
$subscriptionIds
Expand All @@ -161,6 +164,10 @@ public function getSubscription($subscriptionIds)
if ($response->isError()) {
throw new RequestException($response->errorMessage, null, $response);
}
$subscriptions = $response->json['subscriptions'];
if (!count($subscriptions)) {
throw new ResponseException('No data was found', 404);
}

return $response->json;
}
Expand Down Expand Up @@ -242,6 +249,7 @@ protected function buildSubscriptionData($subscriptionArray, $orderId, $paymentI
) {
$subscriptionData['payment_id'] = $paymentId;
}

return $subscriptionData;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Entities/Insurance/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ class Subscription
*/
private $callbackUrl;

CONST STATE_STARTED = 'started';
CONST STATE_FAILED = 'failed';
CONST STATE_CANCELLED = 'canceled';
CONST STATE_PENDING = 'pending';
const STATE_STARTED = 'started';
const STATE_FAILED = 'failed';
const STATE_CANCELLED = 'canceled';
const STATE_PENDING = 'pending';
const STATE_PENDING_CANCELLATION = 'pending_cancellation';

/**
* @param string $contractId
Expand Down
64 changes: 59 additions & 5 deletions tests/Unit/Legacy/Endpoints/InsuranceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Alma\API\Exceptions\MissingKeyException;
use Alma\API\Exceptions\ParametersException;
use Alma\API\Exceptions\RequestException;
use Alma\API\Exceptions\ResponseException;
use Alma\API\Lib\ArrayUtils;
use Alma\API\Lib\InsuranceValidator;
use Alma\API\Request;
Expand Down Expand Up @@ -439,6 +440,23 @@ public function getSubscriptionsRightParamDataProvider()
];
}

/**
* @return array[]
*/
public function subscriptionIdInvalidDataProvider()
{
return [
'Test with subscription id invalid' => [
[
'id' => 'toto'
],
'{
"subscriptions": []
}'
]
];
}

/**
* @return void
*/
Expand Down Expand Up @@ -728,26 +746,33 @@ public function testSubscriptionMethodExist()
* @throws ParametersException
* @throws RequestError
* @throws RequestException
* @throws ResponseException
*/
public function testGetSubscriptionRequestIsCalled($subscriptionIds)
public function testGetSubscriptionRequestIsCalled($subscriptionIds, $json)
{
$this->responseMock->json = json_decode($json, true);
$this->responseMock->shouldReceive('isError')->once()->andReturn(false);
$this->requestObject->shouldReceive('setQueryParams')->once()->andReturn($this->requestObject);
$this->requestObject->shouldReceive('get')->once()->andReturn($this->responseMock);
$this->insuranceValidatorMock->shouldReceive('checkSubscriptionIds')->once();
$this->requestObject->shouldReceive('setQueryParams')
->once()
->with($subscriptionIds)
->andReturn($this->requestObject);
$this->insuranceMock->shouldReceive('request')
->with(self::INSURANCE_SUBSCRIPTIONS_PATH)
->once()
->andReturn($this->requestObject);

$this->insuranceValidatorMock->shouldReceive('checkSubscriptionIds')->once();
$this->insuranceMock->getSubscription($subscriptionIds);
}

/**
* @dataProvider getSubscriptionsRightParamDataProvider
* @param $subscriptionIds
* @throws ParametersException
* @throws RequestError
* @throws RequestException
* @throws ResponseException
*/
public function testGetSubscriptionThrowExceptionIfResponseHasAnError($subscriptionIds)
{
Expand All @@ -773,6 +798,7 @@ public function testGetSubscriptionThrowExceptionIfResponseHasAnError($subscript
* @throws ParametersException
* @throws RequestError
* @throws RequestException
* @throws ResponseException
*/
public function testGetSubscriptionThrowExceptionIfWeDontSendAnArray($subscriptionIds)
{
Expand All @@ -795,7 +821,7 @@ public function testGetSubscriptionThrowExceptionIfWeDontSendAnArray($subscripti
*/
public function testGetSubscriptionsReturnApiResponse($subscriptionIds, $json)
{
$this->responseMock->json = $json;
$this->responseMock->json = json_decode($json, true);
$this->responseMock->shouldReceive('isError')->once()->andReturn(false);
$this->requestObject->shouldReceive('get')->once()->andReturn($this->responseMock);
$this->requestObject->shouldReceive('setQueryParams')->once()->andReturn($this->requestObject);
Expand All @@ -805,7 +831,7 @@ public function testGetSubscriptionsReturnApiResponse($subscriptionIds, $json)
->andReturn($this->requestObject);
$this->insuranceValidatorMock->shouldReceive('checkSubscriptionIds')->once();

$this->assertEquals($json, $this->insuranceMock->getSubscription($subscriptionIds));
$this->assertEquals($this->responseMock->json, $this->insuranceMock->getSubscription($subscriptionIds));
}

/**
Expand Down Expand Up @@ -917,4 +943,32 @@ public function cancelSubscriptionErrorPayloadDataProvider()
],
];
}

/**
* @dataProvider subscriptionIdInvalidDataProvider
* @param $subscriptionIdInvalid
* @param $jsonReturnRequest
* @return void
* @throws ParametersException
* @throws RequestError
* @throws RequestException
*/
public function testGetSubscriptionIfReturnZeroDataThrowException($subscriptionIdInvalid, $jsonReturnRequest)
{
$this->responseMock->json = json_decode($jsonReturnRequest, true);
$this->responseMock->shouldReceive('isError')->once()->andReturn(false);
$this->requestObject->shouldReceive('setQueryParams')->once()->andReturn($this->requestObject);
$this->requestObject->shouldReceive('get')->once()->andReturn($this->responseMock);
$this->insuranceMock->shouldReceive('request')
->with(self::INSURANCE_SUBSCRIPTIONS_PATH)
->once()
->andReturn($this->requestObject);

$this->insuranceValidatorMock->shouldReceive('checkSubscriptionIds')
->once()
->with($subscriptionIdInvalid);

$this->expectException(ResponseException::class);
$this->insuranceMock->getSubscription($subscriptionIdInvalid);
}
}

0 comments on commit ce024b0

Please sign in to comment.