Skip to content

Commit

Permalink
Call endpoint with CMS references
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Freoua-Alma committed Feb 13, 2024
1 parent 431642e commit 2d69016
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Endpoints/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Base
public function __construct($client_context)
{
$this->setClientContext($client_context);
$this->logger = $client_context->logger;
}

/**
Expand All @@ -60,5 +59,6 @@ public function request($path)
public function setClientContext($clientContext)
{
$this->clientContext = $clientContext;
$this->logger = $clientContext->logger;
}
}
23 changes: 21 additions & 2 deletions src/Endpoints/Insurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,28 @@ public function getSubscription($subscriptionIds)
return $response->json;
}

public function sendCustomerCart($cmsReferenceArray)
/**
* @param $cmsReferenceArray
* @param $cartId
* @return void
* @throws RequestError
*/
public function sendCustomerCart($cmsReferenceArray, $cartId)
{
$this->insuranceValidator->checkCmsReference($cmsReferenceArray);
try {
$this->insuranceValidator->checkCmsReference($cmsReferenceArray);
$request = $this->request(self::INSURANCE_PATH . 'customer-cart')
->setRequestBody(
[
'cms_references' => $cmsReferenceArray
]
);

$this->addCustomerSessionToRequest($request, null, $cartId);
$request->post();
} catch (ParametersException $e) {
$this->logger->error('Impossible to send customer cart data', [$e->getMessage()]);
}
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/Lib/InsuranceValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ public function checkSubscriptionIds($subscriptionIds)
*/
public function checkCmsReference($cmsReferenceArray)
{
if (is_array($cmsReferenceArray) && !empty($cmsReferenceArray)) {
foreach ($cmsReferenceArray as $cmsReference) {
if (!is_string($cmsReference)) {
throw new ParametersException(sprintf(
'Invalid parameters : %s',
json_encode($cmsReferenceArray)
));
}
if (!is_array($cmsReferenceArray) || empty($cmsReferenceArray)) {
throw new ParametersException(sprintf(
'Invalid parameters must be an array with strings : %s',
gettype($cmsReferenceArray)
));
}
foreach ($cmsReferenceArray as $cmsReference) {
if (!is_string($cmsReference)) {
throw new ParametersException(sprintf(
'Cms references must be a string : %s',
json_encode($cmsReference)
));
}
}
throw new ParametersException(sprintf(
'Invalid parameters : %s',
json_encode($cmsReferenceArray)
));
}
}
41 changes: 36 additions & 5 deletions tests/Unit/Legacy/Endpoints/InsuranceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Alma\API\Response;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class InsuranceTest extends TestCase
{
const INSURANCE_SUBSCRIPTIONS_PATH = '/v1/insurance/subscriptions';
const INSURANCE_CONTRACTS_PATH = '/v1/insurance/insurance-contracts/';
const INSURANCE_CUSTOMER_CART_PATH = '/v1/insurance/customer-cart';
/**
* @var ClientContext
*/
Expand All @@ -31,6 +33,7 @@ class InsuranceTest extends TestCase
* @var Response
*/
private $responseMock;
private $requestObject;

/**
* @return array
Expand Down Expand Up @@ -695,18 +698,46 @@ public function testGetSubscriptionsReturnApiResponse($subscriptionIds, $json)
$this->assertEquals($json, $this->insuranceMock->getSubscription($subscriptionIds));
}


/**
* @return void
* @throws RequestError
*/
public function testGivenInvalidCmsReferenceArrayReturnFalse()
public function testGivenInvalidCmsReferenceArrayNoCallEndpointAndReturnFalse()
{
//$this->assertFalse($this->insuranceMock->sendCustomerCart($cmsReferenceArray));
$this->insuranceValidatorMock->shouldReceive('checkCmsReference')
->once()
->andThrow(ParametersException::class);
$loggerMock = Mockery::mock(LoggerInterface::class);
$loggerMock->shouldReceive('error')->once();
$this->clientContext->logger = $loggerMock;
$this->insuranceMock->setClientContext($this->clientContext);
$this->assertNull($this->insuranceMock->sendCustomerCart(['123','456'],42));
}

/**
* @return void
* @throws RequestError
*/
public function testSendCustomerCartCallApiPostCustomerCartWithCmsReferencesArray()
{


$cartId = 42;
$this->insuranceValidatorMock->shouldReceive('checkCmsReference')
->once();
$this->requestObject->shouldReceive('setRequestBody')->once()->with(
[
'cms_references' => ['123','456']
]
)->andReturn($this->requestObject);
$this->insuranceMock->shouldReceive('request')
->with(self::INSURANCE_CUSTOMER_CART_PATH)
->once()
->andReturn($this->requestObject);
$this->insuranceMock->shouldReceive('addCustomerSessionToRequest')->once()->with(
$this->requestObject,
null,
$cartId
);
$this->requestObject->shouldReceive('post')->once();
$this->assertNull($this->insuranceMock->sendCustomerCart(['123','456'], $cartId));
}
}
24 changes: 22 additions & 2 deletions tests/Unit/Legacy/Lib/InsuranceValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class InsuranceValidatorTest extends TestCase
{


/**
* @var InsuranceValidator
*/
protected $insuranceValidator;

public function setUp()
{
$this->insuranceValidator = new InsuranceValidator();
}

/**
* @dataProvider checkCmsReferenceInvalidPayloadDataProvider
* @param $invalidPayload
Expand All @@ -19,10 +29,20 @@ class InsuranceValidatorTest extends TestCase
public function testCheckCmsReferenceWithInvalidPayloadThrowParameterException($invalidPayload)
{
$this->expectException(ParametersException::class);
$insuranceValidator = new InsuranceValidator();
$insuranceValidator->checkCmsReference($invalidPayload);
$this->insuranceValidator->checkCmsReference($invalidPayload);
}

/**
* @throws ParametersException
*/
public function testCheckCmsReferenceWithValidPayload()
{
$this->assertNull($this->insuranceValidator->checkCmsReference(['123', '456']));
}

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

0 comments on commit 2d69016

Please sign in to comment.