-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from alma/feature/ecom-2116-php-client-create…
…-endpoint-that-returns-data Create endpoint and formatter for gather cms data
- Loading branch information
Showing
8 changed files
with
675 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Alma\API\Entities\MerchantData; | ||
|
||
class CmsFeatures | ||
{ | ||
/** | ||
* @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<array{name: string}> | ||
*/ | ||
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 !== ''; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Alma\API\Entities\MerchantData; | ||
|
||
class CmsInfo | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $cmsName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $cmsVersion; | ||
|
||
/** | ||
* @var array<array{name: string, version: string}> | ||
*/ | ||
private $thirdPartiesPlugins; | ||
|
||
/** | ||
* @var array<array{name: string, version: string}> | ||
*/ | ||
private $themes; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $languageName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $languageVersion; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $almaPluginVersion; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $almaSdkVersion; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
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'] : ''; | ||
$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'] : ''; | ||
$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'] : ''; | ||
} | ||
|
||
/** | ||
* @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 !== ''; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Alma\API\Lib; | ||
|
||
use Alma\API\Entities\MerchantData\CmsFeatures; | ||
use Alma\API\Entities\MerchantData\CmsInfo; | ||
|
||
class PayloadFormatter | ||
{ | ||
/** | ||
* @param CmsInfo $cmsInfo | ||
* @param CmsFeatures $cmsFeatures | ||
* @return array | ||
*/ | ||
public function formatConfigurationPayload(CmsInfo $cmsInfo, CmsFeatures $cmsFeatures) | ||
{ | ||
return [ | ||
"cms_info" => $cmsInfo->getProperties(), | ||
"cms_features" => $cmsFeatures->getProperties(), | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Unit\Endpoints; | ||
|
||
use Alma\API\Endpoints\Merchants; | ||
use Alma\API\Exceptions\RequestException; | ||
use Alma\API\Request; | ||
use Alma\API\Response; | ||
use Mockery; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MerchantsTest extends TestCase | ||
{ | ||
const URL = "https://www.example.com/integrations/configurations"; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->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' => self::URL]) | ||
->andReturn($this->requestObject); | ||
|
||
$this->merchantEndpoint->shouldReceive('request') | ||
->with(Merchants::ME_PATH . "/configuration") | ||
->once() | ||
->andReturn($this->requestObject); | ||
$this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); | ||
|
||
$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' => self::URL]) | ||
->andReturn($this->requestObject); | ||
|
||
$this->merchantEndpoint->shouldReceive('request') | ||
->with(Merchants::ME_PATH . "/configuration") | ||
->once() | ||
->andReturn($this->requestObject); | ||
$this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); | ||
|
||
$url = self::URL; | ||
$this->expectException(RequestException::class); | ||
$this->merchantEndpoint->sendIntegrationsConfigurationsUrl($url); | ||
|
||
} | ||
} |
Oops, something went wrong.