Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fa6c068
ECP-81:[WIP] unit tests in prestashop module
Jun 5, 2024
c0ac00a
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
88df30e
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
c31e342
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
de274e7
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
62965f1
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
4989c09
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
b9da7b0
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
e7d7981
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
94637e1
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
b802134
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
47992a3
ECP-81:[WIP] unit tests in prestashop module
Jun 6, 2024
2382d67
ECP-81:[WIP] unit tests in prestashop module
Jun 7, 2024
8d22002
ECP-81:[WIP] unit tests in prestashop module
Jun 7, 2024
824236d
ECP-81:[WIP] unit tests in prestashop module
Jun 7, 2024
f7385d5
ECP-81:[WIP] unit tests in prestashop module
Jun 7, 2024
1d58e97
ECP-81:[WIP] unit tests in prestashop module
Jun 7, 2024
f621e1b
ECP-81:[WIP] unit tests in prestashop module
Jun 10, 2024
a8b0c88
ECP-81:[WIP] unit tests in prestashop module
Jun 10, 2024
f9cbf06
ECP-81:[WIP] unit tests in prestashop module
Jun 10, 2024
663263e
ECP-81:[WIP] unit tests in prestashop module
Jun 10, 2024
aa1c508
ECP-81:[WIP] unit tests in prestashop module
Jun 10, 2024
7aa22c7
ECP-81:[WIP] unit tests in prestashop module
Jun 11, 2024
c72f46c
ECP-81: unit test for LengowOrderDetail in progress
Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion classes/controllers/LengowHomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private function connectCms()
$cmsConnected = LengowSync::syncCatalog(true);
if (!$cmsConnected) {
$syncData = json_encode(LengowSync::getSyncData());
$result = LengowConnector::queryApi(LengowConnector::POST, LengowConnector::API_CMS, [], $syncData);
$result = LengowConnector::getInstance()->requestApi(LengowConnector::POST, LengowConnector::API_CMS, [], $syncData);
if (isset($result->common_account)) {
$cmsConnected = true;
$messageKey = 'log.connection.cms_creation_success';
Expand Down
12 changes: 9 additions & 3 deletions classes/models/LengowAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public static function getActionByActionId($actionId)
public static function getActionsByOrderId($idOrder, $onlyActive = false, $actionType = null, $load = true)
{
try {

$sqlOnlyActive = $onlyActive ? ' AND state = ' . self::STATE_NEW : '';
$sqlType = $actionType === null ? '' : ' AND action_type = "' . pSQL($actionType) . '"';
$rows = Db::getInstance()->executeS(
Expand Down Expand Up @@ -326,7 +327,12 @@ public static function canSendAction($params, $lengowOrder)
unset($getParams[$param]);
}
}
$result = LengowConnector::queryApi(LengowConnector::GET, LengowConnector::API_ORDER_ACTION, $getParams);

$result = LengowConnector::getInstance()->requestApi(
LengowConnector::GET,
LengowConnector::API_ORDER_ACTION,
$getParams
);
if (isset($result->error, $result->error->message)) {
throw new LengowException($result->error->message);
}
Expand Down Expand Up @@ -375,7 +381,7 @@ public static function canSendAction($params, $lengowOrder)
public static function sendAction($params, $lengowOrder)
{
if (!LengowConfiguration::debugModeIsActive()) {
$result = LengowConnector::queryApi(LengowConnector::POST, LengowConnector::API_ORDER_ACTION, $params);
$result = LengowConnector::getInstance()->requestApi(LengowConnector::POST, LengowConnector::API_ORDER_ACTION, $params);
if (isset($result->id)) {
self::createAction(
[
Expand Down Expand Up @@ -584,7 +590,7 @@ public static function checkFinishAction($logOutput = false)
$logOutput
);
do {
$results = LengowConnector::queryApi(
$results = LengowConnector::getInstance()->requestApi(
LengowConnector::GET,
LengowConnector::API_ORDER_ACTION,
[
Expand Down
6 changes: 3 additions & 3 deletions classes/models/LengowCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LengowCatalog
*/
public static function hasCatalogNotLinked()
{
$lengowCatalogs = LengowConnector::queryApi(LengowConnector::GET, LengowConnector::API_CMS_CATALOG);
$lengowCatalogs = LengowConnector::getInstance()->requestApi(LengowConnector::GET, LengowConnector::API_CMS_CATALOG);
if (!$lengowCatalogs) {
return false;
}
Expand All @@ -56,7 +56,7 @@ public static function hasCatalogNotLinked()
public static function getCatalogList()
{
$catalogList = [];
$lengowCatalogs = LengowConnector::queryApi(LengowConnector::GET, LengowConnector::API_CMS_CATALOG);
$lengowCatalogs = LengowConnector::getInstance()->requestApi(LengowConnector::GET, LengowConnector::API_CMS_CATALOG);
if (!$lengowCatalogs) {
return $catalogList;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public static function linkCatalogs(array $catalogsByShops)
);
}
if ($hasCatalogToLink) {
$result = LengowConnector::queryApi(
$result = LengowConnector::getInstance()->requestApi(
LengowConnector::POST,
LengowConnector::API_CMS_MAPPING,
[],
Expand Down
112 changes: 106 additions & 6 deletions classes/models/LengowConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class LengowConnector
self::API_PLUGIN,
];

/** @var LengowConnector|null instance */
protected static $instance;

/**
* Make a new Lengow API Connector.
*
Expand All @@ -167,6 +170,51 @@ public function __construct($accessToken, $secret)
$this->secret = $secret;
}

/**
* Returns a LengowConnector instance
*
* @param string $accessToken
* @param string $secret
*
* @return null|\LengowConnector
*/
public static function getInstance($accessToken = '', $secret = '')
{
if (!empty(self::$instance)
&& self::$instance instanceof LengowConnector) {
return self::$instance;
}
list($accountIdConfig, $accessTokenConfig, $secretConfig) = LengowConfiguration::getAccessIds();

if ($accountIdConfig) {
$connector = new LengowConnector($accessTokenConfig, $secretConfig);
} else {
$connector = new LengowConnector($accessToken, $secret);
}

self::$instance = $connector;

return $connector;
}

/**
* Will set the instance for testing
*
* @param LengowConnector $testConnector
*/
public static function setInstanceForTesting($testConnector)
{
self::$instance = $testConnector;
}

/**
* Will reset $intance array
*/
public static function disableTestingInstance()
{
self::$instance = null;
}

/**
* Check API Authentication
*
Expand All @@ -179,12 +227,13 @@ public static function isValidAuth($logOutput = false)
if (!LengowToolbox::isCurlActivated()) {
return false;
}
list($accountId, $accessToken, $secret) = LengowConfiguration::getAccessIds();
if ($accountId === null) {

$connector = self::getInstance();
if (is_null($connector)) {
return false;
}
$connector = new LengowConnector($accessToken, $secret);
try {

$connector->connect(false, $logOutput);
} catch (LengowException $e) {
$message = LengowMain::decodeLogMessage($e->getMessage(), LengowTranslation::DEFAULT_ISO_CODE);
Expand Down Expand Up @@ -225,7 +274,10 @@ public static function queryApi($type, $api, $args = [], $body = '', $logOutput
if ($accountId === null && $authorizationRequired) {
return false;
}
$connector = new LengowConnector($accessToken, $secret);
$connector = self::getInstance($accessToken, $secret);
if (is_null($connector)) {
return false;
}
$type = (string) Tools::strtolower($type);
$args = $authorizationRequired
? array_merge([LengowImport::ARG_ACCOUNT_ID => $accountId], $args)
Expand Down Expand Up @@ -259,7 +311,10 @@ public static function queryApi($type, $api, $args = [], $body = '', $logOutput
*/
public static function getAccountIdByCredentials($accessToken, $secret, $logOutput = false)
{
$connector = new LengowConnector($accessToken, $secret);
$connector = self::getInstance($accessToken, $secret);
if (is_null($connector)) {
return null;
}
try {
$data = $connector->callAction(
self::API_ACCESS_TOKEN,
Expand Down Expand Up @@ -388,6 +443,51 @@ public function patch($api, $args = [], $format = self::FORMAT_JSON, $body = '',
return $this->call($api, $args, self::PATCH, $format, $body, $logOutput);
}

/**
* Get result for a request to Api
*
* @param string $type request type (GET / POST / PUT / PATCH)
* @param string $api request api
* @param array $args request params
* @param string $body body data for request
* @param bool $logOutput see log or not
*
* @return array|false
*/
public function requestApi($type, $api, $args = [], $body = '', $logOutput = false)
{
if (!in_array($type, [self::GET, self::POST, self::PUT, self::PATCH])) {
return false;
}
try {
$authorizationRequired = !in_array($api, self::$apiWithoutAuthorizations, true);
list($accountId) = LengowConfiguration::getAccessIds();
if ($accountId === null && $authorizationRequired) {
return false;
}

$type = (string) Tools::strtolower($type);
$args = $authorizationRequired
? array_merge([LengowImport::ARG_ACCOUNT_ID => $accountId], $args)
: $args;
$results = $this->$type($api, $args, self::FORMAT_STREAM, $body, $logOutput);
} catch (LengowException $e) {
$message = LengowMain::decodeLogMessage($e->getMessage(), LengowTranslation::DEFAULT_ISO_CODE);
$error = LengowMain::setLogMessage(
'log.connector.error_api',
[
'error_code' => $e->getCode(),
'error_message' => $message,
]
);
LengowMain::log(LengowLog::CODE_CONNECTOR, $error, $logOutput);

return false;
}

return json_decode($results);
}

/**
* The API method
*
Expand Down Expand Up @@ -442,7 +542,7 @@ private function call($api, $args, $type, $format, $body, $logOutput)
*
* @throws LengowException
*/
private function callAction($api, $args, $type, $format, $body, $logOutput)
protected function callAction($api, $args, $type, $format, $body, $logOutput)
{
$result = $this->makeRequest($type, $api, $args, $this->token, $body, $logOutput);

Expand Down
2 changes: 1 addition & 1 deletion classes/models/LengowLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function getPaths()
}
foreach ($files as $file) {
preg_match('/^logs-([0-9]{4}-[0-9]{2}-[0-9]{2})\.txt$/', $file->fileName, $match);
$date = $match[1];
$date = isset($match[1]) ? $match[1] : '';
$logs[] = [
self::LOG_DATE => $date,
self::LOG_LINK => LengowMain::getToolboxUrl()
Expand Down
7 changes: 5 additions & 2 deletions classes/models/LengowMarketplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,16 @@ class LengowMarketplace
* Construct a new Marketplace instance with marketplace API
*
* @param string $name name of the marketplace
* @param string $marketplacesData the json data
*
* @throws LengowException marketplace not present
*/
public function __construct($name)
public function __construct($name, $marketplacesData = '')
{
self::loadApiMarketplace();
$this->name = (string) Tools::strtolower($name);
if (!empty($marketplacesData)) {
self::$marketplaces = json_decode($marketplacesData);
}
if (!isset(self::$marketplaces->{$this->name})) {
self::loadApiMarketplace(true);
}
Expand Down
2 changes: 1 addition & 1 deletion classes/models/LengowOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ public function callAction($action)
public function getOrderLineByApi()
{
$orderLines = [];
$results = LengowConnector::queryApi(
$results = LengowConnector::getInstance()->requestApi(
LengowConnector::GET,
LengowConnector::API_ORDER,
[
Expand Down
10 changes: 5 additions & 5 deletions classes/models/LengowSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static function syncCatalog($force = false, $logOutput = false)
return $success;
}
}
$result = LengowConnector::queryApi(LengowConnector::GET, LengowConnector::API_CMS, [], '', $logOutput);
$result = LengowConnector::getInstance()->requestApi(LengowConnector::GET, LengowConnector::API_CMS, [], '', $logOutput);
if (isset($result->cms)) {
$cmsToken = LengowMain::getToken();
foreach ($result->cms as $cms) {
Expand Down Expand Up @@ -276,7 +276,7 @@ public static function setCmsOption($force = false, $logOutput = false)
}
}
$options = json_encode(self::getOptionData());
LengowConnector::queryApi(LengowConnector::PUT, LengowConnector::API_CMS, [], $options, $logOutput);
LengowConnector::getInstance()->requestApi(LengowConnector::PUT, LengowConnector::API_CMS, [], $options, $logOutput);
LengowConfiguration::updateGlobalValue(LengowConfiguration::LAST_UPDATE_OPTION_CMS, time());

return true;
Expand All @@ -301,7 +301,7 @@ public static function getStatusAccount($force = false, $logOutput = false)
);
}
}
$result = LengowConnector::queryApi(LengowConnector::GET, LengowConnector::API_PLAN, [], '', $logOutput);
$result = LengowConnector::getInstance()->requestApi(LengowConnector::GET, LengowConnector::API_PLAN, [], '', $logOutput);
if (isset($result->isFreeTrial)) {
$status = [
'type' => $result->isFreeTrial ? 'free_trial' : '',
Expand Down Expand Up @@ -352,7 +352,7 @@ public static function getMarketplaces($force = false, $logOutput = false)
}
}
// recovering data with the API
$result = LengowConnector::queryApi(
$result = LengowConnector::getInstance()->requestApi(
LengowConnector::GET,
LengowConnector::API_MARKETPLACE,
[],
Expand Down Expand Up @@ -414,7 +414,7 @@ public static function getPluginData($force = false, $logOutput = false)
return json_decode(LengowConfiguration::getGlobalValue(LengowConfiguration::PLUGIN_DATA), true);
}
}
$plugins = LengowConnector::queryApi(
$plugins = LengowConnector::getInstance()->requestApi(
LengowConnector::GET,
LengowConnector::API_PLUGIN,
[],
Expand Down
16 changes: 14 additions & 2 deletions classes/models/LengowTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ class LengowTranslation

/**
* Construct
*
* @param string $isoCode
*/
public function __construct()
public function __construct($isoCode = '')
{
$this->isoCode = Context::getContext()->language->iso_code;
$this->isoCode = !empty($isoCode) ? $isoCode : $this->getCurrentIsoCode();
}

/**
Expand Down Expand Up @@ -147,4 +149,14 @@ public function loadFile($isoCode, $filename = null)

return !empty($translation);
}

/**
* Returns current language is code
*
* @return string
*/
protected function getCurrentIsoCode()
{
return Context::getContext()->language->iso_code;
}
}
1 change: 1 addition & 0 deletions tests/Unit/.phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Lengow\\Connector\\Test\\Unit\\Model\\ConnectorTest::testClassInstantiation":3,"Lengow\\Connector\\Test\\Unit\\Model\\ConnectorTest::testFormat":4,"Lengow\\Connector\\Test\\Unit\\Model\\ConnectorTest::testConnect":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowExportTest::testClassInstantiation":5,"Lengow\\Connector\\Test\\Unit\\Model\\LengowLogTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowFileTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowTranslationTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowAddressTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowCarrierTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowCustomerTest::testClassInstantiation":3,"Lengow\\Connector\\Test\\Unit\\Model\\LengowFeedTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowImportOrderTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowInstallTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowListTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowMarketplaceTest::testClassInstantiation":4,"Lengow\\Connector\\Test\\Unit\\Model\\LengowToolboxElementTest::testClassInstantiation":3},"times":{"Lengow\\Connector\\Test\\Unit\\Model\\ConnectorTest::testClassInstantiation":0.012,"Lengow\\Connector\\Test\\Unit\\Model\\ConnectorTest::testFormat":0.001,"Lengow\\Connector\\Test\\Unit\\Model\\ConnectorTest::testConnect":0.017,"Lengow\\Connector\\Test\\Unit\\Model\\LengowConnectorTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowConnectorTest::testFormat":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowExceptionTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowExportTest::testClassInstantiation":0.009,"Lengow\\Connector\\Test\\Unit\\Model\\LengowLogTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowFileTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowTranslationTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowAddressTest::testClassInstantiation":0.006,"Lengow\\Connector\\Test\\Unit\\Model\\LengowActionTest::testClassInstantiation":0.008,"Lengow\\Connector\\Test\\Unit\\Model\\LengowCarrierTest::testClassInstantiation":0.007,"Lengow\\Connector\\Test\\Unit\\Model\\LengowCartTest::testClassInstantiation":0.009,"Lengow\\Connector\\Test\\Unit\\Model\\LengowCatalogTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowBackupTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowConfigurationTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowConfigurationFormTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowCountryTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowCustomerTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowFeedTest::testClassInstantiation":0.006,"Lengow\\Connector\\Test\\Unit\\Model\\LengowGenderTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowHookTest::testClassInstantiation":0.027,"Lengow\\Connector\\Test\\Unit\\Model\\LengowImportTest::testClassInstantiation":0.007,"Lengow\\Connector\\Test\\Unit\\Model\\LengowImportOrderTest::testClassInstantiation":0.006,"Lengow\\Connector\\Test\\Unit\\Model\\LengowInstallTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowLinkTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowMainTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowListTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowMarketplaceTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowMethodTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowNameParserTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowOrderTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowOrderCarrierTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowOrderDetailTest::testClassInstantiation":0.006,"Lengow\\Connector\\Test\\Unit\\Model\\LengowOrderErrorTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowOrderLineTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowPaymentModuleTest::testClassInstantiation":0.007,"Lengow\\Connector\\Test\\Unit\\Model\\LengowProductTest::testClassInstantiation":0.019,"Lengow\\Connector\\Test\\Unit\\Model\\LengowShopTest::testClassInstantiation":0.006,"Lengow\\Connector\\Test\\Unit\\Model\\LengowSyncTest::testClassInstantiation":0.004,"Lengow\\Connector\\Test\\Unit\\Model\\LengowToolboxTest::testClassInstantiation":0.005,"Lengow\\Connector\\Test\\Unit\\Model\\LengowToolboxElementTest::testClassInstantiation":0.004}}
Loading