From f10ee6905eded86211f861078a782110c98316b6 Mon Sep 17 00:00:00 2001 From: "alma-renovate-bot[bot]" <163289924+alma-renovate-bot[bot]@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:16:24 +0000 Subject: [PATCH 01/18] chore(deps): update lovetoknow/slackify-markdown-action action to v1.1.1 --- .github/workflows/release-publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml index ccd9ad82..6b795a80 100644 --- a/.github/workflows/release-publish.yml +++ b/.github/workflows/release-publish.yml @@ -100,7 +100,7 @@ jobs: - name: Format release notes for Slack # v1.0.2 cannot be used as it is not correctly handling the newlines - uses: LoveToKnow/slackify-markdown-action@v1.0.0 + uses: LoveToKnow/slackify-markdown-action@v1.1.1 id: slack-markdown-release-notes with: text: | From 3bfed2899dac1f8fcd39aa479d289d194c138443 Mon Sep 17 00:00:00 2001 From: Carine Bonnafous Date: Wed, 10 Jul 2024 10:35:41 +0200 Subject: [PATCH 02/18] Ping EM/PM in the Slack release message --- .github/workflows/release-publish.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml index d8bd10a7..038dc05a 100644 --- a/.github/workflows/release-publish.yml +++ b/.github/workflows/release-publish.yml @@ -102,7 +102,6 @@ jobs: }); - name: Format release notes for Slack - # v1.0.2 cannot be used as it is not correctly handling the newlines uses: LoveToKnow/slackify-markdown-action@v1.1.1 id: slack-markdown-release-notes with: @@ -111,12 +110,12 @@ jobs: ${{ steps.fetch-release-draft.outputs.body }} + cc <@france.berut> <@khadija.cherif> + - name: Send changelog to Slack uses: slackapi/slack-github-action@v1.26.0 with: - # TODO: Replace with channel #alma_changelog (id: CR9C57YM6) once full testing is done - # Channel `#devx-experiments` - channel-id: C04MQ9VEWRF + channel-id: CR9C57YM6 slack-message: ${{ steps.slack-markdown-release-notes.outputs.text }} payload: | { From 5b24fad049db1fd5415ead13fc5b2c7843148e00 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Fri, 12 Jul 2024 16:41:11 +0200 Subject: [PATCH 03/18] Rework integration test --- .gitignore | 3 +- Taskfile.php.yml | 8 +- phpunit.dist.xml | 2 +- .../{Legacy => }/Endpoints/PaymentsTest.php | 5 +- .../PHP7_0/Endpoints/PaymentsTest.php | 102 ------------------ .../PHP7_2/Endpoints/PaymentsTest.php | 102 ------------------ .../PHP8_1/Endpoints/PaymentsTest.php | 102 ------------------ tests/legacy_integration_tests.sh | 14 +++ 8 files changed, 26 insertions(+), 312 deletions(-) rename tests/Integration/{Legacy => }/Endpoints/PaymentsTest.php (97%) delete mode 100644 tests/Integration/PHP7_0/Endpoints/PaymentsTest.php delete mode 100644 tests/Integration/PHP7_2/Endpoints/PaymentsTest.php delete mode 100644 tests/Integration/PHP8_1/Endpoints/PaymentsTest.php create mode 100755 tests/legacy_integration_tests.sh diff --git a/.gitignore b/.gitignore index bd35edbc..dd74e98b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ dist/ phpunit.xml .phpunit.cache/ composer.lock -.DS_Store \ No newline at end of file +.DS_Store +.env \ No newline at end of file diff --git a/Taskfile.php.yml b/Taskfile.php.yml index db7aae31..9c03f34f 100644 --- a/Taskfile.php.yml +++ b/Taskfile.php.yml @@ -37,8 +37,12 @@ tasks: deps: - docker:build cmds: - - sed 's/{MYVERSION}/{{ .PHPUNIT_VERSION }}/g' phpunit.dist.xml > phpunit.xml - - docker compose run {{ .COMPOSE_SERVICE }} composer exec phpunit --verbose -- --testsuite "Alma PHP Client Integration Test Suite" + - >- + {{ if eq .PHP_VERSION "5.6" "7.0" }} + docker compose run {{ .COMPOSE_SERVICE }} ./tests/legacy_integration_tests.sh + {{ else }} + docker compose run {{ .COMPOSE_SERVICE }} composer exec phpunit --verbose -- --configuration phpunit.dist.xml --testsuite "Alma PHP Client Integration Test Suite" + {{ end }} shell: desc: Connect to PHP container diff --git a/phpunit.dist.xml b/phpunit.dist.xml index 08cf52d7..2f62d498 100644 --- a/phpunit.dist.xml +++ b/phpunit.dist.xml @@ -5,7 +5,7 @@ tests/Unit - tests/Integration/{MYVERSION} + tests/Integration/Endpoints diff --git a/tests/Integration/Legacy/Endpoints/PaymentsTest.php b/tests/Integration/Endpoints/PaymentsTest.php similarity index 97% rename from tests/Integration/Legacy/Endpoints/PaymentsTest.php rename to tests/Integration/Endpoints/PaymentsTest.php index 22229c1b..fe7220bc 100644 --- a/tests/Integration/Legacy/Endpoints/PaymentsTest.php +++ b/tests/Integration/Endpoints/PaymentsTest.php @@ -16,8 +16,9 @@ final class PaymentsTest extends TestCase * @throws DependenciesError * @throws ParamsError */ - public static function setUpBeforeClass() - { + public static function setUpBeforeClass(): void + { + var_dump($_ENV);die(); self::$almaClient = new Client( $_ENV['ALMA_API_KEY'], ['mode' => 'test', 'api_root' => $_ENV['ALMA_API_ROOT'], 'force_tls' => false] diff --git a/tests/Integration/PHP7_0/Endpoints/PaymentsTest.php b/tests/Integration/PHP7_0/Endpoints/PaymentsTest.php deleted file mode 100644 index d6f778ca..00000000 --- a/tests/Integration/PHP7_0/Endpoints/PaymentsTest.php +++ /dev/null @@ -1,102 +0,0 @@ - 'test', 'api_root' => $_ENV['ALMA_API_ROOT'], 'force_tls' => false] - ); - } - - - private function paymentData($amount) - { - return [ - 'payment' => [ - 'purchase_amount' => $amount, - 'shipping_address' => [ - 'first_name' => 'Jane', - 'last_name' => 'Doe', - 'line1' => '2 rue de la rue', - 'city' => 'Paris', - 'postal_code' => '75002', - 'country' => 'FR', - ] - ] - ]; - } - - /** - * @param int $amount - * @throws RequestError - */ - private function createPayment($amount) - { - return self::$almaClient->payments->create(self::paymentData($amount)); - } - - - /** - * @throws RequestError - */ - private function checkEligibility($amount, $eligible) - { - $eligibility = self::$almaClient->payments->eligibility(self::paymentData($amount)); - self::assertEquals($eligible, $eligibility->isEligible); - - if (!$eligible) { - self::assertArrayHasKey('purchase_amount', $eligibility->reasons); - self::assertEquals('invalid_value', $eligibility->reasons['purchase_amount']); - - self::assertArrayHasKey('purchase_amount', $eligibility->constraints); - self::assertArrayHasKey('minimum', $eligibility->constraints['purchase_amount']); - self::assertArrayHasKey('maximum', $eligibility->constraints['purchase_amount']); - } - } - - /** - * @throws RequestError - */ - public function testCanCheckEligibility() - { - self::checkEligibility(1, false); - self::checkEligibility(20000, true); - self::checkEligibility(500000, false); - } - - /** - * @throws RequestError - */ - public function testCanCreateAPayment() - { - $payment = self::createPayment(26300); - self::assertEquals(26300, $payment->purchase_amount); - } - - /** - * @throws RequestError - */ - public function testCanFetchAPayment() - { - $p1 = self::createPayment(26500); - $p2 = self::$almaClient->payments->fetch($p1->id); - - self::assertEquals($p1->id, $p2->id); - } -} diff --git a/tests/Integration/PHP7_2/Endpoints/PaymentsTest.php b/tests/Integration/PHP7_2/Endpoints/PaymentsTest.php deleted file mode 100644 index 1c8c33f8..00000000 --- a/tests/Integration/PHP7_2/Endpoints/PaymentsTest.php +++ /dev/null @@ -1,102 +0,0 @@ - 'test', 'api_root' => $_ENV['ALMA_API_ROOT'], 'force_tls' => false] - ); - } - - - private function paymentData($amount) - { - return [ - 'payment' => [ - 'purchase_amount' => $amount, - 'shipping_address' => [ - 'first_name' => 'Jane', - 'last_name' => 'Doe', - 'line1' => '2 rue de la rue', - 'city' => 'Paris', - 'postal_code' => '75002', - 'country' => 'FR', - ] - ] - ]; - } - - /** - * @param int $amount - * @throws RequestError - */ - private function createPayment($amount) - { - return self::$almaClient->payments->create(self::paymentData($amount)); - } - - - /** - * @throws RequestError - */ - private function checkEligibility($amount, $eligible) - { - $eligibility = self::$almaClient->payments->eligibility(self::paymentData($amount)); - self::assertEquals($eligible, $eligibility->isEligible); - - if (!$eligible) { - self::assertArrayHasKey('purchase_amount', $eligibility->reasons); - self::assertEquals('invalid_value', $eligibility->reasons['purchase_amount']); - - self::assertArrayHasKey('purchase_amount', $eligibility->constraints); - self::assertArrayHasKey('minimum', $eligibility->constraints['purchase_amount']); - self::assertArrayHasKey('maximum', $eligibility->constraints['purchase_amount']); - } - } - - /** - * @throws RequestError - */ - public function testCanCheckEligibility() - { - self::checkEligibility(1, false); - self::checkEligibility(20000, true); - self::checkEligibility(500000, false); - } - - /** - * @throws RequestError - */ - public function testCanCreateAPayment() - { - $payment = self::createPayment(26300); - self::assertEquals(26300, $payment->purchase_amount); - } - - /** - * @throws RequestError - */ - public function testCanFetchAPayment() - { - $p1 = self::createPayment(26500); - $p2 = self::$almaClient->payments->fetch($p1->id); - - self::assertEquals($p1->id, $p2->id); - } -} diff --git a/tests/Integration/PHP8_1/Endpoints/PaymentsTest.php b/tests/Integration/PHP8_1/Endpoints/PaymentsTest.php deleted file mode 100644 index 46c49e93..00000000 --- a/tests/Integration/PHP8_1/Endpoints/PaymentsTest.php +++ /dev/null @@ -1,102 +0,0 @@ - 'test', 'api_root' => $_ENV['ALMA_API_ROOT'], 'force_tls' => false] - ); - } - - - private function paymentData($amount) - { - return [ - 'payment' => [ - 'purchase_amount' => $amount, - 'shipping_address' => [ - 'first_name' => 'Jane', - 'last_name' => 'Doe', - 'line1' => '2 rue de la rue', - 'city' => 'Paris', - 'postal_code' => '75002', - 'country' => 'FR', - ] - ] - ]; - } - - /** - * @param int $amount - * @throws RequestError - */ - private function createPayment($amount) - { - return self::$almaClient->payments->create(self::paymentData($amount)); - } - - - /** - * @throws RequestError - */ - private function checkEligibility($amount, $eligible) - { - $eligibility = self::$almaClient->payments->eligibility(self::paymentData($amount)); - self::assertEquals($eligible, $eligibility->isEligible); - - if (!$eligible) { - self::assertArrayHasKey('purchase_amount', $eligibility->reasons); - self::assertEquals('invalid_value', $eligibility->reasons['purchase_amount']); - - self::assertArrayHasKey('purchase_amount', $eligibility->constraints); - self::assertArrayHasKey('minimum', $eligibility->constraints['purchase_amount']); - self::assertArrayHasKey('maximum', $eligibility->constraints['purchase_amount']); - } - } - - /** - * @throws RequestError - */ - public function testCanCheckEligibility() - { - self::checkEligibility(1, false); - self::checkEligibility(20000, true); - self::checkEligibility(500000, false); - } - - /** - * @throws RequestError - */ - public function testCanCreateAPayment() - { - $payment = self::createPayment(26300); - self::assertEquals(26300, $payment->purchase_amount); - } - - /** - * @throws RequestError - */ - public function testCanFetchAPayment() - { - $p1 = self::createPayment(26500); - $p2 = self::$almaClient->payments->fetch($p1->id); - - self::assertEquals($p1->id, $p2->id); - } -} diff --git a/tests/legacy_integration_tests.sh b/tests/legacy_integration_tests.sh new file mode 100755 index 00000000..872520d8 --- /dev/null +++ b/tests/legacy_integration_tests.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -ex + +# Copy folder to /tmp +cp -r . /tmp/alma-php-client +cd /tmp/alma-php-client + +# Replace string in files +string=': void' +grep -r -l "$string" tests/ | xargs sed -i "s/$string//g" + +# Run tests +composer exec phpunit --verbose -- --configuration phpunit.dist.xml --testsuite "Alma PHP Client Integration Test Suite" From 821cedbecb92ca56b998dcae8edc345104fde82f Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Mon, 15 Jul 2024 09:53:57 +0200 Subject: [PATCH 04/18] remove dump and die --- phpunit.dist.xml | 2 +- tests/Integration/Endpoints/PaymentsTest.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/phpunit.dist.xml b/phpunit.dist.xml index 2f62d498..72837d2a 100644 --- a/phpunit.dist.xml +++ b/phpunit.dist.xml @@ -5,7 +5,7 @@ tests/Unit - tests/Integration/Endpoints + tests/Integration diff --git a/tests/Integration/Endpoints/PaymentsTest.php b/tests/Integration/Endpoints/PaymentsTest.php index fe7220bc..c35657dd 100644 --- a/tests/Integration/Endpoints/PaymentsTest.php +++ b/tests/Integration/Endpoints/PaymentsTest.php @@ -17,8 +17,7 @@ final class PaymentsTest extends TestCase * @throws ParamsError */ public static function setUpBeforeClass(): void - { - var_dump($_ENV);die(); + { self::$almaClient = new Client( $_ENV['ALMA_API_KEY'], ['mode' => 'test', 'api_root' => $_ENV['ALMA_API_ROOT'], 'force_tls' => false] From 5fe87ebf90bea6f9546ea7bdb099d1272ae56459 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Thu, 11 Jul 2024 18:21:37 +0200 Subject: [PATCH 05/18] feature: refactor order and add updateTracking put endpoint --- src/Endpoints/Orders.php | 37 ++- src/Entities/Order.php | 179 +++++++++++- tests/Unit/Endpoints/OrdersTest.php | 167 +++++++++-- tests/Unit/Endpoints/PaymentsTest.php | 384 +++++++++++++------------- 4 files changed, 539 insertions(+), 228 deletions(-) diff --git a/src/Endpoints/Orders.php b/src/Endpoints/Orders.php index c495a326..27ff15b3 100644 --- a/src/Endpoints/Orders.php +++ b/src/Endpoints/Orders.php @@ -64,6 +64,25 @@ public function update($orderId, $orderData) return new Order($response->json); } + /** + * @param string $orderId + * @param string|null $carrier + * @param string|null $trackingNumber + * @param string|null $trackingUrl + * @return Order + * @throws AlmaException + */ + public function updateTracking($orderId, $carrier = null, $trackingNumber = null, $trackingUrl = null) + { + $trackingData = array_filter([ + 'carrier' => $carrier, + 'tracking_number' => $trackingNumber, + 'tracking_url' => $trackingUrl + ]); + $response = $this->request(self::ORDERS_PATH . "/{$orderId}")->setRequestBody($trackingData)->put(); + return new Order($response->json); + } + /** * @param int $limit * @param string|null $startingAfter @@ -91,7 +110,7 @@ public function fetchAll($limit = 20, $startingAfter = null, $filters = array()) $response = $this->request(self::ORDERS_PATH)->setQueryParams($args)->get(); return new PaginatedResults( $response, - function($startingAfter) use ($limit, $filters) { + function ($startingAfter) use ($limit, $filters) { return $this->fetchAll($limit, $startingAfter, $filters); } ); @@ -123,11 +142,11 @@ public function sendStatus($orderExternalId, $orderData = array()) try { $response = $this->request(self::ORDERS_PATH_V2 . "/{$orderExternalId}/status")->setRequestBody(array( - 'status' => $label, + 'status' => $label, 'is_shipped' => $orderData['is_shipped'], ))->post(); - }catch (AlmaException $e) { - $this->logger->error('Error sending status'); + } catch (AlmaException $e) { + $this->logger->error('Error sending status'); throw new RequestException('Error sending status', $e); } @@ -143,21 +162,21 @@ public function sendStatus($orderExternalId, $orderData = array()) */ public function validateStatusData($orderData = array()) { - if(count($orderData) == 0) { + if (count($orderData) == 0) { throw new ParametersException('Missing in the required parameters (status, is_shipped) when calling orders.sendStatus'); } try { $this->arrayUtils->checkMandatoryKeys(['status', 'is_shipped'], $orderData); - } catch (MissingKeyException $e ) { - throw new ParametersException('Error in the required parameters (status, is_shipped) when calling orders.sendStatus',0, $e); + } catch (MissingKeyException $e) { + throw new ParametersException('Error in the required parameters (status, is_shipped) when calling orders.sendStatus', 0, $e); } - if(!is_bool($orderData['is_shipped'])) { + if (!is_bool($orderData['is_shipped'])) { throw new ParametersException('Parameter "is_shipped" must be a boolean'); } - if(!$orderData['status']) { + if (!$orderData['status']) { throw new ParametersException('Missing the required parameter "status" when calling orders.sendStatus'); } } diff --git a/src/Entities/Order.php b/src/Entities/Order.php index 6d87a923..b39b0d0b 100644 --- a/src/Entities/Order.php +++ b/src/Entities/Order.php @@ -25,17 +25,186 @@ namespace Alma\API\Entities; -class Order extends Base +class Order { - /** @var string ID of the Payment owning this Order */ + /** @var string | null Order carrier */ + private $carrier; + + /** @var string | null Order carrier tracking number */ + private $trackingNumber; + + /** @var string | null Order carrier tracking URL */ + private $trackingUrl; + + /** @var string ID of the Payment owning this Order + * @deprecated + */ public $payment; - /** @var string Order reference from the merchant's platform */ + /** @var string ID of the Payment owning this Order */ + private $paymentId; + + /** + * @var string | null Order reference from the merchant's platform + * @deprecated + */ public $merchant_reference; - /** @var string URL to the merchant's backoffice for that Order */ + /** + * @var string | null Order reference from the merchant's platform + */ + private $merchantReference; + + /** + * @var string | null URL to the merchant's backoffice for that Order + * @deprecated + */ public $merchant_url; - /** @var array Free-form custom data */ + /** @var string | null URL to the merchant's backoffice for that Order */ + private $merchantUrl; + + /** @var array Free-form custom data */ public $data; + + /** + * @var string | null Order comment + */ + private $comment; + /** + * @var int Order creation timestamp + */ + private $createdAt; + + /** + * @var string | null Customer URL + */ + private $customerUrl; + + /** + * @var string Order external ID + */ + private $externalId; + + /** + * @var string Order updated timestamp + */ + private $updatedAt; + /** + * @var string Order ID + * @deprecated + */ + public $id; + + + public function __construct($orderDataArray) + { + $this->carrier = $orderDataArray['carrier']; + $this->comment = $orderDataArray['comment']; + $this->createdAt = $orderDataArray['created']; + $this->customerUrl = $orderDataArray['customer_url']; + $this->data = $orderDataArray['data']; + $this->id = $orderDataArray['id']; + $this->externalId = $orderDataArray['id']; + $this->merchant_reference = $orderDataArray['merchant_reference']; + $this->merchantReference = $orderDataArray['merchant_reference']; + $this->merchant_url = $orderDataArray['merchant_url']; + $this->merchantUrl = $orderDataArray['merchant_url']; + $this->payment = $orderDataArray['payment']; + $this->paymentId = $orderDataArray['payment']; + $this->trackingNumber = $orderDataArray['tracking_number']; + $this->trackingUrl = $orderDataArray['tracking_url']; + $this->updatedAt = $orderDataArray['updated']; + } + + + + /** + * @return string|null + */ + public function getCarrier() + { + return $this->carrier; + } + + /** + * @return string|null + */ + public function getTrackingNumber() + { + return $this->trackingNumber; + } + + /** + * @return string|null + */ + public function getTrackingUrl() + { + return $this->trackingUrl; + } + + /** + * @return string + */ + public function getPaymentId() + { + return $this->paymentId; + } + + /** + * @return string|null + */ + public function getMerchantReference() + { + return $this->merchantReference; + } + + /** + * @return string|null + */ + public function getMerchantUrl() + { + return $this->merchantUrl; + } + + /** + * @return string + */ + public function getExternalId() + { + return $this->externalId; + } + + /** + * @return string|null + */ + public function getComment() + { + return $this->comment; + } + + /** + * @return int + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + /** + * @return string|null + */ + public function getCustomerUrl() + { + return $this->customerUrl; + } + + /** + * @return int + */ + public function getUpdatedAt() + { + return $this->updatedAt; + } + } diff --git a/tests/Unit/Endpoints/OrdersTest.php b/tests/Unit/Endpoints/OrdersTest.php index 3383df4a..3f6561fb 100644 --- a/tests/Unit/Endpoints/OrdersTest.php +++ b/tests/Unit/Endpoints/OrdersTest.php @@ -6,11 +6,14 @@ use Alma\API\ClientContext; use Alma\API\Endpoints\Orders; +use Alma\API\Entities\Order; +use Alma\API\Exceptions\AlmaException; use Alma\API\Exceptions\MissingKeyException; use Alma\API\Exceptions\ParametersException; use Alma\API\Exceptions\RequestException; use Alma\API\Lib\ArrayUtils; use Alma\API\Request; +use Alma\API\RequestError; use Alma\API\Response; use Mockery; use PHPUnit\Framework\TestCase; @@ -20,9 +23,28 @@ class OrdersTest extends TestCase { /** - * @var \Mockery\Mock|(\Mockery\MockInterface&Orders) + * @var ClientContext + */ + private $clientContext; + /** + * @var Orders */ protected $orderEndpoint; + /** + * @var ArrayUtils + */ + protected $arrayUtils; + /** + * @var Response + */ + protected $responseMock; + /** + * @var Request + */ + protected $requestObject; + + private $status; + private $externalId; public function setUp(): void { @@ -30,27 +52,27 @@ public function setUp(): void $this->orderEndpoint = Mockery::mock(Orders::class)->makePartial(); $this->arrayUtils = Mockery::mock(ArrayUtils::class)->makePartial(); $this->responseMock = Mockery::mock(Response::class); - $this->responseMock->errorMessage = 'Exception Error message'; - $this->requestObject = Mockery::mock(Request::class); + $this->responseMock->errorMessage = 'Exception Error message'; + $this->requestObject = Mockery::mock(Request::class); $loggerMock = Mockery::mock(LoggerInterface::class); $loggerMock->shouldReceive('error'); $this->orderEndpoint->arrayUtils = $this->arrayUtils; - $this->externalId = 'Mon external Id'; + $this->externalId = 'Mon external Id'; $this->status = 'Mon Status - 1'; $this->clientContext->logger = $loggerMock; $this->orderEndpoint->setClientContext($this->clientContext); } - public function tearDown(): void - { - $this->orderEndpoint = null; - $this->arrayUtils = null; - $this->responseMock = null; - $this->requestObject = null; - Mockery::close(); - } + public function tearDown(): void + { + $this->orderEndpoint = null; + $this->arrayUtils = null; + $this->responseMock = null; + $this->requestObject = null; + Mockery::close(); + } public function testValidateStatusDataNoOrderData() { @@ -112,11 +134,11 @@ public function testSendStatusOk() $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); $this->orderEndpoint->shouldReceive('request') - ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") + ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") ->once() ->andReturn($this->requestObject); - $this->orderEndpoint->sendStatus($this->externalId , array( + $this->orderEndpoint->sendStatus($this->externalId, array( 'status' => $this->status, 'is_shipped' => true, )); @@ -131,17 +153,18 @@ public function testSendStatusRequestError() $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); $this->orderEndpoint->shouldReceive('request') - ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") + ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") ->once() ->andReturn($this->requestObject); $this->expectException(RequestException::class); - $this->orderEndpoint->sendStatus($this->externalId , array( + $this->orderEndpoint->sendStatus($this->externalId, array( 'status' => $this->status, 'is_shipped' => true, )); } + public function testSendStatusWithException() { $this->orderEndpoint->shouldReceive('validateStatusData')->andReturn(null); @@ -151,16 +174,124 @@ public function testSendStatusWithException() $this->requestObject->shouldReceive('post')->andThrow(new RequestException()); $this->orderEndpoint->shouldReceive('request') - ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") + ->with(Orders::ORDERS_PATH_V2 . "/{$this->externalId}/status") ->once() ->andReturn($this->requestObject); $this->expectException(RequestException::class); - $this->orderEndpoint->sendStatus($this->externalId , array( + $this->orderEndpoint->sendStatus($this->externalId, array( 'status' => $this->status, 'is_shipped' => true, )); } + public function testUpdateTrackingThrowsAlmaException() + { + $this->expectException(AlmaException::class); + $this->requestObject->shouldReceive('put')->andThrow(new RequestError()); + $this->requestObject->shouldReceive('setRequestBody')->andReturn($this->requestObject); + $this->orderEndpoint->shouldReceive('request') + ->with('/v1/orders/123') + ->once() + ->andReturn($this->requestObject); + $this->orderEndpoint->updateTracking('123', 'ups', '123456', 'myUrl'); + } + + + /** + * @dataProvider updateTrackingDataProvider + * @param $carrier + * @param $trackingNumber + * @param $trackingUrl + * @param $trackingData + * @return void + * @throws AlmaException + */ + public function testUpdateTracking($carrier, $trackingNumber, $trackingUrl, $trackingData) + { + $this->responseMock->json = $this->orderDataFactory($carrier, $trackingNumber, $trackingUrl); + $this->requestObject->shouldReceive('put')->andReturn($this->responseMock); + $this->requestObject->shouldReceive('setRequestBody')->with($trackingData)->andReturn($this->requestObject); + $this->orderEndpoint->shouldReceive('request') + ->with('/v1/orders/123') + ->once() + ->andReturn($this->requestObject); + $order = $this->orderEndpoint->updateTracking('123', $carrier, $trackingNumber, $trackingUrl); + $this->isInstanceOf(Order::class, $order); + $this->assertEquals($carrier, $order->getCarrier()); + $this->assertEquals($trackingNumber, $order->getTrackingNumber()); + $this->assertEquals($trackingUrl, $order->getTrackingUrl()); + } + + /** + * @return array[] + */ + public static function updateTrackingDataProvider() + { + return [ + 'no data' => [ + null, + null, + null, + [] + ], + 'Only Carrier' => [ + 'ups', + null, + null, + ['carrier' => 'ups'] + ], + 'Only Url' => [ + null, + null, + 'myUrl', + ['tracking_url' => 'myUrl'] + ], + 'Carrier and Url' => [ + 'ups', + null, + 'myUrl', + ['carrier' => 'ups', 'tracking_url' => 'myUrl'] + ], + 'All params' => [ + 'ups', + '123456', + 'myUrl', + ['carrier' => 'ups', 'tracking_number' => '123456', 'tracking_url' => 'myUrl'] + ], + ]; + } + + public function orderDataFactory( + $carrier = 'ups', + $tracking_number = 'ups_123456', + $tracking_url = 'http://tracking.url', + $comment = 'my comment', + $created = 1715331839, + $customer_url = 'http://customer.url', + $data = ['key' => 'value'], + $id = 'order_123', + $merchant_reference = 'my reference', + $merchant_url = 'http://merchant.url', + $payment = 'payment_123456', + $updated = 1715331839 + ) + { + return [ + 'carrier' => $carrier, + 'comment' => $comment, + 'created' => $created, + 'customer_url' => $customer_url, + 'data' => $data, + 'id' => $id, + 'merchant_reference' => $merchant_reference, + 'merchant_url' => $merchant_url, + 'payment' => $payment, + 'tracking_number' => $tracking_number, + 'tracking_url' => $tracking_url, + 'updated' => $updated + ]; + } + } diff --git a/tests/Unit/Endpoints/PaymentsTest.php b/tests/Unit/Endpoints/PaymentsTest.php index c693a4ab..3da4ee26 100644 --- a/tests/Unit/Endpoints/PaymentsTest.php +++ b/tests/Unit/Endpoints/PaymentsTest.php @@ -18,7 +18,7 @@ class PaymentsTest extends TestCase { const MERCHANT_REF = "merchant_ref"; - const SERVER_REQUEST_ERROR_RESPONSE_JON = '{ + const SERVER_REQUEST_ERROR_RESPONSE_JON = '{ "payment_plan":[ { "customer_can_postpone":false, @@ -56,8 +56,12 @@ class PaymentsTest extends TestCase ], "orders":[ { + "carrier":null, + "tracking_number":null, + "tracking_url":null, "comment":null, "created":1649672451, + "updated":1649672451, "customer_url":null, "data":{}, "id":"order_11uPRjP4L9Dgbttx3cFUKGFPppdZIlrR2V", @@ -80,112 +84,112 @@ class PaymentsTest extends TestCase ] }'; - public function tearDown(): void - { - Mockery::close(); - } - - /** - * Return input to test testPartialRefund - * @return array[] - */ - public static function getPartialRefundData() - { - return [ - [[ - 'id' => "some_id", - 'amount' => 15000, - 'merchant_ref' => self::MERCHANT_REF, - 'comment' => "some comment" - ]], - [[ - 'id' => "some_id", - 'amount' => 15000, - 'merchant_ref' => self::MERCHANT_REF - ]], - [[ - 'id' => "some_id", - 'amount' => 15000 - ]] - ]; - } - - /** - * Return invalid input to test testPartialRefund - * @return array[] - */ - public static function getPartialRefundInvalidData() - { - return [ - [[ - 'id' => "negative_amount", - 'amount' => -1 - ], ParametersException::class], - [[ - 'id' => "", - 'amount' => 1500, - 'merchant_ref' => "no id", - ], ParametersException::class], - ]; - } - - /** - * Return input to test testFullRefund - * @return array[] - */ - public static function getFullRefundData() - { - return [ - [[ - 'id' => "some_id", - 'merchant_ref' => self::MERCHANT_REF, - 'comment' => "some comment" - ]], - [[ - 'id' => "some_id", - 'merchant_ref' => self::MERCHANT_REF - ]], - [[ - 'id' => "some_id", - ]] - ]; - } - - /** - * Return input to test testRefund - * @return array[] - */ - public static function getRefundData() - { - return [ - [[ - 'id' => "some_id", - 'amount' => 15000, - 'merchant_ref' => self::MERCHANT_REF, - ]], - [[ - 'id' => "some_id", - ]], - [[ - 'id' => "some_id", - 'amount' => 15000 - ]] - ]; - } - - /** - * Return invalid input to test testFullRefund - * @return array[] - */ - public static function getFullRefundInvalidData() - { - return [ - [[ - 'id' => "", - 'merchant_ref' => "no id", - ], ParametersException::class], - ]; - } + public function tearDown(): void + { + Mockery::close(); + } + + /** + * Return input to test testPartialRefund + * @return array[] + */ + public static function getPartialRefundData() + { + return [ + [[ + 'id' => "some_id", + 'amount' => 15000, + 'merchant_ref' => self::MERCHANT_REF, + 'comment' => "some comment" + ]], + [[ + 'id' => "some_id", + 'amount' => 15000, + 'merchant_ref' => self::MERCHANT_REF + ]], + [[ + 'id' => "some_id", + 'amount' => 15000 + ]] + ]; + } + + /** + * Return invalid input to test testPartialRefund + * @return array[] + */ + public static function getPartialRefundInvalidData() + { + return [ + [[ + 'id' => "negative_amount", + 'amount' => -1 + ], ParametersException::class], + [[ + 'id' => "", + 'amount' => 1500, + 'merchant_ref' => "no id", + ], ParametersException::class], + ]; + } + + /** + * Return input to test testFullRefund + * @return array[] + */ + public static function getFullRefundData() + { + return [ + [[ + 'id' => "some_id", + 'merchant_ref' => self::MERCHANT_REF, + 'comment' => "some comment" + ]], + [[ + 'id' => "some_id", + 'merchant_ref' => self::MERCHANT_REF + ]], + [[ + 'id' => "some_id", + ]] + ]; + } + + /** + * Return input to test testRefund + * @return array[] + */ + public static function getRefundData() + { + return [ + [[ + 'id' => "some_id", + 'amount' => 15000, + 'merchant_ref' => self::MERCHANT_REF, + ]], + [[ + 'id' => "some_id", + ]], + [[ + 'id' => "some_id", + 'amount' => 15000 + ]] + ]; + } + + /** + * Return invalid input to test testFullRefund + * @return array[] + */ + public static function getFullRefundInvalidData() + { + return [ + [[ + 'id' => "", + 'merchant_ref' => "no id", + ], ParametersException::class], + ]; + } /** * Mock ClientContext, Response and Request to allow us to test @@ -232,71 +236,71 @@ private function callPartialRefund($payments, $data) } } - private function callFullRefund($payments, $data) - { - if (isset($data['merchant_ref']) && isset($data['comment'])) { - $payments->fullRefund($data['id'], $data['merchant_ref'], $data['comment']); - } elseif (isset($data['merchant_ref'])) { - $payments->fullRefund($data['id'], $data['merchant_ref']); - } elseif (isset($data['comment'])) { - $payments->fullRefund($data['id'], '', $data['comment']); - } else { - $payments->fullRefund($data['id']); - } - } - - private function callRefund($payments, $data) - { - if (isset($data['merchant_ref']) && isset($data['amount'])) { - $payments->refund($data['id'], $data['amount'], $data['merchant_ref']); - } elseif (isset($data['amount'])) { - $payments->refund($data['id'], $data['amount']); - } else { - $payments->refund($data['id']); - } - } - - /** - * Mock ClientContext, Response and Request to allow us to test - * Payment without sending any requests but returns an error - */ - private function mockServerRequestError() - { - // ClientContext - $clientContext = Mockery::mock(ClientContext::class); - $clientContext->shouldReceive('urlFor'); - $clientContext->shouldReceive('getUserAgentString'); - $clientContext->shouldReceive('forcedTLSVersion'); - - // Response - $json = self::SERVER_REQUEST_ERROR_RESPONSE_JON; - - $responseMock = Mockery::mock(Response::class); - $responseMock->shouldReceive('isError')->andReturn(true); - $responseMock->json = json_decode($json, true); - $responseMock->errorMessage = "a very important error message"; - - // Request - $requestMock = Mockery::mock(Request::class); - $requestMock->shouldReceive('setRequestBody'); - $requestMock->shouldReceive('post')->andReturn($responseMock); - return $requestMock; - } - - /** - * Ensure that the methods exists - */ - public function testRefundMethodExist() - { - $clientContext = Mockery::mock(ClientContext::class); - - $payments = new Payments($clientContext); - - $this->assertTrue(method_exists($payments, 'partialRefund')); - $this->assertTrue(method_exists($payments, 'fullRefund')); - # ensure backward compatibility - $this->assertTrue(method_exists($payments, 'refund')); - } + private function callFullRefund($payments, $data) + { + if (isset($data['merchant_ref']) && isset($data['comment'])) { + $payments->fullRefund($data['id'], $data['merchant_ref'], $data['comment']); + } elseif (isset($data['merchant_ref'])) { + $payments->fullRefund($data['id'], $data['merchant_ref']); + } elseif (isset($data['comment'])) { + $payments->fullRefund($data['id'], '', $data['comment']); + } else { + $payments->fullRefund($data['id']); + } + } + + private function callRefund($payments, $data) + { + if (isset($data['merchant_ref']) && isset($data['amount'])) { + $payments->refund($data['id'], $data['amount'], $data['merchant_ref']); + } elseif (isset($data['amount'])) { + $payments->refund($data['id'], $data['amount']); + } else { + $payments->refund($data['id']); + } + } + + /** + * Mock ClientContext, Response and Request to allow us to test + * Payment without sending any requests but returns an error + */ + private function mockServerRequestError() + { + // ClientContext + $clientContext = Mockery::mock(ClientContext::class); + $clientContext->shouldReceive('urlFor'); + $clientContext->shouldReceive('getUserAgentString'); + $clientContext->shouldReceive('forcedTLSVersion'); + + // Response + $json = self::SERVER_REQUEST_ERROR_RESPONSE_JON; + + $responseMock = Mockery::mock(Response::class); + $responseMock->shouldReceive('isError')->andReturn(true); + $responseMock->json = json_decode($json, true); + $responseMock->errorMessage = "a very important error message"; + + // Request + $requestMock = Mockery::mock(Request::class); + $requestMock->shouldReceive('setRequestBody'); + $requestMock->shouldReceive('post')->andReturn($responseMock); + return $requestMock; + } + + /** + * Ensure that the methods exists + */ + public function testRefundMethodExist() + { + $clientContext = Mockery::mock(ClientContext::class); + + $payments = new Payments($clientContext); + + $this->assertTrue(method_exists($payments, 'partialRefund')); + $this->assertTrue(method_exists($payments, 'fullRefund')); + # ensure backward compatibility + $this->assertTrue(method_exists($payments, 'refund')); + } /** * Test the partialRefund method with valid datas @@ -312,14 +316,12 @@ public function testPartialRefund($data) // Payment $payments = Mockery::mock(Payments::class) ->shouldAllowMockingProtectedMethods() - ->makePartial() - ; + ->makePartial(); $id = $data['id']; $payments->shouldReceive('request') ->with("/v1/payments/$id/refund") ->once() - ->andReturn($this->mockServerRequest()) - ; + ->andReturn($this->mockServerRequest()); $payments->setClientContext($clientContext); /* Test */ @@ -341,13 +343,11 @@ public function testInvalidPartialRefund($data, $expectedException) // Payment $payments = Mockery::mock(Payments::class) ->shouldAllowMockingProtectedMethods() - ->makePartial() - ; + ->makePartial(); $id = $data['id']; $payments->shouldReceive('request') ->with("/v1/payments/$id/refund") - ->andReturn($this->mockServerRequest()) - ; + ->andReturn($this->mockServerRequest()); $payments->setClientContext($clientContext); $this->expectException($expectedException); @@ -368,14 +368,12 @@ public function testFullRefund($data) // Payment $payments = Mockery::mock(Payments::class) ->shouldAllowMockingProtectedMethods() - ->makePartial() - ; + ->makePartial(); $id = $data['id']; $payments->shouldReceive('request') ->with("/v1/payments/$id/refund") ->once() - ->andReturn($this->mockServerRequest()) - ; + ->andReturn($this->mockServerRequest()); $payments->setClientContext($clientContext); /* Test */ @@ -395,14 +393,12 @@ public function testRefund($data) // Payment $payments = Mockery::mock(Payments::class) ->shouldAllowMockingProtectedMethods() - ->makePartial() - ; + ->makePartial(); $id = $data['id']; $payments->shouldReceive('request') ->with("/v1/payments/$id/refund") ->once() - ->andReturn($this->mockServerRequest()) - ; + ->andReturn($this->mockServerRequest()); $payments->setClientContext($clientContext); /* Test */ @@ -421,13 +417,11 @@ public function testInvalidFullRefund($data, $expectedException) // Payment $payments = Mockery::mock(Payments::class) ->shouldAllowMockingProtectedMethods() - ->makePartial() - ; + ->makePartial(); $id = $data['id']; $payments->shouldReceive('request') ->with("/v1/payments/$id/refund") - ->andReturn($this->mockServerRequest()) - ; + ->andReturn($this->mockServerRequest()); $payments->setClientContext($clientContext); $this->expectException($expectedException); @@ -445,13 +439,11 @@ public function testFullRefundRequestError() // Payment $payments = Mockery::mock(Payments::class) ->shouldAllowMockingProtectedMethods() - ->makePartial() - ; + ->makePartial(); $payments->shouldReceive('request') ->with("/v1/payments/$id/refund") ->once() - ->andReturn($this->mockServerRequestError()) - ; + ->andReturn($this->mockServerRequestError()); $payments->setClientContext($clientContext); $this->expectException(RequestException::class); From 61a9c6181f3583147ec260b12148a44e4d61242d Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Thu, 11 Jul 2024 18:46:50 +0200 Subject: [PATCH 06/18] tests: add test for order entity --- src/Entities/Order.php | 17 +++++++- tests/Unit/Endpoints/OrdersTest.php | 31 +++----------- tests/Unit/Entities/OrderTest.php | 66 +++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 27 deletions(-) create mode 100644 tests/Unit/Entities/OrderTest.php diff --git a/src/Entities/Order.php b/src/Entities/Order.php index b39b0d0b..a86c1ad2 100644 --- a/src/Entities/Order.php +++ b/src/Entities/Order.php @@ -64,9 +64,15 @@ class Order /** @var string | null URL to the merchant's backoffice for that Order */ private $merchantUrl; - /** @var array Free-form custom data */ + /** + * @var array Free-form custom data + * @deprecated + * */ public $data; + /** @var array Free-form custom data */ + private $orderData; + /** * @var string | null Order comment */ @@ -104,6 +110,7 @@ public function __construct($orderDataArray) $this->createdAt = $orderDataArray['created']; $this->customerUrl = $orderDataArray['customer_url']; $this->data = $orderDataArray['data']; + $this->orderData = $orderDataArray['data']; $this->id = $orderDataArray['id']; $this->externalId = $orderDataArray['id']; $this->merchant_reference = $orderDataArray['merchant_reference']; @@ -207,4 +214,12 @@ public function getUpdatedAt() return $this->updatedAt; } + /** + * @return array + */ + public function getOrderData() + { + return $this->orderData; + } + } diff --git a/tests/Unit/Endpoints/OrdersTest.php b/tests/Unit/Endpoints/OrdersTest.php index 3f6561fb..80bdd0b4 100644 --- a/tests/Unit/Endpoints/OrdersTest.php +++ b/tests/Unit/Endpoints/OrdersTest.php @@ -18,6 +18,7 @@ use Mockery; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; +use Unit\Entities\OrderTest; class OrdersTest extends TestCase { @@ -263,34 +264,12 @@ public static function updateTrackingDataProvider() } public function orderDataFactory( - $carrier = 'ups', - $tracking_number = 'ups_123456', - $tracking_url = 'http://tracking.url', - $comment = 'my comment', - $created = 1715331839, - $customer_url = 'http://customer.url', - $data = ['key' => 'value'], - $id = 'order_123', - $merchant_reference = 'my reference', - $merchant_url = 'http://merchant.url', - $payment = 'payment_123456', - $updated = 1715331839 + $carrier, + $tracking_number, + $tracking_url ) { - return [ - 'carrier' => $carrier, - 'comment' => $comment, - 'created' => $created, - 'customer_url' => $customer_url, - 'data' => $data, - 'id' => $id, - 'merchant_reference' => $merchant_reference, - 'merchant_url' => $merchant_url, - 'payment' => $payment, - 'tracking_number' => $tracking_number, - 'tracking_url' => $tracking_url, - 'updated' => $updated - ]; + return OrderTest::orderDataFactory($carrier, $tracking_number, $tracking_url); } diff --git a/tests/Unit/Entities/OrderTest.php b/tests/Unit/Entities/OrderTest.php new file mode 100644 index 00000000..b890fb7c --- /dev/null +++ b/tests/Unit/Entities/OrderTest.php @@ -0,0 +1,66 @@ +orderDataFactory(); + $order = new Order($orderData); + + $this->assertEquals($orderData['carrier'], $order->getCarrier()); + $this->assertEquals($orderData['tracking_number'], $order->getTrackingNumber()); + $this->assertEquals($orderData['tracking_url'], $order->getTrackingUrl()); + $this->assertEquals($orderData['payment'], $order->payment); + $this->assertEquals($orderData['payment'], $order->getPaymentId()); + $this->assertEquals($orderData['merchant_reference'], $order->merchant_reference); + $this->assertEquals($orderData['merchant_reference'], $order->getMerchantReference()); + $this->assertEquals($orderData['merchant_url'], $order->getMerchantUrl()); + $this->assertEquals($orderData['merchant_url'], $order->merchant_url ); + $this->assertEquals($orderData['data'], $order->data); + $this->assertEquals($orderData['data'], $order->getOrderData()); + $this->assertEquals($orderData['comment'], $order->getComment()); + $this->assertEquals($orderData['created'], $order->getCreatedAt()); + $this->assertEquals($orderData['customer_url'], $order->getCustomerUrl()); + $this->assertEquals($orderData['id'], $order->id); + $this->assertEquals($orderData['id'], $order->getExternalId()); + $this->assertEquals($orderData['updated'], $order->getUpdatedAt()); + } + + + public static function orderDataFactory( + $carrier = 'ups', + $tracking_number = 'ups_123456', + $tracking_url = 'http://tracking.url', + $comment = 'my comment', + $created = 1715331839, + $customer_url = 'http://customer.url', + $data = ['key' => 'value'], + $id = 'order_123', + $merchant_reference = 'my reference', + $merchant_url = 'https://merchant.url', + $payment = 'payment_123456', + $updated = 1715331845 + ) + { + return [ + 'carrier' => $carrier, + 'comment' => $comment, + 'created' => $created, + 'customer_url' => $customer_url, + 'data' => $data, + 'id' => $id, + 'merchant_reference' => $merchant_reference, + 'merchant_url' => $merchant_url, + 'payment' => $payment, + 'tracking_number' => $tracking_number, + 'tracking_url' => $tracking_url, + 'updated' => $updated + ]; + } +} \ No newline at end of file From 39854cf8cc5e790345b7dc63249521de5b89a167 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 15 Jul 2024 17:05:11 +0200 Subject: [PATCH 07/18] fix: fix Order creation object remove mandatory updated data --- src/Entities/Order.php | 4 +--- tests/Unit/Endpoints/PaymentsTest.php | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Entities/Order.php b/src/Entities/Order.php index a86c1ad2..1aa8fbde 100644 --- a/src/Entities/Order.php +++ b/src/Entities/Order.php @@ -121,11 +121,9 @@ public function __construct($orderDataArray) $this->paymentId = $orderDataArray['payment']; $this->trackingNumber = $orderDataArray['tracking_number']; $this->trackingUrl = $orderDataArray['tracking_url']; - $this->updatedAt = $orderDataArray['updated']; + $this->updatedAt = isset($orderDataArray['updated']) ? $orderDataArray['updated'] : null; } - - /** * @return string|null */ diff --git a/tests/Unit/Endpoints/PaymentsTest.php b/tests/Unit/Endpoints/PaymentsTest.php index 3da4ee26..d6027b1f 100644 --- a/tests/Unit/Endpoints/PaymentsTest.php +++ b/tests/Unit/Endpoints/PaymentsTest.php @@ -61,7 +61,6 @@ class PaymentsTest extends TestCase "tracking_url":null, "comment":null, "created":1649672451, - "updated":1649672451, "customer_url":null, "data":{}, "id":"order_11uPRjP4L9Dgbttx3cFUKGFPppdZIlrR2V", From 5418e1fafe930dc71d9e8dccf11806fe37fdf61d Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 15 Jul 2024 17:07:32 +0200 Subject: [PATCH 08/18] tests: rework integration tests for payment --- tests/Integration/Endpoints/PaymentsTest.php | 81 +++++++------------ .../TestHelpers/ClientTestHelper.php | 17 ++++ .../TestHelpers/PaymentTestHelper.php | 49 +++++++++++ 3 files changed, 96 insertions(+), 51 deletions(-) create mode 100644 tests/Integration/TestHelpers/ClientTestHelper.php create mode 100644 tests/Integration/TestHelpers/PaymentTestHelper.php diff --git a/tests/Integration/Endpoints/PaymentsTest.php b/tests/Integration/Endpoints/PaymentsTest.php index c35657dd..8772b1c9 100644 --- a/tests/Integration/Endpoints/PaymentsTest.php +++ b/tests/Integration/Endpoints/PaymentsTest.php @@ -1,72 +1,51 @@ 'test', 'api_root' => $_ENV['ALMA_API_ROOT'], 'force_tls' => false] - ); - } - - - private function paymentData($amount) - { - return [ - 'payment' => [ - 'purchase_amount' => $amount, - 'shipping_address' => [ - 'first_name' => 'Jane', - 'last_name' => 'Doe', - 'line1' => '2 rue de la rue', - 'city' => 'Paris', - 'postal_code' => '75002', - 'country' => 'FR', - ] - ] - ]; - } - - /** - * @param int $amount * @throws RequestError */ - private function createPayment($amount) + public static function setUpBeforeClass(): void { - return self::$almaClient->payments->create(self::paymentData($amount)); + self::$payment = PaymentTestHelper::createPayment(26500, 3); + self::$almaClient = ClientTestHelper::getAlmaClient(); } - /** * @throws RequestError */ private function checkEligibility($amount, $eligible) { - $eligibility = self::$almaClient->payments->eligibility(self::paymentData($amount)); - self::assertEquals($eligible, $eligibility->isEligible); + $eligibilityPayload = [ + 'purchase_amount' => $amount, + 'queries' => [ + ['installments_count' => 3], + ] + ]; + $eligibility = self::$almaClient->payments->eligibility($eligibilityPayload); + $eligibility = $eligibility['general_3_0_0']; + + $this->assertEquals($eligible, $eligibility->isEligible); if (!$eligible) { - self::assertArrayHasKey('purchase_amount', $eligibility->reasons); - self::assertEquals('invalid_value', $eligibility->reasons['purchase_amount']); + $this->assertArrayHasKey('purchase_amount', $eligibility->reasons); + $this->assertEquals('invalid_value', $eligibility->reasons['purchase_amount']); - self::assertArrayHasKey('purchase_amount', $eligibility->constraints); - self::assertArrayHasKey('minimum', $eligibility->constraints['purchase_amount']); - self::assertArrayHasKey('maximum', $eligibility->constraints['purchase_amount']); + $this->assertArrayHasKey('purchase_amount', $eligibility->constraints); + $this->assertArrayHasKey('minimum', $eligibility->constraints['purchase_amount']); + $this->assertArrayHasKey('maximum', $eligibility->constraints['purchase_amount']); } } @@ -75,9 +54,9 @@ private function checkEligibility($amount, $eligible) */ public function testCanCheckEligibility() { - self::checkEligibility(1, false); - self::checkEligibility(20000, true); - self::checkEligibility(500000, false); + $this->checkEligibility(1, false); + $this->checkEligibility(20000, true); + $this->checkEligibility(500000, false); } /** @@ -85,8 +64,8 @@ public function testCanCheckEligibility() */ public function testCanCreateAPayment() { - $payment = self::createPayment(26300); - self::assertEquals(26300, $payment->purchase_amount); + $payment = self::$payment; + $this->assertEquals(26500, $payment->purchase_amount); } /** @@ -94,9 +73,9 @@ public function testCanCreateAPayment() */ public function testCanFetchAPayment() { - $p1 = self::createPayment(26500); + $p1 = self::$payment; $p2 = self::$almaClient->payments->fetch($p1->id); - self::assertEquals($p1->id, $p2->id); + $this->assertEquals($p1->id, $p2->id); } } diff --git a/tests/Integration/TestHelpers/ClientTestHelper.php b/tests/Integration/TestHelpers/ClientTestHelper.php new file mode 100644 index 00000000..f094e7ef --- /dev/null +++ b/tests/Integration/TestHelpers/ClientTestHelper.php @@ -0,0 +1,17 @@ + 'test', 'api_root' => $_ENV['ALMA_API_ROOT'], 'force_tls' => false] + ); + } + +} \ No newline at end of file diff --git a/tests/Integration/TestHelpers/PaymentTestHelper.php b/tests/Integration/TestHelpers/PaymentTestHelper.php new file mode 100644 index 00000000..aed1744a --- /dev/null +++ b/tests/Integration/TestHelpers/PaymentTestHelper.php @@ -0,0 +1,49 @@ +payments->create(self::paymentData($amount, $installmentsCount)); + } + + private static function paymentData($amount, $installmentsCount = 3) + { + return [ + 'payment' => [ + 'purchase_amount' => $amount, + 'installments_count' => $installmentsCount, + 'shipping_address' => [ + 'first_name' => 'Jane', + 'last_name' => 'Doe', + 'line1' => '2 rue de la rue', + 'city' => 'Paris', + 'postal_code' => '75002', + 'country' => 'FR', + ] + ], + 'customer' => [ + 'first_name' => 'Test Integration', + 'last_name' => 'Ecom', + 'email' => 'test@almapay.com', + ], + 'order' => [ + 'merchant_reference' => 'ABC-123', + ] + ]; + } + + +} \ No newline at end of file From 80e0a2cd729c54571bc8dcc72b9568d6643a9a4f Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Tue, 16 Jul 2024 11:08:42 +0200 Subject: [PATCH 09/18] tests: add integration test for order Tracking --- tests/Integration/Endpoints/OrdersTest.php | 59 ++++++++++++++++++++++ tests/Unit/Endpoints/OrdersTest.php | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/Endpoints/OrdersTest.php diff --git a/tests/Integration/Endpoints/OrdersTest.php b/tests/Integration/Endpoints/OrdersTest.php new file mode 100644 index 00000000..f42d49dc --- /dev/null +++ b/tests/Integration/Endpoints/OrdersTest.php @@ -0,0 +1,59 @@ +orders[0]; + $this->assertInstanceOf(Order::class, $order); + $this->assertEquals('ABC-123', $order->getMerchantReference()); + $newOrder = self::$almaClient->payments->addOrder($payment->id, ['merchant_reference' => 'ABC-123-NEW']); + $this->assertInstanceOf(Order::class, $newOrder); + $this->assertEquals('ABC-123-NEW', $newOrder->getMerchantReference()); + } + + public function testCanUpdateOrderTracking() + { + $payment = self::$payment; + $order = $payment->orders[0]; + $this->assertInstanceOf(Order::class, $order); + $this->assertNull($order->getCarrier()); + $this->assertNull($order->getTrackingUrl()); + $this->assertNull($order->getTrackingNumber()); + + $updatedOrder = self::$almaClient->orders->updateTracking($order->getExternalId(), null,null , 'https://tracking.com'); + $this->assertInstanceOf(Order::class, $updatedOrder); + $this->assertNull($order->getCarrier()); + $this->assertNull($updatedOrder->getTrackingNumber()); + $this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl()); + + $updatedOrder = self::$almaClient->orders->updateTracking($order->getExternalId(), 'UPS'); + $this->assertInstanceOf(Order::class, $updatedOrder); + $this->assertEquals('UPS', $updatedOrder->getCarrier()); + $this->assertNull($updatedOrder->getTrackingNumber()); + $this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl()); + + $updatedOrder = self::$almaClient->orders->updateTracking($order->getExternalId(), 'LAPOSTE','123456789' , 'https://laposte.com'); + $this->assertInstanceOf(Order::class, $updatedOrder); + $this->assertEquals('LAPOSTE', $updatedOrder->getCarrier()); + $this->assertEquals('123456789', $updatedOrder->getTrackingNumber()); + $this->assertEquals('https://laposte.com', $updatedOrder->getTrackingUrl()); + } +} \ No newline at end of file diff --git a/tests/Unit/Endpoints/OrdersTest.php b/tests/Unit/Endpoints/OrdersTest.php index 80bdd0b4..633c35f7 100644 --- a/tests/Unit/Endpoints/OrdersTest.php +++ b/tests/Unit/Endpoints/OrdersTest.php @@ -218,7 +218,7 @@ public function testUpdateTracking($carrier, $trackingNumber, $trackingUrl, $tra ->once() ->andReturn($this->requestObject); $order = $this->orderEndpoint->updateTracking('123', $carrier, $trackingNumber, $trackingUrl); - $this->isInstanceOf(Order::class, $order); + $this->assertInstanceOf(Order::class, $order); $this->assertEquals($carrier, $order->getCarrier()); $this->assertEquals($trackingNumber, $order->getTrackingNumber()); $this->assertEquals($trackingUrl, $order->getTrackingUrl()); From 0f9692333d95310078b70dec6f517cfe912cfec5 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Tue, 16 Jul 2024 11:18:27 +0200 Subject: [PATCH 10/18] tests: change static call method self to className --- tests/Integration/Endpoints/OrdersTest.php | 16 ++++++++-------- tests/Integration/Endpoints/PaymentsTest.php | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/Integration/Endpoints/OrdersTest.php b/tests/Integration/Endpoints/OrdersTest.php index f42d49dc..e0ab2d78 100644 --- a/tests/Integration/Endpoints/OrdersTest.php +++ b/tests/Integration/Endpoints/OrdersTest.php @@ -14,43 +14,43 @@ class OrdersTest extends TestCase public static function setUpBeforeClass(): void { - self::$almaClient = ClientTestHelper::getAlmaClient(); - self::$payment = PaymentTestHelper::createPayment(26500, 3); + OrdersTest::$almaClient = ClientTestHelper::getAlmaClient(); + OrdersTest::$payment = PaymentTestHelper::createPayment(26500, 3); } public function testCanCreateANewOrder() { - $payment = self::$payment; + $payment = OrdersTest::$payment; $order = $payment->orders[0]; $this->assertInstanceOf(Order::class, $order); $this->assertEquals('ABC-123', $order->getMerchantReference()); - $newOrder = self::$almaClient->payments->addOrder($payment->id, ['merchant_reference' => 'ABC-123-NEW']); + $newOrder = OrdersTest::$almaClient->payments->addOrder($payment->id, ['merchant_reference' => 'ABC-123-NEW']); $this->assertInstanceOf(Order::class, $newOrder); $this->assertEquals('ABC-123-NEW', $newOrder->getMerchantReference()); } public function testCanUpdateOrderTracking() { - $payment = self::$payment; + $payment = OrdersTest::$payment; $order = $payment->orders[0]; $this->assertInstanceOf(Order::class, $order); $this->assertNull($order->getCarrier()); $this->assertNull($order->getTrackingUrl()); $this->assertNull($order->getTrackingNumber()); - $updatedOrder = self::$almaClient->orders->updateTracking($order->getExternalId(), null,null , 'https://tracking.com'); + $updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), null,null , 'https://tracking.com'); $this->assertInstanceOf(Order::class, $updatedOrder); $this->assertNull($order->getCarrier()); $this->assertNull($updatedOrder->getTrackingNumber()); $this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl()); - $updatedOrder = self::$almaClient->orders->updateTracking($order->getExternalId(), 'UPS'); + $updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), 'UPS'); $this->assertInstanceOf(Order::class, $updatedOrder); $this->assertEquals('UPS', $updatedOrder->getCarrier()); $this->assertNull($updatedOrder->getTrackingNumber()); $this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl()); - $updatedOrder = self::$almaClient->orders->updateTracking($order->getExternalId(), 'LAPOSTE','123456789' , 'https://laposte.com'); + $updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), 'LAPOSTE','123456789' , 'https://laposte.com'); $this->assertInstanceOf(Order::class, $updatedOrder); $this->assertEquals('LAPOSTE', $updatedOrder->getCarrier()); $this->assertEquals('123456789', $updatedOrder->getTrackingNumber()); diff --git a/tests/Integration/Endpoints/PaymentsTest.php b/tests/Integration/Endpoints/PaymentsTest.php index 8772b1c9..49056bfb 100644 --- a/tests/Integration/Endpoints/PaymentsTest.php +++ b/tests/Integration/Endpoints/PaymentsTest.php @@ -19,8 +19,8 @@ final class PaymentsTest extends TestCase */ public static function setUpBeforeClass(): void { - self::$payment = PaymentTestHelper::createPayment(26500, 3); - self::$almaClient = ClientTestHelper::getAlmaClient(); + PaymentsTest::$payment = PaymentTestHelper::createPayment(26500, 3); + PaymentsTest::$almaClient = ClientTestHelper::getAlmaClient(); } /** @@ -34,7 +34,7 @@ private function checkEligibility($amount, $eligible) ['installments_count' => 3], ] ]; - $eligibility = self::$almaClient->payments->eligibility($eligibilityPayload); + $eligibility = PaymentsTest::$almaClient->payments->eligibility($eligibilityPayload); $eligibility = $eligibility['general_3_0_0']; $this->assertEquals($eligible, $eligibility->isEligible); @@ -64,7 +64,7 @@ public function testCanCheckEligibility() */ public function testCanCreateAPayment() { - $payment = self::$payment; + $payment = PaymentsTest::$payment; $this->assertEquals(26500, $payment->purchase_amount); } @@ -73,8 +73,8 @@ public function testCanCreateAPayment() */ public function testCanFetchAPayment() { - $p1 = self::$payment; - $p2 = self::$almaClient->payments->fetch($p1->id); + $p1 = PaymentsTest::$payment; + $p2 = PaymentsTest::$almaClient->payments->fetch($p1->id); $this->assertEquals($p1->id, $p2->id); } From 7f7150761ed46cbf159c646b9c848f47b85dca94 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 24 Jul 2024 00:31:21 +0200 Subject: [PATCH 11/18] feature: create addTracking endpoint --- src/Endpoints/Orders.php | 14 +++-- src/Entities/Order.php | 36 ----------- tests/Unit/Endpoints/OrdersTest.php | 98 ++++++++++------------------- tests/Unit/Entities/OrderTest.php | 9 --- 4 files changed, 40 insertions(+), 117 deletions(-) diff --git a/src/Endpoints/Orders.php b/src/Endpoints/Orders.php index 27ff15b3..d2b0f68d 100644 --- a/src/Endpoints/Orders.php +++ b/src/Endpoints/Orders.php @@ -66,21 +66,23 @@ public function update($orderId, $orderData) /** * @param string $orderId - * @param string|null $carrier - * @param string|null $trackingNumber + * @param string $carrier + * @param string $trackingNumber * @param string|null $trackingUrl - * @return Order + * @return void * @throws AlmaException */ - public function updateTracking($orderId, $carrier = null, $trackingNumber = null, $trackingUrl = null) + public function addTracking($orderId, $carrier, $trackingNumber, $trackingUrl = null) { $trackingData = array_filter([ 'carrier' => $carrier, 'tracking_number' => $trackingNumber, 'tracking_url' => $trackingUrl ]); - $response = $this->request(self::ORDERS_PATH . "/{$orderId}")->setRequestBody($trackingData)->put(); - return new Order($response->json); + $response = $this->request(self::ORDERS_PATH_V2 . "/{$orderId}/shipment")->setRequestBody($trackingData)->post(); + if ($response->isError()) { + throw new RequestException($response->errorMessage, null, $response); + } } /** diff --git a/src/Entities/Order.php b/src/Entities/Order.php index 1aa8fbde..46fb40c1 100644 --- a/src/Entities/Order.php +++ b/src/Entities/Order.php @@ -27,15 +27,6 @@ class Order { - /** @var string | null Order carrier */ - private $carrier; - - /** @var string | null Order carrier tracking number */ - private $trackingNumber; - - /** @var string | null Order carrier tracking URL */ - private $trackingUrl; - /** @var string ID of the Payment owning this Order * @deprecated */ @@ -105,7 +96,6 @@ class Order public function __construct($orderDataArray) { - $this->carrier = $orderDataArray['carrier']; $this->comment = $orderDataArray['comment']; $this->createdAt = $orderDataArray['created']; $this->customerUrl = $orderDataArray['customer_url']; @@ -119,35 +109,9 @@ public function __construct($orderDataArray) $this->merchantUrl = $orderDataArray['merchant_url']; $this->payment = $orderDataArray['payment']; $this->paymentId = $orderDataArray['payment']; - $this->trackingNumber = $orderDataArray['tracking_number']; - $this->trackingUrl = $orderDataArray['tracking_url']; $this->updatedAt = isset($orderDataArray['updated']) ? $orderDataArray['updated'] : null; } - /** - * @return string|null - */ - public function getCarrier() - { - return $this->carrier; - } - - /** - * @return string|null - */ - public function getTrackingNumber() - { - return $this->trackingNumber; - } - - /** - * @return string|null - */ - public function getTrackingUrl() - { - return $this->trackingUrl; - } - /** * @return string */ diff --git a/tests/Unit/Endpoints/OrdersTest.php b/tests/Unit/Endpoints/OrdersTest.php index 633c35f7..3b12554f 100644 --- a/tests/Unit/Endpoints/OrdersTest.php +++ b/tests/Unit/Endpoints/OrdersTest.php @@ -186,91 +186,57 @@ public function testSendStatusWithException() )); } - public function testUpdateTrackingThrowsAlmaException() + public function testAddTrackingThrowsAlmaException() { $this->expectException(AlmaException::class); - $this->requestObject->shouldReceive('put')->andThrow(new RequestError()); + $this->requestObject->shouldReceive('post')->andThrow(new RequestError()); $this->requestObject->shouldReceive('setRequestBody')->andReturn($this->requestObject); $this->orderEndpoint->shouldReceive('request') - ->with('/v1/orders/123') + ->with('/v2/orders/123/shipment') ->once() ->andReturn($this->requestObject); - $this->orderEndpoint->updateTracking('123', 'ups', '123456', 'myUrl'); + $this->orderEndpoint->addTracking('123', 'ups', '123456', 'myUrl'); + } + public function testAddTrackingThrowsAlmaExceptionForBadData() + { + $this->expectException(RequestException::class); + $this->responseMock->shouldReceive('isError')->once()->andReturn(true); + $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); + $this->requestObject->shouldReceive('setRequestBody')->andReturn($this->requestObject); + $this->orderEndpoint->shouldReceive('request') + ->with('/v2/orders/123/shipment') + ->once() + ->andReturn($this->requestObject); + $this->orderEndpoint->addTracking('123', 'ups', null); } /** - * @dataProvider updateTrackingDataProvider - * @param $carrier - * @param $trackingNumber - * @param $trackingUrl - * @param $trackingData * @return void * @throws AlmaException */ - public function testUpdateTracking($carrier, $trackingNumber, $trackingUrl, $trackingData) + public function testAddTracking() { - $this->responseMock->json = $this->orderDataFactory($carrier, $trackingNumber, $trackingUrl); - $this->requestObject->shouldReceive('put')->andReturn($this->responseMock); + $trackingData = [ + 'carrier' => 'UPS', + 'tracking_number' => 'UPS_123456', + 'tracking_url' => 'https://tracking.com' + ]; + $this->responseMock->shouldReceive('isError')->once()->andReturn(false); + $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); $this->requestObject->shouldReceive('setRequestBody')->with($trackingData)->andReturn($this->requestObject); + $this->orderEndpoint->shouldReceive('request') - ->with('/v1/orders/123') + ->with('/v2/orders/123/shipment') ->once() ->andReturn($this->requestObject); - $order = $this->orderEndpoint->updateTracking('123', $carrier, $trackingNumber, $trackingUrl); - $this->assertInstanceOf(Order::class, $order); - $this->assertEquals($carrier, $order->getCarrier()); - $this->assertEquals($trackingNumber, $order->getTrackingNumber()); - $this->assertEquals($trackingUrl, $order->getTrackingUrl()); - } - /** - * @return array[] - */ - public static function updateTrackingDataProvider() - { - return [ - 'no data' => [ - null, - null, - null, - [] - ], - 'Only Carrier' => [ - 'ups', - null, - null, - ['carrier' => 'ups'] - ], - 'Only Url' => [ - null, - null, - 'myUrl', - ['tracking_url' => 'myUrl'] - ], - 'Carrier and Url' => [ - 'ups', - null, - 'myUrl', - ['carrier' => 'ups', 'tracking_url' => 'myUrl'] - ], - 'All params' => [ - 'ups', - '123456', - 'myUrl', - ['carrier' => 'ups', 'tracking_number' => '123456', 'tracking_url' => 'myUrl'] - ], - ]; + $this->orderEndpoint->addTracking( + '123', + $trackingData['carrier'], + $trackingData['tracking_number'], + $trackingData['tracking_url'] + ); } - public function orderDataFactory( - $carrier, - $tracking_number, - $tracking_url - ) - { - return OrderTest::orderDataFactory($carrier, $tracking_number, $tracking_url); - } - - } diff --git a/tests/Unit/Entities/OrderTest.php b/tests/Unit/Entities/OrderTest.php index b890fb7c..c0a659f1 100644 --- a/tests/Unit/Entities/OrderTest.php +++ b/tests/Unit/Entities/OrderTest.php @@ -13,9 +13,6 @@ public function testOrderGetters() $orderData = $this->orderDataFactory(); $order = new Order($orderData); - $this->assertEquals($orderData['carrier'], $order->getCarrier()); - $this->assertEquals($orderData['tracking_number'], $order->getTrackingNumber()); - $this->assertEquals($orderData['tracking_url'], $order->getTrackingUrl()); $this->assertEquals($orderData['payment'], $order->payment); $this->assertEquals($orderData['payment'], $order->getPaymentId()); $this->assertEquals($orderData['merchant_reference'], $order->merchant_reference); @@ -34,9 +31,6 @@ public function testOrderGetters() public static function orderDataFactory( - $carrier = 'ups', - $tracking_number = 'ups_123456', - $tracking_url = 'http://tracking.url', $comment = 'my comment', $created = 1715331839, $customer_url = 'http://customer.url', @@ -49,7 +43,6 @@ public static function orderDataFactory( ) { return [ - 'carrier' => $carrier, 'comment' => $comment, 'created' => $created, 'customer_url' => $customer_url, @@ -58,8 +51,6 @@ public static function orderDataFactory( 'merchant_reference' => $merchant_reference, 'merchant_url' => $merchant_url, 'payment' => $payment, - 'tracking_number' => $tracking_number, - 'tracking_url' => $tracking_url, 'updated' => $updated ]; } From d60cf8aa93101d3178be8e4058f17e1474dbcac1 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 24 Jul 2024 10:26:08 +0200 Subject: [PATCH 12/18] feature: create addTracking endpoint integration test --- tests/Integration/Endpoints/OrdersTest.php | 39 +++++++++------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/tests/Integration/Endpoints/OrdersTest.php b/tests/Integration/Endpoints/OrdersTest.php index e0ab2d78..506c2445 100644 --- a/tests/Integration/Endpoints/OrdersTest.php +++ b/tests/Integration/Endpoints/OrdersTest.php @@ -3,6 +3,8 @@ namespace Alma\API\Tests\Integration\Endpoints; use Alma\API\Entities\Order; +use Alma\API\Exceptions\AlmaException; +use Alma\API\Exceptions\RequestException; use Alma\API\Tests\Integration\TestHelpers\ClientTestHelper; use Alma\API\Tests\Integration\TestHelpers\PaymentTestHelper; use PHPUnit\Framework\TestCase; @@ -28,32 +30,23 @@ public function testCanCreateANewOrder() $this->assertInstanceOf(Order::class, $newOrder); $this->assertEquals('ABC-123-NEW', $newOrder->getMerchantReference()); } - - public function testCanUpdateOrderTracking() + public function testAddOrderTrackingThrowErrorWithBadData() { + $this->expectException(RequestException::class); $payment = OrdersTest::$payment; $order = $payment->orders[0]; $this->assertInstanceOf(Order::class, $order); - $this->assertNull($order->getCarrier()); - $this->assertNull($order->getTrackingUrl()); - $this->assertNull($order->getTrackingNumber()); - - $updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), null,null , 'https://tracking.com'); - $this->assertInstanceOf(Order::class, $updatedOrder); - $this->assertNull($order->getCarrier()); - $this->assertNull($updatedOrder->getTrackingNumber()); - $this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl()); - - $updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), 'UPS'); - $this->assertInstanceOf(Order::class, $updatedOrder); - $this->assertEquals('UPS', $updatedOrder->getCarrier()); - $this->assertNull($updatedOrder->getTrackingNumber()); - $this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl()); - - $updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), 'LAPOSTE','123456789' , 'https://laposte.com'); - $this->assertInstanceOf(Order::class, $updatedOrder); - $this->assertEquals('LAPOSTE', $updatedOrder->getCarrier()); - $this->assertEquals('123456789', $updatedOrder->getTrackingNumber()); - $this->assertEquals('https://laposte.com', $updatedOrder->getTrackingUrl()); + $this->assertNull( + OrdersTest::$almaClient->orders->addTracking($order->getExternalId(), 'UPS',null ) + ); + } + public function testAddOrderTracking() + { + $payment = OrdersTest::$payment; + $order = $payment->orders[0]; + $this->assertInstanceOf(Order::class, $order); + $this->assertNull( + OrdersTest::$almaClient->orders->addTracking($order->getExternalId(), 'UPS','UPS_123456' , 'https://tracking.com') + ); } } \ No newline at end of file From 68747300e134c466726380e978f8847ced6396c2 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 24 Jul 2024 22:33:31 +0200 Subject: [PATCH 13/18] fix: orderTest and insurance lint --- src/Endpoints/Insurance.php | 2 +- tests/Integration/Endpoints/OrdersTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Endpoints/Insurance.php b/src/Endpoints/Insurance.php index bdd20c4a..9f08452a 100644 --- a/src/Endpoints/Insurance.php +++ b/src/Endpoints/Insurance.php @@ -76,7 +76,7 @@ public function getInsuranceContract( throw new RequestException($response->errorMessage, null, $response); } - // @todo is it a possible case, or do we need to throw an exception + // Is it a possible case, or do we need to throw an exception if (!$response->json) { return null; } diff --git a/tests/Integration/Endpoints/OrdersTest.php b/tests/Integration/Endpoints/OrdersTest.php index 506c2445..c1f1d21e 100644 --- a/tests/Integration/Endpoints/OrdersTest.php +++ b/tests/Integration/Endpoints/OrdersTest.php @@ -30,23 +30,23 @@ public function testCanCreateANewOrder() $this->assertInstanceOf(Order::class, $newOrder); $this->assertEquals('ABC-123-NEW', $newOrder->getMerchantReference()); } + public function testAddOrderTrackingThrowErrorWithBadData() { $this->expectException(RequestException::class); $payment = OrdersTest::$payment; $order = $payment->orders[0]; $this->assertInstanceOf(Order::class, $order); - $this->assertNull( - OrdersTest::$almaClient->orders->addTracking($order->getExternalId(), 'UPS',null ) - ); + OrdersTest::$almaClient->orders->addTracking($order->getExternalId(), 'UPS', null); } + public function testAddOrderTracking() { $payment = OrdersTest::$payment; $order = $payment->orders[0]; $this->assertInstanceOf(Order::class, $order); $this->assertNull( - OrdersTest::$almaClient->orders->addTracking($order->getExternalId(), 'UPS','UPS_123456' , 'https://tracking.com') + OrdersTest::$almaClient->orders->addTracking($order->getExternalId(), 'UPS', 'UPS_123456', 'https://tracking.com') ); } } \ No newline at end of file From 974ca6297677f1cd1bdaf044e8c0881fea6e3fc0 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 24 Jul 2024 22:41:39 +0200 Subject: [PATCH 14/18] fix: split 126 characters long --- src/Endpoints/Orders.php | 4 +++- tests/Integration/Endpoints/OrdersTest.php | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Endpoints/Orders.php b/src/Endpoints/Orders.php index d2b0f68d..75003ff3 100644 --- a/src/Endpoints/Orders.php +++ b/src/Endpoints/Orders.php @@ -79,7 +79,9 @@ public function addTracking($orderId, $carrier, $trackingNumber, $trackingUrl = 'tracking_number' => $trackingNumber, 'tracking_url' => $trackingUrl ]); - $response = $this->request(self::ORDERS_PATH_V2 . "/{$orderId}/shipment")->setRequestBody($trackingData)->post(); + $response = $this->request(self::ORDERS_PATH_V2 . "/{$orderId}/shipment") + ->setRequestBody($trackingData) + ->post(); if ($response->isError()) { throw new RequestException($response->errorMessage, null, $response); } diff --git a/tests/Integration/Endpoints/OrdersTest.php b/tests/Integration/Endpoints/OrdersTest.php index c1f1d21e..dd12d9e0 100644 --- a/tests/Integration/Endpoints/OrdersTest.php +++ b/tests/Integration/Endpoints/OrdersTest.php @@ -46,7 +46,12 @@ public function testAddOrderTracking() $order = $payment->orders[0]; $this->assertInstanceOf(Order::class, $order); $this->assertNull( - OrdersTest::$almaClient->orders->addTracking($order->getExternalId(), 'UPS', 'UPS_123456', 'https://tracking.com') + OrdersTest::$almaClient->orders->addTracking( + $order->getExternalId(), + 'UPS', + 'UPS_123456', + 'https://tracking.com' + ) ); } } \ No newline at end of file From 3b1e8e9c94a7d807f051b3ee3db8a735784abebd Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Fri, 26 Jul 2024 14:58:45 +0200 Subject: [PATCH 15/18] fix: add beStrictAboutTestsThatDoNotTestAnything="false" property --- phpunit.ci.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/phpunit.ci.xml b/phpunit.ci.xml index 1ef46110..36343cc3 100644 --- a/phpunit.ci.xml +++ b/phpunit.ci.xml @@ -8,7 +8,9 @@ convertNoticesToExceptions = "true" convertWarningsToExceptions = "true" processIsolation = "false" - stopOnFailure = "true"> + stopOnFailure = "true" + beStrictAboutTestsThatDoNotTestAnything="false" + > From 299f6bd35045fec2db71e15990d78566dd9d246a Mon Sep 17 00:00:00 2001 From: Francois-Gomis <97046219+Francois-Gomis@users.noreply.github.com> Date: Mon, 29 Jul 2024 08:24:30 +0000 Subject: [PATCH 16/18] chore: update version --- CHANGELOG.md | 220 +++++++++++++++++++++++-------------------------- composer.json | 2 +- src/Client.php | 2 +- 3 files changed, 107 insertions(+), 117 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69202ce..e4492837 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,135 +1,152 @@ -CHANGELOG -========= +# CHANGELOG + +## v2.1.0 - 2024-07-29 + +### Changes + +- chore(deps): update lovetoknow/slackify-markdown-action action to v1.1.1 (#115) +- Merge release workflow and unit tests improvement into main (#120) +- [DEVX] Fix release zip filename (#117) +- [DEX-889] Add new release workflow (#111) +- Improve unit tests (#110) +- [DEX-889] Add a template for release-drafter (#112) +- Update the local docker development experience (#107) +- Run CI on multiple PHP versions (#108) +- [Security] Update deprecated Aqua scanner options (#106) +- chore(deps): update dependency ubuntu to v22 (#109) +- Add missing Renovate base branch configuration (#105) + +### 🚀 New Features + +- Change shipment data endpoint (#127) +- feature: refactor order and add updateTracking put endpoint (#124) +- Feature/ecom 1867 php client rework integration test in php client (#125) + +#### Contributors + +@Francois-Gomis, @alma-renovate-bot, @alma-renovate-bot[bot], @carine-bonnafous, @gdraynz, @joyet-simon and @remic-alma + +## v2.0.7 -v2.0.7 -------- * Add endpoint Order State +## v2.0.6 -v2.0.6 -------- * Fix error in throw exception without sprintf -v2.0.5 -------- +## v2.0.5 + * Fix endpoint customer-carts -v2.0.4 -------- +## v2.0.4 + * Fix json_decode in getSubscriptionDetails -* Add subscription pending_cancellation status +* Add subscription pending_cancellation status + +## v2.0.3 -v2.0.3 -------- * Added order_id in subscription model * Added sendCustomerCart endpoint Insurance * Added cancelSubscription endpoint Insurance -v2.0.2 -------- +## v2.0.2 + * Added getSubscription endpoint Insurance * Fix Subscription amount model -v2.0.1 -------- +## v2.0.1 + * Fix : Don't remove amount in refund if value equal zero -v2.0.0 -------- +## v2.0.0 + * Added Alma insurance endpoints -v1.11.2 -------- +## v1.11.2 + * Fix : Compatibility psr/log 1/2/3 * Fix : Unit tests and Integration tests for PHP5.6 to PHP8.2 -v1.11.1 -------- +## v1.11.1 + * Fix : Check the amount purchase type * Fix: composer -v1.11.0 -------- +## v1.11.0 + * Fix : Compatibility with PHP 8.2 -v1.10.0 -------- +## v1.10.0 + * Added pay now payment plan behavior -v1.9.3 -------- +## v1.9.3 + * Added payment cancel endpoint -v1.9.2 -------- +## v1.9.2 + * Fixed tests PSR4 namespaces -v1.9.1 -------- +## v1.9.1 + * Added `addConsent` and `removeConsent` method to `ShareOfCheckout` endpoint -v1.9.0 -------- +## v1.9.0 + * Added `expired_at` property to `Payment` entity * Added `getErrorMessage` method to `RequestError` to fetch error message from the response when not accessible from `getMessage` -v1.8.0 -------- +## v1.8.0 + * Splited `refund` endpoint into `partialRefund` and `fullRefund` * Added github CI for lint and unit tests purposes * Added unit-tests * Extract RequestError from Request.php file * Added share of checkout endpoint -v1.7.1 -------- +## v1.7.1 + * Fix merchant reference only on partial refunds (#23) -v1.7.0 -------- +## v1.7.0 + * Allow merchant reference on refund endpoint -v1.6.0 -------- +## v1.6.0 + * Add payment upon trigger fields * Add payment customer field * Add payment billing address field -v1.5.1 -------- +## v1.5.1 * Added configuration validation * Utils functions now moved into classes to respect php good practices * Added docker-compose to ease dev and local testing -v1.5.0 -------- +## v1.5.0 * Add trigger payment in endpoint payment -v1.4.0 -------- +## v1.4.0 * Add edit payment in endpoint payment -v1.3.1 -------- +## v1.3.1 * Add annual interest rate from eligibility -v1.3.0 -------- +## v1.3.0 * Add P10x informations -v1.2.0 -------- +## v1.2.0 * New eligibility v2 endpoint * Add customerTotalCostAmount, customerTotalCostBps in eligibility -v1.1.0 -------- +## v1.1.0 * Add an option to depart from a legacy behaviour where the eligibility endpoint would not raise RequestErrors on 4xx and 5xx errors. The default is to keep the original behaviour so as not to break existing implementations. @@ -141,157 +158,130 @@ v1.1.0 } catch (RequestError $e) { // Handle errors } + ``` * Add fields and docs to the Payment entity * Add a Refund entity and extract refunds data within the Payment entity constructor * Add the `feePlans` endpoint to the `merchants` scope, so that one can fetch "fee plans" configured for their merchant account: `$alma->merchants->feePlans();` — see function's docs for available options -v1.0.15 -------- +## v1.0.15 * Add missing fields to Instalment entity -v1.0.14 -------- +## v1.0.14 * Fix non-working code in the Orders endpoint & PaginatedResults class * Improve type hints * Fix compatibility with PHP versions older than 5.6 -v1.0.13 -------- +## v1.0.13 * Move `LIVE_MODE` & `TEST_MODE` constants into the `Client` class so that they're more easily addressable using `Alma\API\Client::LIVE_MODE` & `Alma\API\Client::TEST_MODE` - -v1.0.12 -------- +## v1.0.12 * Fixes webhook signature computation (use url-safe base64 encoding) * Updates Webhook type constant name -v1.0.11 -------- +## v1.0.11 * Adds endpoint to send payment link to customer via SMS - -v1.0.10 -------- +## v1.0.10 * Properly deserialize orders array in Payment entity * Documents Order entity fields * Adds endpoint to add an Order to a Payment -v1.0.9 ------- +## v1.0.9 * Document usage of the lib without Composer & release a ready-to-use zip of the API client -v1.0.8 ------- +## v1.0.8 * Fixes handling of server errors in eligibility endpoint -v1.0.7 ------- +## v1.0.7 * Fixes eligibility response handling, which was buggy in case of non-eligibility responses and legacy response types - -v1.0.6 ------- +## v1.0.6 * Minor bug fix - -v1.0.5 ------- +## v1.0.5 * Fixes missing Webhooks endpoint instance on Client class -v1.0.4 ------- +## v1.0.4 * Adds the Orders endpoint and Order entity to handle orders associated to a payment * Improves the eligibility endpoint to handle multiple eligibility results (for different installments counts) * Adds the Webhooks endpoint to be able to register webhook URLs against the API -v1.0.3 ------- +## v1.0.3 * Adds payment_plan to eligibility result -v1.0.2 ------- +## v1.0.2 * Bug fix: Always include a body for POST requests to prevent HTTP 411 error -v1.0.1 ------- +## v1.0.1 * Adds `fee_plans` attribute to the `Merchant` entity * Deprecate `Payments::createPayment` in favor of `Payments::create` * Adds the `Payments::refund` endpoint to partially or totally refund a payment * [Adds PHPUnit and a few unit tests, but nothing big just yet] -v1.0.0 ------- +## v1.0.0 Getting more serious with a 1.0.0 release! 🎉 * Adds User-Agent with PHP and client version * Adds ability to add User-Agent "components" to the request -v0.0.7 -------- +## v0.0.7 * Eligibility check now returns `200 OK` with `{"eligible": false}` for non-eligible purchases: - supports legacy `406` status code and the new version + supports legacy `406` status code and the new version * New eligibility check's negative response includes constraints that should be met to be eligible - -v0.0.6 -------- +## v0.0.6 * Adds the possibility to flag a payment as potentially fraudulent -v0.0.5 ------- +## v0.0.5 * Adds `Alma\API\Payment::STATE_PAID` - -v0.0.4 ------- +## v0.0.4 * Adds support for Sandbox API root - -v0.0.3 ------- +## v0.0.3 * Bug fixes: - * Missing default Logger in Client instantiation - now using Psr\Log\NullLogger - * Alma\Api\Entity\Payment attributes had been wrongly converted to `camelCase` + * Missing default Logger in Client instantiation - now using Psr\Log\NullLogger + * Alma\Api\Entity\Payment attributes had been wrongly converted to `camelCase` + -v0.0.2 ------- +## v0.0.2 * Updates root namespace to `Alma\API` instead of just `Alma` * Adds PSR-4 autoload config to `composer.json` * Logger is now just an option to the Client creation * Uses PSR-3 logger spec -v0.0.1 ------- +## v0.0.1 * Initial "pre-release" of the API Client * Includes two main endpoints: Payments and Merchants * Provides what's necessary for a typical payment flow: - * `Payments.createPayment` and `Payments.eligibility` - * `Merchants.me` + * `Payments.createPayment` and `Payments.eligibility` + * `Merchants.me` + * Base `Alma\API\Client` class can be configured with API key and live/test mode * TLS is automatically forced to TLS 1.2 to meet Alma's security requirements, but configurable diff --git a/composer.json b/composer.json index f0b54593..6e02ebf4 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "alma/alma-php-client", "description": "PHP API client for the Alma payments API", - "version": "2.0.7", + "version": "2.1.0", "type": "library", "require": { "php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1 || ~8.2 || ~8.3", diff --git a/src/Client.php b/src/Client.php index 78766314..ed371ea6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -30,7 +30,7 @@ class Client { - const VERSION = '2.0.7'; + const VERSION = '2.1.0'; const LIVE_MODE = 'live'; const TEST_MODE = 'test'; From 2ca417d6bb22ba35e9330e2166402322a3519c53 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 29 Jul 2024 10:53:18 +0200 Subject: [PATCH 17/18] fix: remove array_filter in add tracking endpoint --- src/Endpoints/Orders.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Endpoints/Orders.php b/src/Endpoints/Orders.php index 75003ff3..66fa4c9b 100644 --- a/src/Endpoints/Orders.php +++ b/src/Endpoints/Orders.php @@ -74,11 +74,11 @@ public function update($orderId, $orderData) */ public function addTracking($orderId, $carrier, $trackingNumber, $trackingUrl = null) { - $trackingData = array_filter([ + $trackingData = [ 'carrier' => $carrier, 'tracking_number' => $trackingNumber, 'tracking_url' => $trackingUrl - ]); + ]; $response = $this->request(self::ORDERS_PATH_V2 . "/{$orderId}/shipment") ->setRequestBody($trackingData) ->post(); From b96c9b96cdf5e64184ccf9e75a12da1f99340315 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 29 Jul 2024 10:53:42 +0200 Subject: [PATCH 18/18] changelog: edit changelog --- CHANGELOG.md | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4492837..75370ae0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,27 +4,17 @@ ### Changes -- chore(deps): update lovetoknow/slackify-markdown-action action to v1.1.1 (#115) -- Merge release workflow and unit tests improvement into main (#120) -- [DEVX] Fix release zip filename (#117) -- [DEX-889] Add new release workflow (#111) +- Add new release workflow (#111) - Improve unit tests (#110) -- [DEX-889] Add a template for release-drafter (#112) -- Update the local docker development experience (#107) -- Run CI on multiple PHP versions (#108) -- [Security] Update deprecated Aqua scanner options (#106) -- chore(deps): update dependency ubuntu to v22 (#109) -- Add missing Renovate base branch configuration (#105) +- Rework integration test in php client (#125) ### 🚀 New Features -- Change shipment data endpoint (#127) -- feature: refactor order and add updateTracking put endpoint (#124) -- Feature/ecom 1867 php client rework integration test in php client (#125) +- Add updateTracking put endpoint & refactor Order Model (#124) #### Contributors -@Francois-Gomis, @alma-renovate-bot, @alma-renovate-bot[bot], @carine-bonnafous, @gdraynz, @joyet-simon and @remic-alma +@Francois-Gomis, @carine-bonnafous, @gdraynz, @joyet-simon and @remic-alma ## v2.0.7