update
This commit is contained in:
35
modules/ps_mbo/vendor/prestashop/circuit-breaker/.github/main.workflow
vendored
Normal file
35
modules/ps_mbo/vendor/prestashop/circuit-breaker/.github/main.workflow
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
workflow "Code Quality" {
|
||||
on = "push"
|
||||
resolves = [
|
||||
"PHPStan",
|
||||
"PHP-CS-Fixer",
|
||||
"Psalm",
|
||||
"PHPQA"
|
||||
]
|
||||
}
|
||||
|
||||
action "PHPStan" {
|
||||
uses = "docker://oskarstark/phpstan-ga:with-extensions"
|
||||
args = "analyse src tests --level max --configuration extension.neon"
|
||||
secrets = ["GITHUB_TOKEN"]
|
||||
}
|
||||
|
||||
action "PHP-CS-Fixer" {
|
||||
uses = "docker://oskarstark/php-cs-fixer-ga"
|
||||
secrets = ["GITHUB_TOKEN"]
|
||||
args = "--config=.php_cs.dist --diff --dry-run"
|
||||
}
|
||||
|
||||
action "Psalm" {
|
||||
needs="PHPStan"
|
||||
uses = "docker://mickaelandrieu/psalm-ga"
|
||||
secrets = ["GITHUB_TOKEN"]
|
||||
args = "--find-dead-code --diff --diff-methods"
|
||||
}
|
||||
|
||||
action "PHPQA" {
|
||||
needs="PHP-CS-Fixer"
|
||||
uses = "docker://mickaelandrieu/phpqa-ga"
|
||||
secrets = ["GITHUB_TOKEN"]
|
||||
args = "--report --tools phpcs:0,phpmd:0,phpcpd:0,parallel-lint:0,phpmetrics,phploc,pdepend --ignoredDirs vendor,tests"
|
||||
}
|
||||
11
modules/ps_mbo/vendor/prestashop/circuit-breaker/.github/release-drafter.yml
vendored
Normal file
11
modules/ps_mbo/vendor/prestashop/circuit-breaker/.github/release-drafter.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
change-template: '- $TITLE (#$NUMBER)'
|
||||
branches:
|
||||
- master
|
||||
template: |
|
||||
## Changelog
|
||||
|
||||
$CHANGES
|
||||
|
||||
### Contributors
|
||||
|
||||
$CONTRIBUTORS
|
||||
5
modules/ps_mbo/vendor/prestashop/circuit-breaker/.gitignore
vendored
Normal file
5
modules/ps_mbo/vendor/prestashop/circuit-breaker/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
build/
|
||||
vendor/
|
||||
composer.lock
|
||||
.php_cs.cache
|
||||
psalm.xml
|
||||
39
modules/ps_mbo/vendor/prestashop/circuit-breaker/.php_cs.dist
vendored
Normal file
39
modules/ps_mbo/vendor/prestashop/circuit-breaker/.php_cs.dist
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()->in([
|
||||
__DIR__.'/src',
|
||||
__DIR__.'/tests',
|
||||
]);
|
||||
|
||||
return PhpCsFixer\Config::create()
|
||||
->setRiskyAllowed(true)
|
||||
->setRules([
|
||||
'@Symfony' => true,
|
||||
'concat_space' => [
|
||||
'spacing' => 'one',
|
||||
],
|
||||
'cast_spaces' => [
|
||||
'space' => 'single',
|
||||
],
|
||||
'error_suppression' => [
|
||||
'mute_deprecation_error' => false,
|
||||
'noise_remaining_usages' => false,
|
||||
'noise_remaining_usages_exclude' => [],
|
||||
],
|
||||
'function_to_constant' => false,
|
||||
'no_alias_functions' => false,
|
||||
'non_printable_character' => false,
|
||||
'phpdoc_summary' => false,
|
||||
'phpdoc_align' => [
|
||||
'align' => 'left',
|
||||
],
|
||||
'protected_to_private' => false,
|
||||
'psr4' => false,
|
||||
'self_accessor' => false,
|
||||
'yoda_style' => null,
|
||||
'non_printable_character' => true,
|
||||
'phpdoc_no_empty_return' => false,
|
||||
])
|
||||
->setFinder($finder)
|
||||
->setCacheFile(__DIR__.'/.php_cs.cache')
|
||||
;
|
||||
30
modules/ps_mbo/vendor/prestashop/circuit-breaker/.travis.yml
vendored
Normal file
30
modules/ps_mbo/vendor/prestashop/circuit-breaker/.travis.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
language: php
|
||||
|
||||
notifications:
|
||||
email:
|
||||
on_success: never
|
||||
|
||||
php:
|
||||
- 7.2
|
||||
- 7.3
|
||||
- 5.6
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
allow_failures:
|
||||
- php: 7.3
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.composer/cache
|
||||
|
||||
before_script:
|
||||
- composer install -n
|
||||
|
||||
script:
|
||||
- composer test -- --coverage-clover=coverage.xml
|
||||
- composer phpcs
|
||||
- composer psalm
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
19
modules/ps_mbo/vendor/prestashop/circuit-breaker/LICENSE
vendored
Normal file
19
modules/ps_mbo/vendor/prestashop/circuit-breaker/LICENSE
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2018-present PrestaShop
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
157
modules/ps_mbo/vendor/prestashop/circuit-breaker/README.md
vendored
Normal file
157
modules/ps_mbo/vendor/prestashop/circuit-breaker/README.md
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
# Circuit Breaker, an implementation for resilient PHP applications
|
||||
|
||||
[](https://codecov.io/gh/PrestaShop/circuit-breaker) [](https://shields.io/#/) [](https://shields.io/#/) [](https://travis-ci.com/PrestaShop/circuit-breaker)
|
||||
|
||||
## Main principles
|
||||
|
||||

|
||||
|
||||
This library is compatible with PHP 5.6+.
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
composer require prestashop/circuit-breaker
|
||||
```
|
||||
|
||||
## Use
|
||||
|
||||
### Simple Circuit Breaker
|
||||
|
||||
You can use the factory to create a simple circuit breaker.
|
||||
|
||||
By default, you need to define 3 parameters for the circuit breaker:
|
||||
|
||||
* the **failures**: define how many times we try to access the service;
|
||||
* the **timeout**: define how much time we wait before consider the service unreachable;
|
||||
* the **threshold**: define how much time we wait before trying to access again the service (once it is considered unreachable);
|
||||
|
||||
The **fallback** callback will be used if the distant service is unreachable when the Circuit Breaker is Open (means "is used" if the service is unreachable).
|
||||
|
||||
> You'd better return the same type of response expected from your distant call.
|
||||
|
||||
```php
|
||||
use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
|
||||
use PrestaShop\CircuitBreaker\FactorySettings;
|
||||
|
||||
$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
|
||||
$circuitBreaker = $circuitBreakerFactory->create(new FactorySettings(2, 0.1, 10));
|
||||
|
||||
$fallbackResponse = function () {
|
||||
return '{}';
|
||||
};
|
||||
|
||||
$response = $circuitBreaker->call('https://api.domain.com', [], $fallbackResponse);
|
||||
```
|
||||
|
||||
If you don't specify any fallback, by default the circuit breaker will return an empty string.
|
||||
|
||||
```php
|
||||
use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
|
||||
use PrestaShop\CircuitBreaker\FactorySettings;
|
||||
|
||||
$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
|
||||
$circuitBreaker = $circuitBreakerFactory->create(new FactorySettings(2, 0.1, 10));
|
||||
$response = $circuitBreaker->call('https://unreacheable.api.domain.com', []); // $response == ''
|
||||
```
|
||||
|
||||
You can also define the client options (or even set your own client if you prefer).
|
||||
|
||||
```php
|
||||
use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
|
||||
use PrestaShop\CircuitBreaker\FactorySettings;
|
||||
|
||||
$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
|
||||
$settings = new FactorySettings(2, 0.1, 10);
|
||||
$settings->setClientOptions(['method' => 'POST']);
|
||||
$circuitBreaker = $circuitBreakerFactory->create($settings);
|
||||
$response = $circuitBreaker->call('https://api.domain.com/create/user', ['body' => ['firstname' => 'John', 'lastname' => 'Doe']]);
|
||||
```
|
||||
|
||||
> For the Guzzle implementation, the Client options are described
|
||||
> in the [HttpGuzzle documentation](http://docs.guzzlephp.org/en/stable/index.html).
|
||||
|
||||
### Advanced Circuit Breaker
|
||||
|
||||
If you need more control on your circuit breaker, you should use the `AdvancedCircuitBreaker` which manages more features:
|
||||
|
||||
* the **stripped failures**: define how many times we try to access the service when the circuit breaker is Half Open (when it retires to reach the service after it was unreachable);
|
||||
* the **stripped timeout**: define how much time we wait before consider the service unreachable (again in Half open state);
|
||||
* the **storage**: used to store the circuit breaker states and transitions. By default it's an `SimpleArray` so if you want to "cache" the fact that your service is unreachable you should use a persistent storage;
|
||||
* the **transition dispatcher**: used if you need to subscribe to transition events (ex: a dispatcher based on Symfony EventDispatcher is available)
|
||||
|
||||
#### Storage
|
||||
|
||||
```php
|
||||
use Doctrine\Common\Cache\FilesystemCache;
|
||||
use PrestaShop\CircuitBreaker\AdvancedCircuitBreakerFactory;
|
||||
use PrestaShop\CircuitBreaker\FactorySettings;
|
||||
use PrestaShop\CircuitBreaker\Storage\DoctrineCache;
|
||||
|
||||
$circuitBreakerFactory = new AdvancedCircuitBreakerFactory();
|
||||
$settings = new FactorySettings(2, 0.1, 60); //60 seconds threshold
|
||||
|
||||
//Once the circuit breaker is open, the fallback response will be returned instantly during the next 60 seconds
|
||||
//Since the state is persisted even other requests/processes will be aware that the circuit breaker is open
|
||||
$doctrineCache = new FilesystemCache(_PS_CACHE_DIR_ . '/addons_category');
|
||||
$storage = new DoctrineCache($doctrineCache);
|
||||
$settings->setStorage($storage);
|
||||
|
||||
$circuitBreaker = $circuitBreakerFactory->create($settings);
|
||||
$response = $circuitBreaker->call('https://unreachable.api.domain.com/create/user', []);
|
||||
```
|
||||
|
||||
### Guzzle Cache
|
||||
|
||||
Besides caching the circuit breaker state, it may be interesting to cache the successful responses. The circuit breaker library doesn't manage the cache itself,
|
||||
however you can use [Guzzle Subscriber](https://github.com/guzzle/cache-subscriber) to manage it.
|
||||
|
||||
```php
|
||||
use Doctrine\Common\Cache\FilesystemCache;
|
||||
use GuzzleHttp\Subscriber\Cache\CacheStorage;
|
||||
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;
|
||||
use PrestaShop\CircuitBreaker\FactorySettings;
|
||||
use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
|
||||
|
||||
$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
|
||||
$settings = new FactorySettings(2, 0.1, 60); //60 seconds threshold
|
||||
|
||||
//Guzzle subsriber is also compatible with doctrine cache, which is why we proposed a storage based on it, this allows you to use for storage and cache
|
||||
$doctrineCache = new FilesystemCache(_PS_CACHE_DIR_ . '/addons_category');
|
||||
//By default the ttl is defined by the response headers but you can set a default one
|
||||
$cacheStorage = new CacheStorage($doctrineCache, null, 60);
|
||||
$cacheSubscriber = new CacheSubscriber($cacheStorage, function (Request $request) { return true; });
|
||||
|
||||
$settings->setClientOptions(['subscribers' => [$cacheSubscriber]]);
|
||||
|
||||
$circuitBreaker = $circuitBreakerFactory->create($settings);
|
||||
$response = $circuitBreaker->call('https://api.domain.com/create/user', []);
|
||||
```
|
||||
|
||||
## Tests
|
||||
|
||||
```
|
||||
composer test
|
||||
```
|
||||
|
||||
## Code quality
|
||||
|
||||
```
|
||||
composer cs-fix && composer phpcb && composer psalm && composer phpcs
|
||||
```
|
||||
|
||||
We also use [PHPQA](https://github.com/EdgedesignCZ/phpqa#phpqa) to check the Code quality
|
||||
during the CI management of the contributions.
|
||||
|
||||
If you want to use it (using Docker):
|
||||
|
||||
```
|
||||
docker run --rm -u $UID -v $(pwd):/app eko3alpha/docker-phpqa --report --ignoredDirs vendor,tests
|
||||
```
|
||||
|
||||
If you want to use it (using Composer):
|
||||
|
||||
```
|
||||
composer global require edgedesign/phpqa=v1.20.0 --update-no-dev
|
||||
phpqa --report --ignoredDirs vendor,tests
|
||||
```
|
||||
66
modules/ps_mbo/vendor/prestashop/circuit-breaker/composer.json
vendored
Normal file
66
modules/ps_mbo/vendor/prestashop/circuit-breaker/composer.json
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"name": "prestashop/circuit-breaker",
|
||||
"description": "A circuit breaker implementation for PHP",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "PrestaShop SA",
|
||||
"email": "contact@prestashop.com"
|
||||
},
|
||||
{
|
||||
"name": "PrestaShop Community",
|
||||
"homepage": "http://contributors.prestashop.com/"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.6",
|
||||
"guzzlehttp/guzzle": "^5"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^2.12",
|
||||
"phpunit/phpunit": "^5.7.0",
|
||||
"doctrine/cache": "^1.6.0",
|
||||
"symfony/cache": "^3.4.0",
|
||||
"symfony/event-dispatcher": "^3.4",
|
||||
"vimeo/psalm": "^1.1",
|
||||
"squizlabs/php_codesniffer": "3.*"
|
||||
},
|
||||
"suggest": {
|
||||
"symfony/cache": "Allows use of Symfony Cache adapters to store transactions",
|
||||
"doctrine/cache": "Allows use of Doctrine Cache adapters to store transactions",
|
||||
"ext-apcu": "Allows use of APCu adapter (performant) to store transactions",
|
||||
"guzzlehttp/cache-subscriber": "Allow use of Guzzle cache (use dev-master for most recent changes)"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PrestaShop\\CircuitBreaker\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Tests\\PrestaShop\\CircuitBreaker\\": "tests/"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"psalm": [
|
||||
"@php ./vendor/bin/psalm --init",
|
||||
"@php ./vendor/bin/psalm --find-dead-code --threads=8 --diff"
|
||||
],
|
||||
"phpcs": "@php ./vendor/bin/phpcs -p --standard=PSR2 --ignore=\"vendor/*\" \"./\" --extensions=php",
|
||||
"phpcbf": "@php ./vendor/bin/phpcbf -p --standard=PSR2 --ignore=\"vendor/*\" \"./\" --extensions=php",
|
||||
"cs-fix": "@php ./vendor/bin/php-cs-fixer fix",
|
||||
"test": "@php ./vendor/bin/phpunit"
|
||||
},
|
||||
"scripts-descriptions": {
|
||||
"psalm": "Execute Psalm on PHP7.0+, you need to install it",
|
||||
"cs-fix": "Check and fix coding styles using PHP CS Fixer",
|
||||
"test": "Launch PHPUnit test suite"
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true,
|
||||
"platform": {
|
||||
"php": "5.6"
|
||||
}
|
||||
}
|
||||
}
|
||||
6
modules/ps_mbo/vendor/prestashop/circuit-breaker/extension.neon
vendored
Normal file
6
modules/ps_mbo/vendor/prestashop/circuit-breaker/extension.neon
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
parameters:
|
||||
ignoreErrors:
|
||||
# Can't use the PHPUnit extension on PHP 5.6, ignore Mocks false positive
|
||||
- '#Access to an undefined property PHPUnit_Framework_MockObject_Invocation#'
|
||||
- '#should return PHPUnit_Framework_MockObject_MockObject#'
|
||||
- '#PHPUnit_Framework_MockObject_MockObject given#'
|
||||
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/phpunit.xml.dist
vendored
Normal file
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/phpunit.xml.dist
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<phpunit colors="true" bootstrap="./vendor/autoload.php">
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="PrestaShop Circuit Breaker Test suite">
|
||||
<directory>./tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
|
||||
</phpunit>
|
||||
55
modules/ps_mbo/vendor/prestashop/circuit-breaker/psalm.xml.dist
vendored
Normal file
55
modules/ps_mbo/vendor/prestashop/circuit-breaker/psalm.xml.dist
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0"?>
|
||||
<psalm
|
||||
name="Psalm configuration for the PrestaShop Circuit Breaker"
|
||||
totallyTyped="false"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="https://getpsalm.org/schema/config"
|
||||
xsi:schemaLocation="https://getpsalm.org/schema/config file:///home/dev/Projects/circuit-breaker/vendor/vimeo/psalm/config.xsd"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="src" />
|
||||
<ignoreFiles>
|
||||
<directory name="vendor" />
|
||||
<directory name="tests" />
|
||||
</ignoreFiles>
|
||||
</projectFiles>
|
||||
|
||||
<issueHandlers>
|
||||
<LessSpecificReturnType errorLevel="info" />
|
||||
|
||||
<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->
|
||||
|
||||
<DeprecatedMethod errorLevel="info" />
|
||||
<DeprecatedProperty errorLevel="info" />
|
||||
<DeprecatedClass errorLevel="info" />
|
||||
<DeprecatedConstant errorLevel="info" />
|
||||
<DeprecatedInterface errorLevel="info" />
|
||||
<DeprecatedTrait errorLevel="info" />
|
||||
|
||||
<InternalMethod errorLevel="info" />
|
||||
<InternalProperty errorLevel="info" />
|
||||
<InternalClass errorLevel="info" />
|
||||
|
||||
<MissingClosureReturnType errorLevel="info" />
|
||||
<MissingReturnType errorLevel="info" />
|
||||
<MissingPropertyType errorLevel="info" />
|
||||
<InvalidDocblock errorLevel="info" />
|
||||
<MisplacedRequiredParam errorLevel="info" />
|
||||
|
||||
<PropertyNotSetInConstructor errorLevel="info" />
|
||||
<MissingConstructor errorLevel="info" />
|
||||
<MissingClosureParamType errorLevel="info" />
|
||||
<MissingParamType errorLevel="info" />
|
||||
|
||||
<RedundantCondition errorLevel="info" />
|
||||
|
||||
<DocblockTypeContradiction errorLevel="info" />
|
||||
<RedundantConditionGivenDocblockType errorLevel="info" />
|
||||
|
||||
<UnresolvableInclude errorLevel="info" />
|
||||
|
||||
<RawObjectIteration errorLevel="info" />
|
||||
|
||||
<InvalidStringClass errorLevel="info" />
|
||||
</issueHandlers>
|
||||
</psalm>
|
||||
176
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/AdvancedCircuitBreaker.php
vendored
Normal file
176
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/AdvancedCircuitBreaker.php
vendored
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\ClientInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\SystemInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransitionDispatcherInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\UnavailableServiceException;
|
||||
|
||||
/**
|
||||
* This implementation of the CircuitBreaker is a bit more advanced than the SimpleCircuitBreaker,
|
||||
* it allows you to setup your client, system, storage and dispatcher.
|
||||
*/
|
||||
class AdvancedCircuitBreaker extends PartialCircuitBreaker
|
||||
{
|
||||
/** @var TransitionDispatcherInterface */
|
||||
protected $dispatcher;
|
||||
|
||||
/** @var callable|null */
|
||||
protected $defaultFallback;
|
||||
|
||||
/**
|
||||
* @param SystemInterface $system
|
||||
* @param ClientInterface $client
|
||||
* @param StorageInterface $storage
|
||||
* @param TransitionDispatcherInterface $dispatcher
|
||||
*/
|
||||
public function __construct(
|
||||
SystemInterface $system,
|
||||
ClientInterface $client,
|
||||
StorageInterface $storage,
|
||||
TransitionDispatcherInterface $dispatcher
|
||||
) {
|
||||
parent::__construct($system, $client, $storage);
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function call(
|
||||
$service,
|
||||
array $serviceParameters = [],
|
||||
callable $fallback = null
|
||||
) {
|
||||
$transaction = $this->initTransaction($service);
|
||||
|
||||
try {
|
||||
if ($this->isOpened()) {
|
||||
if (!$this->canAccessService($transaction)) {
|
||||
return $this->callFallback($fallback);
|
||||
}
|
||||
|
||||
$this->moveStateTo(State::HALF_OPEN_STATE, $service);
|
||||
$this->dispatchTransition(
|
||||
Transition::CHECKING_AVAILABILITY_TRANSITION,
|
||||
$service,
|
||||
$serviceParameters
|
||||
);
|
||||
}
|
||||
|
||||
$response = $this->request($service, $serviceParameters);
|
||||
$this->moveStateTo(State::CLOSED_STATE, $service);
|
||||
$this->dispatchTransition(
|
||||
Transition::CLOSING_TRANSITION,
|
||||
$service,
|
||||
$serviceParameters
|
||||
);
|
||||
|
||||
return $response;
|
||||
} catch (UnavailableServiceException $exception) {
|
||||
$transaction->incrementFailures();
|
||||
$this->storage->saveTransaction($service, $transaction);
|
||||
if (!$this->isAllowedToRetry($transaction)) {
|
||||
$this->moveStateTo(State::OPEN_STATE, $service);
|
||||
$transition = $this->isHalfOpened() ? Transition::REOPENING_TRANSITION : Transition::OPENING_TRANSITION;
|
||||
$this->dispatchTransition($transition, $service, $serviceParameters);
|
||||
|
||||
return $this->callFallback($fallback);
|
||||
}
|
||||
|
||||
return $this->call(
|
||||
$service,
|
||||
$serviceParameters,
|
||||
$fallback
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return callable|null
|
||||
*/
|
||||
public function getDefaultFallback()
|
||||
{
|
||||
return $this->defaultFallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $defaultFallback|null
|
||||
*
|
||||
* @return AdvancedCircuitBreaker
|
||||
*/
|
||||
public function setDefaultFallback(callable $defaultFallback = null)
|
||||
{
|
||||
$this->defaultFallback = $defaultFallback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function callFallback(callable $fallback = null)
|
||||
{
|
||||
return parent::callFallback(null !== $fallback ? $fallback : $this->defaultFallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $transition
|
||||
* @param string $service
|
||||
* @param array $serviceParameters
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatchTransition($transition, $service, array $serviceParameters)
|
||||
{
|
||||
$this->dispatcher->dispatchTransition($transition, $service, $serviceParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function initTransaction($service)
|
||||
{
|
||||
if (!$this->storage->hasTransaction($service)) {
|
||||
$this->dispatchTransition(Transition::INITIATING_TRANSITION, $service, []);
|
||||
}
|
||||
|
||||
return parent::initTransaction($service);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function request($service, array $parameters = [])
|
||||
{
|
||||
$this->dispatchTransition(Transition::TRIAL_TRANSITION, $service, $parameters);
|
||||
|
||||
return parent::request($service, $parameters);
|
||||
}
|
||||
}
|
||||
53
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/AdvancedCircuitBreakerFactory.php
vendored
Normal file
53
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/AdvancedCircuitBreakerFactory.php
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\ClientInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\FactoryInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\FactorySettingsInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransitionDispatcherInterface;
|
||||
use PrestaShop\CircuitBreaker\Place\ClosedPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\HalfOpenPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\OpenPlace;
|
||||
use PrestaShop\CircuitBreaker\Client\GuzzleClient;
|
||||
use PrestaShop\CircuitBreaker\Storage\SimpleArray;
|
||||
use PrestaShop\CircuitBreaker\System\MainSystem;
|
||||
use PrestaShop\CircuitBreaker\Transition\NullDispatcher;
|
||||
|
||||
/**
|
||||
* Advanced implementation of Circuit Breaker Factory
|
||||
* Used to create an AdvancedCircuitBreaker instance.
|
||||
*/
|
||||
final class AdvancedCircuitBreakerFactory implements FactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create(FactorySettingsInterface $settings)
|
||||
{
|
||||
$closedPlace = new ClosedPlace($settings->getFailures(), $settings->getTimeout(), 0);
|
||||
$openPlace = new OpenPlace(0, 0, $settings->getThreshold());
|
||||
$halfOpenPlace = new HalfOpenPlace($settings->getFailures(), $settings->getStrippedTimeout(), 0);
|
||||
$system = new MainSystem($closedPlace, $halfOpenPlace, $openPlace);
|
||||
|
||||
/** @var ClientInterface $client */
|
||||
$client = $settings->getClient() ?: new GuzzleClient($settings->getClientOptions());
|
||||
/** @var StorageInterface $storage */
|
||||
$storage = $settings->getStorage() ?: new SimpleArray();
|
||||
/** @var TransitionDispatcherInterface $dispatcher */
|
||||
$dispatcher = $settings->getDispatcher() ?: new NullDispatcher();
|
||||
|
||||
$circuitBreaker = new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$client,
|
||||
$storage,
|
||||
$dispatcher
|
||||
);
|
||||
if (null !== $settings->getDefaultFallback()) {
|
||||
$circuitBreaker->setDefaultFallback($settings->getDefaultFallback());
|
||||
}
|
||||
|
||||
return $circuitBreaker;
|
||||
}
|
||||
}
|
||||
122
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Client/GuzzleClient.php
vendored
Normal file
122
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Client/GuzzleClient.php
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Client;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Client as OriginalGuzzleClient;
|
||||
use GuzzleHttp\Subscriber\Mock;
|
||||
use PrestaShop\CircuitBreaker\Contract\ClientInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\UnavailableServiceException;
|
||||
use PrestaShop\CircuitBreaker\Exception\UnsupportedMethodException;
|
||||
|
||||
/**
|
||||
* Guzzle implementation of client.
|
||||
* The possibility of extending this client is intended.
|
||||
*/
|
||||
class GuzzleClient implements ClientInterface
|
||||
{
|
||||
/**
|
||||
* @var string by default, calls are sent using GET method
|
||||
*/
|
||||
const DEFAULT_METHOD = 'GET';
|
||||
|
||||
/**
|
||||
* Supported HTTP methods
|
||||
*/
|
||||
const SUPPORTED_METHODS = [
|
||||
'GET' => true,
|
||||
'HEAD' => true,
|
||||
'POST' => true,
|
||||
'PUT' => true,
|
||||
'DELETE' => true,
|
||||
'OPTIONS' => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array the Client default options
|
||||
*/
|
||||
private $defaultOptions;
|
||||
|
||||
public function __construct(array $defaultOptions = [])
|
||||
{
|
||||
$this->defaultOptions = $defaultOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws UnavailableServiceException
|
||||
*/
|
||||
public function request($resource, array $options)
|
||||
{
|
||||
try {
|
||||
$options = array_merge($this->defaultOptions, $options);
|
||||
$client = $this->buildClient($options);
|
||||
$method = $this->getHttpMethod($options);
|
||||
$options['exceptions'] = true;
|
||||
|
||||
// prevents unhandled method errors in Guzzle 5
|
||||
unset($options['method'], $options['mock']);
|
||||
|
||||
$request = $client->createRequest($method, $resource, $options);
|
||||
|
||||
return (string) $client->send($request)->getBody();
|
||||
} catch (Exception $e) {
|
||||
throw new UnavailableServiceException($e->getMessage(), (int) $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options the list of options
|
||||
*
|
||||
* @return string the method
|
||||
*
|
||||
* @throws UnsupportedMethodException
|
||||
*/
|
||||
private function getHttpMethod(array $options)
|
||||
{
|
||||
if (isset($options['method'])) {
|
||||
if (!array_key_exists($options['method'], self::SUPPORTED_METHODS)) {
|
||||
throw UnsupportedMethodException::unsupportedMethod($options['method']);
|
||||
}
|
||||
|
||||
return $options['method'];
|
||||
}
|
||||
|
||||
return self::DEFAULT_METHOD;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
*
|
||||
* @return OriginalGuzzleClient
|
||||
*/
|
||||
private function buildClient(array $options)
|
||||
{
|
||||
if (isset($options['mock']) && $options['mock'] instanceof Mock) {
|
||||
return $this->buildMockClient($options);
|
||||
}
|
||||
|
||||
return new OriginalGuzzleClient($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a client with a mock
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return OriginalGuzzleClient
|
||||
*/
|
||||
private function buildMockClient(array $options)
|
||||
{
|
||||
/** @var Mock $mock */
|
||||
$mock = $options['mock'];
|
||||
unset($options['mock']);
|
||||
|
||||
$client = new OriginalGuzzleClient($options);
|
||||
|
||||
$client->getEmitter()->attach($mock);
|
||||
|
||||
return $client;
|
||||
}
|
||||
}
|
||||
42
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/CircuitBreakerInterface.php
vendored
Normal file
42
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/CircuitBreakerInterface.php
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
/**
|
||||
* A circuit breaker is used to provide
|
||||
* an alternative response when a tiers service
|
||||
* is unreachable.
|
||||
*/
|
||||
interface CircuitBreakerInterface
|
||||
{
|
||||
/**
|
||||
* @return string the circuit breaker state
|
||||
*/
|
||||
public function getState();
|
||||
|
||||
/**
|
||||
* The function that execute the service.
|
||||
*
|
||||
* @param string $service the service to call
|
||||
* @param array $parameters the parameters for the request
|
||||
* @param callable|null $fallback if the service is unavailable, rely on the fallback
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function call($service, array $parameters = [], callable $fallback = null);
|
||||
|
||||
/**
|
||||
* @return bool checks if the circuit breaker is open
|
||||
*/
|
||||
public function isOpened();
|
||||
|
||||
/**
|
||||
* @return bool checks if the circuit breaker is half open
|
||||
*/
|
||||
public function isHalfOpened();
|
||||
|
||||
/**
|
||||
* @return bool checks if the circuit breaker is closed
|
||||
*/
|
||||
public function isClosed();
|
||||
}
|
||||
18
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/ClientInterface.php
vendored
Normal file
18
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/ClientInterface.php
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
/**
|
||||
* In charge of calling the resource and return a response.
|
||||
* Must throw UnavailableService exception if not reachable.
|
||||
*/
|
||||
interface ClientInterface
|
||||
{
|
||||
/**
|
||||
* @param string $resource the URI of the service to be reached
|
||||
* @param array $options the options if needed
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function request($resource, array $options);
|
||||
}
|
||||
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/FactoryInterface.php
vendored
Normal file
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/FactoryInterface.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
/**
|
||||
* Ease the creation of the Circuit Breaker.
|
||||
*/
|
||||
interface FactoryInterface
|
||||
{
|
||||
/**
|
||||
* @param FactorySettingsInterface $settings the settings for the Place
|
||||
*
|
||||
* @return CircuitBreakerInterface
|
||||
*/
|
||||
public function create(FactorySettingsInterface $settings);
|
||||
}
|
||||
91
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/FactorySettingsInterface.php
vendored
Normal file
91
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/FactorySettingsInterface.php
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
/**
|
||||
* Interface FactorySettingsInterface contains the settings used by the Factory
|
||||
*/
|
||||
interface FactorySettingsInterface
|
||||
{
|
||||
/**
|
||||
* @param FactorySettingsInterface $settingsA
|
||||
* @param FactorySettingsInterface $settingsB
|
||||
*
|
||||
* @return FactorySettingsInterface
|
||||
*/
|
||||
public static function merge(FactorySettingsInterface $settingsA, FactorySettingsInterface $settingsB);
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getFailures();
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getTimeout();
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getThreshold();
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getStrippedTimeout();
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStrippedFailures();
|
||||
|
||||
/**
|
||||
* @return StorageInterface|null
|
||||
*/
|
||||
public function getStorage();
|
||||
|
||||
/**
|
||||
* @return TransitionDispatcherInterface|null
|
||||
*/
|
||||
public function getDispatcher();
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getClientOptions();
|
||||
|
||||
/**
|
||||
* @return ClientInterface|null
|
||||
*/
|
||||
public function getClient();
|
||||
|
||||
/**
|
||||
* @return callable|null
|
||||
*/
|
||||
public function getDefaultFallback();
|
||||
}
|
||||
33
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/PlaceInterface.php
vendored
Normal file
33
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/PlaceInterface.php
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
/**
|
||||
* A circuit breaker can be in 3 places:
|
||||
* closed, half open or open. Each place have its
|
||||
* own properties and behaviors.
|
||||
*/
|
||||
interface PlaceInterface
|
||||
{
|
||||
/**
|
||||
* Return the current state of the Circuit Breaker.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getState();
|
||||
|
||||
/**
|
||||
* @return int the number of failures
|
||||
*/
|
||||
public function getFailures();
|
||||
|
||||
/**
|
||||
* @return int the allowed number of trials
|
||||
*/
|
||||
public function getThreshold();
|
||||
|
||||
/**
|
||||
* @return float the allowed timeout
|
||||
*/
|
||||
public function getTimeout();
|
||||
}
|
||||
49
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/StorageInterface.php
vendored
Normal file
49
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/StorageInterface.php
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Exception\TransactionNotFoundException;
|
||||
|
||||
/**
|
||||
* Store the transaction between the Circuit Breaker
|
||||
* and the tiers service.
|
||||
*/
|
||||
interface StorageInterface
|
||||
{
|
||||
/**
|
||||
* Save the CircuitBreaker transaction.
|
||||
*
|
||||
* @param string $service The service name
|
||||
* @param TransactionInterface $transaction the transaction
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function saveTransaction($service, TransactionInterface $transaction);
|
||||
|
||||
/**
|
||||
* Retrieve the CircuitBreaker transaction for a specific service.
|
||||
*
|
||||
* @param string $service the service name
|
||||
*
|
||||
* @return TransactionInterface
|
||||
*
|
||||
* @throws TransactionNotFoundException
|
||||
*/
|
||||
public function getTransaction($service);
|
||||
|
||||
/**
|
||||
* Checks if the transaction exists.
|
||||
*
|
||||
* @param string $service the service name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasTransaction($service);
|
||||
|
||||
/**
|
||||
* Clear the Circuit Breaker storage.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clear();
|
||||
}
|
||||
20
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/SystemInterface.php
vendored
Normal file
20
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/SystemInterface.php
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
/**
|
||||
* The System define the places available
|
||||
* for the Circuit Breaker and the initial Place.
|
||||
*/
|
||||
interface SystemInterface
|
||||
{
|
||||
/**
|
||||
* @return PlaceInterface[] the list of places of the system
|
||||
*/
|
||||
public function getPlaces();
|
||||
|
||||
/**
|
||||
* @return PlaceInterface the initial place of the system
|
||||
*/
|
||||
public function getInitialPlace();
|
||||
}
|
||||
40
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/TransactionInterface.php
vendored
Normal file
40
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/TransactionInterface.php
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* Once the circuit breaker call a service,
|
||||
* a transaction is initialized and stored.
|
||||
*/
|
||||
interface TransactionInterface
|
||||
{
|
||||
/**
|
||||
* @return string the service name
|
||||
*/
|
||||
public function getService();
|
||||
|
||||
/**
|
||||
* @return int the number of failures to call the service
|
||||
*/
|
||||
public function getFailures();
|
||||
|
||||
/**
|
||||
* @return string the current state of the Circuit Breaker
|
||||
*/
|
||||
public function getState();
|
||||
|
||||
/**
|
||||
* @return DateTime the time when the circuit breaker move
|
||||
* from open to half open state
|
||||
*/
|
||||
public function getThresholdDateTime();
|
||||
|
||||
/**
|
||||
* Everytime the service call fails, increment the number of failures.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function incrementFailures();
|
||||
}
|
||||
42
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/TransitionDispatcherInterface.php
vendored
Normal file
42
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Contract/TransitionDispatcherInterface.php
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Contract;
|
||||
|
||||
/**
|
||||
* This interface is used for a circuit breaker to dispatch its state transitions.
|
||||
*/
|
||||
interface TransitionDispatcherInterface
|
||||
{
|
||||
/**
|
||||
* @param string $transition the transition name
|
||||
* @param string $service the Service URI
|
||||
* @param array $serviceParameters the Service parameters
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function dispatchTransition($transition, $service, array $serviceParameters);
|
||||
}
|
||||
59
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Event/TransitionEvent.php
vendored
Normal file
59
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Event/TransitionEvent.php
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Event;
|
||||
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
|
||||
class TransitionEvent extends Event
|
||||
{
|
||||
/**
|
||||
* @var string the Transition name
|
||||
*/
|
||||
private $eventName;
|
||||
|
||||
/**
|
||||
* @var string the Service URI
|
||||
*/
|
||||
private $service;
|
||||
|
||||
/**
|
||||
* @var array the Service parameters
|
||||
*/
|
||||
private $parameters;
|
||||
|
||||
/**
|
||||
* @param string $eventName the transition name
|
||||
* @param string $service the Service URI
|
||||
* @param array $parameters the Service parameters
|
||||
*/
|
||||
public function __construct($eventName, $service, array $parameters)
|
||||
{
|
||||
$this->eventName = $eventName;
|
||||
$this->service = $service;
|
||||
$this->parameters = $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string the Transition name
|
||||
*/
|
||||
public function getEvent()
|
||||
{
|
||||
return $this->eventName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string the Service URI
|
||||
*/
|
||||
public function getService()
|
||||
{
|
||||
return $this->service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array the Service parameters
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
}
|
||||
12
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Exception/CircuitBreakerException.php
vendored
Normal file
12
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Exception/CircuitBreakerException.php
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Base exception for Circuit Breaker exceptions
|
||||
*/
|
||||
class CircuitBreakerException extends Exception
|
||||
{
|
||||
}
|
||||
26
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Exception/InvalidPlaceException.php
vendored
Normal file
26
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Exception/InvalidPlaceException.php
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Exception;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Util\ErrorFormatter;
|
||||
|
||||
final class InvalidPlaceException extends CircuitBreakerException
|
||||
{
|
||||
/**
|
||||
* @param mixed $failures the failures
|
||||
* @param mixed $timeout the timeout
|
||||
* @param mixed $threshold the threshold
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function invalidSettings($failures, $timeout, $threshold)
|
||||
{
|
||||
$exceptionMessage = 'Invalid settings for Place' . PHP_EOL .
|
||||
ErrorFormatter::format('failures', $failures, 'isPositiveInteger', 'a positive integer') .
|
||||
ErrorFormatter::format('timeout', $timeout, 'isPositiveValue', 'a float') .
|
||||
ErrorFormatter::format('threshold', $threshold, 'isPositiveInteger', 'a positive integer')
|
||||
;
|
||||
|
||||
return new self($exceptionMessage);
|
||||
}
|
||||
}
|
||||
28
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Exception/InvalidTransactionException.php
vendored
Normal file
28
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Exception/InvalidTransactionException.php
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Exception;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Util\ErrorFormatter;
|
||||
|
||||
final class InvalidTransactionException extends CircuitBreakerException
|
||||
{
|
||||
/**
|
||||
* @param mixed $service the service URI
|
||||
* @param mixed $failures the failures
|
||||
* @param mixed $state the Circuit Breaker
|
||||
* @param mixed $threshold the threshold
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function invalidParameters($service, $failures, $state, $threshold)
|
||||
{
|
||||
$exceptionMessage = 'Invalid parameters for Transaction' . PHP_EOL .
|
||||
ErrorFormatter::format('service', $service, 'isURI', 'an URI') .
|
||||
ErrorFormatter::format('failures', $failures, 'isPositiveInteger', 'a positive integer') .
|
||||
ErrorFormatter::format('state', $state, 'isString', 'a string') .
|
||||
ErrorFormatter::format('threshold', $threshold, 'isPositiveInteger', 'a positive integer')
|
||||
;
|
||||
|
||||
return new self($exceptionMessage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Exception;
|
||||
|
||||
final class TransactionNotFoundException extends CircuitBreakerException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Exception;
|
||||
|
||||
final class UnavailableServiceException extends CircuitBreakerException
|
||||
{
|
||||
}
|
||||
19
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Exception/UnsupportedMethodException.php
vendored
Normal file
19
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Exception/UnsupportedMethodException.php
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Exception;
|
||||
|
||||
/**
|
||||
* Used when trying to use an unsupported HTTP method
|
||||
*/
|
||||
class UnsupportedMethodException extends CircuitBreakerException
|
||||
{
|
||||
/**
|
||||
* @param string $methodName
|
||||
*
|
||||
* @return UnsupportedMethodException
|
||||
*/
|
||||
public static function unsupportedMethod($methodName)
|
||||
{
|
||||
return new static(sprintf('Unsupported method: "%s"', $methodName));
|
||||
}
|
||||
}
|
||||
313
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/FactorySettings.php
vendored
Normal file
313
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/FactorySettings.php
vendored
Normal file
@@ -0,0 +1,313 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\ClientInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\FactorySettingsInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransitionDispatcherInterface;
|
||||
|
||||
/**
|
||||
* Class FactorySettings is a simple implementation of FactorySettingsInterface, it is mainly
|
||||
* a settings container and can be used with any Factory class.
|
||||
*/
|
||||
class FactorySettings implements FactorySettingsInterface
|
||||
{
|
||||
/** @var int */
|
||||
private $failures;
|
||||
|
||||
/** @var float */
|
||||
private $timeout;
|
||||
|
||||
/** @var int */
|
||||
private $threshold;
|
||||
|
||||
/** @var float */
|
||||
private $strippedTimeout;
|
||||
|
||||
/** @var int */
|
||||
private $strippedFailures;
|
||||
|
||||
/** @var StorageInterface|null */
|
||||
private $storage;
|
||||
|
||||
/** @var TransitionDispatcherInterface|null */
|
||||
private $dispatcher;
|
||||
|
||||
/** @var array */
|
||||
private $clientOptions = [];
|
||||
|
||||
/** @var ClientInterface|null */
|
||||
private $client;
|
||||
|
||||
/** @var callable|null */
|
||||
private $defaultFallback;
|
||||
|
||||
/**
|
||||
* @param int $failures
|
||||
* @param float $timeout
|
||||
* @param int $threshold
|
||||
*/
|
||||
public function __construct(
|
||||
$failures,
|
||||
$timeout,
|
||||
$threshold
|
||||
) {
|
||||
$this->failures = $this->strippedFailures = $failures;
|
||||
$this->timeout = $this->strippedTimeout = $timeout;
|
||||
$this->threshold = $threshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function merge(FactorySettingsInterface $settingsA, FactorySettingsInterface $settingsB)
|
||||
{
|
||||
$mergedSettings = new FactorySettings(
|
||||
$settingsB->getFailures(),
|
||||
$settingsB->getTimeout(),
|
||||
$settingsB->getThreshold()
|
||||
);
|
||||
$mergedSettings
|
||||
->setStrippedFailures($settingsB->getStrippedFailures())
|
||||
->setStrippedTimeout($settingsB->getStrippedTimeout())
|
||||
;
|
||||
|
||||
$mergedSettings->setClientOptions(array_merge(
|
||||
$settingsA->getClientOptions(),
|
||||
$settingsB->getClientOptions()
|
||||
));
|
||||
|
||||
if (null !== $settingsB->getClient()) {
|
||||
$mergedSettings->setClient($settingsB->getClient());
|
||||
} elseif (null !== $settingsA->getClient()) {
|
||||
$mergedSettings->setClient($settingsA->getClient());
|
||||
}
|
||||
|
||||
return $mergedSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFailures()
|
||||
{
|
||||
return $this->failures;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $failures
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setFailures($failures)
|
||||
{
|
||||
$this->failures = $failures;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTimeout()
|
||||
{
|
||||
return $this->timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $timeout
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setTimeout($timeout)
|
||||
{
|
||||
$this->timeout = $timeout;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getThreshold()
|
||||
{
|
||||
return $this->threshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $threshold
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setThreshold($threshold)
|
||||
{
|
||||
$this->threshold = $threshold;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStrippedTimeout()
|
||||
{
|
||||
return $this->strippedTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $strippedTimeout
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setStrippedTimeout($strippedTimeout)
|
||||
{
|
||||
$this->strippedTimeout = $strippedTimeout;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStrippedFailures()
|
||||
{
|
||||
return $this->strippedFailures;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $strippedFailures
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setStrippedFailures($strippedFailures)
|
||||
{
|
||||
$this->strippedFailures = $strippedFailures;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getStorage()
|
||||
{
|
||||
return $this->storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StorageInterface $storage
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setStorage(StorageInterface $storage)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDispatcher()
|
||||
{
|
||||
return $this->dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransitionDispatcherInterface $dispatcher
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setDispatcher(TransitionDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getClientOptions()
|
||||
{
|
||||
return $this->clientOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $clientOptions
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setClientOptions(array $clientOptions)
|
||||
{
|
||||
$this->clientOptions = $clientOptions;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClientInterface|null $client
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setClient(ClientInterface $client = null)
|
||||
{
|
||||
$this->client = $client;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultFallback()
|
||||
{
|
||||
return $this->defaultFallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $defaultFallback
|
||||
*
|
||||
* @return FactorySettings
|
||||
*/
|
||||
public function setDefaultFallback(callable $defaultFallback)
|
||||
{
|
||||
$this->defaultFallback = $defaultFallback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
183
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/PartialCircuitBreaker.php
vendored
Normal file
183
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/PartialCircuitBreaker.php
vendored
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Transaction\SimpleTransaction;
|
||||
use PrestaShop\CircuitBreaker\Contract\CircuitBreakerInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransactionInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\SystemInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\ClientInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\PlaceInterface;
|
||||
use DateTime;
|
||||
|
||||
abstract class PartialCircuitBreaker implements CircuitBreakerInterface
|
||||
{
|
||||
/**
|
||||
* @param SystemInterface $system
|
||||
* @param ClientInterface $client
|
||||
* @param StorageInterface $storage
|
||||
*/
|
||||
public function __construct(
|
||||
SystemInterface $system,
|
||||
ClientInterface $client,
|
||||
StorageInterface $storage
|
||||
) {
|
||||
$this->currentPlace = $system->getInitialPlace();
|
||||
$this->places = $system->getPlaces();
|
||||
$this->client = $client;
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var ClientInterface the Client that consumes the service URI
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* @var PlaceInterface the current Place of the Circuit Breaker
|
||||
*/
|
||||
protected $currentPlace;
|
||||
|
||||
/**
|
||||
* @var PlaceInterface[] the Circuit Breaker places
|
||||
*/
|
||||
protected $places = [];
|
||||
|
||||
/**
|
||||
* @var StorageInterface the Circuit Breaker storage
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
abstract public function call($service, array $serviceParameters = [], callable $fallback = null);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return $this->currentPlace->getState();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isOpened()
|
||||
{
|
||||
return State::OPEN_STATE === $this->currentPlace->getState();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isHalfOpened()
|
||||
{
|
||||
return State::HALF_OPEN_STATE === $this->currentPlace->getState();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isClosed()
|
||||
{
|
||||
return State::CLOSED_STATE === $this->currentPlace->getState();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable|null $fallback
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function callFallback(callable $fallback = null)
|
||||
{
|
||||
if (null === $fallback) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return call_user_func($fallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $state the Place state
|
||||
* @param string $service the service URI
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function moveStateTo($state, $service)
|
||||
{
|
||||
$this->currentPlace = $this->places[$state];
|
||||
$transaction = SimpleTransaction::createFromPlace(
|
||||
$this->currentPlace,
|
||||
$service
|
||||
);
|
||||
|
||||
return $this->storage->saveTransaction($service, $transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $service the service URI
|
||||
*
|
||||
* @return TransactionInterface
|
||||
*/
|
||||
protected function initTransaction($service)
|
||||
{
|
||||
if ($this->storage->hasTransaction($service)) {
|
||||
$transaction = $this->storage->getTransaction($service);
|
||||
// CircuitBreaker needs to be in the same state as its last transaction
|
||||
if ($this->getState() !== $transaction->getState()) {
|
||||
$this->currentPlace = $this->places[$transaction->getState()];
|
||||
}
|
||||
} else {
|
||||
$transaction = SimpleTransaction::createFromPlace(
|
||||
$this->currentPlace,
|
||||
$service
|
||||
);
|
||||
|
||||
$this->storage->saveTransaction($service, $transaction);
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionInterface $transaction the Transaction
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isAllowedToRetry(TransactionInterface $transaction)
|
||||
{
|
||||
return $transaction->getFailures() < $this->currentPlace->getFailures();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionInterface $transaction the Transaction
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function canAccessService(TransactionInterface $transaction)
|
||||
{
|
||||
return $transaction->getThresholdDateTime() < new DateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the client with the right information.
|
||||
*
|
||||
* @param string $service the service URI
|
||||
* @param array $parameters the service URI parameters
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function request($service, array $parameters = [])
|
||||
{
|
||||
return $this->client->request(
|
||||
$service,
|
||||
array_merge($parameters, [
|
||||
'connect_timeout' => $this->currentPlace->getTimeout(),
|
||||
'timeout' => $this->currentPlace->getTimeout(),
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
95
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Place/AbstractPlace.php
vendored
Normal file
95
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Place/AbstractPlace.php
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Place;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\PlaceInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\InvalidPlaceException;
|
||||
use PrestaShop\CircuitBreaker\Util\Assert;
|
||||
|
||||
abstract class AbstractPlace implements PlaceInterface
|
||||
{
|
||||
/**
|
||||
* @var int the Place failures
|
||||
*/
|
||||
private $failures;
|
||||
|
||||
/**
|
||||
* @var float the Place timeout
|
||||
*/
|
||||
private $timeout;
|
||||
|
||||
/**
|
||||
* @var int the Place threshold
|
||||
*/
|
||||
private $threshold;
|
||||
|
||||
/**
|
||||
* @param int $failures the Place failures
|
||||
* @param float $timeout the Place timeout
|
||||
* @param int $threshold the Place threshold
|
||||
*
|
||||
* @throws InvalidPlaceException
|
||||
*/
|
||||
public function __construct($failures, $timeout, $threshold)
|
||||
{
|
||||
$this->validate($failures, $timeout, $threshold);
|
||||
|
||||
$this->failures = $failures;
|
||||
$this->timeout = $timeout;
|
||||
$this->threshold = $threshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
abstract public function getState();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFailures()
|
||||
{
|
||||
return $this->failures;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTimeout()
|
||||
{
|
||||
return $this->timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getThreshold()
|
||||
{
|
||||
return $this->threshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the place is valid (PHP5 is permissive).
|
||||
*
|
||||
* @param int $failures the failures should be a positive value
|
||||
* @param float $timeout the timeout should be a positive value
|
||||
* @param int $threshold the threshold should be a positive value
|
||||
*
|
||||
* @return bool true if valid
|
||||
*
|
||||
* @throws InvalidPlaceException
|
||||
*/
|
||||
private function validate($failures, $timeout, $threshold)
|
||||
{
|
||||
$assertionsAreValid = Assert::isPositiveInteger($failures)
|
||||
&& Assert::isPositiveValue($timeout)
|
||||
&& Assert::isPositiveInteger($threshold)
|
||||
;
|
||||
|
||||
if ($assertionsAreValid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
throw InvalidPlaceException::invalidSettings($failures, $timeout, $threshold);
|
||||
}
|
||||
}
|
||||
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Place/ClosedPlace.php
vendored
Normal file
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Place/ClosedPlace.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Place;
|
||||
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
|
||||
final class ClosedPlace extends AbstractPlace
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return State::CLOSED_STATE;
|
||||
}
|
||||
}
|
||||
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Place/HalfOpenPlace.php
vendored
Normal file
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Place/HalfOpenPlace.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Place;
|
||||
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
|
||||
final class HalfOpenPlace extends AbstractPlace
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return State::HALF_OPEN_STATE;
|
||||
}
|
||||
}
|
||||
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Place/OpenPlace.php
vendored
Normal file
16
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Place/OpenPlace.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Place;
|
||||
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
|
||||
final class OpenPlace extends AbstractPlace
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return State::OPEN_STATE;
|
||||
}
|
||||
}
|
||||
60
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/SimpleCircuitBreaker.php
vendored
Normal file
60
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/SimpleCircuitBreaker.php
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\PlaceInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\ClientInterface;
|
||||
use PrestaShop\CircuitBreaker\System\MainSystem;
|
||||
use PrestaShop\CircuitBreaker\Storage\SimpleArray;
|
||||
use PrestaShop\CircuitBreaker\Exception\UnavailableServiceException;
|
||||
|
||||
/**
|
||||
* Main implementation of Circuit Breaker.
|
||||
*/
|
||||
final class SimpleCircuitBreaker extends PartialCircuitBreaker
|
||||
{
|
||||
public function __construct(
|
||||
PlaceInterface $openPlace,
|
||||
PlaceInterface $halfOpenPlace,
|
||||
PlaceInterface $closedPlace,
|
||||
ClientInterface $client
|
||||
) {
|
||||
$system = new MainSystem($closedPlace, $halfOpenPlace, $openPlace);
|
||||
|
||||
parent::__construct($system, $client, new SimpleArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function call(
|
||||
$service,
|
||||
array $serviceParameters = [],
|
||||
callable $fallback = null
|
||||
) {
|
||||
$transaction = $this->initTransaction($service);
|
||||
try {
|
||||
if ($this->isOpened()) {
|
||||
if (!$this->canAccessService($transaction)) {
|
||||
return $this->callFallback($fallback);
|
||||
}
|
||||
|
||||
$this->moveStateTo(State::HALF_OPEN_STATE, $service);
|
||||
}
|
||||
$response = $this->request($service, $serviceParameters);
|
||||
$this->moveStateTo(State::CLOSED_STATE, $service);
|
||||
|
||||
return $response;
|
||||
} catch (UnavailableServiceException $exception) {
|
||||
$transaction->incrementFailures();
|
||||
$this->storage->saveTransaction($service, $transaction);
|
||||
if (!$this->isAllowedToRetry($transaction)) {
|
||||
$this->moveStateTo(State::OPEN_STATE, $service);
|
||||
|
||||
return $this->callFallback($fallback);
|
||||
}
|
||||
|
||||
return $this->call($service, $serviceParameters, $fallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/SimpleCircuitBreakerFactory.php
vendored
Normal file
38
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/SimpleCircuitBreakerFactory.php
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\ClientInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\FactoryInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\FactorySettingsInterface;
|
||||
use PrestaShop\CircuitBreaker\Place\ClosedPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\HalfOpenPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\OpenPlace;
|
||||
use PrestaShop\CircuitBreaker\Client\GuzzleClient;
|
||||
|
||||
/**
|
||||
* Main implementation of Circuit Breaker Factory
|
||||
* Used to create a SimpleCircuitBreaker instance.
|
||||
*/
|
||||
final class SimpleCircuitBreakerFactory implements FactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create(FactorySettingsInterface $settings)
|
||||
{
|
||||
$closedPlace = new ClosedPlace($settings->getFailures(), $settings->getTimeout(), 0);
|
||||
$openPlace = new OpenPlace(0, 0, $settings->getThreshold());
|
||||
$halfOpenPlace = new HalfOpenPlace($settings->getFailures(), $settings->getStrippedTimeout(), 0);
|
||||
|
||||
/** @var ClientInterface $client */
|
||||
$client = $settings->getClient() ?: new GuzzleClient($settings->getClientOptions());
|
||||
|
||||
return new SimpleCircuitBreaker(
|
||||
$openPlace,
|
||||
$halfOpenPlace,
|
||||
$closedPlace,
|
||||
$client
|
||||
);
|
||||
}
|
||||
}
|
||||
30
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/State.php
vendored
Normal file
30
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/State.php
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
/**
|
||||
* Define the available states of the Circuit Breaker;.
|
||||
*/
|
||||
final class State
|
||||
{
|
||||
/**
|
||||
* Once opened, a circuit breaker doesn't do any call
|
||||
* to third-party services. Only the alternative call is done.
|
||||
*/
|
||||
const OPEN_STATE = 'OPEN';
|
||||
|
||||
/**
|
||||
* After some conditions are valid, the circuit breaker
|
||||
* try to access the third-party service. If the service is valid,
|
||||
* the circuit breaker go to CLOSED state. If it's not, the circuit breaker
|
||||
* go to OPEN state.
|
||||
*/
|
||||
const HALF_OPEN_STATE = 'HALF OPEN';
|
||||
|
||||
/**
|
||||
* On the first call of the service, or if the service is valid
|
||||
* the circuit breaker is in CLOSED state. This means that the callable
|
||||
* to evaluate is done and not the alternative call.
|
||||
*/
|
||||
const CLOSED_STATE = 'CLOSED';
|
||||
}
|
||||
103
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Storage/DoctrineCache.php
vendored
Normal file
103
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Storage/DoctrineCache.php
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Storage;
|
||||
|
||||
use Doctrine\Common\Cache\CacheProvider;
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransactionInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\TransactionNotFoundException;
|
||||
|
||||
/**
|
||||
* Implementation of Storage using the Doctrine Cache.
|
||||
*/
|
||||
class DoctrineCache implements StorageInterface
|
||||
{
|
||||
/** @var CacheProvider */
|
||||
private $cacheProvider;
|
||||
|
||||
/**
|
||||
* @param CacheProvider $cacheProvider
|
||||
*/
|
||||
public function __construct(CacheProvider $cacheProvider)
|
||||
{
|
||||
$this->cacheProvider = $cacheProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function saveTransaction($service, TransactionInterface $transaction)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
return $this->cacheProvider->save($key, $transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTransaction($service)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
if ($this->hasTransaction($service)) {
|
||||
return $this->cacheProvider->fetch($key);
|
||||
}
|
||||
|
||||
throw new TransactionNotFoundException();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasTransaction($service)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
return $this->cacheProvider->contains($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
return $this->cacheProvider->deleteAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to properly store the transaction.
|
||||
*
|
||||
* @param string $service the service URI
|
||||
*
|
||||
* @return string the transaction unique identifier
|
||||
*/
|
||||
private function getKey($service)
|
||||
{
|
||||
return md5($service);
|
||||
}
|
||||
}
|
||||
76
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Storage/SimpleArray.php
vendored
Normal file
76
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Storage/SimpleArray.php
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Storage;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransactionInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\TransactionNotFoundException;
|
||||
|
||||
/**
|
||||
* Very simple implementation of Storage using a simple PHP array.
|
||||
*/
|
||||
final class SimpleArray implements StorageInterface
|
||||
{
|
||||
/**
|
||||
* @var array the circuit breaker transactions
|
||||
*/
|
||||
public static $transactions = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function saveTransaction($service, TransactionInterface $transaction)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
self::$transactions[$key] = $transaction;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTransaction($service)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
if ($this->hasTransaction($service)) {
|
||||
return self::$transactions[$key];
|
||||
}
|
||||
|
||||
throw new TransactionNotFoundException();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasTransaction($service)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
return array_key_exists($key, self::$transactions);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
self::$transactions = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to properly store the transaction.
|
||||
*
|
||||
* @param string $service the service URI
|
||||
*
|
||||
* @return string the transaction unique identifier
|
||||
*/
|
||||
private function getKey($service)
|
||||
{
|
||||
return md5($service);
|
||||
}
|
||||
}
|
||||
78
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Storage/SymfonyCache.php
vendored
Normal file
78
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Storage/SymfonyCache.php
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Storage;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransactionInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\TransactionNotFoundException;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
/**
|
||||
* Implementation of Storage using the Symfony Cache Component.
|
||||
*/
|
||||
final class SymfonyCache implements StorageInterface
|
||||
{
|
||||
/**
|
||||
* @var CacheInterface the Symfony Cache
|
||||
*/
|
||||
private $symfonyCache;
|
||||
|
||||
public function __construct(CacheInterface $symfonyCache)
|
||||
{
|
||||
$this->symfonyCache = $symfonyCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function saveTransaction($service, TransactionInterface $transaction)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
return $this->symfonyCache->set($key, $transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTransaction($service)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
if ($this->hasTransaction($service)) {
|
||||
return $this->symfonyCache->get($key);
|
||||
}
|
||||
|
||||
throw new TransactionNotFoundException();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasTransaction($service)
|
||||
{
|
||||
$key = $this->getKey($service);
|
||||
|
||||
return $this->symfonyCache->has($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
return $this->symfonyCache->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to properly store the transaction.
|
||||
*
|
||||
* @param string $service the service URI
|
||||
*
|
||||
* @return string the transaction unique identifier
|
||||
*/
|
||||
private function getKey($service)
|
||||
{
|
||||
return md5($service);
|
||||
}
|
||||
}
|
||||
37
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/SymfonyCircuitBreaker.php
vendored
Normal file
37
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/SymfonyCircuitBreaker.php
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\ClientInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\SystemInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Transition\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* Symfony implementation of Circuit Breaker.
|
||||
*/
|
||||
final class SymfonyCircuitBreaker extends AdvancedCircuitBreaker
|
||||
{
|
||||
/**
|
||||
* @var EventDispatcherInterface the Symfony Event Dispatcher
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
|
||||
/**
|
||||
* @param SystemInterface $system
|
||||
* @param ClientInterface $client
|
||||
* @param StorageInterface $storage
|
||||
* @param EventDispatcherInterface $eventDispatcher
|
||||
*/
|
||||
public function __construct(
|
||||
SystemInterface $system,
|
||||
ClientInterface $client,
|
||||
StorageInterface $storage,
|
||||
EventDispatcherInterface $eventDispatcher
|
||||
) {
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
|
||||
parent::__construct($system, $client, $storage, new EventDispatcher($eventDispatcher));
|
||||
}
|
||||
}
|
||||
50
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/System/MainSystem.php
vendored
Normal file
50
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/System/MainSystem.php
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\System;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\PlaceInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\SystemInterface;
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
|
||||
/**
|
||||
* Implement the system described by the documentation.
|
||||
* The main system is built with 3 places:
|
||||
* - A Closed place
|
||||
* - A Half Open Place
|
||||
* - An Open Place
|
||||
*/
|
||||
final class MainSystem implements SystemInterface
|
||||
{
|
||||
/**
|
||||
* @var PlaceInterface[]
|
||||
*/
|
||||
private $places;
|
||||
|
||||
public function __construct(
|
||||
PlaceInterface $closedPlace,
|
||||
PlaceInterface $halfOpenPlace,
|
||||
PlaceInterface $openPlace
|
||||
) {
|
||||
$this->places = [
|
||||
$closedPlace->getState() => $closedPlace,
|
||||
$halfOpenPlace->getState() => $halfOpenPlace,
|
||||
$openPlace->getState() => $openPlace,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getInitialPlace()
|
||||
{
|
||||
return $this->places[State::CLOSED_STATE];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPlaces()
|
||||
{
|
||||
return $this->places;
|
||||
}
|
||||
}
|
||||
155
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Transaction/SimpleTransaction.php
vendored
Normal file
155
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Transaction/SimpleTransaction.php
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Transaction;
|
||||
|
||||
use DateTime;
|
||||
use PrestaShop\CircuitBreaker\Contract\PlaceInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransactionInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\InvalidTransactionException;
|
||||
use PrestaShop\CircuitBreaker\Util\Assert;
|
||||
|
||||
/**
|
||||
* Main implementation of Circuit Breaker transaction.
|
||||
*/
|
||||
final class SimpleTransaction implements TransactionInterface
|
||||
{
|
||||
/**
|
||||
* @var string the URI of the service
|
||||
*/
|
||||
private $service;
|
||||
|
||||
/**
|
||||
* @var int the failures when we call the service
|
||||
*/
|
||||
private $failures;
|
||||
|
||||
/**
|
||||
* @var string the Circuit Breaker state
|
||||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @var DateTime the Transaction threshold datetime
|
||||
*/
|
||||
private $thresholdDateTime;
|
||||
|
||||
/**
|
||||
* @param string $service the service URI
|
||||
* @param int $failures the allowed failures
|
||||
* @param string $state the circuit breaker state/place
|
||||
* @param int $threshold the place threshold
|
||||
*/
|
||||
public function __construct($service, $failures, $state, $threshold)
|
||||
{
|
||||
$this->validate($service, $failures, $state, $threshold);
|
||||
|
||||
$this->service = $service;
|
||||
$this->failures = $failures;
|
||||
$this->state = $state;
|
||||
$this->initThresholdDateTime($threshold);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getService()
|
||||
{
|
||||
return $this->service;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFailures()
|
||||
{
|
||||
return $this->failures;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getThresholdDateTime()
|
||||
{
|
||||
return $this->thresholdDateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function incrementFailures()
|
||||
{
|
||||
++$this->failures;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to create a transaction from the Place.
|
||||
*
|
||||
* @param PlaceInterface $place the Circuit Breaker place
|
||||
* @param string $service the service URI
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function createFromPlace(PlaceInterface $place, $service)
|
||||
{
|
||||
$threshold = $place->getThreshold();
|
||||
|
||||
return new self(
|
||||
$service,
|
||||
0,
|
||||
$place->getState(),
|
||||
$threshold
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the right DateTime from the threshold value.
|
||||
*
|
||||
* @param int $threshold the Transaction threshold
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function initThresholdDateTime($threshold)
|
||||
{
|
||||
$thresholdDateTime = new DateTime();
|
||||
$thresholdDateTime->modify("+$threshold second");
|
||||
|
||||
$this->thresholdDateTime = $thresholdDateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the transaction is valid (PHP5 is permissive).
|
||||
*
|
||||
* @param string $service the service URI
|
||||
* @param int $failures the failures should be a positive value
|
||||
* @param string $state the Circuit Breaker state
|
||||
* @param int $threshold the threshold should be a positive value
|
||||
*
|
||||
* @return bool true if valid
|
||||
*
|
||||
* @throws InvalidTransactionException
|
||||
*/
|
||||
private function validate($service, $failures, $state, $threshold)
|
||||
{
|
||||
$assertionsAreValid = Assert::isURI($service)
|
||||
&& Assert::isPositiveInteger($failures)
|
||||
&& Assert::isString($state)
|
||||
&& Assert::isPositiveInteger($threshold)
|
||||
;
|
||||
|
||||
if ($assertionsAreValid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
throw InvalidTransactionException::invalidParameters($service, $failures, $state, $threshold);
|
||||
}
|
||||
}
|
||||
43
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Transition.php
vendored
Normal file
43
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Transition.php
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker;
|
||||
|
||||
/**
|
||||
* Define the available transitions of the Circuit Breaker;.
|
||||
*/
|
||||
final class Transition
|
||||
{
|
||||
/**
|
||||
* Happened only once when calling the Circuit Breaker.
|
||||
*/
|
||||
const INITIATING_TRANSITION = 'INITIATING';
|
||||
|
||||
/**
|
||||
* Happened when we open the Circuit Breaker.
|
||||
* This means once the Circuit Breaker is in failure.
|
||||
*/
|
||||
const OPENING_TRANSITION = 'OPENING';
|
||||
|
||||
/**
|
||||
* Happened once the conditions of retry are met
|
||||
* in OPEN state to move to HALF_OPEN state in the
|
||||
* Circuit Breaker.
|
||||
*/
|
||||
const CHECKING_AVAILABILITY_TRANSITION = 'CHECKING AVAILABILITY';
|
||||
|
||||
/**
|
||||
* Happened when we come back to OPEN state
|
||||
* in the Circuit Breaker from the HALF_OPEN state.
|
||||
*/
|
||||
const REOPENING_TRANSITION = 'REOPENING';
|
||||
|
||||
/**
|
||||
* Happened if the service is available again.
|
||||
*/
|
||||
const CLOSING_TRANSITION = 'CLOSING';
|
||||
|
||||
/**
|
||||
* Happened on each try to call the service.
|
||||
*/
|
||||
const TRIAL_TRANSITION = 'TRIAL';
|
||||
}
|
||||
57
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Transition/EventDispatcher.php
vendored
Normal file
57
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Transition/EventDispatcher.php
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Transition;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\TransitionDispatcherInterface;
|
||||
use PrestaShop\CircuitBreaker\Event\TransitionEvent;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* Class EventDispatcher implements the TransitionDispatcher using the Symfony EventDispatcherInterface
|
||||
*/
|
||||
class EventDispatcher implements TransitionDispatcherInterface
|
||||
{
|
||||
/**
|
||||
* @var EventDispatcherInterface the Symfony Event Dispatcher
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
|
||||
public function __construct(EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dispatchTransition($transition, $service, array $serviceParameters)
|
||||
{
|
||||
$event = new TransitionEvent($transition, $service, $serviceParameters);
|
||||
|
||||
$this->eventDispatcher->dispatch($transition, $event);
|
||||
}
|
||||
}
|
||||
44
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Transition/NullDispatcher.php
vendored
Normal file
44
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Transition/NullDispatcher.php
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Transition;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Contract\TransitionDispatcherInterface;
|
||||
|
||||
/**
|
||||
* Class NullDispatcher is used when you have no TransitionDispatcher to inject
|
||||
* because you don't need it.
|
||||
*/
|
||||
class NullDispatcher implements TransitionDispatcherInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dispatchTransition($transition, $service, array $serviceParameters)
|
||||
{
|
||||
// Simply does nothing
|
||||
}
|
||||
}
|
||||
55
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Util/Assert.php
vendored
Normal file
55
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Util/Assert.php
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Util;
|
||||
|
||||
/**
|
||||
* Util class to handle object validation
|
||||
* Should be deprecated for most parts once
|
||||
* the library will drop PHP5 support.
|
||||
*/
|
||||
final class Assert
|
||||
{
|
||||
/**
|
||||
* @param mixed $value the value to evaluate
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isPositiveValue($value)
|
||||
{
|
||||
return !is_string($value) && is_numeric($value) && $value >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value the value to evaluate
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isPositiveInteger($value)
|
||||
{
|
||||
return self::isPositiveValue($value) && is_int($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value the value to evaluate
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isURI($value)
|
||||
{
|
||||
return null !== $value
|
||||
&& !is_numeric($value)
|
||||
&& !is_bool($value)
|
||||
&& false !== filter_var($value, FILTER_SANITIZE_URL)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value the value to evaluate
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isString($value)
|
||||
{
|
||||
return !empty($value) && is_string($value);
|
||||
}
|
||||
}
|
||||
46
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Util/ErrorFormatter.php
vendored
Normal file
46
modules/ps_mbo/vendor/prestashop/circuit-breaker/src/Util/ErrorFormatter.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\CircuitBreaker\Util;
|
||||
|
||||
/**
|
||||
* Helper to provide complete and easy to read
|
||||
* error messages.
|
||||
* Mostly used to build Exception messages.
|
||||
*/
|
||||
final class ErrorFormatter
|
||||
{
|
||||
/**
|
||||
* Format error message.
|
||||
*
|
||||
* @param string $parameter the parameter to evaluate
|
||||
* @param mixed $value the value to format
|
||||
* @param string $function the validation function
|
||||
* @param string $expectedType the expected type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function format($parameter, $value, $function, $expectedType)
|
||||
{
|
||||
$errorMessage = '';
|
||||
$isValid = Assert::$function($value);
|
||||
$type = gettype($value);
|
||||
$hasStringValue = in_array($type, ['integer', 'float', 'string'], true);
|
||||
|
||||
if (!$isValid) {
|
||||
$errorMessage = sprintf(
|
||||
'Excepted %s to be %s, got %s',
|
||||
$parameter,
|
||||
$expectedType,
|
||||
$type
|
||||
);
|
||||
|
||||
if ($hasStringValue) {
|
||||
$errorMessage .= sprintf(' (%s)', (string) $value);
|
||||
}
|
||||
|
||||
$errorMessage .= PHP_EOL;
|
||||
}
|
||||
|
||||
return $errorMessage;
|
||||
}
|
||||
}
|
||||
150
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/AdvancedCircuitBreakerFactoryTest.php
vendored
Normal file
150
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/AdvancedCircuitBreakerFactoryTest.php
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\AdvancedCircuitBreaker;
|
||||
use PrestaShop\CircuitBreaker\AdvancedCircuitBreakerFactory;
|
||||
use PrestaShop\CircuitBreaker\Client\GuzzleClient;
|
||||
use PrestaShop\CircuitBreaker\Contract\FactorySettingsInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransitionDispatcherInterface;
|
||||
use PrestaShop\CircuitBreaker\FactorySettings;
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
use PrestaShop\CircuitBreaker\Transition;
|
||||
|
||||
class AdvancedCircuitBreakerFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getSettings
|
||||
*
|
||||
* @param FactorySettingsInterface $settings the Circuit Breaker settings
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCircuitBreakerCreation(FactorySettingsInterface $settings)
|
||||
{
|
||||
$factory = new AdvancedCircuitBreakerFactory();
|
||||
$circuitBreaker = $factory->create($settings);
|
||||
|
||||
$this->assertInstanceOf(AdvancedCircuitBreaker::class, $circuitBreaker);
|
||||
}
|
||||
|
||||
public function testCircuitBreakerWithDispatcher()
|
||||
{
|
||||
$dispatcher = $this->getMockBuilder(TransitionDispatcherInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock()
|
||||
;
|
||||
|
||||
$localeService = 'file://' . __FILE__;
|
||||
$expectedParameters = ['toto' => 'titi', 42 => 51];
|
||||
|
||||
$dispatcher
|
||||
->expects($this->at(0))
|
||||
->method('dispatchTransition')
|
||||
->with(
|
||||
$this->equalTo(Transition::INITIATING_TRANSITION),
|
||||
$this->equalTo($localeService),
|
||||
$this->equalTo([])
|
||||
)
|
||||
;
|
||||
$dispatcher
|
||||
->expects($this->at(1))
|
||||
->method('dispatchTransition')
|
||||
->with(
|
||||
$this->equalTo(Transition::TRIAL_TRANSITION),
|
||||
$this->equalTo($localeService),
|
||||
$this->equalTo($expectedParameters)
|
||||
)
|
||||
;
|
||||
|
||||
$factory = new AdvancedCircuitBreakerFactory();
|
||||
$settings = new FactorySettings(2, 0.1, 10);
|
||||
$settings
|
||||
->setStrippedTimeout(0.2)
|
||||
->setDispatcher($dispatcher)
|
||||
;
|
||||
$circuitBreaker = $factory->create($settings);
|
||||
|
||||
$this->assertInstanceOf(AdvancedCircuitBreaker::class, $circuitBreaker);
|
||||
$circuitBreaker->call($localeService, $expectedParameters, function () {
|
||||
});
|
||||
}
|
||||
|
||||
public function testCircuitBreakerWithStorage()
|
||||
{
|
||||
$storage = $this->getMockBuilder(StorageInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock()
|
||||
;
|
||||
|
||||
$factory = new AdvancedCircuitBreakerFactory();
|
||||
$settings = new FactorySettings(2, 0.1, 10);
|
||||
$settings
|
||||
->setStrippedTimeout(0.2)
|
||||
->setStorage($storage)
|
||||
;
|
||||
$circuitBreaker = $factory->create($settings);
|
||||
|
||||
$this->assertInstanceOf(AdvancedCircuitBreaker::class, $circuitBreaker);
|
||||
}
|
||||
|
||||
public function testCircuitBreakerWithDefaultFallback()
|
||||
{
|
||||
$factory = new AdvancedCircuitBreakerFactory();
|
||||
$settings = new FactorySettings(2, 0.1, 10);
|
||||
$settings->setDefaultFallback(function () {
|
||||
return 'default_fallback';
|
||||
});
|
||||
$circuitBreaker = $factory->create($settings);
|
||||
|
||||
$this->assertInstanceOf(AdvancedCircuitBreaker::class, $circuitBreaker);
|
||||
$response = $circuitBreaker->call('unknown_service');
|
||||
$this->assertEquals(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
$this->assertEquals('default_fallback', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSettings()
|
||||
{
|
||||
return [
|
||||
[
|
||||
(new FactorySettings(2, 0.1, 10))
|
||||
->setStrippedTimeout(0.2)
|
||||
->setClientOptions(['proxy' => '192.168.16.1:10']),
|
||||
],
|
||||
[
|
||||
(new FactorySettings(2, 0.1, 10))
|
||||
->setStrippedTimeout(0.2)
|
||||
->setClient(new GuzzleClient(['proxy' => '192.168.16.1:10'])),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
289
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/AdvancedCircuitBreakerTest.php
vendored
Normal file
289
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/AdvancedCircuitBreakerTest.php
vendored
Normal file
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker;
|
||||
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use GuzzleHttp\Message\Request;
|
||||
use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
|
||||
use GuzzleHttp\Message\Response;
|
||||
use GuzzleHttp\Stream\Stream;
|
||||
use GuzzleHttp\Subscriber\Mock;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
use PrestaShop\CircuitBreaker\AdvancedCircuitBreaker;
|
||||
use PrestaShop\CircuitBreaker\Client\GuzzleClient;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransitionDispatcherInterface;
|
||||
use PrestaShop\CircuitBreaker\Place\ClosedPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\HalfOpenPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\OpenPlace;
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
use PrestaShop\CircuitBreaker\Storage\SymfonyCache;
|
||||
use PrestaShop\CircuitBreaker\System\MainSystem;
|
||||
use PrestaShop\CircuitBreaker\Transition\NullDispatcher;
|
||||
use Symfony\Component\Cache\Simple\ArrayCache;
|
||||
|
||||
class AdvancedCircuitBreakerTest extends CircuitBreakerTestCase
|
||||
{
|
||||
/**
|
||||
* Used to track the dispatched events.
|
||||
*
|
||||
* @var PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount
|
||||
*/
|
||||
private $spy;
|
||||
|
||||
/**
|
||||
* We should see the circuit breaker initialized,
|
||||
* a call being done and then the circuit breaker closed.
|
||||
*/
|
||||
public function testCircuitBreakerEventsOnFirstFailedCall()
|
||||
{
|
||||
$circuitBreaker = $this->createCircuitBreaker();
|
||||
|
||||
$circuitBreaker->call(
|
||||
'https://httpbin.org/get/foo',
|
||||
['toto' => 'titi'],
|
||||
function () {
|
||||
return '{}';
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* The circuit breaker is initiated
|
||||
* the 2 failed trials are done
|
||||
* then the conditions are met to open the circuit breaker
|
||||
*/
|
||||
$invocations = $this->spy->getInvocations();
|
||||
$this->assertCount(4, $invocations);
|
||||
$this->assertSame('INITIATING', $invocations[0]->parameters[0]);
|
||||
$this->assertSame('TRIAL', $invocations[1]->parameters[0]);
|
||||
$this->assertSame('TRIAL', $invocations[2]->parameters[0]);
|
||||
$this->assertSame('OPENING', $invocations[3]->parameters[0]);
|
||||
}
|
||||
|
||||
public function testSimpleCall()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, 1)
|
||||
);
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
$mock = new Mock([
|
||||
new Response(200, [], Stream::factory('{"hello": "world"}')),
|
||||
]);
|
||||
$client = new GuzzleClient(['mock' => $mock]);
|
||||
|
||||
$circuitBreaker = new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$client,
|
||||
$symfonyCache,
|
||||
new NullDispatcher()
|
||||
);
|
||||
|
||||
$response = $circuitBreaker->call('anything', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertSame(State::CLOSED_STATE, $circuitBreaker->getState());
|
||||
$this->assertEquals(0, $mock->count());
|
||||
$this->assertEquals('{"hello": "world"}', $response);
|
||||
}
|
||||
|
||||
public function testOpenStateAfterTooManyFailures()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, 1)
|
||||
);
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
$mock = new Mock([
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
]);
|
||||
$client = new GuzzleClient(['mock' => $mock]);
|
||||
|
||||
$circuitBreaker = new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$client,
|
||||
$symfonyCache,
|
||||
new NullDispatcher()
|
||||
);
|
||||
|
||||
$response = $circuitBreaker->call('anything', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(0, $mock->count());
|
||||
$this->assertEquals(false, $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
}
|
||||
|
||||
public function testNoFallback()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, 1)
|
||||
);
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
$mock = new Mock([
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
]);
|
||||
$client = new GuzzleClient(['mock' => $mock]);
|
||||
|
||||
$circuitBreaker = new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$client,
|
||||
$symfonyCache,
|
||||
new NullDispatcher()
|
||||
);
|
||||
|
||||
$response = $circuitBreaker->call('anything');
|
||||
$this->assertEquals(0, $mock->count());
|
||||
$this->assertEquals('', $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
}
|
||||
|
||||
public function testBackToClosedStateAfterSuccess()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, 1)
|
||||
);
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
$mock = new Mock([
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
new Response(200, [], Stream::factory('{"hello": "world"}')),
|
||||
]);
|
||||
$client = new GuzzleClient(['mock' => $mock]);
|
||||
|
||||
$circuitBreaker = new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$client,
|
||||
$symfonyCache,
|
||||
new NullDispatcher()
|
||||
);
|
||||
|
||||
$response = $circuitBreaker->call('anything', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(1, $mock->count());
|
||||
$this->assertEquals(false, $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
|
||||
//Stay in OPEN state
|
||||
$response = $circuitBreaker->call('anything', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(1, $mock->count());
|
||||
$this->assertEquals(false, $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
|
||||
sleep(2);
|
||||
//Switch to CLOSED state on success
|
||||
$response = $circuitBreaker->call('anything', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(0, $mock->count());
|
||||
$this->assertEquals('{"hello": "world"}', $response);
|
||||
$this->assertSame(State::CLOSED_STATE, $circuitBreaker->getState());
|
||||
}
|
||||
|
||||
public function testStayInOpenStateAfterFailure()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, 1)
|
||||
);
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
$mock = new Mock([
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
]);
|
||||
$client = new GuzzleClient(['mock' => $mock]);
|
||||
|
||||
$circuitBreaker = new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$client,
|
||||
$symfonyCache,
|
||||
new NullDispatcher()
|
||||
);
|
||||
|
||||
$response = $circuitBreaker->call('anything', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(1, $mock->count());
|
||||
$this->assertEquals(false, $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
|
||||
//Stay in OPEN state
|
||||
$response = $circuitBreaker->call('anything', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(1, $mock->count());
|
||||
$this->assertEquals(false, $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
|
||||
sleep(2);
|
||||
//Switch to OPEN state on failure
|
||||
$response = $circuitBreaker->call('anything', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(0, $mock->count());
|
||||
$this->assertEquals(false, $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AdvancedCircuitBreaker the circuit breaker for testing purposes
|
||||
*/
|
||||
private function createCircuitBreaker()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, 1)
|
||||
);
|
||||
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
/** @var PHPUnit_Framework_MockObject_MockObject|TransitionDispatcherInterface $dispatcher */
|
||||
$dispatcher = $this->createMock(TransitionDispatcherInterface::class);
|
||||
$dispatcher->expects($this->spy = $this->any())
|
||||
->method('dispatchTransition')
|
||||
;
|
||||
|
||||
return new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$this->getTestClient(),
|
||||
$symfonyCache,
|
||||
$dispatcher
|
||||
);
|
||||
}
|
||||
}
|
||||
34
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/CircuitBreakerTestCase.php
vendored
Normal file
34
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/CircuitBreakerTestCase.php
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker;
|
||||
|
||||
use GuzzleHttp\Message\Request;
|
||||
use GuzzleHttp\Message\Response;
|
||||
use GuzzleHttp\Stream\Stream;
|
||||
use GuzzleHttp\Subscriber\Mock;
|
||||
use PrestaShop\CircuitBreaker\Client\GuzzleClient;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Helper to get a fake Guzzle client.
|
||||
*/
|
||||
abstract class CircuitBreakerTestCase extends TestCase
|
||||
{
|
||||
/**
|
||||
* Returns an instance of Client able to emulate
|
||||
* available and not available services.
|
||||
*
|
||||
* @return GuzzleClient
|
||||
*/
|
||||
protected function getTestClient()
|
||||
{
|
||||
$mock = new Mock([
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
new RequestException('Service unavailable', new Request('GET', 'test')),
|
||||
new Response(200, [], Stream::factory('{"hello": "world"}')),
|
||||
]);
|
||||
|
||||
return new GuzzleClient(['mock' => $mock]);
|
||||
}
|
||||
}
|
||||
263
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/CircuitBreakerWorkflowTest.php
vendored
Normal file
263
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/CircuitBreakerWorkflowTest.php
vendored
Normal file
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker;
|
||||
|
||||
use PrestaShop\CircuitBreaker\AdvancedCircuitBreaker;
|
||||
use PrestaShop\CircuitBreaker\Client\GuzzleClient;
|
||||
use PrestaShop\CircuitBreaker\Contract\CircuitBreakerInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\UnavailableServiceException;
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
use PrestaShop\CircuitBreaker\Storage\SimpleArray;
|
||||
use PrestaShop\CircuitBreaker\Transition\NullDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use PrestaShop\CircuitBreaker\Storage\SymfonyCache;
|
||||
use PrestaShop\CircuitBreaker\SymfonyCircuitBreaker;
|
||||
use PrestaShop\CircuitBreaker\SimpleCircuitBreaker;
|
||||
use PrestaShop\CircuitBreaker\Place\HalfOpenPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\ClosedPlace;
|
||||
use PrestaShop\CircuitBreaker\System\MainSystem;
|
||||
use PrestaShop\CircuitBreaker\Place\OpenPlace;
|
||||
use Symfony\Component\Cache\Simple\ArrayCache;
|
||||
|
||||
class CircuitBreakerWorkflowTest extends CircuitBreakerTestCase
|
||||
{
|
||||
const OPEN_THRESHOLD = 1;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
//For SimpleCircuitBreaker tests we need to clear the storage cache because it is stored in a static variable
|
||||
$storage = new SimpleArray();
|
||||
$storage->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* When we use the circuit breaker on unreachable service
|
||||
* the fallback response is used.
|
||||
*
|
||||
* @dataProvider getCircuitBreakers
|
||||
*
|
||||
* @param CircuitBreakerInterface $circuitBreaker
|
||||
*/
|
||||
public function testCircuitBreakerIsInClosedStateAtStart($circuitBreaker)
|
||||
{
|
||||
$this->assertSame(State::CLOSED_STATE, $circuitBreaker->getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* Once the number of failures is reached, the circuit breaker
|
||||
* is open. This time no calls to the services are done.
|
||||
*
|
||||
* @dataProvider getCircuitBreakers
|
||||
*
|
||||
* @param CircuitBreakerInterface $circuitBreaker
|
||||
*/
|
||||
public function testCircuitBreakerWillBeOpenInCaseOfFailures($circuitBreaker)
|
||||
{
|
||||
// CLOSED
|
||||
$this->assertSame(State::CLOSED_STATE, $circuitBreaker->getState());
|
||||
$response = $circuitBreaker->call('https://httpbin.org/get/foo', [], $this->createFallbackResponse());
|
||||
$this->assertSame('{}', $response);
|
||||
|
||||
//After two failed calls switch to OPEN state
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
$this->assertSame(
|
||||
'{}',
|
||||
$circuitBreaker->call(
|
||||
'https://httpbin.org/get/foo',
|
||||
[],
|
||||
$this->createFallbackResponse()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Once the number of failures is reached, the circuit breaker
|
||||
* is open. This time no calls to the services are done.
|
||||
*
|
||||
* @dataProvider getCircuitBreakers
|
||||
*
|
||||
* @param CircuitBreakerInterface $circuitBreaker
|
||||
*/
|
||||
public function testCircuitBreakerWillBeOpenWithoutFallback($circuitBreaker)
|
||||
{
|
||||
// CLOSED
|
||||
$this->assertSame(State::CLOSED_STATE, $circuitBreaker->getState());
|
||||
$response = $circuitBreaker->call('https://httpbin.org/get/foo');
|
||||
$this->assertSame('', $response);
|
||||
|
||||
//After two failed calls switch to OPEN state
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
$this->assertSame(
|
||||
'{}',
|
||||
$circuitBreaker->call(
|
||||
'https://httpbin.org/get/foo',
|
||||
[],
|
||||
$this->createFallbackResponse()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* In HalfOpen state, if the service is back we can
|
||||
* close the CircuitBreaker.
|
||||
*
|
||||
* @dataProvider getCircuitBreakers
|
||||
*
|
||||
* @param CircuitBreakerInterface $circuitBreaker
|
||||
*/
|
||||
public function testOnceInHalfOpenModeServiceIsFinallyReachable($circuitBreaker)
|
||||
{
|
||||
// CLOSED - first call fails (twice)
|
||||
$this->assertSame(State::CLOSED_STATE, $circuitBreaker->getState());
|
||||
$response = $circuitBreaker->call('https://httpbin.org/get/foo', [], $this->createFallbackResponse());
|
||||
$this->assertSame('{}', $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
|
||||
// OPEN - no call to client
|
||||
$response = $circuitBreaker->call('https://httpbin.org/get/foo', [], $this->createFallbackResponse());
|
||||
$this->assertSame('{}', $response);
|
||||
$this->assertSame(State::OPEN_STATE, $circuitBreaker->getState());
|
||||
|
||||
sleep(2 * self::OPEN_THRESHOLD);
|
||||
// SWITCH TO HALF OPEN - retry to call the service
|
||||
$this->assertSame(
|
||||
'{"hello": "world"}',
|
||||
$circuitBreaker->call(
|
||||
'https://httpbin.org/get/foo',
|
||||
[],
|
||||
$this->createFallbackResponse()
|
||||
)
|
||||
);
|
||||
$this->assertSame(State::CLOSED_STATE, $circuitBreaker->getState());
|
||||
$this->assertTrue($circuitBreaker->isClosed());
|
||||
}
|
||||
|
||||
/**
|
||||
* This is not useful for SimpleCircuitBreaker since it has a SimpleArray storage
|
||||
*/
|
||||
public function testRememberLastTransactionState()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(1, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, 1)
|
||||
);
|
||||
$storage = new SymfonyCache(new ArrayCache());
|
||||
$client = $this->createMock(GuzzleClient::class);
|
||||
$client
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->willThrowException(new UnavailableServiceException())
|
||||
;
|
||||
|
||||
$firstCircuitBreaker = new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$client,
|
||||
$storage,
|
||||
new NullDispatcher()
|
||||
);
|
||||
$this->assertEquals(State::CLOSED_STATE, $firstCircuitBreaker->getState());
|
||||
$firstCircuitBreaker->call('fake_service', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(State::OPEN_STATE, $firstCircuitBreaker->getState());
|
||||
$this->assertTrue($storage->hasTransaction('fake_service'));
|
||||
|
||||
$secondCircuitBreaker = new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$client,
|
||||
$storage,
|
||||
new NullDispatcher()
|
||||
);
|
||||
$this->assertEquals(State::CLOSED_STATE, $secondCircuitBreaker->getState());
|
||||
$secondCircuitBreaker->call('fake_service', [], function () {
|
||||
return false;
|
||||
});
|
||||
$this->assertEquals(State::OPEN_STATE, $secondCircuitBreaker->getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of supported circuit breakers
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCircuitBreakers()
|
||||
{
|
||||
return [
|
||||
'simple' => [$this->createSimpleCircuitBreaker()],
|
||||
'symfony' => [$this->createSymfonyCircuitBreaker()],
|
||||
'advanced' => [$this->createAdvancedCircuitBreaker()],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SimpleCircuitBreaker the circuit breaker for testing purposes
|
||||
*/
|
||||
private function createSimpleCircuitBreaker()
|
||||
{
|
||||
return new SimpleCircuitBreaker(
|
||||
new OpenPlace(0, 0, self::OPEN_THRESHOLD), // threshold 1s
|
||||
new HalfOpenPlace(0, 0.2, 0), // timeout 0.2s to test the service
|
||||
new ClosedPlace(2, 0.2, 0), // 2 failures allowed, 0.2s timeout
|
||||
$this->getTestClient()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AdvancedCircuitBreaker the circuit breaker for testing purposes
|
||||
*/
|
||||
private function createAdvancedCircuitBreaker()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, self::OPEN_THRESHOLD)
|
||||
);
|
||||
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
|
||||
return new AdvancedCircuitBreaker(
|
||||
$system,
|
||||
$this->getTestClient(),
|
||||
$symfonyCache,
|
||||
new NullDispatcher()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SymfonyCircuitBreaker the circuit breaker for testing purposes
|
||||
*/
|
||||
private function createSymfonyCircuitBreaker()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, self::OPEN_THRESHOLD)
|
||||
);
|
||||
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
$eventDispatcherS = $this->createMock(EventDispatcher::class);
|
||||
|
||||
return new SymfonyCircuitBreaker(
|
||||
$system,
|
||||
$this->getTestClient(),
|
||||
$symfonyCache,
|
||||
$eventDispatcherS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return callable the fallback callable
|
||||
*/
|
||||
private function createFallbackResponse()
|
||||
{
|
||||
return function () {
|
||||
return '{}';
|
||||
};
|
||||
}
|
||||
}
|
||||
36
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Client/GuzzleClientTest.php
vendored
Normal file
36
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Client/GuzzleClientTest.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Client;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Client\GuzzleClient;
|
||||
use PrestaShop\CircuitBreaker\Exception\UnavailableServiceException;
|
||||
|
||||
class GuzzleClientTest extends TestCase
|
||||
{
|
||||
public function testRequestWorksAsExpected()
|
||||
{
|
||||
$client = new GuzzleClient();
|
||||
|
||||
$this->assertNotNull($client->request('https://www.google.com', [
|
||||
'method' => 'GET',
|
||||
]));
|
||||
}
|
||||
|
||||
public function testWrongRequestThrowsAnException()
|
||||
{
|
||||
$this->expectException(UnavailableServiceException::class);
|
||||
|
||||
$client = new GuzzleClient();
|
||||
$client->request('http://not-even-a-valid-domain.xxx', []);
|
||||
}
|
||||
|
||||
public function testTheClientAcceptsHttpMethodOverride()
|
||||
{
|
||||
$client = new GuzzleClient([
|
||||
'method' => 'HEAD',
|
||||
]);
|
||||
|
||||
$this->assertEmpty($client->request('https://www.google.fr', []));
|
||||
}
|
||||
}
|
||||
51
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Event/TransitionEventTest.php
vendored
Normal file
51
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Event/TransitionEventTest.php
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Event;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Event\TransitionEvent;
|
||||
|
||||
class TransitionEventTest extends TestCase
|
||||
{
|
||||
public function testCreation()
|
||||
{
|
||||
$event = new TransitionEvent('foo', 'bar', []);
|
||||
|
||||
$this->assertInstanceOf(TransitionEvent::class, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetService()
|
||||
{
|
||||
$event = new TransitionEvent('eventName', 'service', []);
|
||||
|
||||
$this->assertSame('service', $event->getService());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetEvent()
|
||||
{
|
||||
$event = new TransitionEvent('eventName', 'service', []);
|
||||
|
||||
$this->assertSame('eventName', $event->getEvent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetParameters()
|
||||
{
|
||||
$parameters = [
|
||||
'foo' => 'myFoo',
|
||||
'bar' => true,
|
||||
];
|
||||
|
||||
$event = new TransitionEvent('eventName', 'service', $parameters);
|
||||
|
||||
$this->assertSame($parameters, $event->getParameters());
|
||||
}
|
||||
}
|
||||
64
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Exception/InvalidPlaceTest.php
vendored
Normal file
64
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Exception/InvalidPlaceTest.php
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Exception;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Exception\InvalidPlaceException;
|
||||
|
||||
class InvalidPlaceTest extends TestCase
|
||||
{
|
||||
public function testCreation()
|
||||
{
|
||||
$invalidPlace = new InvalidPlaceException();
|
||||
|
||||
$this->assertInstanceOf(InvalidPlaceException::class, $invalidPlace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getSettings
|
||||
*
|
||||
* @param array $settings
|
||||
* @param string $expectedExceptionMessage
|
||||
*/
|
||||
public function testInvalidSettings($settings, $expectedExceptionMessage)
|
||||
{
|
||||
$invalidPlace = InvalidPlaceException::invalidSettings(
|
||||
$settings[0], // failures
|
||||
$settings[1], // timeout
|
||||
$settings[2] // threshold
|
||||
);
|
||||
|
||||
$this->assertSame($invalidPlace->getMessage(), $expectedExceptionMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSettings()
|
||||
{
|
||||
return [
|
||||
'all_invalid_settings' => [
|
||||
['0', '1', null],
|
||||
'Invalid settings for Place' . PHP_EOL .
|
||||
'Excepted failures to be a positive integer, got string (0)' . PHP_EOL .
|
||||
'Excepted timeout to be a float, got string (1)' . PHP_EOL .
|
||||
'Excepted threshold to be a positive integer, got NULL' . PHP_EOL,
|
||||
],
|
||||
'2_invalid_settings' => [
|
||||
[0, '1', null],
|
||||
'Invalid settings for Place' . PHP_EOL .
|
||||
'Excepted timeout to be a float, got string (1)' . PHP_EOL .
|
||||
'Excepted threshold to be a positive integer, got NULL' . PHP_EOL,
|
||||
],
|
||||
'1_invalid_settings' => [
|
||||
[0, '1', 2],
|
||||
'Invalid settings for Place' . PHP_EOL .
|
||||
'Excepted timeout to be a float, got string (1)' . PHP_EOL,
|
||||
],
|
||||
'all_valid_settings' => [
|
||||
[0, 1.1, 2],
|
||||
'Invalid settings for Place' . PHP_EOL,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
68
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Exception/InvalidTransactionTest.php
vendored
Normal file
68
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Exception/InvalidTransactionTest.php
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Exception;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Exception\InvalidTransactionException;
|
||||
|
||||
class InvalidTransactionTest extends TestCase
|
||||
{
|
||||
public function testCreation()
|
||||
{
|
||||
$invalidPlace = new InvalidTransactionException();
|
||||
|
||||
$this->assertInstanceOf(InvalidTransactionException::class, $invalidPlace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getParameters
|
||||
*
|
||||
* @param array $parameters
|
||||
* @param string $expectedExceptionMessage
|
||||
*/
|
||||
public function testInvalidParameters($parameters, $expectedExceptionMessage)
|
||||
{
|
||||
$invalidPlace = InvalidTransactionException::invalidParameters(
|
||||
$parameters[0], // service
|
||||
$parameters[1], // failures
|
||||
$parameters[2], // state
|
||||
$parameters[3] // threshold
|
||||
);
|
||||
|
||||
$this->assertSame($invalidPlace->getMessage(), $expectedExceptionMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return [
|
||||
'all_invalid_parameters' => [
|
||||
[100, '0', null, 'toto'],
|
||||
'Invalid parameters for Transaction' . PHP_EOL .
|
||||
'Excepted service to be an URI, got integer (100)' . PHP_EOL .
|
||||
'Excepted failures to be a positive integer, got string (0)' . PHP_EOL .
|
||||
'Excepted state to be a string, got NULL' . PHP_EOL .
|
||||
'Excepted threshold to be a positive integer, got string (toto)' . PHP_EOL,
|
||||
],
|
||||
'3_invalid_parameters' => [
|
||||
['http://www.prestashop.com', '1', null, 'toto'],
|
||||
'Invalid parameters for Transaction' . PHP_EOL .
|
||||
'Excepted failures to be a positive integer, got string (1)' . PHP_EOL .
|
||||
'Excepted state to be a string, got NULL' . PHP_EOL .
|
||||
'Excepted threshold to be a positive integer, got string (toto)' . PHP_EOL,
|
||||
],
|
||||
'2_invalid_parameters' => [
|
||||
['http://www.prestashop.com', 10, null, null],
|
||||
'Invalid parameters for Transaction' . PHP_EOL .
|
||||
'Excepted state to be a string, got NULL' . PHP_EOL .
|
||||
'Excepted threshold to be a positive integer, got NULL' . PHP_EOL,
|
||||
],
|
||||
'none_invalid' => [
|
||||
['http://www.prestashop.com', 10, 'CLOSED_STATE', 1],
|
||||
'Invalid parameters for Transaction' . PHP_EOL,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
68
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/FactorySettingsTest.php
vendored
Normal file
68
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/FactorySettingsTest.php
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2019 PrestaShop SA and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2019 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\FactorySettings;
|
||||
|
||||
class FactorySettingsTest extends TestCase
|
||||
{
|
||||
public function testSimpleSettings()
|
||||
{
|
||||
$settings = new FactorySettings(2, 0.5, 10);
|
||||
$this->assertNotNull($settings);
|
||||
$this->assertEquals(2, $settings->getFailures());
|
||||
$this->assertEquals(0.5, $settings->getTimeout());
|
||||
$this->assertEquals(10, $settings->getThreshold());
|
||||
$this->assertEquals(2, $settings->getStrippedFailures());
|
||||
$this->assertEquals(0.5, $settings->getStrippedTimeout());
|
||||
}
|
||||
|
||||
public function testMergeSettings()
|
||||
{
|
||||
$defaultSettings = new FactorySettings(2, 0.5, 10);
|
||||
$defaultSettings
|
||||
->setStrippedTimeout(1.2)
|
||||
->setStrippedFailures(1)
|
||||
;
|
||||
|
||||
$this->assertEquals(2, $defaultSettings->getFailures());
|
||||
$this->assertEquals(0.5, $defaultSettings->getTimeout());
|
||||
$this->assertEquals(10, $defaultSettings->getThreshold());
|
||||
$this->assertEquals(1, $defaultSettings->getStrippedFailures());
|
||||
$this->assertEquals(1.2, $defaultSettings->getStrippedTimeout());
|
||||
|
||||
$settings = new FactorySettings(2, 1.5, 20);
|
||||
|
||||
$mergedSettings = FactorySettings::merge($defaultSettings, $settings);
|
||||
$this->assertEquals(2, $mergedSettings->getFailures());
|
||||
$this->assertEquals(1.5, $mergedSettings->getTimeout());
|
||||
$this->assertEquals(20, $mergedSettings->getThreshold());
|
||||
$this->assertEquals(2, $mergedSettings->getStrippedFailures());
|
||||
$this->assertEquals(1.5, $mergedSettings->getStrippedTimeout());
|
||||
}
|
||||
}
|
||||
47
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Place/ClosedPlaceTest.php
vendored
Normal file
47
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Place/ClosedPlaceTest.php
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Place;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Exception\InvalidPlaceException;
|
||||
use PrestaShop\CircuitBreaker\Place\ClosedPlace;
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
|
||||
class ClosedPlaceTest extends PlaceTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getFixtures
|
||||
*
|
||||
* @param mixed $failures
|
||||
* @param mixed $timeout
|
||||
* @param mixed $threshold
|
||||
*/
|
||||
public function testCreationWith($failures, $timeout, $threshold)
|
||||
{
|
||||
$closedPlace = new ClosedPlace($failures, $timeout, $threshold);
|
||||
|
||||
$this->assertSame($failures, $closedPlace->getFailures());
|
||||
$this->assertSame($timeout, $closedPlace->getTimeout());
|
||||
$this->assertSame($threshold, $closedPlace->getThreshold());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidFixtures
|
||||
*
|
||||
* @param mixed $failures
|
||||
* @param mixed $timeout
|
||||
* @param mixed $threshold
|
||||
*/
|
||||
public function testCreationWithInvalidValues($failures, $timeout, $threshold)
|
||||
{
|
||||
$this->expectException(InvalidPlaceException::class);
|
||||
|
||||
new ClosedPlace($failures, $timeout, $threshold);
|
||||
}
|
||||
|
||||
public function testGetExpectedState()
|
||||
{
|
||||
$closedPlace = new ClosedPlace(1, 1, 1);
|
||||
|
||||
$this->assertSame(State::CLOSED_STATE, $closedPlace->getState());
|
||||
}
|
||||
}
|
||||
47
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Place/HalfOpenPlaceTest.php
vendored
Normal file
47
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Place/HalfOpenPlaceTest.php
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Place;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Exception\InvalidPlaceException;
|
||||
use PrestaShop\CircuitBreaker\Place\HalfOpenPlace;
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
|
||||
class HalfOpenPlaceTest extends PlaceTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getFixtures
|
||||
*
|
||||
* @param mixed $failures
|
||||
* @param mixed $timeout
|
||||
* @param mixed $threshold
|
||||
*/
|
||||
public function testCreationWith($failures, $timeout, $threshold)
|
||||
{
|
||||
$halfOpenPlace = new HalfOpenPlace($failures, $timeout, $threshold);
|
||||
|
||||
$this->assertSame($failures, $halfOpenPlace->getFailures());
|
||||
$this->assertSame($timeout, $halfOpenPlace->getTimeout());
|
||||
$this->assertSame($threshold, $halfOpenPlace->getThreshold());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidFixtures
|
||||
*
|
||||
* @param mixed $failures
|
||||
* @param mixed $timeout
|
||||
* @param mixed $threshold
|
||||
*/
|
||||
public function testCreationWithInvalidValues($failures, $timeout, $threshold)
|
||||
{
|
||||
$this->expectException(InvalidPlaceException::class);
|
||||
|
||||
new HalfOpenPlace($failures, $timeout, $threshold);
|
||||
}
|
||||
|
||||
public function testGetExpectedState()
|
||||
{
|
||||
$halfOpenPlace = new HalfOpenPlace(1, 1, 1);
|
||||
|
||||
$this->assertSame(State::HALF_OPEN_STATE, $halfOpenPlace->getState());
|
||||
}
|
||||
}
|
||||
47
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Place/OpenPlaceTest.php
vendored
Normal file
47
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Place/OpenPlaceTest.php
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Place;
|
||||
|
||||
use PrestaShop\CircuitBreaker\Exception\InvalidPlaceException;
|
||||
use PrestaShop\CircuitBreaker\Place\OpenPlace;
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
|
||||
class OpenPlaceTest extends PlaceTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getFixtures
|
||||
*
|
||||
* @param mixed $failures
|
||||
* @param mixed $timeout
|
||||
* @param mixed $threshold
|
||||
*/
|
||||
public function testCreationWith($failures, $timeout, $threshold)
|
||||
{
|
||||
$openPlace = new OpenPlace($failures, $timeout, $threshold);
|
||||
|
||||
$this->assertSame($failures, $openPlace->getFailures());
|
||||
$this->assertSame($timeout, $openPlace->getTimeout());
|
||||
$this->assertSame($threshold, $openPlace->getThreshold());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidFixtures
|
||||
*
|
||||
* @param mixed $failures
|
||||
* @param mixed $timeout
|
||||
* @param mixed $threshold
|
||||
*/
|
||||
public function testCreationWithInvalidValues($failures, $timeout, $threshold)
|
||||
{
|
||||
$this->expectException(InvalidPlaceException::class);
|
||||
|
||||
new OpenPlace($failures, $timeout, $threshold);
|
||||
}
|
||||
|
||||
public function testGetExpectedState()
|
||||
{
|
||||
$openPlace = new OpenPlace(1, 1, 1);
|
||||
|
||||
$this->assertSame(State::OPEN_STATE, $openPlace->getState());
|
||||
}
|
||||
}
|
||||
67
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Place/PlaceTestCase.php
vendored
Normal file
67
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Place/PlaceTestCase.php
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Place;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Helper to share fixtures accross Place tests.
|
||||
*/
|
||||
class PlaceTestCase extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFixtures()
|
||||
{
|
||||
return [
|
||||
'0_0_0' => [0, 0, 0],
|
||||
'1_100_0' => [1, 100, 0],
|
||||
'3_0.6_3' => [3, 0.6, 3],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getArrayFixtures()
|
||||
{
|
||||
return [
|
||||
'assoc_array' => [[
|
||||
'timeout' => 3,
|
||||
'threshold' => 2,
|
||||
'failures' => 1,
|
||||
]],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getInvalidFixtures()
|
||||
{
|
||||
return [
|
||||
'minus1_null_false' => [-1, null, false],
|
||||
'3_0.6_3.14' => [3, 0.6, 3.14],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getInvalidArrayFixtures()
|
||||
{
|
||||
return [
|
||||
'invalid_indexes' => [[
|
||||
0 => 3,
|
||||
1 => 2,
|
||||
4 => 1,
|
||||
]],
|
||||
'invalid_keys' => [[
|
||||
'timeout' => 3,
|
||||
'max_wait' => 2,
|
||||
'failures' => 1,
|
||||
]],
|
||||
];
|
||||
}
|
||||
}
|
||||
58
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/SimpleCircuitBreakerFactoryTest.php
vendored
Normal file
58
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/SimpleCircuitBreakerFactoryTest.php
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Contract\FactorySettingsInterface;
|
||||
use PrestaShop\CircuitBreaker\FactorySettings;
|
||||
use PrestaShop\CircuitBreaker\SimpleCircuitBreaker;
|
||||
use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
|
||||
|
||||
class SimpleCircuitBreakerFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreation()
|
||||
{
|
||||
$factory = new SimpleCircuitBreakerFactory();
|
||||
|
||||
$this->assertInstanceOf(SimpleCircuitBreakerFactory::class, $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @dataProvider getSettings
|
||||
*
|
||||
* @param FactorySettingsInterface $settings the Circuit Breaker settings
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCircuitBreakerCreation(FactorySettingsInterface $settings)
|
||||
{
|
||||
$factory = new SimpleCircuitBreakerFactory();
|
||||
$circuitBreaker = $factory->create($settings);
|
||||
|
||||
$this->assertInstanceOf(SimpleCircuitBreaker::class, $circuitBreaker);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSettings()
|
||||
{
|
||||
return [
|
||||
[
|
||||
(new FactorySettings(2, 0.1, 10))
|
||||
->setStrippedTimeout(0.2)
|
||||
->setStrippedFailures(1),
|
||||
],
|
||||
[
|
||||
(new FactorySettings(2, 0.1, 10))
|
||||
->setStrippedTimeout(0.2)
|
||||
->setStrippedFailures(1)
|
||||
->setClientOptions(['proxy' => '192.168.16.1:10']),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
114
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Storage/DoctrineCacheTest.php
vendored
Normal file
114
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Storage/DoctrineCacheTest.php
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Storage;
|
||||
|
||||
use Doctrine\Common\Cache\FilesystemCache;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Contract\StorageInterface;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransactionInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\TransactionNotFoundException;
|
||||
use PrestaShop\CircuitBreaker\Storage\DoctrineCache;
|
||||
|
||||
class DoctrineCacheTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var StorageInterface the Doctrine Cache storage
|
||||
*/
|
||||
private $doctrineCache;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->doctrineCache = new DoctrineCache(
|
||||
new FilesystemCache(sys_get_temp_dir() . '/ps__circuit_breaker')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function tearDown()
|
||||
{
|
||||
$filesystemAdapter = new FilesystemCache(sys_get_temp_dir() . '/ps__circuit_breaker');
|
||||
$filesystemAdapter->deleteAll();
|
||||
}
|
||||
|
||||
public function testCreation()
|
||||
{
|
||||
$doctrineCache = new DoctrineCache(
|
||||
new FilesystemCache(sys_get_temp_dir() . '/ps__circuit_breaker')
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(DoctrineCache::class, $doctrineCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testSaveTransaction()
|
||||
{
|
||||
$operation = $this->doctrineCache->saveTransaction(
|
||||
'http://test.com',
|
||||
$this->createMock(TransactionInterface::class)
|
||||
);
|
||||
|
||||
$this->assertTrue($operation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testSaveTransaction
|
||||
*/
|
||||
public function testHasTransaction()
|
||||
{
|
||||
$this->doctrineCache->saveTransaction('http://test.com', $this->createMock(TransactionInterface::class));
|
||||
|
||||
$this->assertTrue($this->doctrineCache->hasTransaction('http://test.com'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testSaveTransaction
|
||||
* @depends testHasTransaction
|
||||
*/
|
||||
public function testGetTransaction()
|
||||
{
|
||||
$translationStub = $this->createMock(TransactionInterface::class);
|
||||
$this->doctrineCache->saveTransaction('http://test.com', $translationStub);
|
||||
|
||||
$transaction = $this->doctrineCache->getTransaction('http://test.com');
|
||||
|
||||
$this->assertEquals($transaction, $translationStub);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testGetTransaction
|
||||
* @depends testHasTransaction
|
||||
*/
|
||||
public function testGetNotFoundTransactionThrowsAnException()
|
||||
{
|
||||
$this->expectException(TransactionNotFoundException::class);
|
||||
|
||||
$this->doctrineCache->getTransaction('http://test.com');
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSaveTransaction
|
||||
* @depends testGetTransaction
|
||||
*/
|
||||
public function testClear()
|
||||
{
|
||||
$translationStub = $this->createMock(TransactionInterface::class);
|
||||
$this->doctrineCache->saveTransaction('http://a.com', $translationStub);
|
||||
$this->doctrineCache->saveTransaction('http://b.com', $translationStub);
|
||||
|
||||
// We have stored 2 transactions
|
||||
$this->assertTrue($this->doctrineCache->clear());
|
||||
$this->expectException(TransactionNotFoundException::class);
|
||||
|
||||
$this->doctrineCache->getTransaction('http://a.com');
|
||||
}
|
||||
}
|
||||
113
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Storage/SimpleArrayTest.php
vendored
Normal file
113
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Storage/SimpleArrayTest.php
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Storage;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransactionInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\TransactionNotFoundException;
|
||||
use PrestaShop\CircuitBreaker\Storage\SimpleArray;
|
||||
|
||||
class SimpleArrayTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$simpleArray = new SimpleArray();
|
||||
$simpleArray::$transactions = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreation()
|
||||
{
|
||||
$simpleArray = new SimpleArray();
|
||||
|
||||
$this->assertCount(0, $simpleArray::$transactions);
|
||||
$this->assertInstanceOf(SimpleArray::class, $simpleArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSaveTransaction()
|
||||
{
|
||||
$simpleArray = new SimpleArray();
|
||||
$operation = $simpleArray->saveTransaction(
|
||||
'http://test.com',
|
||||
$this->createMock(TransactionInterface::class)
|
||||
);
|
||||
$this->assertTrue($operation);
|
||||
$this->assertCount(1, $simpleArray::$transactions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testSaveTransaction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHasTransaction()
|
||||
{
|
||||
$simpleArray = new SimpleArray();
|
||||
$simpleArray->saveTransaction('http://test.com', $this->createMock(TransactionInterface::class));
|
||||
|
||||
$this->assertTrue($simpleArray->hasTransaction('http://test.com'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testSaveTransaction
|
||||
* @depends testHasTransaction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetTransaction()
|
||||
{
|
||||
$simpleArray = new SimpleArray();
|
||||
$translationStub = $this->createMock(TransactionInterface::class);
|
||||
$simpleArray->saveTransaction('http://test.com', $translationStub);
|
||||
|
||||
$transaction = $simpleArray->getTransaction('http://test.com');
|
||||
|
||||
$this->assertSame($transaction, $translationStub);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testGetTransaction
|
||||
* @depends testHasTransaction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetNotFoundTransactionThrowsAnException()
|
||||
{
|
||||
$this->expectException(TransactionNotFoundException::class);
|
||||
|
||||
$simpleArray = new SimpleArray();
|
||||
$simpleArray->getTransaction('http://test.com');
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSaveTransaction
|
||||
* @depends testGetTransaction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testClear()
|
||||
{
|
||||
$simpleArray = new SimpleArray();
|
||||
$translationStub = $this->createMock(TransactionInterface::class);
|
||||
$simpleArray->saveTransaction('http://a.com', $translationStub);
|
||||
$simpleArray->saveTransaction('http://b.com', $translationStub);
|
||||
|
||||
// We have stored 2 transactions
|
||||
$simpleArray->clear();
|
||||
$transactions = $simpleArray::$transactions;
|
||||
$this->assertEmpty($transactions);
|
||||
}
|
||||
}
|
||||
113
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Storage/SymfonyCacheTest.php
vendored
Normal file
113
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Storage/SymfonyCacheTest.php
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Storage;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Contract\TransactionInterface;
|
||||
use PrestaShop\CircuitBreaker\Exception\TransactionNotFoundException;
|
||||
use PrestaShop\CircuitBreaker\Storage\SymfonyCache;
|
||||
use Symfony\Component\Cache\Simple\FilesystemCache;
|
||||
|
||||
class SymfonyCacheTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var SymfonyCache the Symfony Cache storage
|
||||
*/
|
||||
private $symfonyCache;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->symfonyCache = new SymfonyCache(
|
||||
new FilesystemCache('ps__circuit_breaker', 20)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function tearDown()
|
||||
{
|
||||
$filesystemAdapter = new FilesystemCache('ps__circuit_breaker', 20);
|
||||
$filesystemAdapter->clear();
|
||||
}
|
||||
|
||||
public function testCreation()
|
||||
{
|
||||
$symfonyCache = new SymfonyCache(
|
||||
new FilesystemCache('ps__circuit_breaker')
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(SymfonyCache::class, $symfonyCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testSaveTransaction()
|
||||
{
|
||||
$operation = $this->symfonyCache->saveTransaction(
|
||||
'http://test.com',
|
||||
$this->createMock(TransactionInterface::class)
|
||||
);
|
||||
|
||||
$this->assertTrue($operation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testSaveTransaction
|
||||
*/
|
||||
public function testHasTransaction()
|
||||
{
|
||||
$this->symfonyCache->saveTransaction('http://test.com', $this->createMock(TransactionInterface::class));
|
||||
|
||||
$this->assertTrue($this->symfonyCache->hasTransaction('http://test.com'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testSaveTransaction
|
||||
* @depends testHasTransaction
|
||||
*/
|
||||
public function testGetTransaction()
|
||||
{
|
||||
$translationStub = $this->createMock(TransactionInterface::class);
|
||||
$this->symfonyCache->saveTransaction('http://test.com', $translationStub);
|
||||
|
||||
$transaction = $this->symfonyCache->getTransaction('http://test.com');
|
||||
|
||||
$this->assertEquals($transaction, $translationStub);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testGetTransaction
|
||||
* @depends testHasTransaction
|
||||
*/
|
||||
public function testGetNotFoundTransactionThrowsAnException()
|
||||
{
|
||||
$this->expectException(TransactionNotFoundException::class);
|
||||
|
||||
$this->symfonyCache->getTransaction('http://test.com');
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSaveTransaction
|
||||
* @depends testGetTransaction
|
||||
*/
|
||||
public function testClear()
|
||||
{
|
||||
$translationStub = $this->createMock(TransactionInterface::class);
|
||||
$this->symfonyCache->saveTransaction('http://a.com', $translationStub);
|
||||
$this->symfonyCache->saveTransaction('http://b.com', $translationStub);
|
||||
|
||||
// We have stored 2 transactions
|
||||
$this->assertTrue($this->symfonyCache->clear());
|
||||
$this->expectException(TransactionNotFoundException::class);
|
||||
|
||||
$this->symfonyCache->getTransaction('http://a.com');
|
||||
}
|
||||
}
|
||||
77
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/SymfonyCircuitBreakerEventsTest.php
vendored
Normal file
77
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/SymfonyCircuitBreakerEventsTest.php
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker;
|
||||
|
||||
use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use PrestaShop\CircuitBreaker\SymfonyCircuitBreaker;
|
||||
use PrestaShop\CircuitBreaker\Storage\SymfonyCache;
|
||||
use PrestaShop\CircuitBreaker\Place\HalfOpenPlace;
|
||||
use PrestaShop\CircuitBreaker\System\MainSystem;
|
||||
use PrestaShop\CircuitBreaker\Place\ClosedPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\OpenPlace;
|
||||
use Symfony\Component\Cache\Simple\ArrayCache;
|
||||
|
||||
class SymfonyCircuitBreakerEventsTest extends CircuitBreakerTestCase
|
||||
{
|
||||
/**
|
||||
* Used to track the dispatched events.
|
||||
*
|
||||
* @var PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount
|
||||
*/
|
||||
private $spy;
|
||||
|
||||
/**
|
||||
* We should see the circuit breaker initialized,
|
||||
* a call being done and then the circuit breaker closed.
|
||||
*/
|
||||
public function testCircuitBreakerEventsOnFirstFailedCall()
|
||||
{
|
||||
$circuitBreaker = $this->createCircuitBreaker();
|
||||
|
||||
$circuitBreaker->call(
|
||||
'https://httpbin.org/get/foo',
|
||||
[],
|
||||
function () {
|
||||
return '{}';
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* The circuit breaker is initiated
|
||||
* the 2 failed trials are done
|
||||
* then the conditions are met to open the circuit breaker
|
||||
*/
|
||||
$invocations = $this->spy->getInvocations();
|
||||
$this->assertCount(4, $invocations);
|
||||
$this->assertSame('INITIATING', $invocations[0]->parameters[0]);
|
||||
$this->assertSame('TRIAL', $invocations[1]->parameters[0]);
|
||||
$this->assertSame('TRIAL', $invocations[2]->parameters[0]);
|
||||
$this->assertSame('OPENING', $invocations[3]->parameters[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SymfonyCircuitBreaker the circuit breaker for testing purposes
|
||||
*/
|
||||
private function createCircuitBreaker()
|
||||
{
|
||||
$system = new MainSystem(
|
||||
new ClosedPlace(2, 0.2, 0),
|
||||
new HalfOpenPlace(0, 0.2, 0),
|
||||
new OpenPlace(0, 0, 1)
|
||||
);
|
||||
|
||||
$symfonyCache = new SymfonyCache(new ArrayCache());
|
||||
$eventDispatcherS = $this->createMock(EventDispatcher::class);
|
||||
$eventDispatcherS->expects($this->spy = $this->any())
|
||||
->method('dispatch')
|
||||
;
|
||||
|
||||
return new SymfonyCircuitBreaker(
|
||||
$system,
|
||||
$this->getTestClient(),
|
||||
$symfonyCache,
|
||||
$eventDispatcherS
|
||||
);
|
||||
}
|
||||
}
|
||||
75
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/System/MainSystemTest.php
vendored
Normal file
75
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/System/MainSystemTest.php
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\System;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\State;
|
||||
use PrestaShop\CircuitBreaker\Place\OpenPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\HalfOpenPlace;
|
||||
use PrestaShop\CircuitBreaker\Place\ClosedPlace;
|
||||
use PrestaShop\CircuitBreaker\Contract\PlaceInterface;
|
||||
use PrestaShop\CircuitBreaker\System\MainSystem;
|
||||
|
||||
class MainSystemTest extends TestCase
|
||||
{
|
||||
public function testCreation()
|
||||
{
|
||||
$openPlace = new OpenPlace(1, 1, 1);
|
||||
$halfOpenPlace = new HalfOpenPlace(1, 1, 1);
|
||||
$closedPlace = new ClosedPlace(1, 1, 1);
|
||||
|
||||
$mainSystem = new MainSystem(
|
||||
$openPlace,
|
||||
$halfOpenPlace,
|
||||
$closedPlace
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(MainSystem::class, $mainSystem);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetInitialPlace()
|
||||
{
|
||||
$mainSystem = $this->createMainSystem();
|
||||
$initialPlace = $mainSystem->getInitialPlace();
|
||||
|
||||
$this->assertInstanceOf(PlaceInterface::class, $initialPlace);
|
||||
$this->assertSame(State::CLOSED_STATE, $initialPlace->getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetPlaces()
|
||||
{
|
||||
$mainSystem = $this->createMainSystem();
|
||||
$places = $mainSystem->getPlaces();
|
||||
|
||||
$this->assertInternalType('array', $places);
|
||||
$this->assertCount(3, $places);
|
||||
|
||||
foreach ($places as $place) {
|
||||
$this->assertInstanceOf(PlaceInterface::class, $place);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of MainSystem for tests.
|
||||
*
|
||||
* @return MainSystem
|
||||
*/
|
||||
private function createMainSystem()
|
||||
{
|
||||
$openPlace = new OpenPlace(1, 1, 1);
|
||||
$halfOpenPlace = new HalfOpenPlace(1, 1, 1);
|
||||
$closedPlace = new ClosedPlace(1, 1, 1);
|
||||
|
||||
return new MainSystem(
|
||||
$openPlace,
|
||||
$halfOpenPlace,
|
||||
$closedPlace
|
||||
);
|
||||
}
|
||||
}
|
||||
140
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Transaction/SimpleTransactionTest.php
vendored
Normal file
140
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Transaction/SimpleTransactionTest.php
vendored
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Transaction;
|
||||
|
||||
use DateTime;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
use PrestaShop\CircuitBreaker\Contract\PlaceInterface;
|
||||
use PrestaShop\CircuitBreaker\Transaction\SimpleTransaction;
|
||||
|
||||
class SimpleTransactionTest extends TestCase
|
||||
{
|
||||
public function testCreation()
|
||||
{
|
||||
$placeStub = $this->createPlaceStub();
|
||||
|
||||
$simpleTransaction = new SimpleTransaction(
|
||||
'http://some-uri.domain',
|
||||
0,
|
||||
$placeStub->getState(),
|
||||
2
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(SimpleTransaction::class, $simpleTransaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetService()
|
||||
{
|
||||
$simpleTransaction = $this->createSimpleTransaction();
|
||||
|
||||
$this->assertSame('http://some-uri.domain', $simpleTransaction->getService());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetFailures()
|
||||
{
|
||||
$simpleTransaction = $this->createSimpleTransaction();
|
||||
|
||||
$this->assertSame(0, $simpleTransaction->getFailures());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetState()
|
||||
{
|
||||
$simpleTransaction = $this->createSimpleTransaction();
|
||||
|
||||
$this->assertSame('FAKE_STATE', $simpleTransaction->getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testGetThresholdDateTime()
|
||||
{
|
||||
$simpleTransaction = $this->createSimpleTransaction();
|
||||
$expectedDateTime = (new DateTime('+2 second'))->format('d/m/Y H:i:s');
|
||||
$simpleTransactionDateTime = $simpleTransaction->getThresholdDateTime()->format('d/m/Y H:i:s');
|
||||
|
||||
$this->assertSame($expectedDateTime, $simpleTransactionDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
* @depends testGetFailures
|
||||
*/
|
||||
public function testIncrementFailures()
|
||||
{
|
||||
$simpleTransaction = $this->createSimpleTransaction();
|
||||
$simpleTransaction->incrementFailures();
|
||||
|
||||
$this->assertSame(1, $simpleTransaction->getFailures());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreation
|
||||
*/
|
||||
public function testCreationFromPlaceHelper()
|
||||
{
|
||||
$simpleTransactionFromHelper = SimpleTransaction::createFromPlace(
|
||||
$this->createPlaceStub(),
|
||||
'http://some-uri.domain'
|
||||
);
|
||||
|
||||
$simpleTransaction = $this->createSimpleTransaction();
|
||||
|
||||
$this->assertSame($simpleTransactionFromHelper->getState(), $simpleTransaction->getState());
|
||||
$this->assertSame($simpleTransactionFromHelper->getFailures(), $simpleTransaction->getFailures());
|
||||
$fromPlaceDate = $simpleTransactionFromHelper->getThresholdDateTime()->format('d/m/Y H:i:s');
|
||||
$expectedDate = $simpleTransaction->getThresholdDateTime()->format('d/m/Y H:i:s');
|
||||
|
||||
$this->assertSame($fromPlaceDate, $expectedDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of SimpleTransaction for tests.
|
||||
*
|
||||
* @return SimpleTransaction
|
||||
*/
|
||||
private function createSimpleTransaction()
|
||||
{
|
||||
$placeStub = $this->createPlaceStub();
|
||||
|
||||
return new SimpleTransaction(
|
||||
'http://some-uri.domain',
|
||||
0,
|
||||
$placeStub->getState(),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of Place with State equals to "FAKE_STATE"
|
||||
* and threshold equals to 2.
|
||||
*
|
||||
* @return PlaceInterface&PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private function createPlaceStub()
|
||||
{
|
||||
$placeStub = $this->createMock(PlaceInterface::class);
|
||||
|
||||
$placeStub->expects($this->any())
|
||||
->method('getState')
|
||||
->willReturn('FAKE_STATE')
|
||||
;
|
||||
|
||||
$placeStub->expects($this->any())
|
||||
->method('getThreshold')
|
||||
->willReturn(2)
|
||||
;
|
||||
|
||||
return $placeStub;
|
||||
}
|
||||
}
|
||||
91
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Util/AssertTest.php
vendored
Normal file
91
modules/ps_mbo/vendor/prestashop/circuit-breaker/tests/Util/AssertTest.php
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\PrestaShop\CircuitBreaker\Util;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PrestaShop\CircuitBreaker\Util\Assert;
|
||||
use stdClass;
|
||||
|
||||
class AssertTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getValues
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testIsPositiveValue($value, $expected)
|
||||
{
|
||||
$this->assertSame($expected, Assert::isPositiveValue($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getURIs
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testIsURI($value, $expected)
|
||||
{
|
||||
$this->assertSame($expected, Assert::isURI($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getStrings
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testIsString($value, $expected)
|
||||
{
|
||||
$this->assertSame($expected, Assert::isString($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getValues()
|
||||
{
|
||||
return [
|
||||
'0' => [0, true],
|
||||
'str_0' => ['0', false],
|
||||
'float' => [0.1, true],
|
||||
'stdclass' => [new stdClass(), false],
|
||||
'callable' => [
|
||||
function () {
|
||||
return 0;
|
||||
},
|
||||
false,
|
||||
],
|
||||
'negative' => [-1, false],
|
||||
'bool' => [false, false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getURIs()
|
||||
{
|
||||
return [
|
||||
'valid' => ['http://www.prestashop.com', true],
|
||||
'int' => [0, false],
|
||||
'null' => [null, false],
|
||||
'bool' => [false, false],
|
||||
'local' => ['http://localhost', true],
|
||||
'ssh' => ['ssh://git@git.example.com/FOO/my_project.git', true],
|
||||
];
|
||||
}
|
||||
|
||||
public function getStrings()
|
||||
{
|
||||
return [
|
||||
'valid' => ['foo', true],
|
||||
'empty' => ['', false],
|
||||
'null' => [null, false],
|
||||
'bool' => [false, false],
|
||||
'stdclass' => [new stdClass(), false],
|
||||
'valid2' => ['INVALID_STATE', true],
|
||||
];
|
||||
}
|
||||
}
|
||||
108
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/.github/workflows/accounts-qc-php.yml
vendored
Normal file
108
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/.github/workflows/accounts-qc-php.yml
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
name: PsAccounts Installer Quality Control PHP
|
||||
|
||||
on: [push]
|
||||
|
||||
env:
|
||||
PHP_VERSION: 7.4
|
||||
|
||||
jobs:
|
||||
php-linter:
|
||||
name: PHP Syntax check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: PHP syntax checker 5.6
|
||||
uses: prestashop/github-action-php-lint/5.6@master
|
||||
|
||||
- name: PHP syntax checker 7.4
|
||||
uses: prestashop/github-action-php-lint/7.4@master
|
||||
|
||||
- name: PHP syntax checker 8.0
|
||||
uses: prestashop/github-action-php-lint/8.0@master
|
||||
|
||||
- name: PHP syntax checker 8.1
|
||||
uses: prestashop/github-action-php-lint/8.1@master
|
||||
|
||||
php-cs-fixer:
|
||||
name: PHP-CS-FIXER
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Run PHP-CS-Fixer
|
||||
uses: prestashopcorp/github-action-php-cs-fixer@master
|
||||
|
||||
phpstan:
|
||||
name: PHPSTAN
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
presta-versions: ['latest', '8.0', '1.7', '1.7.0.3', '1.6.1.21', '1.6.1.0']
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ env.PHP_VERSION }}
|
||||
|
||||
- name: Cache vendor folder
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: vendor
|
||||
key: php-${{ hashFiles('composer.lock') }}
|
||||
|
||||
- name: Cache composer folder
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.composer/cache
|
||||
key: php-composer-cache
|
||||
|
||||
- name: git clone ps_accounts
|
||||
run: |
|
||||
git clone https://${{ secrets.ACCESS_TOKEN }}@github.com/PrestaShopCorp/ps_accounts.git ~/ps_accounts
|
||||
cd ~/ps_accounts
|
||||
|
||||
- name: composer install
|
||||
run: composer install --prefer-dist && cd ~/ps_accounts && composer install --prefer-dist
|
||||
|
||||
- name: Pull PrestaShop files (Tag ${{ matrix.presta-versions }})
|
||||
run: docker run -tid --rm -v ps-volume:/var/www/html --name temp-ps prestashop/prestashop:${{ matrix.presta-versions }}
|
||||
|
||||
- name: Select .neon file to run with PHPStan
|
||||
id: neon
|
||||
run: |
|
||||
PS_VERSION=$(docker exec temp-ps bash -c 'echo "$PS_VERSION"')
|
||||
[[ "${PS_VERSION:0:3}" != '1.7' ]] && echo ::set-output name=filename::phpstan-PS-1.6.neon || echo ::set-output name=filename::phpstan-PS-1.7.neon
|
||||
|
||||
- name : Run PHPStan
|
||||
run: docker run --rm --volumes-from temp-ps -v $PWD:/web/module -v ~/ps_accounts:/web/ps_accounts -e _PS_ROOT_DIR_=/var/www/html --workdir=/web/module phpstan/phpstan:0.12 analyse --configuration=/web/module/tests/phpstan/${{steps.neon.outputs.filename}}
|
||||
|
||||
phpunit:
|
||||
name: PHPUNIT
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout project
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ env.PHP_VERSION }}
|
||||
coverage: xdebug
|
||||
|
||||
- name: Cache vendor folder
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: vendor
|
||||
key: php-${{ hashFiles('composer.lock') }}
|
||||
|
||||
- name: composer install missing files
|
||||
run: composer install --prefer-dist
|
||||
|
||||
- name: PHPUnit tests
|
||||
run: composer test
|
||||
7
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/.gitignore
vendored
Normal file
7
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
### IDE ###
|
||||
.idea
|
||||
|
||||
### composer ###
|
||||
vendor
|
||||
composer-dev.json
|
||||
composer-dev.lock
|
||||
12
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/.php_cs.dist
vendored
Normal file
12
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/.php_cs.dist
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
$config = new PrestaShop\CodingStandards\CsFixer\Config();
|
||||
|
||||
$config
|
||||
->setUsingCache(false)
|
||||
->getFinder()
|
||||
->in(__DIR__)
|
||||
->exclude('vendor')
|
||||
->exclude('tests');
|
||||
|
||||
return $config;
|
||||
21
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/LICENSE
vendored
Normal file
21
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/LICENSE
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 PrestaShopCorp
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
107
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/README.md
vendored
Normal file
107
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/README.md
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
# prestashop-accounts-installer
|
||||
|
||||
Utility package to install `ps_accounts` module or present data to trigger manual install from psx configuration page.
|
||||
|
||||
This module also give you access to `ps_accounts` services through its module service container dealing with the installation status of the module.
|
||||
|
||||
### Compatibility Matrix
|
||||
|
||||
We aims to follow partially the Prestashop compatibility charts
|
||||
- [Compatibility Chart Prestashop 1.6 & 1.7](https://devdocs.prestashop.com/1.7/basics/installation/system-requirements/#php-compatibility-chart)
|
||||
- [Compatibility Chart Prestashop 8](https://devdocs.prestashop.com/8/basics/installation/system-requirements/#php-compatibility-chart)
|
||||
|
||||
| ps_account version | Prestashop Version | PHP Version | Event Bus installation
|
||||
|-----------------------------------------|----------------------|-----------------|-------------------------
|
||||
| 6.x | >=8.0.0 | ≥7.2 \|\| ≤8.1 | Yes
|
||||
| 5.x | >=1.7.0 \|\| <8.0.0 | ≥5.6 \|\| ≤7.4 | Yes
|
||||
| 5.x | >=1.6.1 \|\| <1.7.0 | ≥5.6 \|\| ≤7.4 | No
|
||||
|
||||
## Installation
|
||||
|
||||
This package is available on [Packagist](https://packagist.org/packages/prestashop/prestashop-accounts-installer),
|
||||
you can install it via [Composer](https://getcomposer.org).
|
||||
|
||||
```shell script
|
||||
composer require prestashop/prestashop-accounts-installer
|
||||
```
|
||||
|
||||
## Register as a service in your PSx container (recommended)
|
||||
|
||||
Example :
|
||||
|
||||
```yaml
|
||||
services:
|
||||
<your_module>.ps_accounts_installer:
|
||||
class: 'PrestaShop\PsAccountsInstaller\Installer\Installer'
|
||||
arguments:
|
||||
- '5.0.0'
|
||||
|
||||
<your_module>.ps_accounts_facade:
|
||||
class: 'PrestaShop\PsAccountsInstaller\Installer\Facade\PsAccounts'
|
||||
arguments:
|
||||
- '@<your_module>.ps_accounts_installer'
|
||||
```
|
||||
|
||||
The name under which you register both services in your service container must be unique to avoid collision with other modules including it.
|
||||
|
||||
The `5.0.0` specified argument is the minimum required `ps_account` module version. You should modify it if you need another version.
|
||||
|
||||
## How to use it
|
||||
|
||||
### Installer
|
||||
|
||||
In your module main class `install` method. (Will only do something on PrestaShop 1.7 and above)
|
||||
|
||||
```php
|
||||
$this->getService('ps_accounts.installer')->install();
|
||||
```
|
||||
|
||||
### Presenter
|
||||
|
||||
For example in your main module's class `getContent` method.
|
||||
|
||||
```php
|
||||
Media::addJsDef([
|
||||
'contextPsAccounts' => $this->getService('ps_accounts.facade')
|
||||
->getPsAccountsPresenter()
|
||||
->present($this->name),
|
||||
]);
|
||||
```
|
||||
|
||||
This presenter will serve as default minimal presenter and switch to PsAccountsPresenter data when `ps_accounts` module is installed.
|
||||
|
||||
### Accessing PsAccounts Services
|
||||
|
||||
Installer class includes accessors to get instances of services from PsAccounts Module :
|
||||
|
||||
* getPsAccountsService
|
||||
* getPsBillingService
|
||||
|
||||
The methods above will throw an exception in case `ps_accounts` module is not installed or not in the required version.
|
||||
|
||||
Example :
|
||||
|
||||
```php
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Exception\ModuleVersionException;
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Exception\ModuleNotInstalledException;
|
||||
|
||||
try {
|
||||
$psAccountsService = $this->getService('ps_accounts.facade')->getPsAccountsService();
|
||||
|
||||
$shopJwt = $psAccountsService->getOrRefreshToken();
|
||||
|
||||
$shopUuid = $psAccountsService->getShopUuid();
|
||||
|
||||
$apiUrl = $psAccountsService->getAdminAjaxUrl();
|
||||
|
||||
// Your code here
|
||||
|
||||
} catch (ModuleNotInstalledException $e) {
|
||||
|
||||
// You handle exception here
|
||||
|
||||
} catch (ModuleVersionException $e) {
|
||||
|
||||
// You handle exception here
|
||||
}
|
||||
```
|
||||
28
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/composer.json
vendored
Normal file
28
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/composer.json
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "prestashop/prestashop-accounts-installer",
|
||||
"type": "library",
|
||||
"description": "Utility package to install `ps_accounts` module or present data to trigger manual install from psx configuration page.",
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PrestaShop\\PsAccountsInstaller\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"PrestaShop\\PsAccountsInstaller\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.6"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^5.7",
|
||||
"prestashop/php-dev-tools": "3.*",
|
||||
"friendsofphp/php-cs-fixer": "^2.16",
|
||||
"fzaninotto/faker": "^1.9"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "XDEBUG_MODE=coverage ./vendor/phpunit/phpunit/phpunit --configuration './phpunit.xml' --bootstrap './tests/bootstrap.php' --test-suffix 'Test.php,.phpt' --coverage-text"
|
||||
}
|
||||
}
|
||||
3151
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/composer.lock
generated
vendored
Normal file
3151
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/composer.lock
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
13
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/phpunit.xml
vendored
Normal file
13
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/phpunit.xml
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="./tests/bootstrap.php" colors="true" stopOnFailure="false">
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<testsuites>
|
||||
<testsuite name="prestashop-accounts-installer tests">
|
||||
<directory suffix="Test.php">./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\PsAccountsInstaller\Installer\Exception;
|
||||
|
||||
class InstallerException extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\PsAccountsInstaller\Installer\Exception;
|
||||
|
||||
class ModuleNotInstalledException extends InstallerException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\PsAccountsInstaller\Installer\Exception;
|
||||
|
||||
class ModuleVersionException extends InstallerException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\PsAccountsInstaller\Installer\Facade;
|
||||
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Exception\ModuleNotInstalledException;
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Exception\ModuleVersionException;
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Installer;
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Presenter\InstallerPresenter;
|
||||
|
||||
class PsAccounts
|
||||
{
|
||||
/**
|
||||
* Available services class names
|
||||
*/
|
||||
const PS_ACCOUNTS_PRESENTER = 'PrestaShop\Module\PsAccounts\Presenter\PsAccountsPresenter';
|
||||
const PS_ACCOUNTS_SERVICE = 'PrestaShop\Module\PsAccounts\Service\PsAccountsService';
|
||||
const PS_BILLING_SERVICE = 'PrestaShop\Module\PsAccounts\Service\PsBillingService';
|
||||
|
||||
/**
|
||||
* @var Installer
|
||||
*/
|
||||
private $installer;
|
||||
|
||||
/**
|
||||
* PsAccounts constructor.
|
||||
*
|
||||
* @param Installer $installer
|
||||
*/
|
||||
public function __construct(Installer $installer)
|
||||
{
|
||||
$this->installer = $installer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $serviceName
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws ModuleNotInstalledException
|
||||
* @throws ModuleVersionException
|
||||
*/
|
||||
public function getService($serviceName)
|
||||
{
|
||||
if ($this->installer->isModuleInstalled()) {
|
||||
if ($this->installer->checkModuleVersion()) {
|
||||
return \Module::getInstanceByName($this->installer->getModuleName())
|
||||
->getService($serviceName);
|
||||
}
|
||||
throw new ModuleVersionException('The current version of the module "' . $this->installer->getModuleName() . '" is below the required one and should be upgraded. The minimum expected version is: ' . $this->installer->getModuleVersion());
|
||||
}
|
||||
throw new ModuleNotInstalledException('Module not installed : ' . $this->installer->getModuleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*
|
||||
* @throws ModuleNotInstalledException
|
||||
* @throws ModuleVersionException
|
||||
*/
|
||||
public function getPsAccountsService()
|
||||
{
|
||||
return $this->getService(self::PS_ACCOUNTS_SERVICE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*
|
||||
* @throws ModuleNotInstalledException
|
||||
* @throws ModuleVersionException
|
||||
*/
|
||||
public function getPsBillingService()
|
||||
{
|
||||
return $this->getService(self::PS_BILLING_SERVICE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*
|
||||
* @throws ModuleNotInstalledException
|
||||
* @throws ModuleVersionException
|
||||
*/
|
||||
public function getPsAccountsPresenter()
|
||||
{
|
||||
if ($this->installer->isModuleInstalled() &&
|
||||
$this->installer->checkModuleVersion() &&
|
||||
$this->installer->isModuleEnabled()
|
||||
) {
|
||||
return $this->getService(self::PS_ACCOUNTS_PRESENTER);
|
||||
} else {
|
||||
return new InstallerPresenter($this->installer);
|
||||
}
|
||||
}
|
||||
}
|
||||
242
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/src/Installer/Installer.php
vendored
Normal file
242
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/src/Installer/Installer.php
vendored
Normal file
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\PsAccountsInstaller\Installer;
|
||||
|
||||
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
|
||||
use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder;
|
||||
|
||||
class Installer
|
||||
{
|
||||
const PS_ACCOUNTS_MODULE_NAME = 'ps_accounts';
|
||||
|
||||
/**
|
||||
* @var string required version
|
||||
*/
|
||||
private $moduleVersion;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $moduleName = self::PS_ACCOUNTS_MODULE_NAME;
|
||||
|
||||
/**
|
||||
* @var \Link
|
||||
*/
|
||||
private $link;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $moduleManager;
|
||||
|
||||
/**
|
||||
* Installer constructor.
|
||||
*
|
||||
* @param string $psAccountsVersion
|
||||
* @param \Link|null $link
|
||||
*/
|
||||
public function __construct($psAccountsVersion, \Link $link = null)
|
||||
{
|
||||
$this->moduleVersion = $psAccountsVersion;
|
||||
|
||||
if (null === $link) {
|
||||
$link = new \Link();
|
||||
}
|
||||
$this->link = $link;
|
||||
|
||||
if (true === $this->isShopVersion17()) {
|
||||
$moduleManagerBuilder = ModuleManagerBuilder::getInstance();
|
||||
$this->moduleManager = $moduleManagerBuilder->build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Install ps_accounts module if not installed
|
||||
* Method to call in every psx modules during the installation process
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function install()
|
||||
{
|
||||
if (true === $this->isModuleInstalled()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (false === $this->isShopVersion17()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->moduleManager->install($this->getModuleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isModuleInstalled()
|
||||
{
|
||||
if (false === $this->isShopVersion17()) {
|
||||
return \Module::isInstalled($this->getModuleName());
|
||||
}
|
||||
|
||||
return $this->moduleManager->isInstalled($this->getModuleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isModuleEnabled()
|
||||
{
|
||||
if (false === $this->isShopVersion17()) {
|
||||
return \Module::isEnabled($this->getModuleName());
|
||||
}
|
||||
|
||||
return $this->moduleManager->isEnabled($this->getModuleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*
|
||||
* @throws \PrestaShopException
|
||||
*/
|
||||
public function getInstallLink()
|
||||
{
|
||||
if ($this->isShopVersion173()) {
|
||||
$router = SymfonyContainer::getInstance()->get('router');
|
||||
|
||||
return \Tools::getHttpHost(true) . $router->generate('admin_module_manage_action', [
|
||||
'action' => 'install',
|
||||
'module_name' => $this->moduleName,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->getAdminLink('AdminModules', true, [], [
|
||||
'module_name' => $this->moduleName,
|
||||
'install' => $this->moduleName,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*
|
||||
* @throws \PrestaShopException
|
||||
*/
|
||||
public function getEnableLink()
|
||||
{
|
||||
if ($this->isShopVersion173()) {
|
||||
$router = SymfonyContainer::getInstance()->get('router');
|
||||
|
||||
return \Tools::getHttpHost(true) . $router->generate('admin_module_manage_action', [
|
||||
'action' => 'enable',
|
||||
'module_name' => $this->moduleName,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->getAdminLink('AdminModules', true, [], [
|
||||
'module_name' => $this->moduleName,
|
||||
'enable' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*
|
||||
* @throws \PrestaShopException
|
||||
*/
|
||||
public function getUpgradeLink()
|
||||
{
|
||||
if ($this->isShopVersion173()) {
|
||||
$router = SymfonyContainer::getInstance()->get('router');
|
||||
|
||||
return \Tools::getHttpHost(true) . $router->generate('admin_module_manage_action', [
|
||||
'action' => 'upgrade',
|
||||
'module_name' => $this->moduleName,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->getAdminLink('AdminModules', true, [], [
|
||||
'module_name' => $this->moduleName,
|
||||
'upgrade' => $this->moduleName,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isShopVersion17()
|
||||
{
|
||||
return version_compare(_PS_VERSION_, '1.7.0.0', '>=');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isShopVersion173()
|
||||
{
|
||||
return version_compare(_PS_VERSION_, '1.7.3.0', '>=');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function checkModuleVersion()
|
||||
{
|
||||
$module = \Module::getInstanceByName($this->getModuleName());
|
||||
|
||||
if ($module instanceof \Ps_accounts) {
|
||||
return version_compare(
|
||||
$module->version,
|
||||
$this->moduleVersion,
|
||||
'>='
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getModuleVersion()
|
||||
{
|
||||
return $this->moduleVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getModuleName()
|
||||
{
|
||||
return $this->moduleName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapter for getAdminLink from prestashop link class
|
||||
*
|
||||
* @param string $controller controller name
|
||||
* @param bool $withToken include or not the token in the url
|
||||
* @param array $sfRouteParams
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \PrestaShopException
|
||||
*/
|
||||
protected function getAdminLink($controller, $withToken = true, $sfRouteParams = [], $params = [])
|
||||
{
|
||||
if ($this->isShopVersion17()) {
|
||||
return $this->link->getAdminLink($controller, $withToken, $sfRouteParams, $params);
|
||||
}
|
||||
$paramsAsString = '';
|
||||
foreach ($params as $key => $value) {
|
||||
$paramsAsString .= "&$key=$value";
|
||||
}
|
||||
|
||||
return \Tools::getShopDomainSsl(true)
|
||||
. __PS_BASE_URI__
|
||||
. basename(_PS_ADMIN_DIR_)
|
||||
. '/' . $this->link->getAdminLink($controller, $withToken)
|
||||
. $paramsAsString;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\PsAccountsInstaller\Installer\Presenter;
|
||||
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Installer;
|
||||
|
||||
class InstallerPresenter
|
||||
{
|
||||
/**
|
||||
* @var Installer
|
||||
*/
|
||||
private $installer;
|
||||
|
||||
/**
|
||||
* @var \Context
|
||||
*/
|
||||
private $context;
|
||||
|
||||
/**
|
||||
* InstallerPresenter constructor.
|
||||
*
|
||||
* @param Installer $installer
|
||||
* @param \Context|null $context
|
||||
*/
|
||||
public function __construct(Installer $installer, \Context $context = null)
|
||||
{
|
||||
$this->installer = $installer;
|
||||
|
||||
if (null === $context) {
|
||||
$context = \Context::getContext();
|
||||
}
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function present()
|
||||
{
|
||||
// Fallback minimal Presenter
|
||||
return [
|
||||
'psIs17' => $this->installer->isShopVersion17(),
|
||||
|
||||
'psAccountsInstallLink' => $this->installer->getInstallLink(),
|
||||
'psAccountsEnableLink' => $this->installer->getEnableLink(),
|
||||
'psAccountsUpdateLink' => $this->installer->getUpgradeLink(),
|
||||
|
||||
'psAccountsIsInstalled' => $this->installer->isModuleInstalled(),
|
||||
'psAccountsIsEnabled' => $this->installer->isModuleEnabled(),
|
||||
'psAccountsIsUptodate' => $this->installer->checkModuleVersion(),
|
||||
|
||||
'onboardingLink' => null,
|
||||
'user' => [
|
||||
'email' => null,
|
||||
'emailIsValidated' => false,
|
||||
'isSuperAdmin' => $this->isEmployeeSuperAdmin(),
|
||||
],
|
||||
'currentShop' => null,
|
||||
'shops' => [],
|
||||
'superAdminEmail' => null,
|
||||
'ssoResendVerificationEmail' => null,
|
||||
'manageAccountLink' => null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmployeeSuperAdmin()
|
||||
{
|
||||
return $this->context->employee->isSuperAdmin();
|
||||
}
|
||||
}
|
||||
23
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/tests/TestCase.php
vendored
Normal file
23
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/tests/TestCase.php
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace PrestaShop\PsAccountsInstaller\Tests;
|
||||
|
||||
use Faker\Generator;
|
||||
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var Generator
|
||||
*/
|
||||
public $faker;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->faker = \Faker\Factory::create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\PsAccountsInstaller\Tests\Unit\Installer\Presenter\InstallerPresenter;
|
||||
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Installer;
|
||||
use PrestaShop\PsAccountsInstaller\Installer\Presenter\InstallerPresenter;
|
||||
use PrestaShop\PsAccountsInstaller\Tests\TestCase;
|
||||
|
||||
class PresentTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function itShouldBeCompliantWithPsAccountsPresenter()
|
||||
{
|
||||
$expected = [
|
||||
'psIs17' => $this->faker->boolean,
|
||||
'psAccountsInstallLink' => $this->faker->url,
|
||||
'psAccountsEnableLink' => $this->faker->url,
|
||||
'psAccountsUpdateLink' => $this->faker->url,
|
||||
'psAccountsIsInstalled' => $this->faker->boolean,
|
||||
'psAccountsIsEnabled' => $this->faker->boolean,
|
||||
'psAccountsIsUptodate' => $this->faker->boolean,
|
||||
'onboardingLink' => null,
|
||||
'user' => [
|
||||
'email' => null,
|
||||
'emailIsValidated' => false,
|
||||
'isSuperAdmin' => true,
|
||||
],
|
||||
'currentShop' => null,
|
||||
'shops' => [],
|
||||
'superAdminEmail' => null,
|
||||
'ssoResendVerificationEmail' => null,
|
||||
'manageAccountLink' => null,
|
||||
];
|
||||
|
||||
$installer = $this->createConfiguredMock(Installer::class, [
|
||||
'isShopVersion17' => $expected['psIs17'],
|
||||
'isModuleInstalled' => $expected['psAccountsIsInstalled'],
|
||||
'getInstallLink' => $expected['psAccountsInstallLink'],
|
||||
'isModuleEnabled' => $expected['psAccountsIsEnabled'],
|
||||
'getEnableLink' => $expected['psAccountsEnableLink'],
|
||||
'checkModuleVersion' => $expected['psAccountsIsUptodate'],
|
||||
'getUpgradeLink' => $expected['psAccountsUpdateLink'],
|
||||
]);
|
||||
|
||||
$presenter = $this->getMockBuilder(InstallerPresenter::class)
|
||||
//->disableOriginalConstructor()
|
||||
->setConstructorArgs([$installer, new \Context()])
|
||||
->disableOriginalClone()
|
||||
->disableArgumentCloning()
|
||||
->disallowMockingUnknownTypes()
|
||||
->setMethods([
|
||||
'isEmployeeSuperAdmin',
|
||||
])
|
||||
->getMock();
|
||||
|
||||
$presenter->method('isEmployeeSuperAdmin')
|
||||
->willReturn(true);
|
||||
|
||||
/** @var InstallerPresenter $presenter */
|
||||
$presenterData = $presenter->present();
|
||||
|
||||
$this->assertArraySubset($expected, $presenterData);
|
||||
}
|
||||
}
|
||||
6
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/tests/bootstrap.php
vendored
Normal file
6
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/tests/bootstrap.php
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// Empty shell for mocking
|
||||
class Context {}
|
||||
27
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/tests/phpstan/phpstan-PS-1.6.neon
vendored
Normal file
27
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/tests/phpstan/phpstan-PS-1.6.neon
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
includes:
|
||||
- %currentWorkingDirectory%/vendor/prestashop/php-dev-tools/phpstan/ps-module-extension.neon
|
||||
parameters:
|
||||
autoload_files:
|
||||
- /web/ps_accounts/vendor/autoload.php
|
||||
checkMissingIterableValueType: false
|
||||
reportUnmatchedIgnoredErrors: false
|
||||
paths:
|
||||
- ../../src
|
||||
dynamicConstantNames:
|
||||
- _PS_VERSION_
|
||||
ignoreErrors:
|
||||
- '#Result of \|\| is always false.#'
|
||||
- '#Strict comparison using === between false and string will always evaluate to false.#'
|
||||
- '#Access to an undefined property Language::\$locale.#'
|
||||
- '#Call to static method getInstance\(\) on an unknown class PrestaShop\\PrestaShop\\Adapter\\SymfonyContainer.#'
|
||||
- '#Call to static method getInstance\(\) on an unknown class PrestaShop\\PrestaShop\\Core\\Addon\\Module\\ModuleManagerBuilder.#'
|
||||
- '#Call to method get\(\) on an unknown class Symfony\\Component\\DependencyInjection\\ContainerInterface.#'
|
||||
- '#Property PrestaShop\\AccountsAuth\\Service\\PsAccountsService::\$container has unknown class Symfony\\Component\\DependencyInjection\\ContainerInterface as its type.#'
|
||||
- '#Property PrestaShop\\AccountsAuth\\Service\\PsBillingService::\$container has unknown class Symfony\\Component\\DependencyInjection\\ContainerInterface as its type.#'
|
||||
- '#Method LinkCore::getAdminLink\(\) invoked with 4 parameters, 1-2 required.#'
|
||||
- '#Class PrestaShop\\Module\\PsAccounts\\Presenter\\PsAccountsPresenter not found.#'
|
||||
- '#Call to method get\(\) on an unknown class PrestaShop\\PrestaShop\\Adapter\\SymfonyContainer.#'
|
||||
- '#Call to an undefined method object::generate\(\).#'
|
||||
- '#Cannot call method getService\(\) on ModuleCore|false.#'
|
||||
|
||||
level: 7
|
||||
22
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/tests/phpstan/phpstan-PS-1.7.neon
vendored
Normal file
22
modules/ps_mbo/vendor/prestashop/prestashop-accounts-installer/tests/phpstan/phpstan-PS-1.7.neon
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
includes:
|
||||
- %currentWorkingDirectory%/vendor/prestashop/php-dev-tools/phpstan/ps-module-extension.neon
|
||||
parameters:
|
||||
autoload_files:
|
||||
- /web/ps_accounts/vendor/autoload.php
|
||||
checkMissingIterableValueType: false
|
||||
reportUnmatchedIgnoredErrors: false
|
||||
paths:
|
||||
- ../../src
|
||||
dynamicConstantNames:
|
||||
- _PS_VERSION_
|
||||
ignoreErrors:
|
||||
- '#Result of \|\| is always false.#'
|
||||
- '#Strict comparison using === between false and string will always evaluate to false.#'
|
||||
- '#Call to static method getInstance\(\) on an unknown class PrestaShop\\PrestaShop\\Adapter\\SymfonyContainer.#'
|
||||
- '#Method LinkCore::getAdminLink\(\) invoked with 4 parameters, 1-2 required.#'
|
||||
- '#Class PrestaShop\\Module\\PsAccounts\\Presenter\\PsAccountsPresenter not found.#'
|
||||
- '#Call to method get\(\) on an unknown class PrestaShop\\PrestaShop\\Adapter\\SymfonyContainer.#'
|
||||
- '#Call to an undefined method object::generate\(\).#'
|
||||
- '#Cannot call method getService\(\) on ModuleCore|false.#'
|
||||
|
||||
level: 7
|
||||
Reference in New Issue
Block a user