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

Release 2.0.4 #91

Merged
merged 12 commits into from
Feb 16, 2024
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 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);
}
}