From e9b8bed8043cc13b869e7b032a043b9321120d24 Mon Sep 17 00:00:00 2001 From: "alma-renovate-bot[bot]" <163289924+alma-renovate-bot[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 10:14:20 +0000 Subject: [PATCH 01/21] chore(deps): update github actions --- .github/workflows/backport-pull-request.yml | 2 +- .github/workflows/hotfix-pull-request.yml | 2 +- .github/workflows/release-publish.yml | 2 +- .github/workflows/release-pull-request.yml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/backport-pull-request.yml b/.github/workflows/backport-pull-request.yml index 61b98b74..c79d9741 100644 --- a/.github/workflows/backport-pull-request.yml +++ b/.github/workflows/backport-pull-request.yml @@ -28,7 +28,7 @@ jobs: git reset --hard main - name: Create Pull Request - uses: peter-evans/create-pull-request@v6 + uses: peter-evans/create-pull-request@v7 with: commit-message: 'chore: backport main to develop' title: Backport main to develop diff --git a/.github/workflows/hotfix-pull-request.yml b/.github/workflows/hotfix-pull-request.yml index 02eb960d..8cdcce27 100644 --- a/.github/workflows/hotfix-pull-request.yml +++ b/.github/workflows/hotfix-pull-request.yml @@ -49,7 +49,7 @@ jobs: ./scripts/update-files-with-release-version.sh ${{ steps.release-drafter.outputs.tag_name }} - name: Create Pull Request - uses: peter-evans/create-pull-request@v6 + uses: peter-evans/create-pull-request@v7 with: commit-message: 'chore: update version' title: Release ${{ steps.release-drafter.outputs.tag_name }} diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml index b1723126..97d61fd2 100644 --- a/.github/workflows/release-publish.yml +++ b/.github/workflows/release-publish.yml @@ -113,7 +113,7 @@ jobs: cc <@france.berut> <@khadija.cherif> - name: Send changelog to Slack - uses: slackapi/slack-github-action@v1.26.0 + uses: slackapi/slack-github-action@v1.27.0 with: channel-id: CR9C57YM6 slack-message: ${{ steps.slack-markdown-release-notes.outputs.text }} diff --git a/.github/workflows/release-pull-request.yml b/.github/workflows/release-pull-request.yml index 70ee17ed..16e60ee9 100644 --- a/.github/workflows/release-pull-request.yml +++ b/.github/workflows/release-pull-request.yml @@ -55,7 +55,7 @@ jobs: repositories: alma-php-client - name: Create Pull Request - uses: peter-evans/create-pull-request@v6 + uses: peter-evans/create-pull-request@v7 with: token: ${{ steps.github-token.outputs.token }} commit-message: 'chore: update version' From ccd813b4d08974f76117c8884cf0e4d62910f8d0 Mon Sep 17 00:00:00 2001 From: "alma-renovate-bot[bot]" <163289924+alma-renovate-bot[bot]@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:28:03 +0000 Subject: [PATCH 02/21] chore(deps): update dependency ubuntu to v24 --- .github/workflows/aqua.yml | 2 +- .github/workflows/backport-pull-request.yml | 2 +- .github/workflows/ci.yml | 2 +- .github/workflows/hotfix-pull-request.yml | 2 +- .github/workflows/pr-labeler.yml | 2 +- .github/workflows/release-publish.yml | 2 +- .github/workflows/release-pull-request.yml | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/aqua.yml b/.github/workflows/aqua.yml index 633e7bc4..c02326ff 100644 --- a/.github/workflows/aqua.yml +++ b/.github/workflows/aqua.yml @@ -8,7 +8,7 @@ on: jobs: aqua: name: Aqua scanner - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/.github/workflows/backport-pull-request.yml b/.github/workflows/backport-pull-request.yml index c79d9741..da739105 100644 --- a/.github/workflows/backport-pull-request.yml +++ b/.github/workflows/backport-pull-request.yml @@ -13,7 +13,7 @@ jobs: create-backport-pull-request: if: ${{ (github.event.pull_request.merged == true) && (contains(github.event.pull_request.labels.*.name, 'release')) }} - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae228866..fe2d47a3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ on: jobs: ci: name: Linting, tests and coverage - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 strategy: fail-fast: false diff --git a/.github/workflows/hotfix-pull-request.yml b/.github/workflows/hotfix-pull-request.yml index 8cdcce27..9325fc77 100644 --- a/.github/workflows/hotfix-pull-request.yml +++ b/.github/workflows/hotfix-pull-request.yml @@ -11,7 +11,7 @@ on: jobs: create-hotfix-pull-request: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: diff --git a/.github/workflows/pr-labeler.yml b/.github/workflows/pr-labeler.yml index 62015b27..d3f2d9ee 100644 --- a/.github/workflows/pr-labeler.yml +++ b/.github/workflows/pr-labeler.yml @@ -9,7 +9,7 @@ jobs: contents: read pull-requests: write - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: - uses: TimonVS/pr-labeler-action@v5 env: diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml index 97d61fd2..fa128798 100644 --- a/.github/workflows/release-publish.yml +++ b/.github/workflows/release-publish.yml @@ -11,7 +11,7 @@ jobs: release: if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'release') - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: diff --git a/.github/workflows/release-pull-request.yml b/.github/workflows/release-pull-request.yml index 16e60ee9..76f6e592 100644 --- a/.github/workflows/release-pull-request.yml +++ b/.github/workflows/release-pull-request.yml @@ -6,7 +6,7 @@ on: jobs: create-release-pull-request: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: From ea76db2f3ff379099acedc4a83bb296c99607c0a Mon Sep 17 00:00:00 2001 From: "alma-renovate-bot[bot]" <163289924+alma-renovate-bot[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 10:14:05 +0000 Subject: [PATCH 03/21] chore(deps): update pre-commit hook pre-commit/pre-commit-hooks to v5 (#139) Co-authored-by: alma-renovate-bot[bot] <163289924+alma-renovate-bot[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1c838fa1..95908ab7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 + rev: v5.0.0 hooks: - id: check-added-large-files args: ["--maxkb=1024"] From 99f8d00d37fe824a2dd36ee08c45089614d19265 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Wed, 16 Oct 2024 16:15:12 +0200 Subject: [PATCH 04/21] feat: create endpoint and formatter for gather cms data --- src/Endpoints/Merchants.php | 104 +++++++++++-------- src/Entities/MerchantData/CmsFeatures.php | 120 ++++++++++++++++++++++ src/Entities/MerchantData/CmsInfo.php | 91 ++++++++++++++++ src/Lib/PayloadFormatter.php | 25 +++++ tests/Unit/Endpoints/MerchantsTest.php | 62 +++++++++++ tests/Unit/Entities/CmsFeaturesTest.php | 115 +++++++++++++++++++++ tests/Unit/Entities/CmsInfoTest.php | 89 ++++++++++++++++ tests/Unit/Lib/PayloadFormatterTest.php | 105 +++++++++++++++++++ 8 files changed, 668 insertions(+), 43 deletions(-) create mode 100644 src/Entities/MerchantData/CmsFeatures.php create mode 100644 src/Entities/MerchantData/CmsInfo.php create mode 100644 src/Lib/PayloadFormatter.php create mode 100644 tests/Unit/Endpoints/MerchantsTest.php create mode 100644 tests/Unit/Entities/CmsFeaturesTest.php create mode 100644 tests/Unit/Entities/CmsInfoTest.php create mode 100644 tests/Unit/Lib/PayloadFormatterTest.php diff --git a/src/Endpoints/Merchants.php b/src/Endpoints/Merchants.php index f23b8af9..8d186056 100644 --- a/src/Endpoints/Merchants.php +++ b/src/Endpoints/Merchants.php @@ -27,58 +27,76 @@ use Alma\API\Entities\FeePlan; use Alma\API\Entities\Merchant; +use Alma\API\Entities\MerchantData\MerchantData; +use Alma\API\Exceptions\RequestException; use Alma\API\RequestError; class Merchants extends Base { - const MERCHANTS_PATH = '/v1/merchants'; - const ME_PATH = '/v1/me'; + const MERCHANTS_PATH = '/v1/merchants'; + const ME_PATH = '/v1/me'; - /** - * @return Merchant - * @throws RequestError - */ - public function me() - { - $res = $this->request(self::ME_PATH . '/extended-data')->get(); + /** + * @return Merchant + * @throws RequestError + */ + public function me() + { + $res = $this->request(self::ME_PATH . '/extended-data')->get(); - if ($res->isError()) { - throw new RequestError($res->errorMessage, null, $res); - } + if ($res->isError()) { + throw new RequestError($res->errorMessage, null, $res); + } - return new Merchant($res->json); - } + return new Merchant($res->json); + } - /** - * @param $kind string Either FeePlan::KIND_GENERAL or FeePlan::KIND_POS. The former is applied to online payments, - * while the latter will be used when creating a Payment with origin=pos_* for - * retail/point-of-sale use cases. Defaults to FeePlan::KIND_GENERAL - * @param string|int[] $installmentsCounts Only include fee plans that match the given installments counts, or use - * the string "all" (default) to get all available fee plans - * @param bool $includeDeferred Include deferred fee plans (i.e. Pay Later plans) in the response - * @return FeePlan[] An array of available fee plans (some might be disabled, check FeePlan->allowed for each) - * @throws RequestError - */ - public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "all", $includeDeferred = false) - { - if (is_array($installmentsCounts)) { - $only = implode(",", $installmentsCounts); - } else { - $only = $installmentsCounts; - } + /** + * @param $kind string Either FeePlan::KIND_GENERAL or FeePlan::KIND_POS. The former is applied to online payments, + * while the latter will be used when creating a Payment with origin=pos_* for + * retail/point-of-sale use cases. Defaults to FeePlan::KIND_GENERAL + * @param string|int[] $installmentsCounts Only include fee plans that match the given installments counts, or use + * the string "all" (default) to get all available fee plans + * @param bool $includeDeferred Include deferred fee plans (i.e. Pay Later plans) in the response + * @return FeePlan[] An array of available fee plans (some might be disabled, check FeePlan->allowed for each) + * @throws RequestError + */ + public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "all", $includeDeferred = false) + { + if (is_array($installmentsCounts)) { + $only = implode(",", $installmentsCounts); + } else { + $only = $installmentsCounts; + } - $res = $this->request(self::ME_PATH . "/fee-plans")->setQueryParams(array( - "kind" => $kind, - "only" => $only, - "deferred" => $includeDeferred ? "true" : "false" // Avoid conversion to "0"/"1" our API doesn't recognize - ))->get(); + $res = $this->request(self::ME_PATH . "/fee-plans")->setQueryParams(array( + "kind" => $kind, + "only" => $only, + "deferred" => $includeDeferred ? "true" : "false" // Avoid conversion to "0"/"1" our API doesn't recognize + ))->get(); - if ($res->isError()) { - throw new RequestError($res->errorMessage, null, $res); - } + if ($res->isError()) { + throw new RequestError($res->errorMessage, null, $res); + } - return array_map(function ($val) { - return new FeePlan($val); - }, $res->json); - } + return array_map(function ($val) { + return new FeePlan($val); + }, $res->json); + } + + /** + * @param string $url The URL to send to Alma for integrations configuration + * @throws RequestException + * @throws RequestError + */ + public function sendIntegrationsConfigurationsUrl($url) + { + $res = $this->request(self::ME_PATH . "/integrations/configurations")->setRequestBody(array( + "endpoint_url" => $url + ))->post(); + + if ($res->isError()) { + throw new RequestException($res->errorMessage, null, $res); + } + } } diff --git a/src/Entities/MerchantData/CmsFeatures.php b/src/Entities/MerchantData/CmsFeatures.php new file mode 100644 index 00000000..e640b88e --- /dev/null +++ b/src/Entities/MerchantData/CmsFeatures.php @@ -0,0 +1,120 @@ + + */ + private $specificFeatures; + + /** + * @var string[] + */ + private $countryRestriction; + + /** + * @var bool | null + */ + private $isMultisite; + + /** + * @var bool | null + */ + private $customWidgetCss; + + /** + * CmsFeatures constructor. + * @param array $cmsFeaturesDataArray + */ + public function __construct($cmsFeaturesDataArray) + { + // Ensure values are properly initialized + $this->almaEnabled = isset($cmsFeaturesDataArray['alma_enabled']) ? $cmsFeaturesDataArray['alma_enabled'] : null; + $this->widgetCartActivated = isset($cmsFeaturesDataArray['widget_cart_activated']) ? $cmsFeaturesDataArray['widget_cart_activated'] : null; + $this->widgetProductActivated = isset($cmsFeaturesDataArray['widget_product_activated']) ? $cmsFeaturesDataArray['widget_product_activated'] : null; + $this->usedFeePlans = $cmsFeaturesDataArray['used_fee_plans'] ?: ''; + $this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null; + $this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null; + $this->excludedCategories = $cmsFeaturesDataArray['excluded_categories'] ?: []; + $this->excludedCategoriesActivated = isset($cmsFeaturesDataArray['excluded_categories_activated']) ? + $cmsFeaturesDataArray['excluded_categories_activated'] : null; + $this->paymentMethodPosition = $cmsFeaturesDataArray['payment_method_position'] ?: null; + $this->specificFeatures = $cmsFeaturesDataArray['specific_features'] ?: []; + $this->countryRestriction = $cmsFeaturesDataArray['country_restriction'] ?: []; + $this->isMultisite = isset($cmsFeaturesDataArray['is_multisite']) ? $cmsFeaturesDataArray['is_multisite'] : null; + $this->customWidgetCss = isset($cmsFeaturesDataArray['custom_widget_css']) ? $cmsFeaturesDataArray['custom_widget_css'] : null; + } + + /** + * @return array + */ + public function getProperties() + { + // Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values + return array_filter([ + 'alma_enabled' => $this->almaEnabled, + 'widget_cart_activated' => $this->widgetCartActivated, + 'widget_product_activated' => $this->widgetProductActivated, + 'used_fee_plans' => $this->usedFeePlans, + 'in_page_activated' => $this->inPageActivated, + 'log_activated' => $this->logActivated, + 'excluded_categories' => $this->excludedCategories, + 'excluded_categories_activated' => $this->excludedCategoriesActivated, + 'payment_method_position' => $this->paymentMethodPosition, + 'specific_features' => $this->specificFeatures, + 'country_restriction' => $this->countryRestriction, + 'is_multisite' => $this->isMultisite, + 'custom_widget_css' => $this->customWidgetCss, + ], function($value) { + // Keep only values that are not null and not empty + return !is_null($value) && $value !== ''; + }); + } +} \ No newline at end of file diff --git a/src/Entities/MerchantData/CmsInfo.php b/src/Entities/MerchantData/CmsInfo.php new file mode 100644 index 00000000..4d0e50c1 --- /dev/null +++ b/src/Entities/MerchantData/CmsInfo.php @@ -0,0 +1,91 @@ + + */ + private $thirdPartiesPlugins; + + /** + * @var array + */ + private $themes; + + /** + * @var string|null + */ + private $languageName; + + /** + * @var string|null + */ + private $languageVersion; + + /** + * @var string|null + */ + private $almaPluginVersion; + + /** + * @var string|null + */ + private $almaSdkVersion; + + /** + * @var string|null + */ + private $almaSdkName; + + /** + * CmsInfo constructor. + * @param array $cmsInfoDataArray + */ + public function __construct($cmsInfoDataArray) + { + // Initialize values or set them to null if not available + $this->cmsName = $cmsInfoDataArray['cms_name'] ?: null; + $this->cmsVersion = $cmsInfoDataArray['cms_version'] ?: null; + $this->thirdPartiesPlugins = $cmsInfoDataArray['third_parties_plugins'] ?: []; + $this->themes = $cmsInfoDataArray['themes'] ?: []; + $this->languageName = $cmsInfoDataArray['language_name'] ?: null; + $this->languageVersion = $cmsInfoDataArray['language_version'] ?: null; + $this->almaPluginVersion = $cmsInfoDataArray['alma_plugin_version'] ?: null; + $this->almaSdkVersion = $cmsInfoDataArray['alma_sdk_version'] ?: null; + $this->almaSdkName = $cmsInfoDataArray['alma_sdk_name'] ?: null; + } + + /** + * @return array + */ + public function getProperties() + { + // Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values + return array_filter([ + 'cms_name' => $this->cmsName, + 'cms_version' => $this->cmsVersion, + 'third_parties_plugins' => $this->thirdPartiesPlugins, + 'themes' => $this->themes, + 'language_name' => $this->languageName, + 'language_version' => $this->languageVersion, + 'alma_plugin_version' => $this->almaPluginVersion, + 'alma_sdk_version' => $this->almaSdkVersion, + 'alma_sdk_name' => $this->almaSdkName, + ], function($value) { + // Keep only values that are not null and not empty + return !is_null($value) && $value !== ''; + }); + } +} \ No newline at end of file diff --git a/src/Lib/PayloadFormatter.php b/src/Lib/PayloadFormatter.php new file mode 100644 index 00000000..595cd226 --- /dev/null +++ b/src/Lib/PayloadFormatter.php @@ -0,0 +1,25 @@ + $cmsInfo->getProperties(), + "cms_features" => $cmsFeatures->getProperties(), + ]; + + return json_encode($payload); + } + +} \ No newline at end of file diff --git a/tests/Unit/Endpoints/MerchantsTest.php b/tests/Unit/Endpoints/MerchantsTest.php new file mode 100644 index 00000000..f6a55e84 --- /dev/null +++ b/tests/Unit/Endpoints/MerchantsTest.php @@ -0,0 +1,62 @@ +merchantEndpoint = Mockery::mock(Merchants::class)->makePartial(); + $this->responseMock = Mockery::mock(Response::class); + $this->requestObject = Mockery::mock(Request::class); + } + + public function tearDown(): void + { + $this->merchantEndpoint = null; + $this->responseMock = null; + $this->requestObject = null; + Mockery::close(); + } + + public function testSendIntegrationsConfigurationsUrlIsOk(){ + $this->responseMock->shouldReceive('isError')->once()->andReturn(false); + $this->requestObject->shouldReceive('setRequestBody') + ->with(['endpoint_url' => 'https://www.example.com/integrations/configurations']) + ->andReturn($this->requestObject); + + $this->merchantEndpoint->shouldReceive('request') + ->with(Merchants::ME_PATH . "/integrations/configurations") + ->once() + ->andReturn($this->requestObject); + $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); + + $url = "https://www.example.com/integrations/configurations"; + $this->assertNull($this->merchantEndpoint->sendIntegrationsConfigurationsUrl($url)); + } + + public function testSendIntegrationsConfigurationsUrlThrowRequestException(){ + $this->responseMock->shouldReceive('isError')->once()->andReturn(true); + $this->requestObject->shouldReceive('setRequestBody') + ->with(['endpoint_url' => 'https://www.example.com/integrations/configurations']) + ->andReturn($this->requestObject); + + $this->merchantEndpoint->shouldReceive('request') + ->with(Merchants::ME_PATH . "/integrations/configurations") + ->once() + ->andReturn($this->requestObject); + $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); + + $url = "https://www.example.com/integrations/configurations"; + $this->expectException(RequestException::class); + $this->merchantEndpoint->sendIntegrationsConfigurationsUrl($url); + + } +} \ No newline at end of file diff --git a/tests/Unit/Entities/CmsFeaturesTest.php b/tests/Unit/Entities/CmsFeaturesTest.php new file mode 100644 index 00000000..ca4e1d3b --- /dev/null +++ b/tests/Unit/Entities/CmsFeaturesTest.php @@ -0,0 +1,115 @@ + true, + 'widget_cart_activated' => true, + 'widget_product_activated' => false, + 'used_fee_plans' => 'Plan A', + 'payment_method_position' => 1, + 'in_page_activated' => true, + 'log_activated' => false, + 'excluded_categories' => ['category1', 'category2'], + 'excluded_categories_activated' => true, + 'specific_features' => ['feature1', 'feature2'], + 'country_restriction' => ['FR', 'US'], + 'is_multisite' => false, + 'custom_widget_css' => true, + ]; + + $cmsFeatures = new CmsFeatures($data); + + $this->assertEquals(true, $cmsFeatures->getProperties()['alma_enabled']); + $this->assertEquals(true, $cmsFeatures->getProperties()['widget_cart_activated']); + $this->assertEquals(false, $cmsFeatures->getProperties()['widget_product_activated']); + $this->assertEquals('Plan A', $cmsFeatures->getProperties()['used_fee_plans']); + $this->assertEquals(1, $cmsFeatures->getProperties()['payment_method_position']); + $this->assertEquals(true, $cmsFeatures->getProperties()['in_page_activated']); + $this->assertEquals(false, $cmsFeatures->getProperties()['log_activated']); + $this->assertEquals(['category1', 'category2'], $cmsFeatures->getProperties()['excluded_categories']); + $this->assertEquals(true, $cmsFeatures->getProperties()['excluded_categories_activated']); + $this->assertEquals(['feature1', 'feature2'], $cmsFeatures->getProperties()['specific_features']); + $this->assertEquals(['FR', 'US'], $cmsFeatures->getProperties()['country_restriction']); + $this->assertEquals(false, $cmsFeatures->getProperties()['is_multisite']); + $this->assertEquals(true, $cmsFeatures->getProperties()['custom_widget_css']); + } + + public function testConstructorHandlesNullValuesCorrectly() + { + $data = [ + 'alma_enabled' => null, + 'widget_cart_activated' => null, + 'widget_product_activated' => null, + 'used_fee_plans' => '', + 'payment_method_position' => null, + 'in_page_activated' => null, + 'log_activated' => null, + 'excluded_categories' => [], + 'excluded_categories_activated' => null, + 'specific_features' => [], + 'country_restriction' => [], + 'is_multisite' => null, + 'custom_widget_css' => null, + ]; + + $cmsFeatures = new CmsFeatures($data); + $properties = $cmsFeatures->getProperties(); + + $this->assertArrayNotHasKey('alma_enabled', $properties); + $this->assertArrayNotHasKey('widget_cart_activated', $properties); + $this->assertArrayNotHasKey('widget_product_activated', $properties); + $this->assertArrayNotHasKey('used_fee_plans', $properties); + $this->assertArrayNotHasKey('payment_method_position', $properties); + $this->assertArrayNotHasKey('in_page_activated', $properties); + $this->assertArrayNotHasKey('log_activated', $properties); + $this->assertEquals([], $properties['excluded_categories']); // Should be an empty array + $this->assertArrayNotHasKey('excluded_categories_activated', $properties); + $this->assertEquals([], $properties['specific_features']); // Should be an empty array + $this->assertEquals([], $properties['country_restriction']); // Should be an empty array + $this->assertArrayNotHasKey('is_multisite', $properties); + $this->assertArrayNotHasKey('custom_widget_css', $properties); + } + + public function testGetPropertiesFiltersOutNullAndEmptyValues() + { + $data = [ + 'alma_enabled' => true, + 'widget_cart_activated' => null, + 'widget_product_activated' => null, + 'used_fee_plans' => 'Plan B', + 'payment_method_position' => null, + 'in_page_activated' => false, + 'log_activated' => null, + 'excluded_categories' => ['category3'], + 'excluded_categories_activated' => null, + 'specific_features' => [], + 'country_restriction' => [], + 'is_multisite' => false, + 'custom_widget_css' => null, + ]; + + $cmsFeatures = new CmsFeatures($data); + $properties = $cmsFeatures->getProperties(); + + $this->assertArrayHasKey('alma_enabled', $properties); + $this->assertArrayNotHasKey('widget_cart_activated', $properties); // Should be filtered out (empty string) + $this->assertArrayNotHasKey('widget_product_activated', $properties); // Should be filtered out (null) + $this->assertArrayHasKey('used_fee_plans', $properties); + $this->assertArrayNotHasKey('payment_method_position', $properties); // Should be filtered out (null) + $this->assertArrayHasKey('in_page_activated', $properties); + $this->assertArrayHasKey('excluded_categories', $properties); + $this->assertArrayNotHasKey('excluded_categories_activated', $properties); // Should be filtered out (null) + $this->assertArrayHasKey('specific_features', $properties); + $this->assertArrayHasKey('country_restriction', $properties); + $this->assertArrayHasKey('is_multisite', $properties); + $this->assertArrayNotHasKey('custom_widget_css', $properties); // Should be filtered out (null) + } +} \ No newline at end of file diff --git a/tests/Unit/Entities/CmsInfoTest.php b/tests/Unit/Entities/CmsInfoTest.php new file mode 100644 index 00000000..014a1759 --- /dev/null +++ b/tests/Unit/Entities/CmsInfoTest.php @@ -0,0 +1,89 @@ + 'WordPress', + 'cms_version' => '5.8', + 'third_parties_plugins' => ['plugin1', 'plugin2'], + 'themes' => ['theme1'], + 'language_name' => 'PHP', + 'language_version' => '7.4', + 'alma_plugin_version' => '1.0.0', + 'alma_sdk_version' => '2.0.0', + 'alma_sdk_name' => 'Alma SDK' + ]; + + $cmsInfo = new CmsInfo($data); + + $this->assertEquals('WordPress', $cmsInfo->getProperties()['cms_name']); + $this->assertEquals('5.8', $cmsInfo->getProperties()['cms_version']); + $this->assertEquals(['plugin1', 'plugin2'], $cmsInfo->getProperties()['third_parties_plugins']); + $this->assertEquals(['theme1'], $cmsInfo->getProperties()['themes']); + $this->assertEquals('PHP', $cmsInfo->getProperties()['language_name']); + $this->assertEquals('7.4', $cmsInfo->getProperties()['language_version']); + $this->assertEquals('1.0.0', $cmsInfo->getProperties()['alma_plugin_version']); + $this->assertEquals('2.0.0', $cmsInfo->getProperties()['alma_sdk_version']); + $this->assertEquals('Alma SDK', $cmsInfo->getProperties()['alma_sdk_name']); + } + + public function testConstructorHandlesNullValuesCorrectly() + { + $data = [ + 'cms_name' => null, + 'cms_version' => null, + 'third_parties_plugins' => [], + 'themes' => [], + 'language_name' => null, + 'language_version' => null, + 'alma_plugin_version' => null, + 'alma_sdk_version' => null, + 'alma_sdk_name' => null + ]; + + $cmsInfo = new CmsInfo($data); + + $this->assertArrayNotHasKey('cms_name', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('cms_version', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('language_name', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('language_version', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('alma_plugin_version', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('alma_sdk_version', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('alma_sdk_name', $cmsInfo->getProperties()); + $this->assertEquals([], $cmsInfo->getProperties()['third_parties_plugins']); + $this->assertEquals([], $cmsInfo->getProperties()['themes']); + } + + public function testGetPropertiesFiltersOutNullAndEmptyValues() + { + $data = [ + 'cms_name' => 'WordPress', + 'cms_version' => '', + 'third_parties_plugins' => ['plugin1'], + 'themes' => [], + 'language_name' => null, + 'language_version' => '', + 'alma_plugin_version' => null, + 'alma_sdk_version' => '2.0.0', + 'alma_sdk_name' => 'Alma SDK' + ]; + + $cmsInfo = new CmsInfo($data); + $properties = $cmsInfo->getProperties(); + + $this->assertArrayHasKey('cms_name', $properties); + $this->assertArrayNotHasKey('cms_version', $properties); // Should be filtered out (empty string) + $this->assertArrayHasKey('third_parties_plugins', $properties); + $this->assertArrayNotHasKey('language_name', $properties); // Should be filtered out (null) + $this->assertArrayNotHasKey('language_version', $properties); // Should be filtered out (empty string) + $this->assertArrayHasKey('alma_sdk_version', $properties); + $this->assertArrayHasKey('alma_sdk_name', $properties); + } +} \ No newline at end of file diff --git a/tests/Unit/Lib/PayloadFormatterTest.php b/tests/Unit/Lib/PayloadFormatterTest.php new file mode 100644 index 00000000..c7231454 --- /dev/null +++ b/tests/Unit/Lib/PayloadFormatterTest.php @@ -0,0 +1,105 @@ + 'WordPress', + 'cms_version' => '5.8', + 'third_parties_plugins' => [['name' => 'plugin1', 'version' => '1.0']], + 'themes' => [['name' => 'theme1', 'version' => '2.0']], + 'language_name' => 'PHP', + 'language_version' => '7.4', + 'alma_plugin_version' => '1.0.0', + 'alma_sdk_version' => '2.0.0', + 'alma_sdk_name' => 'Alma SDK' + ]; + $cmsInfo = new CmsInfo($cmsInfoData); + + // Simulated input data for CmsFeatures + $cmsFeaturesData = [ + 'alma_enabled' => true, + 'widget_cart_activated' => true, + 'widget_product_activated' => false, + 'used_fee_plans' => '{"plan":"A"}', + 'payment_method_position' => 1, + 'in_page_activated' => true, + 'log_activated' => false, + 'excluded_categories' => ['category1'], + 'excluded_categories_activated' => true, + 'specific_features' => [['name' => 'feature1']], + 'country_restriction' => ['FR', 'US'], + 'is_multisite' => false, + 'custom_widget_css' => true, + ]; + $cmsFeatures = new CmsFeatures($cmsFeaturesData); + + // Call the method to be tested + $result = PayloadFormatter::formatIntegrationConfigurationPayload($cmsInfo, $cmsFeatures); + + // Expected result in JSON format + $expectedPayload = json_encode([ + 'cms_info' => $cmsInfo->getProperties(), + 'cms_features' => $cmsFeatures->getProperties(), + ]); + + // Assertion: Check if the output matches the expected JSON payload + $this->assertJsonStringEqualsJsonString($expectedPayload, $result); + } + + public function testFormatIntegrationConfigurationPayloadWithEmptyValues() + { + // CmsInfo with null or empty values + $cmsInfoData = [ + 'cms_name' => null, + 'cms_version' => '', + 'third_parties_plugins' => [], + 'themes' => [], + 'language_name' => null, + 'language_version' => '', + 'alma_plugin_version' => null, + 'alma_sdk_version' => null, + 'alma_sdk_name' => null + ]; + $cmsInfo = new CmsInfo($cmsInfoData); + + // CmsFeatures with null or empty values + $cmsFeaturesData = [ + 'alma_enabled' => null, + 'widget_cart_activated' => null, + 'widget_product_activated' => null, + 'used_fee_plans' => '', + 'payment_method_position' => null, + 'in_page_activated' => null, + 'log_activated' => null, + 'excluded_categories' => [], + 'excluded_categories_activated' => null, + 'specific_features' => [], + 'country_restriction' => [], + 'is_multisite' => null, + 'custom_widget_css' => null, + ]; + $cmsFeatures = new CmsFeatures($cmsFeaturesData); + + // Call the method to be tested + $result = PayloadFormatter::formatIntegrationConfigurationPayload($cmsInfo, $cmsFeatures); + + // Expected result in JSON format (should not include keys with null or empty values) + $expectedPayload = json_encode([ + 'cms_info' => $cmsInfo->getProperties(), + 'cms_features' => $cmsFeatures->getProperties(), + ]); + + // Assertion: Check if the output matches the expected JSON payload + $this->assertJsonStringEqualsJsonString($expectedPayload, $result); + } +} From 54f89fa3294bf7b40ce33e40829af0fec116b5d5 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Wed, 16 Oct 2024 16:34:24 +0200 Subject: [PATCH 05/21] fix: sonar lint --- tests/Unit/Endpoints/MerchantsTest.php | 10 ++++++---- tests/Unit/Entities/CmsFeaturesTest.php | 16 ++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/Unit/Endpoints/MerchantsTest.php b/tests/Unit/Endpoints/MerchantsTest.php index f6a55e84..71b3fb9f 100644 --- a/tests/Unit/Endpoints/MerchantsTest.php +++ b/tests/Unit/Endpoints/MerchantsTest.php @@ -11,6 +11,8 @@ class MerchantsTest extends TestCase { + const URL = "https://www.example.com/integrations/configurations"; + public function setUp(): void { $this->merchantEndpoint = Mockery::mock(Merchants::class)->makePartial(); @@ -29,7 +31,7 @@ public function tearDown(): void public function testSendIntegrationsConfigurationsUrlIsOk(){ $this->responseMock->shouldReceive('isError')->once()->andReturn(false); $this->requestObject->shouldReceive('setRequestBody') - ->with(['endpoint_url' => 'https://www.example.com/integrations/configurations']) + ->with(['endpoint_url' => self::URL]) ->andReturn($this->requestObject); $this->merchantEndpoint->shouldReceive('request') @@ -38,14 +40,14 @@ public function testSendIntegrationsConfigurationsUrlIsOk(){ ->andReturn($this->requestObject); $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); - $url = "https://www.example.com/integrations/configurations"; + $url = self::URL; $this->assertNull($this->merchantEndpoint->sendIntegrationsConfigurationsUrl($url)); } public function testSendIntegrationsConfigurationsUrlThrowRequestException(){ $this->responseMock->shouldReceive('isError')->once()->andReturn(true); $this->requestObject->shouldReceive('setRequestBody') - ->with(['endpoint_url' => 'https://www.example.com/integrations/configurations']) + ->with(['endpoint_url' => self::URL]) ->andReturn($this->requestObject); $this->merchantEndpoint->shouldReceive('request') @@ -54,7 +56,7 @@ public function testSendIntegrationsConfigurationsUrlThrowRequestException(){ ->andReturn($this->requestObject); $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); - $url = "https://www.example.com/integrations/configurations"; + $url = self::URL; $this->expectException(RequestException::class); $this->merchantEndpoint->sendIntegrationsConfigurationsUrl($url); diff --git a/tests/Unit/Entities/CmsFeaturesTest.php b/tests/Unit/Entities/CmsFeaturesTest.php index ca4e1d3b..2967c892 100644 --- a/tests/Unit/Entities/CmsFeaturesTest.php +++ b/tests/Unit/Entities/CmsFeaturesTest.php @@ -27,19 +27,19 @@ public function testConstructorSetsValuesCorrectly() $cmsFeatures = new CmsFeatures($data); - $this->assertEquals(true, $cmsFeatures->getProperties()['alma_enabled']); - $this->assertEquals(true, $cmsFeatures->getProperties()['widget_cart_activated']); - $this->assertEquals(false, $cmsFeatures->getProperties()['widget_product_activated']); + $this->assertTrue($cmsFeatures->getProperties()['alma_enabled']); + $this->assertTrue($cmsFeatures->getProperties()['widget_cart_activated']); + $this->assertFalse($cmsFeatures->getProperties()['widget_product_activated']); $this->assertEquals('Plan A', $cmsFeatures->getProperties()['used_fee_plans']); $this->assertEquals(1, $cmsFeatures->getProperties()['payment_method_position']); - $this->assertEquals(true, $cmsFeatures->getProperties()['in_page_activated']); - $this->assertEquals(false, $cmsFeatures->getProperties()['log_activated']); + $this->assertTrue($cmsFeatures->getProperties()['in_page_activated']); + $this->assertFalse($cmsFeatures->getProperties()['log_activated']); $this->assertEquals(['category1', 'category2'], $cmsFeatures->getProperties()['excluded_categories']); - $this->assertEquals(true, $cmsFeatures->getProperties()['excluded_categories_activated']); + $this->assertTrue($cmsFeatures->getProperties()['excluded_categories_activated']); $this->assertEquals(['feature1', 'feature2'], $cmsFeatures->getProperties()['specific_features']); $this->assertEquals(['FR', 'US'], $cmsFeatures->getProperties()['country_restriction']); - $this->assertEquals(false, $cmsFeatures->getProperties()['is_multisite']); - $this->assertEquals(true, $cmsFeatures->getProperties()['custom_widget_css']); + $this->assertFalse($cmsFeatures->getProperties()['is_multisite']); + $this->assertTrue($cmsFeatures->getProperties()['custom_widget_css']); } public function testConstructorHandlesNullValuesCorrectly() From 26c6fdffd1ab798d4d37fbaf9d388e67935b77ca Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Wed, 16 Oct 2024 16:37:23 +0200 Subject: [PATCH 06/21] fix: sonar lint --- tests/Unit/Entities/CmsInfoTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/Unit/Entities/CmsInfoTest.php b/tests/Unit/Entities/CmsInfoTest.php index 014a1759..f8ff4c6f 100644 --- a/tests/Unit/Entities/CmsInfoTest.php +++ b/tests/Unit/Entities/CmsInfoTest.php @@ -7,6 +7,9 @@ class CmsInfoTest extends TestCase { + const SDK_VERSION = '2.0.0'; + const SDK_NAME = 'Alma SDK'; + public function testConstructorSetsValuesCorrectly() { $data = [ @@ -17,8 +20,8 @@ public function testConstructorSetsValuesCorrectly() 'language_name' => 'PHP', 'language_version' => '7.4', 'alma_plugin_version' => '1.0.0', - 'alma_sdk_version' => '2.0.0', - 'alma_sdk_name' => 'Alma SDK' + 'alma_sdk_version' => self::SDK_VERSION, + 'alma_sdk_name' => self::SDK_NAME ]; $cmsInfo = new CmsInfo($data); @@ -30,8 +33,8 @@ public function testConstructorSetsValuesCorrectly() $this->assertEquals('PHP', $cmsInfo->getProperties()['language_name']); $this->assertEquals('7.4', $cmsInfo->getProperties()['language_version']); $this->assertEquals('1.0.0', $cmsInfo->getProperties()['alma_plugin_version']); - $this->assertEquals('2.0.0', $cmsInfo->getProperties()['alma_sdk_version']); - $this->assertEquals('Alma SDK', $cmsInfo->getProperties()['alma_sdk_name']); + $this->assertEquals(self::SDK_VERSION, $cmsInfo->getProperties()['alma_sdk_version']); + $this->assertEquals(self::SDK_NAME, $cmsInfo->getProperties()['alma_sdk_name']); } public function testConstructorHandlesNullValuesCorrectly() @@ -71,8 +74,8 @@ public function testGetPropertiesFiltersOutNullAndEmptyValues() 'language_name' => null, 'language_version' => '', 'alma_plugin_version' => null, - 'alma_sdk_version' => '2.0.0', - 'alma_sdk_name' => 'Alma SDK' + 'alma_sdk_version' => self::SDK_VERSION, + 'alma_sdk_name' => self::SDK_NAME ]; $cmsInfo = new CmsInfo($data); From ea3f13d46e4f4b8e108e1fd5b8bc0c2f8d611a65 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Thu, 17 Oct 2024 09:39:42 +0200 Subject: [PATCH 07/21] feat: use isset for constructor in CmsInfo and CmsFeaturs --- src/Entities/MerchantData/CmsFeatures.php | 10 +++++----- src/Entities/MerchantData/CmsInfo.php | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Entities/MerchantData/CmsFeatures.php b/src/Entities/MerchantData/CmsFeatures.php index e640b88e..d2f1c213 100644 --- a/src/Entities/MerchantData/CmsFeatures.php +++ b/src/Entities/MerchantData/CmsFeatures.php @@ -79,15 +79,15 @@ public function __construct($cmsFeaturesDataArray) $this->almaEnabled = isset($cmsFeaturesDataArray['alma_enabled']) ? $cmsFeaturesDataArray['alma_enabled'] : null; $this->widgetCartActivated = isset($cmsFeaturesDataArray['widget_cart_activated']) ? $cmsFeaturesDataArray['widget_cart_activated'] : null; $this->widgetProductActivated = isset($cmsFeaturesDataArray['widget_product_activated']) ? $cmsFeaturesDataArray['widget_product_activated'] : null; - $this->usedFeePlans = $cmsFeaturesDataArray['used_fee_plans'] ?: ''; + $this->usedFeePlans = isset($cmsFeaturesDataArray['used_fee_plans']) ? $cmsFeaturesDataArray['used_fee_plans'] : ''; $this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null; $this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null; - $this->excludedCategories = $cmsFeaturesDataArray['excluded_categories'] ?: []; + $this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : []; $this->excludedCategoriesActivated = isset($cmsFeaturesDataArray['excluded_categories_activated']) ? $cmsFeaturesDataArray['excluded_categories_activated'] : null; - $this->paymentMethodPosition = $cmsFeaturesDataArray['payment_method_position'] ?: null; - $this->specificFeatures = $cmsFeaturesDataArray['specific_features'] ?: []; - $this->countryRestriction = $cmsFeaturesDataArray['country_restriction'] ?: []; + $this->paymentMethodPosition = isset($cmsFeaturesDataArray['payment_method_position']) ? $cmsFeaturesDataArray['payment_method_position'] : null; + $this->specificFeatures = isset($cmsFeaturesDataArray['specific_features']) ? $cmsFeaturesDataArray['specific_features'] : []; + $this->countryRestriction = isset($cmsFeaturesDataArray['country_restriction']) ? $cmsFeaturesDataArray['country_restriction'] : []; $this->isMultisite = isset($cmsFeaturesDataArray['is_multisite']) ? $cmsFeaturesDataArray['is_multisite'] : null; $this->customWidgetCss = isset($cmsFeaturesDataArray['custom_widget_css']) ? $cmsFeaturesDataArray['custom_widget_css'] : null; } diff --git a/src/Entities/MerchantData/CmsInfo.php b/src/Entities/MerchantData/CmsInfo.php index 4d0e50c1..775ee8ad 100644 --- a/src/Entities/MerchantData/CmsInfo.php +++ b/src/Entities/MerchantData/CmsInfo.php @@ -56,15 +56,15 @@ class CmsInfo public function __construct($cmsInfoDataArray) { // Initialize values or set them to null if not available - $this->cmsName = $cmsInfoDataArray['cms_name'] ?: null; - $this->cmsVersion = $cmsInfoDataArray['cms_version'] ?: null; - $this->thirdPartiesPlugins = $cmsInfoDataArray['third_parties_plugins'] ?: []; - $this->themes = $cmsInfoDataArray['themes'] ?: []; - $this->languageName = $cmsInfoDataArray['language_name'] ?: null; - $this->languageVersion = $cmsInfoDataArray['language_version'] ?: null; - $this->almaPluginVersion = $cmsInfoDataArray['alma_plugin_version'] ?: null; - $this->almaSdkVersion = $cmsInfoDataArray['alma_sdk_version'] ?: null; - $this->almaSdkName = $cmsInfoDataArray['alma_sdk_name'] ?: null; + $this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : null; + $this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : null; + $this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : []; + $this->themes = isset($cmsInfoDataArray['themes']) ? $cmsInfoDataArray['themes'] : []; + $this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : null; + $this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : null; + $this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : null; + $this->almaSdkVersion = isset($cmsInfoDataArray['alma_sdk_version']) ? $cmsInfoDataArray['alma_sdk_version'] : null; + $this->almaSdkName = isset($cmsInfoDataArray['alma_sdk_name']) ? $cmsInfoDataArray['alma_sdk_name'] : null; } /** From 6cc87147f96b0faaf385f23aa2eb40bc636d4cb0 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:41:06 +0200 Subject: [PATCH 08/21] feat: after reviews --- src/Endpoints/Merchants.php | 113 ++++++----- src/Entities/MerchantData/CmsFeatures.php | 227 +++++++++++----------- src/Entities/MerchantData/CmsInfo.php | 149 +++++++------- src/Lib/PayloadFormatter.php | 2 +- tests/Unit/Endpoints/MerchantsTest.php | 4 +- tests/Unit/Entities/CmsFeaturesTest.php | 29 ++- tests/Unit/Entities/CmsInfoTest.php | 17 ++ tests/Unit/Lib/PayloadFormatterTest.php | 11 +- 8 files changed, 299 insertions(+), 253 deletions(-) diff --git a/src/Endpoints/Merchants.php b/src/Endpoints/Merchants.php index 8d186056..baa8c790 100644 --- a/src/Endpoints/Merchants.php +++ b/src/Endpoints/Merchants.php @@ -33,70 +33,69 @@ class Merchants extends Base { - const MERCHANTS_PATH = '/v1/merchants'; - const ME_PATH = '/v1/me'; + const ME_PATH = '/v1/me'; - /** - * @return Merchant - * @throws RequestError - */ - public function me() - { - $res = $this->request(self::ME_PATH . '/extended-data')->get(); + /** + * @return Merchant + * @throws RequestError + */ + public function me() + { + $res = $this->request(self::ME_PATH . '/extended-data')->get(); - if ($res->isError()) { - throw new RequestError($res->errorMessage, null, $res); - } + if ($res->isError()) { + throw new RequestError($res->errorMessage, null, $res); + } - return new Merchant($res->json); - } + return new Merchant($res->json); + } - /** - * @param $kind string Either FeePlan::KIND_GENERAL or FeePlan::KIND_POS. The former is applied to online payments, - * while the latter will be used when creating a Payment with origin=pos_* for - * retail/point-of-sale use cases. Defaults to FeePlan::KIND_GENERAL - * @param string|int[] $installmentsCounts Only include fee plans that match the given installments counts, or use - * the string "all" (default) to get all available fee plans - * @param bool $includeDeferred Include deferred fee plans (i.e. Pay Later plans) in the response - * @return FeePlan[] An array of available fee plans (some might be disabled, check FeePlan->allowed for each) - * @throws RequestError - */ - public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "all", $includeDeferred = false) - { - if (is_array($installmentsCounts)) { - $only = implode(",", $installmentsCounts); - } else { - $only = $installmentsCounts; - } + /** + * @param $kind string Either FeePlan::KIND_GENERAL or FeePlan::KIND_POS. The former is applied to online payments, + * while the latter will be used when creating a Payment with origin=pos_* for + * retail/point-of-sale use cases. Defaults to FeePlan::KIND_GENERAL + * @param string|int[] $installmentsCounts Only include fee plans that match the given installments counts, or use + * the string "all" (default) to get all available fee plans + * @param bool $includeDeferred Include deferred fee plans (i.e. Pay Later plans) in the response + * @return FeePlan[] An array of available fee plans (some might be disabled, check FeePlan->allowed for each) + * @throws RequestError + */ + public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "all", $includeDeferred = false) + { + if (is_array($installmentsCounts)) { + $only = implode(",", $installmentsCounts); + } else { + $only = $installmentsCounts; + } - $res = $this->request(self::ME_PATH . "/fee-plans")->setQueryParams(array( - "kind" => $kind, - "only" => $only, - "deferred" => $includeDeferred ? "true" : "false" // Avoid conversion to "0"/"1" our API doesn't recognize - ))->get(); + $res = $this->request(self::ME_PATH . "/fee-plans")->setQueryParams(array( + "kind" => $kind, + "only" => $only, + "deferred" => $includeDeferred ? "true" : "false" // Avoid conversion to "0"/"1" our API doesn't recognize + ))->get(); - if ($res->isError()) { - throw new RequestError($res->errorMessage, null, $res); - } + if ($res->isError()) { + throw new RequestError($res->errorMessage, null, $res); + } - return array_map(function ($val) { - return new FeePlan($val); - }, $res->json); - } + return array_map(function ($val) { + return new FeePlan($val); + }, $res->json); + } - /** - * @param string $url The URL to send to Alma for integrations configuration - * @throws RequestException - * @throws RequestError - */ - public function sendIntegrationsConfigurationsUrl($url) - { - $res = $this->request(self::ME_PATH . "/integrations/configurations")->setRequestBody(array( - "endpoint_url" => $url - ))->post(); + /** + * @param string $url The URL to send to Alma for integrations configuration + * @throws RequestException + * @throws RequestError + */ + public function sendIntegrationsConfigurationsUrl($url) + { + $res = $this->request(self::ME_PATH . "/configuration")->setRequestBody(array( + "endpoint_url" => $url + ))->post(); - if ($res->isError()) { - throw new RequestException($res->errorMessage, null, $res); - } - } + if ($res->isError()) { + throw new RequestException($res->errorMessage, null, $res); + } + } } diff --git a/src/Entities/MerchantData/CmsFeatures.php b/src/Entities/MerchantData/CmsFeatures.php index d2f1c213..9d6d1804 100644 --- a/src/Entities/MerchantData/CmsFeatures.php +++ b/src/Entities/MerchantData/CmsFeatures.php @@ -4,117 +4,118 @@ class CmsFeatures { - /** - * @var bool | null - */ - private $almaEnabled; - - /** - * @var bool | null - */ - private $widgetCartActivated; - - /** - * @var bool | null - */ - private $widgetProductActivated; - - /** - * @var mixed - */ - private $usedFeePlans; - - /** - * @var int | null - */ - private $paymentMethodPosition; - - /** - * @var bool | null - */ - private $inPageActivated; - - /** - * @var bool | null - */ - private $logActivated; - - /** - * @var string[] - */ - private $excludedCategories; - - /** - * @var bool | null - */ - private $excludedCategoriesActivated; - - /** - * @var array - */ - private $specificFeatures; - - /** - * @var string[] - */ - private $countryRestriction; - - /** - * @var bool | null - */ - private $isMultisite; - - /** - * @var bool | null - */ - private $customWidgetCss; - - /** - * CmsFeatures constructor. - * @param array $cmsFeaturesDataArray - */ - public function __construct($cmsFeaturesDataArray) - { - // Ensure values are properly initialized - $this->almaEnabled = isset($cmsFeaturesDataArray['alma_enabled']) ? $cmsFeaturesDataArray['alma_enabled'] : null; - $this->widgetCartActivated = isset($cmsFeaturesDataArray['widget_cart_activated']) ? $cmsFeaturesDataArray['widget_cart_activated'] : null; - $this->widgetProductActivated = isset($cmsFeaturesDataArray['widget_product_activated']) ? $cmsFeaturesDataArray['widget_product_activated'] : null; - $this->usedFeePlans = isset($cmsFeaturesDataArray['used_fee_plans']) ? $cmsFeaturesDataArray['used_fee_plans'] : ''; - $this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null; - $this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null; - $this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : []; - $this->excludedCategoriesActivated = isset($cmsFeaturesDataArray['excluded_categories_activated']) ? - $cmsFeaturesDataArray['excluded_categories_activated'] : null; - $this->paymentMethodPosition = isset($cmsFeaturesDataArray['payment_method_position']) ? $cmsFeaturesDataArray['payment_method_position'] : null; - $this->specificFeatures = isset($cmsFeaturesDataArray['specific_features']) ? $cmsFeaturesDataArray['specific_features'] : []; - $this->countryRestriction = isset($cmsFeaturesDataArray['country_restriction']) ? $cmsFeaturesDataArray['country_restriction'] : []; - $this->isMultisite = isset($cmsFeaturesDataArray['is_multisite']) ? $cmsFeaturesDataArray['is_multisite'] : null; - $this->customWidgetCss = isset($cmsFeaturesDataArray['custom_widget_css']) ? $cmsFeaturesDataArray['custom_widget_css'] : null; - } - - /** - * @return array - */ - public function getProperties() - { - // Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values - return array_filter([ - 'alma_enabled' => $this->almaEnabled, - 'widget_cart_activated' => $this->widgetCartActivated, - 'widget_product_activated' => $this->widgetProductActivated, - 'used_fee_plans' => $this->usedFeePlans, - 'in_page_activated' => $this->inPageActivated, - 'log_activated' => $this->logActivated, - 'excluded_categories' => $this->excludedCategories, - 'excluded_categories_activated' => $this->excludedCategoriesActivated, - 'payment_method_position' => $this->paymentMethodPosition, - 'specific_features' => $this->specificFeatures, - 'country_restriction' => $this->countryRestriction, - 'is_multisite' => $this->isMultisite, - 'custom_widget_css' => $this->customWidgetCss, - ], function($value) { - // Keep only values that are not null and not empty - return !is_null($value) && $value !== ''; - }); - } + /** + * @var bool | null + */ + private $almaEnabled; + + /** + * @var bool | null + */ + private $widgetCartActivated; + + /** + * @var bool | null + */ + private $widgetProductActivated; + + /** + * @var array + */ + private $usedFeePlans; + + /** + * @var int | null + */ + private $paymentMethodPosition; + + /** + * @var bool | null + */ + private $inPageActivated; + + /** + * @var bool | null + */ + private $logActivated; + + /** + * @var string[] + */ + private $excludedCategories; + + /** + * @var bool | null + */ + private $excludedCategoriesActivated; + + /** + * @var array + */ + private $specificFeatures; + + /** + * @var string[] + */ + private $countryRestriction; + + /** + * @var bool | null + */ + private $isMultisite; + + /** + * @var bool | null + */ + private $customWidgetCss; + + /** + * CmsFeatures constructor. + * @param array $cmsFeaturesDataArray + */ + public function __construct($cmsFeaturesDataArray) + { + // Ensure values are properly initialized + $this->almaEnabled = isset($cmsFeaturesDataArray['alma_enabled']) ? $cmsFeaturesDataArray['alma_enabled'] : null; + $this->widgetCartActivated = isset($cmsFeaturesDataArray['widget_cart_activated']) ? $cmsFeaturesDataArray['widget_cart_activated'] : null; + $this->widgetProductActivated = isset($cmsFeaturesDataArray['widget_product_activated']) ? $cmsFeaturesDataArray['widget_product_activated'] : null; + $this->usedFeePlans = isset($cmsFeaturesDataArray['used_fee_plans']) ? $cmsFeaturesDataArray['used_fee_plans'] : []; + $this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null; + $this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null; + $this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : []; + $this->excludedCategoriesActivated = isset($cmsFeaturesDataArray['excluded_categories_activated']) ? + $cmsFeaturesDataArray['excluded_categories_activated'] : null; + $this->paymentMethodPosition = isset($cmsFeaturesDataArray['payment_method_position']) ? $cmsFeaturesDataArray['payment_method_position'] : null; + $this->specificFeatures = isset($cmsFeaturesDataArray['specific_features']) ? $cmsFeaturesDataArray['specific_features'] : []; + $this->countryRestriction = isset($cmsFeaturesDataArray['country_restriction']) ? $cmsFeaturesDataArray['country_restriction'] : []; + $this->isMultisite = isset($cmsFeaturesDataArray['is_multisite']) ? $cmsFeaturesDataArray['is_multisite'] : null; + $this->customWidgetCss = isset($cmsFeaturesDataArray['custom_widget_css']) ? $cmsFeaturesDataArray['custom_widget_css'] : null; + } + + /** + * @return array + */ + public function getProperties() + { + // Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values + return array_filter([ + 'alma_enabled' => $this->almaEnabled, + 'widget_cart_activated' => $this->widgetCartActivated, + 'widget_product_activated' => $this->widgetProductActivated, + 'used_fee_plans' => $this->usedFeePlans, + 'in_page_activated' => $this->inPageActivated, + 'log_activated' => $this->logActivated, + 'excluded_categories' => $this->excludedCategories, + 'excluded_categories_activated' => $this->excludedCategoriesActivated, + 'payment_method_position' => $this->paymentMethodPosition, + 'specific_features' => $this->specificFeatures, + 'country_restriction' => $this->countryRestriction, + 'is_multisite' => $this->isMultisite, + 'custom_widget_css' => $this->customWidgetCss, + ], function ($value) { + // Keep only values that are not null and not empty + // But keep false or 0 values + return !is_null($value) && $value !== ''; + }); + } } \ No newline at end of file diff --git a/src/Entities/MerchantData/CmsInfo.php b/src/Entities/MerchantData/CmsInfo.php index 775ee8ad..e8f24f29 100644 --- a/src/Entities/MerchantData/CmsInfo.php +++ b/src/Entities/MerchantData/CmsInfo.php @@ -4,88 +4,89 @@ class CmsInfo { - /** - * @var string|null - */ - private $cmsName; + /** + * @var string|null + */ + private $cmsName; - /** - * @var string|null - */ - private $cmsVersion; + /** + * @var string|null + */ + private $cmsVersion; - /** - * @var array - */ - private $thirdPartiesPlugins; + /** + * @var array + */ + private $thirdPartiesPlugins; - /** - * @var array - */ - private $themes; + /** + * @var array + */ + private $themes; - /** - * @var string|null - */ - private $languageName; + /** + * @var string|null + */ + private $languageName; - /** - * @var string|null - */ - private $languageVersion; + /** + * @var string|null + */ + private $languageVersion; - /** - * @var string|null - */ - private $almaPluginVersion; + /** + * @var string|null + */ + private $almaPluginVersion; - /** - * @var string|null - */ - private $almaSdkVersion; + /** + * @var string|null + */ + private $almaSdkVersion; - /** - * @var string|null - */ - private $almaSdkName; + /** + * @var string|null + */ + private $almaSdkName; - /** - * CmsInfo constructor. - * @param array $cmsInfoDataArray - */ - public function __construct($cmsInfoDataArray) - { - // Initialize values or set them to null if not available - $this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : null; - $this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : null; - $this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : []; - $this->themes = isset($cmsInfoDataArray['themes']) ? $cmsInfoDataArray['themes'] : []; - $this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : null; - $this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : null; - $this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : null; - $this->almaSdkVersion = isset($cmsInfoDataArray['alma_sdk_version']) ? $cmsInfoDataArray['alma_sdk_version'] : null; - $this->almaSdkName = isset($cmsInfoDataArray['alma_sdk_name']) ? $cmsInfoDataArray['alma_sdk_name'] : null; - } + /** + * CmsInfo constructor. + * @param array $cmsInfoDataArray + */ + public function __construct($cmsInfoDataArray) + { + // Initialize values or set them to null if not available + $this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : null; + $this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : null; + $this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : []; + $this->themes = isset($cmsInfoDataArray['themes']) ? $cmsInfoDataArray['themes'] : []; + $this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : null; + $this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : null; + $this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : null; + $this->almaSdkVersion = isset($cmsInfoDataArray['alma_sdk_version']) ? $cmsInfoDataArray['alma_sdk_version'] : null; + $this->almaSdkName = isset($cmsInfoDataArray['alma_sdk_name']) ? $cmsInfoDataArray['alma_sdk_name'] : null; + } - /** - * @return array - */ - public function getProperties() - { - // Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values - return array_filter([ - 'cms_name' => $this->cmsName, - 'cms_version' => $this->cmsVersion, - 'third_parties_plugins' => $this->thirdPartiesPlugins, - 'themes' => $this->themes, - 'language_name' => $this->languageName, - 'language_version' => $this->languageVersion, - 'alma_plugin_version' => $this->almaPluginVersion, - 'alma_sdk_version' => $this->almaSdkVersion, - 'alma_sdk_name' => $this->almaSdkName, - ], function($value) { - // Keep only values that are not null and not empty - return !is_null($value) && $value !== ''; - }); - } + /** + * @return array + */ + public function getProperties() + { + // Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values + return array_filter([ + 'cms_name' => $this->cmsName, + 'cms_version' => $this->cmsVersion, + 'third_parties_plugins' => $this->thirdPartiesPlugins, + 'themes' => $this->themes, + 'language_name' => $this->languageName, + 'language_version' => $this->languageVersion, + 'alma_plugin_version' => $this->almaPluginVersion, + 'alma_sdk_version' => $this->almaSdkVersion, + 'alma_sdk_name' => $this->almaSdkName, + ], function ($value) { + // Keep only values that are not null and not empty + // But keep false or 0 values + return !is_null($value) && $value !== ''; + }); + } } \ No newline at end of file diff --git a/src/Lib/PayloadFormatter.php b/src/Lib/PayloadFormatter.php index 595cd226..4f091e68 100644 --- a/src/Lib/PayloadFormatter.php +++ b/src/Lib/PayloadFormatter.php @@ -12,7 +12,7 @@ class PayloadFormatter * @param CmsFeatures $cmsFeatures * @return string */ - public static function formatIntegrationConfigurationPayload(CmsInfo $cmsInfo, CmsFeatures $cmsFeatures) + public function formatConfigurationPayload(CmsInfo $cmsInfo, CmsFeatures $cmsFeatures) { $payload = [ "cms_info" => $cmsInfo->getProperties(), diff --git a/tests/Unit/Endpoints/MerchantsTest.php b/tests/Unit/Endpoints/MerchantsTest.php index 71b3fb9f..63ec4991 100644 --- a/tests/Unit/Endpoints/MerchantsTest.php +++ b/tests/Unit/Endpoints/MerchantsTest.php @@ -35,7 +35,7 @@ public function testSendIntegrationsConfigurationsUrlIsOk(){ ->andReturn($this->requestObject); $this->merchantEndpoint->shouldReceive('request') - ->with(Merchants::ME_PATH . "/integrations/configurations") + ->with(Merchants::ME_PATH . "/configuration") ->once() ->andReturn($this->requestObject); $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); @@ -51,7 +51,7 @@ public function testSendIntegrationsConfigurationsUrlThrowRequestException(){ ->andReturn($this->requestObject); $this->merchantEndpoint->shouldReceive('request') - ->with(Merchants::ME_PATH . "/integrations/configurations") + ->with(Merchants::ME_PATH . "/configuration") ->once() ->andReturn($this->requestObject); $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); diff --git a/tests/Unit/Entities/CmsFeaturesTest.php b/tests/Unit/Entities/CmsFeaturesTest.php index 2967c892..05fc6bd5 100644 --- a/tests/Unit/Entities/CmsFeaturesTest.php +++ b/tests/Unit/Entities/CmsFeaturesTest.php @@ -13,7 +13,7 @@ public function testConstructorSetsValuesCorrectly() 'alma_enabled' => true, 'widget_cart_activated' => true, 'widget_product_activated' => false, - 'used_fee_plans' => 'Plan A', + 'used_fee_plans' => ['Plan A'], 'payment_method_position' => 1, 'in_page_activated' => true, 'log_activated' => false, @@ -30,7 +30,7 @@ public function testConstructorSetsValuesCorrectly() $this->assertTrue($cmsFeatures->getProperties()['alma_enabled']); $this->assertTrue($cmsFeatures->getProperties()['widget_cart_activated']); $this->assertFalse($cmsFeatures->getProperties()['widget_product_activated']); - $this->assertEquals('Plan A', $cmsFeatures->getProperties()['used_fee_plans']); + $this->assertEquals(['Plan A'], $cmsFeatures->getProperties()['used_fee_plans']); $this->assertEquals(1, $cmsFeatures->getProperties()['payment_method_position']); $this->assertTrue($cmsFeatures->getProperties()['in_page_activated']); $this->assertFalse($cmsFeatures->getProperties()['log_activated']); @@ -48,7 +48,7 @@ public function testConstructorHandlesNullValuesCorrectly() 'alma_enabled' => null, 'widget_cart_activated' => null, 'widget_product_activated' => null, - 'used_fee_plans' => '', + 'used_fee_plans' => [], 'payment_method_position' => null, 'in_page_activated' => null, 'log_activated' => null, @@ -66,7 +66,7 @@ public function testConstructorHandlesNullValuesCorrectly() $this->assertArrayNotHasKey('alma_enabled', $properties); $this->assertArrayNotHasKey('widget_cart_activated', $properties); $this->assertArrayNotHasKey('widget_product_activated', $properties); - $this->assertArrayNotHasKey('used_fee_plans', $properties); + $this->assertEquals([], $properties['used_fee_plans']); $this->assertArrayNotHasKey('payment_method_position', $properties); $this->assertArrayNotHasKey('in_page_activated', $properties); $this->assertArrayNotHasKey('log_activated', $properties); @@ -112,4 +112,25 @@ public function testGetPropertiesFiltersOutNullAndEmptyValues() $this->assertArrayHasKey('is_multisite', $properties); $this->assertArrayNotHasKey('custom_widget_css', $properties); // Should be filtered out (null) } + + public function testGetPropertiesFiltersOutWithEmptyData() + { + $data = []; + + $cmsFeatures = new CmsFeatures($data); + $properties = $cmsFeatures->getProperties(); + + $this->assertArrayNotHasKey('alma_enabled', $properties); + $this->assertArrayNotHasKey('widget_cart_activated', $properties); + $this->assertArrayNotHasKey('widget_product_activated', $properties); + $this->assertEquals([], $properties['used_fee_plans']); + $this->assertArrayNotHasKey('payment_method_position', $properties); + $this->assertArrayNotHasKey('in_page_activated', $properties); + $this->assertEquals([], $properties['excluded_categories']); + $this->assertArrayNotHasKey('excluded_categories_activated', $properties); + $this->assertEquals([], $properties['specific_features']); + $this->assertEquals([], $properties['country_restriction']); + $this->assertArrayNotHasKey('is_multisite', $properties); + $this->assertArrayNotHasKey('custom_widget_css', $properties); + } } \ No newline at end of file diff --git a/tests/Unit/Entities/CmsInfoTest.php b/tests/Unit/Entities/CmsInfoTest.php index f8ff4c6f..a37eaeb3 100644 --- a/tests/Unit/Entities/CmsInfoTest.php +++ b/tests/Unit/Entities/CmsInfoTest.php @@ -89,4 +89,21 @@ public function testGetPropertiesFiltersOutNullAndEmptyValues() $this->assertArrayHasKey('alma_sdk_version', $properties); $this->assertArrayHasKey('alma_sdk_name', $properties); } + + public function testGetPropertiesFiltersOutWithEmptyData() + { + $data = []; + + $cmsInfo = new CmsInfo($data); + $properties = $cmsInfo->getProperties(); + + $this->assertArrayNotHasKey('cms_name', $properties); + $this->assertArrayNotHasKey('cms_version', $properties); // Should be filtered out (empty string) + $this->assertEquals([], $cmsInfo->getProperties()['third_parties_plugins']); + $this->assertEquals([], $cmsInfo->getProperties()['themes']); + $this->assertArrayNotHasKey('language_name', $properties); // Should be filtered out (null) + $this->assertArrayNotHasKey('language_version', $properties); // Should be filtered out (empty string) + $this->assertArrayNotHasKey('alma_sdk_version', $properties); + $this->assertArrayNotHasKey('alma_sdk_name', $properties); + } } \ No newline at end of file diff --git a/tests/Unit/Lib/PayloadFormatterTest.php b/tests/Unit/Lib/PayloadFormatterTest.php index c7231454..e426d565 100644 --- a/tests/Unit/Lib/PayloadFormatterTest.php +++ b/tests/Unit/Lib/PayloadFormatterTest.php @@ -9,6 +9,13 @@ class PayloadFormatterTest extends TestCase { + var $payloadFormatter; + + public function setUp(): void + { + $this->payloadFormatter = new PayloadFormatter(); + } + public function testFormatIntegrationConfigurationPayload() { // Simulated input data for CmsInfo @@ -44,7 +51,7 @@ public function testFormatIntegrationConfigurationPayload() $cmsFeatures = new CmsFeatures($cmsFeaturesData); // Call the method to be tested - $result = PayloadFormatter::formatIntegrationConfigurationPayload($cmsInfo, $cmsFeatures); + $result = $this->payloadFormatter->formatConfigurationPayload($cmsInfo, $cmsFeatures); // Expected result in JSON format $expectedPayload = json_encode([ @@ -91,7 +98,7 @@ public function testFormatIntegrationConfigurationPayloadWithEmptyValues() $cmsFeatures = new CmsFeatures($cmsFeaturesData); // Call the method to be tested - $result = PayloadFormatter::formatIntegrationConfigurationPayload($cmsInfo, $cmsFeatures); + $result = $this->payloadFormatter->formatConfigurationPayload($cmsInfo, $cmsFeatures); // Expected result in JSON format (should not include keys with null or empty values) $expectedPayload = json_encode([ From 4b9ab7317f57fe5fdc1eea3634f7c958d041fed5 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:05:14 +0200 Subject: [PATCH 09/21] feat: remove typping null in CmsInfo --- src/Entities/MerchantData/CmsInfo.php | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Entities/MerchantData/CmsInfo.php b/src/Entities/MerchantData/CmsInfo.php index e8f24f29..95d50de5 100644 --- a/src/Entities/MerchantData/CmsInfo.php +++ b/src/Entities/MerchantData/CmsInfo.php @@ -5,12 +5,12 @@ class CmsInfo { /** - * @var string|null + * @var string */ private $cmsName; /** - * @var string|null + * @var string */ private $cmsVersion; @@ -25,27 +25,27 @@ class CmsInfo private $themes; /** - * @var string|null + * @var string */ private $languageName; /** - * @var string|null + * @var string */ private $languageVersion; /** - * @var string|null + * @var string */ private $almaPluginVersion; /** - * @var string|null + * @var string */ private $almaSdkVersion; /** - * @var string|null + * @var string */ private $almaSdkName; @@ -56,15 +56,15 @@ class CmsInfo public function __construct($cmsInfoDataArray) { // Initialize values or set them to null if not available - $this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : null; - $this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : null; + $this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : ''; + $this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : ''; $this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : []; $this->themes = isset($cmsInfoDataArray['themes']) ? $cmsInfoDataArray['themes'] : []; - $this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : null; - $this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : null; - $this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : null; - $this->almaSdkVersion = isset($cmsInfoDataArray['alma_sdk_version']) ? $cmsInfoDataArray['alma_sdk_version'] : null; - $this->almaSdkName = isset($cmsInfoDataArray['alma_sdk_name']) ? $cmsInfoDataArray['alma_sdk_name'] : null; + $this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : ''; + $this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : ''; + $this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : ''; + $this->almaSdkVersion = isset($cmsInfoDataArray['alma_sdk_version']) ? $cmsInfoDataArray['alma_sdk_version'] : ''; + $this->almaSdkName = isset($cmsInfoDataArray['alma_sdk_name']) ? $cmsInfoDataArray['alma_sdk_name'] : ''; } /** From 4fad9889551d97fb7faa32dc3523f873e09cf82e Mon Sep 17 00:00:00 2001 From: "alma-renovate-bot[bot]" <163289924+alma-renovate-bot[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:11:40 +0000 Subject: [PATCH 10/21] chore(deps): update pre-commit hook returntocorp/semgrep to v1.92.0 --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 95908ab7..bc207b66 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: stages: [commit] - repo: https://github.com/returntocorp/semgrep - rev: v1.27.0 + rev: v1.92.0 hooks: - id: semgrep args: From 8ed536388ed451cf8a7860c0b296efc12e1f2206 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:41:50 +0200 Subject: [PATCH 11/21] feat: change return type of formatConfigurationPayload --- src/Lib/PayloadFormatter.php | 6 ++---- tests/Unit/Lib/PayloadFormatterTest.php | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Lib/PayloadFormatter.php b/src/Lib/PayloadFormatter.php index 4f091e68..e7d7127b 100644 --- a/src/Lib/PayloadFormatter.php +++ b/src/Lib/PayloadFormatter.php @@ -10,16 +10,14 @@ class PayloadFormatter /** * @param CmsInfo $cmsInfo * @param CmsFeatures $cmsFeatures - * @return string + * @return array */ public function formatConfigurationPayload(CmsInfo $cmsInfo, CmsFeatures $cmsFeatures) { - $payload = [ + return [ "cms_info" => $cmsInfo->getProperties(), "cms_features" => $cmsFeatures->getProperties(), ]; - - return json_encode($payload); } } \ No newline at end of file diff --git a/tests/Unit/Lib/PayloadFormatterTest.php b/tests/Unit/Lib/PayloadFormatterTest.php index e426d565..955d8a7b 100644 --- a/tests/Unit/Lib/PayloadFormatterTest.php +++ b/tests/Unit/Lib/PayloadFormatterTest.php @@ -54,13 +54,13 @@ public function testFormatIntegrationConfigurationPayload() $result = $this->payloadFormatter->formatConfigurationPayload($cmsInfo, $cmsFeatures); // Expected result in JSON format - $expectedPayload = json_encode([ + $expectedPayload = [ 'cms_info' => $cmsInfo->getProperties(), 'cms_features' => $cmsFeatures->getProperties(), - ]); + ]; // Assertion: Check if the output matches the expected JSON payload - $this->assertJsonStringEqualsJsonString($expectedPayload, $result); + $this->assertEquals($expectedPayload, $result); } public function testFormatIntegrationConfigurationPayloadWithEmptyValues() @@ -101,12 +101,12 @@ public function testFormatIntegrationConfigurationPayloadWithEmptyValues() $result = $this->payloadFormatter->formatConfigurationPayload($cmsInfo, $cmsFeatures); // Expected result in JSON format (should not include keys with null or empty values) - $expectedPayload = json_encode([ + $expectedPayload = [ 'cms_info' => $cmsInfo->getProperties(), 'cms_features' => $cmsFeatures->getProperties(), - ]); + ]; // Assertion: Check if the output matches the expected JSON payload - $this->assertJsonStringEqualsJsonString($expectedPayload, $result); + $this->assertEquals($expectedPayload, $result); } } From cddd6d826bedd305c6d5e9541916b1ac0a4c4a48 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:33:24 +0200 Subject: [PATCH 12/21] feat: make nullable for alla attributes in CmsInfo and CmsFeatures --- src/Entities/MerchantData/CmsFeatures.php | 28 ++++++-------- src/Entities/MerchantData/CmsInfo.php | 8 ++-- tests/Unit/Entities/CmsFeaturesTest.php | 45 +++++++++++------------ tests/Unit/Entities/CmsInfoTest.php | 12 +++--- 4 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/Entities/MerchantData/CmsFeatures.php b/src/Entities/MerchantData/CmsFeatures.php index 9d6d1804..5518a349 100644 --- a/src/Entities/MerchantData/CmsFeatures.php +++ b/src/Entities/MerchantData/CmsFeatures.php @@ -20,7 +20,7 @@ class CmsFeatures private $widgetProductActivated; /** - * @var array + * @var array|null */ private $usedFeePlans; @@ -40,22 +40,17 @@ class CmsFeatures private $logActivated; /** - * @var string[] + * @var string[]|null */ private $excludedCategories; /** - * @var bool | null - */ - private $excludedCategoriesActivated; - - /** - * @var array + * @var array|null */ private $specificFeatures; /** - * @var string[] + * @var string[]|null */ private $countryRestriction; @@ -79,15 +74,17 @@ public function __construct($cmsFeaturesDataArray) $this->almaEnabled = isset($cmsFeaturesDataArray['alma_enabled']) ? $cmsFeaturesDataArray['alma_enabled'] : null; $this->widgetCartActivated = isset($cmsFeaturesDataArray['widget_cart_activated']) ? $cmsFeaturesDataArray['widget_cart_activated'] : null; $this->widgetProductActivated = isset($cmsFeaturesDataArray['widget_product_activated']) ? $cmsFeaturesDataArray['widget_product_activated'] : null; - $this->usedFeePlans = isset($cmsFeaturesDataArray['used_fee_plans']) ? $cmsFeaturesDataArray['used_fee_plans'] : []; + $this->usedFeePlans = isset($cmsFeaturesDataArray['used_fee_plans']) ? $cmsFeaturesDataArray['used_fee_plans'] : null; $this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null; $this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null; - $this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : []; - $this->excludedCategoriesActivated = isset($cmsFeaturesDataArray['excluded_categories_activated']) ? - $cmsFeaturesDataArray['excluded_categories_activated'] : null; + if (isset($cmsFeaturesDataArray['excluded_categories_activated']) && $cmsFeaturesDataArray['excluded_categories_activated']) { + $this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : null; + } else { + $this->excludedCategories = null; + } $this->paymentMethodPosition = isset($cmsFeaturesDataArray['payment_method_position']) ? $cmsFeaturesDataArray['payment_method_position'] : null; - $this->specificFeatures = isset($cmsFeaturesDataArray['specific_features']) ? $cmsFeaturesDataArray['specific_features'] : []; - $this->countryRestriction = isset($cmsFeaturesDataArray['country_restriction']) ? $cmsFeaturesDataArray['country_restriction'] : []; + $this->specificFeatures = isset($cmsFeaturesDataArray['specific_features']) ? $cmsFeaturesDataArray['specific_features'] : null; + $this->countryRestriction = isset($cmsFeaturesDataArray['country_restriction']) ? $cmsFeaturesDataArray['country_restriction'] : null; $this->isMultisite = isset($cmsFeaturesDataArray['is_multisite']) ? $cmsFeaturesDataArray['is_multisite'] : null; $this->customWidgetCss = isset($cmsFeaturesDataArray['custom_widget_css']) ? $cmsFeaturesDataArray['custom_widget_css'] : null; } @@ -106,7 +103,6 @@ public function getProperties() 'in_page_activated' => $this->inPageActivated, 'log_activated' => $this->logActivated, 'excluded_categories' => $this->excludedCategories, - 'excluded_categories_activated' => $this->excludedCategoriesActivated, 'payment_method_position' => $this->paymentMethodPosition, 'specific_features' => $this->specificFeatures, 'country_restriction' => $this->countryRestriction, diff --git a/src/Entities/MerchantData/CmsInfo.php b/src/Entities/MerchantData/CmsInfo.php index 95d50de5..223924fd 100644 --- a/src/Entities/MerchantData/CmsInfo.php +++ b/src/Entities/MerchantData/CmsInfo.php @@ -15,12 +15,12 @@ class CmsInfo private $cmsVersion; /** - * @var array + * @var array|null */ private $thirdPartiesPlugins; /** - * @var array + * @var array|null */ private $themes; @@ -58,8 +58,8 @@ public function __construct($cmsInfoDataArray) // Initialize values or set them to null if not available $this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : ''; $this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : ''; - $this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : []; - $this->themes = isset($cmsInfoDataArray['themes']) ? $cmsInfoDataArray['themes'] : []; + $this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : null; + $this->themes = isset($cmsInfoDataArray['themes']) ? $cmsInfoDataArray['themes'] : null; $this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : ''; $this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : ''; $this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : ''; diff --git a/tests/Unit/Entities/CmsFeaturesTest.php b/tests/Unit/Entities/CmsFeaturesTest.php index 05fc6bd5..ec6d057a 100644 --- a/tests/Unit/Entities/CmsFeaturesTest.php +++ b/tests/Unit/Entities/CmsFeaturesTest.php @@ -35,7 +35,6 @@ public function testConstructorSetsValuesCorrectly() $this->assertTrue($cmsFeatures->getProperties()['in_page_activated']); $this->assertFalse($cmsFeatures->getProperties()['log_activated']); $this->assertEquals(['category1', 'category2'], $cmsFeatures->getProperties()['excluded_categories']); - $this->assertTrue($cmsFeatures->getProperties()['excluded_categories_activated']); $this->assertEquals(['feature1', 'feature2'], $cmsFeatures->getProperties()['specific_features']); $this->assertEquals(['FR', 'US'], $cmsFeatures->getProperties()['country_restriction']); $this->assertFalse($cmsFeatures->getProperties()['is_multisite']); @@ -48,14 +47,14 @@ public function testConstructorHandlesNullValuesCorrectly() 'alma_enabled' => null, 'widget_cart_activated' => null, 'widget_product_activated' => null, - 'used_fee_plans' => [], + 'used_fee_plans' => null, 'payment_method_position' => null, 'in_page_activated' => null, 'log_activated' => null, - 'excluded_categories' => [], + 'excluded_categories' => null, 'excluded_categories_activated' => null, - 'specific_features' => [], - 'country_restriction' => [], + 'specific_features' => null, + 'country_restriction' => null, 'is_multisite' => null, 'custom_widget_css' => null, ]; @@ -63,19 +62,18 @@ public function testConstructorHandlesNullValuesCorrectly() $cmsFeatures = new CmsFeatures($data); $properties = $cmsFeatures->getProperties(); - $this->assertArrayNotHasKey('alma_enabled', $properties); - $this->assertArrayNotHasKey('widget_cart_activated', $properties); - $this->assertArrayNotHasKey('widget_product_activated', $properties); - $this->assertEquals([], $properties['used_fee_plans']); - $this->assertArrayNotHasKey('payment_method_position', $properties); - $this->assertArrayNotHasKey('in_page_activated', $properties); - $this->assertArrayNotHasKey('log_activated', $properties); - $this->assertEquals([], $properties['excluded_categories']); // Should be an empty array - $this->assertArrayNotHasKey('excluded_categories_activated', $properties); - $this->assertEquals([], $properties['specific_features']); // Should be an empty array - $this->assertEquals([], $properties['country_restriction']); // Should be an empty array - $this->assertArrayNotHasKey('is_multisite', $properties); - $this->assertArrayNotHasKey('custom_widget_css', $properties); + $this->assertArrayNotHasKey('alma_enabled', $properties); + $this->assertArrayNotHasKey('widget_cart_activated', $properties); + $this->assertArrayNotHasKey('widget_product_activated', $properties); + $this->assertArrayNotHasKey('used_fee_plans', $properties); + $this->assertArrayNotHasKey('payment_method_position', $properties); + $this->assertArrayNotHasKey('in_page_activated', $properties); + $this->assertArrayNotHasKey('excluded_categories', $properties); + $this->assertArrayNotHasKey('excluded_categories_activated', $properties); + $this->assertArrayNotHasKey('specific_features', $properties); + $this->assertArrayNotHasKey('country_restriction', $properties); + $this->assertArrayNotHasKey('is_multisite', $properties); + $this->assertArrayNotHasKey('custom_widget_css', $properties); } public function testGetPropertiesFiltersOutNullAndEmptyValues() @@ -89,7 +87,7 @@ public function testGetPropertiesFiltersOutNullAndEmptyValues() 'in_page_activated' => false, 'log_activated' => null, 'excluded_categories' => ['category3'], - 'excluded_categories_activated' => null, + 'excluded_categories_activated' => true, 'specific_features' => [], 'country_restriction' => [], 'is_multisite' => false, @@ -106,7 +104,6 @@ public function testGetPropertiesFiltersOutNullAndEmptyValues() $this->assertArrayNotHasKey('payment_method_position', $properties); // Should be filtered out (null) $this->assertArrayHasKey('in_page_activated', $properties); $this->assertArrayHasKey('excluded_categories', $properties); - $this->assertArrayNotHasKey('excluded_categories_activated', $properties); // Should be filtered out (null) $this->assertArrayHasKey('specific_features', $properties); $this->assertArrayHasKey('country_restriction', $properties); $this->assertArrayHasKey('is_multisite', $properties); @@ -123,13 +120,13 @@ public function testGetPropertiesFiltersOutWithEmptyData() $this->assertArrayNotHasKey('alma_enabled', $properties); $this->assertArrayNotHasKey('widget_cart_activated', $properties); $this->assertArrayNotHasKey('widget_product_activated', $properties); - $this->assertEquals([], $properties['used_fee_plans']); + $this->assertArrayNotHasKey('used_fee_plans', $properties); $this->assertArrayNotHasKey('payment_method_position', $properties); $this->assertArrayNotHasKey('in_page_activated', $properties); - $this->assertEquals([], $properties['excluded_categories']); + $this->assertArrayNotHasKey('excluded_categories', $properties); $this->assertArrayNotHasKey('excluded_categories_activated', $properties); - $this->assertEquals([], $properties['specific_features']); - $this->assertEquals([], $properties['country_restriction']); + $this->assertArrayNotHasKey('specific_features', $properties); + $this->assertArrayNotHasKey('country_restriction', $properties); $this->assertArrayNotHasKey('is_multisite', $properties); $this->assertArrayNotHasKey('custom_widget_css', $properties); } diff --git a/tests/Unit/Entities/CmsInfoTest.php b/tests/Unit/Entities/CmsInfoTest.php index a37eaeb3..2db53add 100644 --- a/tests/Unit/Entities/CmsInfoTest.php +++ b/tests/Unit/Entities/CmsInfoTest.php @@ -42,8 +42,8 @@ public function testConstructorHandlesNullValuesCorrectly() $data = [ 'cms_name' => null, 'cms_version' => null, - 'third_parties_plugins' => [], - 'themes' => [], + 'third_parties_plugins' => null, + 'themes' => null, 'language_name' => null, 'language_version' => null, 'alma_plugin_version' => null, @@ -60,8 +60,8 @@ public function testConstructorHandlesNullValuesCorrectly() $this->assertArrayNotHasKey('alma_plugin_version', $cmsInfo->getProperties()); $this->assertArrayNotHasKey('alma_sdk_version', $cmsInfo->getProperties()); $this->assertArrayNotHasKey('alma_sdk_name', $cmsInfo->getProperties()); - $this->assertEquals([], $cmsInfo->getProperties()['third_parties_plugins']); - $this->assertEquals([], $cmsInfo->getProperties()['themes']); + $this->assertArrayNotHasKey('third_parties_plugins', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('themes', $cmsInfo->getProperties()); } public function testGetPropertiesFiltersOutNullAndEmptyValues() @@ -99,8 +99,8 @@ public function testGetPropertiesFiltersOutWithEmptyData() $this->assertArrayNotHasKey('cms_name', $properties); $this->assertArrayNotHasKey('cms_version', $properties); // Should be filtered out (empty string) - $this->assertEquals([], $cmsInfo->getProperties()['third_parties_plugins']); - $this->assertEquals([], $cmsInfo->getProperties()['themes']); + $this->assertArrayNotHasKey('third_parties_plugins', $properties); + $this->assertArrayNotHasKey('themes', $properties); $this->assertArrayNotHasKey('language_name', $properties); // Should be filtered out (null) $this->assertArrayNotHasKey('language_version', $properties); // Should be filtered out (empty string) $this->assertArrayNotHasKey('alma_sdk_version', $properties); From 7436dc3f7844a8806fdfa95c29bde1564ac73904 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:39:58 +0200 Subject: [PATCH 13/21] fix: reduce Cognitive Complexity --- src/Entities/MerchantData/CmsFeatures.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Entities/MerchantData/CmsFeatures.php b/src/Entities/MerchantData/CmsFeatures.php index 5518a349..a03d3ad0 100644 --- a/src/Entities/MerchantData/CmsFeatures.php +++ b/src/Entities/MerchantData/CmsFeatures.php @@ -77,10 +77,9 @@ public function __construct($cmsFeaturesDataArray) $this->usedFeePlans = isset($cmsFeaturesDataArray['used_fee_plans']) ? $cmsFeaturesDataArray['used_fee_plans'] : null; $this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null; $this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null; + $this->excludedCategories = null; if (isset($cmsFeaturesDataArray['excluded_categories_activated']) && $cmsFeaturesDataArray['excluded_categories_activated']) { $this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : null; - } else { - $this->excludedCategories = null; } $this->paymentMethodPosition = isset($cmsFeaturesDataArray['payment_method_position']) ? $cmsFeaturesDataArray['payment_method_position'] : null; $this->specificFeatures = isset($cmsFeaturesDataArray['specific_features']) ? $cmsFeaturesDataArray['specific_features'] : null; From 9b6f237e2139af643fc548a8fe56c7c95dab8f0e Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:41:46 +0200 Subject: [PATCH 14/21] feat: remove excluded_categories_activated from CmsFeatures --- src/Entities/MerchantData/CmsFeatures.php | 5 +---- tests/Unit/Entities/CmsFeaturesTest.php | 5 ----- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Entities/MerchantData/CmsFeatures.php b/src/Entities/MerchantData/CmsFeatures.php index a03d3ad0..5b73b0a8 100644 --- a/src/Entities/MerchantData/CmsFeatures.php +++ b/src/Entities/MerchantData/CmsFeatures.php @@ -77,10 +77,7 @@ public function __construct($cmsFeaturesDataArray) $this->usedFeePlans = isset($cmsFeaturesDataArray['used_fee_plans']) ? $cmsFeaturesDataArray['used_fee_plans'] : null; $this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null; $this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null; - $this->excludedCategories = null; - if (isset($cmsFeaturesDataArray['excluded_categories_activated']) && $cmsFeaturesDataArray['excluded_categories_activated']) { - $this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : null; - } + $this->excludedCategories = isset($cmsFeaturesDataArray['excluded_categories']) ? $cmsFeaturesDataArray['excluded_categories'] : null; $this->paymentMethodPosition = isset($cmsFeaturesDataArray['payment_method_position']) ? $cmsFeaturesDataArray['payment_method_position'] : null; $this->specificFeatures = isset($cmsFeaturesDataArray['specific_features']) ? $cmsFeaturesDataArray['specific_features'] : null; $this->countryRestriction = isset($cmsFeaturesDataArray['country_restriction']) ? $cmsFeaturesDataArray['country_restriction'] : null; diff --git a/tests/Unit/Entities/CmsFeaturesTest.php b/tests/Unit/Entities/CmsFeaturesTest.php index ec6d057a..814594f2 100644 --- a/tests/Unit/Entities/CmsFeaturesTest.php +++ b/tests/Unit/Entities/CmsFeaturesTest.php @@ -18,7 +18,6 @@ public function testConstructorSetsValuesCorrectly() 'in_page_activated' => true, 'log_activated' => false, 'excluded_categories' => ['category1', 'category2'], - 'excluded_categories_activated' => true, 'specific_features' => ['feature1', 'feature2'], 'country_restriction' => ['FR', 'US'], 'is_multisite' => false, @@ -52,7 +51,6 @@ public function testConstructorHandlesNullValuesCorrectly() 'in_page_activated' => null, 'log_activated' => null, 'excluded_categories' => null, - 'excluded_categories_activated' => null, 'specific_features' => null, 'country_restriction' => null, 'is_multisite' => null, @@ -69,7 +67,6 @@ public function testConstructorHandlesNullValuesCorrectly() $this->assertArrayNotHasKey('payment_method_position', $properties); $this->assertArrayNotHasKey('in_page_activated', $properties); $this->assertArrayNotHasKey('excluded_categories', $properties); - $this->assertArrayNotHasKey('excluded_categories_activated', $properties); $this->assertArrayNotHasKey('specific_features', $properties); $this->assertArrayNotHasKey('country_restriction', $properties); $this->assertArrayNotHasKey('is_multisite', $properties); @@ -87,7 +84,6 @@ public function testGetPropertiesFiltersOutNullAndEmptyValues() 'in_page_activated' => false, 'log_activated' => null, 'excluded_categories' => ['category3'], - 'excluded_categories_activated' => true, 'specific_features' => [], 'country_restriction' => [], 'is_multisite' => false, @@ -124,7 +120,6 @@ public function testGetPropertiesFiltersOutWithEmptyData() $this->assertArrayNotHasKey('payment_method_position', $properties); $this->assertArrayNotHasKey('in_page_activated', $properties); $this->assertArrayNotHasKey('excluded_categories', $properties); - $this->assertArrayNotHasKey('excluded_categories_activated', $properties); $this->assertArrayNotHasKey('specific_features', $properties); $this->assertArrayNotHasKey('country_restriction', $properties); $this->assertArrayNotHasKey('is_multisite', $properties); From 7c064e360e8e475654ac40482b4d61d2dce6ad4a Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:52:46 +0100 Subject: [PATCH 15/21] feat: add isUrlRefreshRequired function --- src/Lib/IntegrationsConfigurationsUtils.php | 16 ++++++++ .../IntegrationsConfigurationsUtilsTest.php | 37 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/Lib/IntegrationsConfigurationsUtils.php create mode 100644 tests/Unit/Lib/IntegrationsConfigurationsUtilsTest.php diff --git a/src/Lib/IntegrationsConfigurationsUtils.php b/src/Lib/IntegrationsConfigurationsUtils.php new file mode 100644 index 00000000..73f69fda --- /dev/null +++ b/src/Lib/IntegrationsConfigurationsUtils.php @@ -0,0 +1,16 @@ + $oneMonthInSeconds; + } +} \ No newline at end of file diff --git a/tests/Unit/Lib/IntegrationsConfigurationsUtilsTest.php b/tests/Unit/Lib/IntegrationsConfigurationsUtilsTest.php new file mode 100644 index 00000000..c089f674 --- /dev/null +++ b/tests/Unit/Lib/IntegrationsConfigurationsUtilsTest.php @@ -0,0 +1,37 @@ +integrationsConfigurationsUtils = new IntegrationsConfigurationsUtils(); + } + + public function testNewSendIsNotNecessary() + { + $timestamp = time() - 100; + $this->assertFalse($this->integrationsConfigurationsUtils->isUrlRefreshRequired($timestamp)); + } + public function testNewSendIsNecessary() + { + $oneMonthInSecondsMoreTen = 30 * 24 * 60 * 60 + 10; // 30 jours en secondes +10 sec + $timestamp = time() - $oneMonthInSecondsMoreTen; + $this->assertTrue($this->integrationsConfigurationsUtils->isUrlRefreshRequired($timestamp)); + } + + public function testNewSendIsNecessaryWithValueNull() + { + $this->assertTrue($this->integrationsConfigurationsUtils->isUrlRefreshRequired(null)); + } +} \ No newline at end of file From c9d53b5e9268eb8b86c7d944f61dc9b5d147fcb7 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:57:55 +0100 Subject: [PATCH 16/21] feat: make isUrlRefreshRequired static --- src/Lib/IntegrationsConfigurationsUtils.php | 2 +- .../Lib/IntegrationsConfigurationsUtilsTest.php | 16 +++------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Lib/IntegrationsConfigurationsUtils.php b/src/Lib/IntegrationsConfigurationsUtils.php index 73f69fda..dee516c2 100644 --- a/src/Lib/IntegrationsConfigurationsUtils.php +++ b/src/Lib/IntegrationsConfigurationsUtils.php @@ -8,7 +8,7 @@ class IntegrationsConfigurationsUtils * @param int | null $lastSendTimestamp * @return bool */ - public function isUrlRefreshRequired($lastSendTimestamp) + public static function isUrlRefreshRequired($lastSendTimestamp) { $oneMonthInSeconds = 30 * 24 * 60 * 60; // 30 jours en sec return (time() - $lastSendTimestamp) > $oneMonthInSeconds; diff --git a/tests/Unit/Lib/IntegrationsConfigurationsUtilsTest.php b/tests/Unit/Lib/IntegrationsConfigurationsUtilsTest.php index c089f674..dcf5dae6 100644 --- a/tests/Unit/Lib/IntegrationsConfigurationsUtilsTest.php +++ b/tests/Unit/Lib/IntegrationsConfigurationsUtilsTest.php @@ -8,30 +8,20 @@ class IntegrationsConfigurationsUtilsTest extends TestCase { - /** - * @var IntegrationsConfigurationsUtils - */ - private $integrationsConfigurationsUtils; - - public function setUp() : void - { - $this->integrationsConfigurationsUtils = new IntegrationsConfigurationsUtils(); - } - public function testNewSendIsNotNecessary() { $timestamp = time() - 100; - $this->assertFalse($this->integrationsConfigurationsUtils->isUrlRefreshRequired($timestamp)); + $this->assertFalse(IntegrationsConfigurationsUtils::isUrlRefreshRequired($timestamp)); } public function testNewSendIsNecessary() { $oneMonthInSecondsMoreTen = 30 * 24 * 60 * 60 + 10; // 30 jours en secondes +10 sec $timestamp = time() - $oneMonthInSecondsMoreTen; - $this->assertTrue($this->integrationsConfigurationsUtils->isUrlRefreshRequired($timestamp)); + $this->assertTrue(IntegrationsConfigurationsUtils::isUrlRefreshRequired($timestamp)); } public function testNewSendIsNecessaryWithValueNull() { - $this->assertTrue($this->integrationsConfigurationsUtils->isUrlRefreshRequired(null)); + $this->assertTrue(IntegrationsConfigurationsUtils::isUrlRefreshRequired(null)); } } \ No newline at end of file From 1a1cbdbfb86a1d2b2603c686061f51a361b6f5f8 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:54:11 +0100 Subject: [PATCH 17/21] feat: change CmsInfo themes to theme_name and theme_version --- src/Entities/MerchantData/CmsInfo.php | 21 ++++++++++++++------- tests/Unit/Entities/CmsInfoTest.php | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/Entities/MerchantData/CmsInfo.php b/src/Entities/MerchantData/CmsInfo.php index 223924fd..71408ef9 100644 --- a/src/Entities/MerchantData/CmsInfo.php +++ b/src/Entities/MerchantData/CmsInfo.php @@ -19,11 +19,6 @@ class CmsInfo */ private $thirdPartiesPlugins; - /** - * @var array|null - */ - private $themes; - /** * @var string */ @@ -49,6 +44,16 @@ class CmsInfo */ private $almaSdkName; + /** + * @var string | null + */ + private $theme_name; + + /** + * @var null|string + */ + private $theme_version; + /** * CmsInfo constructor. * @param array $cmsInfoDataArray @@ -59,7 +64,8 @@ public function __construct($cmsInfoDataArray) $this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : ''; $this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : ''; $this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : null; - $this->themes = isset($cmsInfoDataArray['themes']) ? $cmsInfoDataArray['themes'] : null; + $this->theme_name = isset($cmsInfoDataArray['theme_name']) ? $cmsInfoDataArray['theme_name'] : ''; + $this->theme_version = isset($cmsInfoDataArray['theme_version']) ? $cmsInfoDataArray['theme_version'] : ''; $this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : ''; $this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : ''; $this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : ''; @@ -77,7 +83,8 @@ public function getProperties() 'cms_name' => $this->cmsName, 'cms_version' => $this->cmsVersion, 'third_parties_plugins' => $this->thirdPartiesPlugins, - 'themes' => $this->themes, + 'theme_name' => $this->theme_name, + 'theme_version' => $this->theme_version, 'language_name' => $this->languageName, 'language_version' => $this->languageVersion, 'alma_plugin_version' => $this->almaPluginVersion, diff --git a/tests/Unit/Entities/CmsInfoTest.php b/tests/Unit/Entities/CmsInfoTest.php index 2db53add..424780b9 100644 --- a/tests/Unit/Entities/CmsInfoTest.php +++ b/tests/Unit/Entities/CmsInfoTest.php @@ -16,7 +16,8 @@ public function testConstructorSetsValuesCorrectly() 'cms_name' => 'WordPress', 'cms_version' => '5.8', 'third_parties_plugins' => ['plugin1', 'plugin2'], - 'themes' => ['theme1'], + 'theme_name' => 'theme1', + 'theme_version' => '1.0.0', 'language_name' => 'PHP', 'language_version' => '7.4', 'alma_plugin_version' => '1.0.0', @@ -29,7 +30,8 @@ public function testConstructorSetsValuesCorrectly() $this->assertEquals('WordPress', $cmsInfo->getProperties()['cms_name']); $this->assertEquals('5.8', $cmsInfo->getProperties()['cms_version']); $this->assertEquals(['plugin1', 'plugin2'], $cmsInfo->getProperties()['third_parties_plugins']); - $this->assertEquals(['theme1'], $cmsInfo->getProperties()['themes']); + $this->assertEquals('theme1', $cmsInfo->getProperties()['theme_name']); + $this->assertEquals('1.0.0', $cmsInfo->getProperties()['theme_version']); $this->assertEquals('PHP', $cmsInfo->getProperties()['language_name']); $this->assertEquals('7.4', $cmsInfo->getProperties()['language_version']); $this->assertEquals('1.0.0', $cmsInfo->getProperties()['alma_plugin_version']); @@ -43,7 +45,8 @@ public function testConstructorHandlesNullValuesCorrectly() 'cms_name' => null, 'cms_version' => null, 'third_parties_plugins' => null, - 'themes' => null, + 'theme_name' => null, + 'theme_version' => null, 'language_name' => null, 'language_version' => null, 'alma_plugin_version' => null, @@ -61,7 +64,8 @@ public function testConstructorHandlesNullValuesCorrectly() $this->assertArrayNotHasKey('alma_sdk_version', $cmsInfo->getProperties()); $this->assertArrayNotHasKey('alma_sdk_name', $cmsInfo->getProperties()); $this->assertArrayNotHasKey('third_parties_plugins', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('themes', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('theme_name', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('theme_version', $cmsInfo->getProperties()); } public function testGetPropertiesFiltersOutNullAndEmptyValues() @@ -70,7 +74,8 @@ public function testGetPropertiesFiltersOutNullAndEmptyValues() 'cms_name' => 'WordPress', 'cms_version' => '', 'third_parties_plugins' => ['plugin1'], - 'themes' => [], + 'theme_name' => '', + 'theme_version' => '', 'language_name' => null, 'language_version' => '', 'alma_plugin_version' => null, @@ -88,6 +93,8 @@ public function testGetPropertiesFiltersOutNullAndEmptyValues() $this->assertArrayNotHasKey('language_version', $properties); // Should be filtered out (empty string) $this->assertArrayHasKey('alma_sdk_version', $properties); $this->assertArrayHasKey('alma_sdk_name', $properties); + $this->assertArrayNotHasKey('theme_name', $properties); // Should be filtered out (empty string) + $this->assertArrayNotHasKey('theme_version', $properties); // Should be filtered out (empty string) } public function testGetPropertiesFiltersOutWithEmptyData() @@ -100,7 +107,8 @@ public function testGetPropertiesFiltersOutWithEmptyData() $this->assertArrayNotHasKey('cms_name', $properties); $this->assertArrayNotHasKey('cms_version', $properties); // Should be filtered out (empty string) $this->assertArrayNotHasKey('third_parties_plugins', $properties); - $this->assertArrayNotHasKey('themes', $properties); + $this->assertArrayNotHasKey('theme_name', $properties); + $this->assertArrayNotHasKey('theme_version', $properties); $this->assertArrayNotHasKey('language_name', $properties); // Should be filtered out (null) $this->assertArrayNotHasKey('language_version', $properties); // Should be filtered out (empty string) $this->assertArrayNotHasKey('alma_sdk_version', $properties); From f4056999e1d4318e8d210bbfcee510daa03c4a52 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:56:18 +0100 Subject: [PATCH 18/21] feat: change indent --- tests/Unit/Entities/CmsInfoTest.php | 150 ++++++++++++++-------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/tests/Unit/Entities/CmsInfoTest.php b/tests/Unit/Entities/CmsInfoTest.php index 424780b9..80a97105 100644 --- a/tests/Unit/Entities/CmsInfoTest.php +++ b/tests/Unit/Entities/CmsInfoTest.php @@ -7,95 +7,95 @@ class CmsInfoTest extends TestCase { - const SDK_VERSION = '2.0.0'; - const SDK_NAME = 'Alma SDK'; + const SDK_VERSION = '2.0.0'; + const SDK_NAME = 'Alma SDK'; - public function testConstructorSetsValuesCorrectly() - { - $data = [ - 'cms_name' => 'WordPress', - 'cms_version' => '5.8', - 'third_parties_plugins' => ['plugin1', 'plugin2'], - 'theme_name' => 'theme1', - 'theme_version' => '1.0.0', - 'language_name' => 'PHP', - 'language_version' => '7.4', - 'alma_plugin_version' => '1.0.0', - 'alma_sdk_version' => self::SDK_VERSION, - 'alma_sdk_name' => self::SDK_NAME - ]; + public function testConstructorSetsValuesCorrectly() + { + $data = [ + 'cms_name' => 'WordPress', + 'cms_version' => '5.8', + 'third_parties_plugins' => ['plugin1', 'plugin2'], + 'theme_name' => 'theme1', + 'theme_version' => '1.1.0', + 'language_name' => 'PHP', + 'language_version' => '7.4', + 'alma_plugin_version' => '1.0.0', + 'alma_sdk_version' => self::SDK_VERSION, + 'alma_sdk_name' => self::SDK_NAME + ]; - $cmsInfo = new CmsInfo($data); + $cmsInfo = new CmsInfo($data); - $this->assertEquals('WordPress', $cmsInfo->getProperties()['cms_name']); - $this->assertEquals('5.8', $cmsInfo->getProperties()['cms_version']); - $this->assertEquals(['plugin1', 'plugin2'], $cmsInfo->getProperties()['third_parties_plugins']); - $this->assertEquals('theme1', $cmsInfo->getProperties()['theme_name']); - $this->assertEquals('1.0.0', $cmsInfo->getProperties()['theme_version']); - $this->assertEquals('PHP', $cmsInfo->getProperties()['language_name']); - $this->assertEquals('7.4', $cmsInfo->getProperties()['language_version']); - $this->assertEquals('1.0.0', $cmsInfo->getProperties()['alma_plugin_version']); - $this->assertEquals(self::SDK_VERSION, $cmsInfo->getProperties()['alma_sdk_version']); - $this->assertEquals(self::SDK_NAME, $cmsInfo->getProperties()['alma_sdk_name']); - } + $this->assertEquals('WordPress', $cmsInfo->getProperties()['cms_name']); + $this->assertEquals('5.8', $cmsInfo->getProperties()['cms_version']); + $this->assertEquals(['plugin1', 'plugin2'], $cmsInfo->getProperties()['third_parties_plugins']); + $this->assertEquals('theme1', $cmsInfo->getProperties()['theme_name']); + $this->assertEquals('1.1.0', $cmsInfo->getProperties()['theme_version']); + $this->assertEquals('PHP', $cmsInfo->getProperties()['language_name']); + $this->assertEquals('7.4', $cmsInfo->getProperties()['language_version']); + $this->assertEquals('1.0.0', $cmsInfo->getProperties()['alma_plugin_version']); + $this->assertEquals(self::SDK_VERSION, $cmsInfo->getProperties()['alma_sdk_version']); + $this->assertEquals(self::SDK_NAME, $cmsInfo->getProperties()['alma_sdk_name']); + } - public function testConstructorHandlesNullValuesCorrectly() - { - $data = [ - 'cms_name' => null, - 'cms_version' => null, - 'third_parties_plugins' => null, - 'theme_name' => null, + public function testConstructorHandlesNullValuesCorrectly() + { + $data = [ + 'cms_name' => null, + 'cms_version' => null, + 'third_parties_plugins' => null, + 'theme_name' => null, 'theme_version' => null, - 'language_name' => null, - 'language_version' => null, - 'alma_plugin_version' => null, - 'alma_sdk_version' => null, - 'alma_sdk_name' => null - ]; + 'language_name' => null, + 'language_version' => null, + 'alma_plugin_version' => null, + 'alma_sdk_version' => null, + 'alma_sdk_name' => null + ]; - $cmsInfo = new CmsInfo($data); + $cmsInfo = new CmsInfo($data); - $this->assertArrayNotHasKey('cms_name', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('cms_version', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('language_name', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('language_version', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('alma_plugin_version', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('alma_sdk_version', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('alma_sdk_name', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('third_parties_plugins', $cmsInfo->getProperties()); - $this->assertArrayNotHasKey('theme_name', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('cms_name', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('cms_version', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('language_name', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('language_version', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('alma_plugin_version', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('alma_sdk_version', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('alma_sdk_name', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('third_parties_plugins', $cmsInfo->getProperties()); + $this->assertArrayNotHasKey('theme_name', $cmsInfo->getProperties()); $this->assertArrayNotHasKey('theme_version', $cmsInfo->getProperties()); - } + } - public function testGetPropertiesFiltersOutNullAndEmptyValues() - { - $data = [ - 'cms_name' => 'WordPress', - 'cms_version' => '', - 'third_parties_plugins' => ['plugin1'], - 'theme_name' => '', + public function testGetPropertiesFiltersOutNullAndEmptyValues() + { + $data = [ + 'cms_name' => 'WordPress', + 'cms_version' => '', + 'third_parties_plugins' => ['plugin1'], + 'theme_name' => '', 'theme_version' => '', - 'language_name' => null, - 'language_version' => '', - 'alma_plugin_version' => null, - 'alma_sdk_version' => self::SDK_VERSION, - 'alma_sdk_name' => self::SDK_NAME - ]; + 'language_name' => null, + 'language_version' => '', + 'alma_plugin_version' => null, + 'alma_sdk_version' => self::SDK_VERSION, + 'alma_sdk_name' => self::SDK_NAME + ]; - $cmsInfo = new CmsInfo($data); - $properties = $cmsInfo->getProperties(); + $cmsInfo = new CmsInfo($data); + $properties = $cmsInfo->getProperties(); - $this->assertArrayHasKey('cms_name', $properties); - $this->assertArrayNotHasKey('cms_version', $properties); // Should be filtered out (empty string) - $this->assertArrayHasKey('third_parties_plugins', $properties); - $this->assertArrayNotHasKey('language_name', $properties); // Should be filtered out (null) - $this->assertArrayNotHasKey('language_version', $properties); // Should be filtered out (empty string) - $this->assertArrayHasKey('alma_sdk_version', $properties); - $this->assertArrayHasKey('alma_sdk_name', $properties); + $this->assertArrayHasKey('cms_name', $properties); + $this->assertArrayNotHasKey('cms_version', $properties); // Should be filtered out (empty string) + $this->assertArrayHasKey('third_parties_plugins', $properties); + $this->assertArrayNotHasKey('language_name', $properties); // Should be filtered out (null) + $this->assertArrayNotHasKey('language_version', $properties); // Should be filtered out (empty string) + $this->assertArrayHasKey('alma_sdk_version', $properties); + $this->assertArrayHasKey('alma_sdk_name', $properties); $this->assertArrayNotHasKey('theme_name', $properties); // Should be filtered out (empty string) $this->assertArrayNotHasKey('theme_version', $properties); // Should be filtered out (empty string) - } + } public function testGetPropertiesFiltersOutWithEmptyData() { From d7cb849713131ac998e1c38f558ea41b080b1b19 Mon Sep 17 00:00:00 2001 From: joyet simon <43644110+joyet-simon@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:02:16 +0100 Subject: [PATCH 19/21] fix: pass theme_name and theme_version to Camelcase --- src/Entities/MerchantData/CmsInfo.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Entities/MerchantData/CmsInfo.php b/src/Entities/MerchantData/CmsInfo.php index 71408ef9..25b4e38e 100644 --- a/src/Entities/MerchantData/CmsInfo.php +++ b/src/Entities/MerchantData/CmsInfo.php @@ -47,12 +47,12 @@ class CmsInfo /** * @var string | null */ - private $theme_name; + private $themeName; /** * @var null|string */ - private $theme_version; + private $themeVersion; /** * CmsInfo constructor. @@ -64,8 +64,8 @@ public function __construct($cmsInfoDataArray) $this->cmsName = isset($cmsInfoDataArray['cms_name']) ? $cmsInfoDataArray['cms_name'] : ''; $this->cmsVersion = isset($cmsInfoDataArray['cms_version']) ? $cmsInfoDataArray['cms_version'] : ''; $this->thirdPartiesPlugins = isset($cmsInfoDataArray['third_parties_plugins']) ? $cmsInfoDataArray['third_parties_plugins'] : null; - $this->theme_name = isset($cmsInfoDataArray['theme_name']) ? $cmsInfoDataArray['theme_name'] : ''; - $this->theme_version = isset($cmsInfoDataArray['theme_version']) ? $cmsInfoDataArray['theme_version'] : ''; + $this->themeName = isset($cmsInfoDataArray['theme_name']) ? $cmsInfoDataArray['theme_name'] : ''; + $this->themeVersion = isset($cmsInfoDataArray['theme_version']) ? $cmsInfoDataArray['theme_version'] : ''; $this->languageName = isset($cmsInfoDataArray['language_name']) ? $cmsInfoDataArray['language_name'] : ''; $this->languageVersion = isset($cmsInfoDataArray['language_version']) ? $cmsInfoDataArray['language_version'] : ''; $this->almaPluginVersion = isset($cmsInfoDataArray['alma_plugin_version']) ? $cmsInfoDataArray['alma_plugin_version'] : ''; @@ -83,8 +83,8 @@ public function getProperties() 'cms_name' => $this->cmsName, 'cms_version' => $this->cmsVersion, 'third_parties_plugins' => $this->thirdPartiesPlugins, - 'theme_name' => $this->theme_name, - 'theme_version' => $this->theme_version, + 'theme_name' => $this->themeName, + 'theme_version' => $this->themeVersion, 'language_name' => $this->languageName, 'language_version' => $this->languageVersion, 'alma_plugin_version' => $this->almaPluginVersion, From dafd8f534982b61f2f6bfc6fe53588f85cd550b0 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Wed, 30 Oct 2024 12:01:50 +0100 Subject: [PATCH 20/21] feat: replace payment validator hmac verification by request hmac validation --- src/Lib/PaymentValidator.php | 7 +- src/Lib/RequestUtils.php | 22 ++++++ tests/Unit/Lib/RequestUtilsTest.php | 118 ++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 src/Lib/RequestUtils.php create mode 100644 tests/Unit/Lib/RequestUtilsTest.php diff --git a/src/Lib/PaymentValidator.php b/src/Lib/PaymentValidator.php index cc4f0f0c..5fe9f904 100644 --- a/src/Lib/PaymentValidator.php +++ b/src/Lib/PaymentValidator.php @@ -58,15 +58,16 @@ public static function checkPurchaseAmount($data) } /** + * Validate the HMAC signature of the request + * * @param string $data * @param string $apiKey * @param string $signature + * @deprecated Use RequestUtils::isHmacValidated instead * @return bool */ public function isHmacValidated($data, $apiKey, $signature) { - return is_string($data) && - is_string($apiKey) && - hash_hmac('sha256', $data, $apiKey) === $signature; + return RequestUtils::isHmacValidated($data, $apiKey, $signature); } } diff --git a/src/Lib/RequestUtils.php b/src/Lib/RequestUtils.php new file mode 100644 index 00000000..3b82601b --- /dev/null +++ b/src/Lib/RequestUtils.php @@ -0,0 +1,22 @@ +assertTrue(RequestUtils::isHmacValidated($data, $apiKey, $signature)); + } + /** + * @dataProvider checkHmacInvalidDataProvider + * @param $data + * @param $apiKey + * @param $signature + * @return void + */ + public function testHmacDataDifferentFromSignature($data, $apiKey, $signature) + { + $this->assertFalse(RequestUtils::isHmacValidated($data, $apiKey, $signature)); + } + + public static function checkHmacInvalidDataProvider() + { + return [ + 'String data' => [ + 'data' => 'payment_id_test', + 'apiKey' => 'api_key_test', + 'signature' => 'wrong_signature' + ], + 'Empty array data' => [ + 'data' => [], + 'apiKey' => 'api_key_test', + 'signature' => 'wrong_signature' + ], + 'Empty array apiKey' => [ + 'data' => 'payment_id_test', + 'apiKey' => [], + 'signature' => 'wrong_signature' + ], + 'Empty array signature' => [ + 'data' => 'payment_id_test', + 'apiKey' => 'api_key_test', + 'signature' => [] + ], + 'Empty string data' => [ + 'data' => '', + 'apiKey' => 'api_key_test', + 'signature' => 'wrong_signature' + ], + 'Empty string apiKey' => [ + 'data' => 'payment_id_test', + 'apiKey' => '', + 'signature' => 'wrong_signature' + ], + 'Empty string signature' => [ + 'data' => 'payment_id_test', + 'apiKey' => 'api_key_test', + 'signature' => '' + ], + 'Object data' => [ + 'data' => new stdClass(), + 'apiKey' => 'api_key_test', + 'signature' => 'wrong_signature' + ], + 'Object apiKey' => [ + 'data' => 'payment_id_test', + 'apiKey' => new stdClass(), + 'signature' => 'wrong_signature' + ], + 'Object signature' => [ + 'data' => 'payment_id_test', + 'apiKey' => 'api_key_test', + 'signature' => new stdClass() + ], + 'Boolean data' => [ + 'data' => false, + 'apiKey' => 'api_key_test', + 'signature' => 'wrong_signature' + ], + 'Boolean apiKey' => [ + 'data' => 'payment_id_test', + 'apiKey' => true, + 'signature' => 'wrong_signature' + ], + 'Boolean signature' => [ + 'data' => 'payment_id_test', + 'apiKey' => 'api_key_test', + 'signature' => true + ], + 'Int data' => [ + 'data' => 1, + 'apiKey' => 'api_key_test', + 'signature' => 'wrong_signature' + ], + 'Int apiKey' => [ + 'data' => 'payment_id_test', + 'apiKey' => 2, + 'signature' => 'wrong_signature' + ], + 'Int signature' => [ + 'data' => 'payment_id_test', + 'apiKey' => 'api_key_test', + 'signature' => 3 + ] + + ]; + } + +} From 9c267c48cc55590ceca016226102c0b8a5ab7af8 Mon Sep 17 00:00:00 2001 From: joyet-simon <43644110+joyet-simon@users.noreply.github.com> Date: Mon, 4 Nov 2024 09:11:57 +0000 Subject: [PATCH 21/21] chore: update version --- CHANGELOG.md | 19 +++++++++++++++++++ composer.json | 2 +- src/Client.php | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b75e4d03..e884bb0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,23 @@ # CHANGELOG +## v2.3.0 - 2024-11-04 + +### Changes + +- chore(deps): update pre-commit hook returntocorp/semgrep to v1.92.0 (#116) + +### 🚀 New Features + +- feat: replace payment validator hmac verification by request hmac val… (#147) +- Change CmsInfo themes to theme_name and theme_version (#146) +- Add isUrlRefreshRequired function (#145) +- Make nullable for all attributes in CmsInfo and CmsFeatures (#143) +- Create endpoint and formatter for gather cms data (#142) + +#### Contributors + +@Benjamin-Freoua-Alma, @Francois-Gomis, @alma-renovate-bot, @alma-renovate-bot[bot], @github-actions, @hyahiaoui, @joyet-simon and @remi-zuffinetti + ## v2.2.0 - 2024-09-05 ### Changes @@ -162,6 +180,7 @@ } + ``` * Add fields and docs to the Payment entity * Add a Refund entity and extract refunds data within the Payment entity constructor diff --git a/composer.json b/composer.json index f9c14f5e..24fe5eeb 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.2.0", + "version": "2.3.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 5176acb3..010c3280 100644 --- a/src/Client.php +++ b/src/Client.php @@ -30,7 +30,7 @@ class Client { - const VERSION = '2.2.0'; + const VERSION = '2.3.0'; const LIVE_MODE = 'live'; const TEST_MODE = 'test';