diff --git a/.gitignore b/.gitignore index f36bee7..df5c186 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,6 @@ build/ composer.lock nbproject -tmp/ \ No newline at end of file +tmp/ +vendor/ +.phpunit.result.cache diff --git a/README.md b/README.md index cb18b9b..cbdcf77 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ Notice, the extra parameters you pass to the factory’s getCommand method are f The factory is instantiated as follows: ```php -use Zend\Config\Config; +use Laminas\Config\Config; use Odesk\Phystrix\ApcStateStorage; use Odesk\Phystrix\CircuitBreakerFactory; use Odesk\Phystrix\CommandMetricsFactory; @@ -87,12 +87,12 @@ $circuitBreakerFactory = new CircuitBreakerFactory($stateStorage); $commandMetricsFactory = new CommandMetricsFactory($stateStorage); $phystrix = new CommandFactory( - $config, new \Zend\Di\ServiceLocator(), $circuitBreakerFactory, $commandMetricsFactory, + $config, new \Laminas\Di\ServiceLocator(), $circuitBreakerFactory, $commandMetricsFactory, new \Odesk\Phystrix\RequestCache(), new \Odesk\Phystrix\RequestLog() ); ``` -The way you store the configuration files is up to you. Phystrix relies on [Zend\Config](https://github.com/zendframework/Component_ZendConfig) to manage configurations. In this case, __phystrix-config.php__ is a PHP array: +The way you store the configuration files is up to you. Phystrix relies on [Laminas\Config](https://github.com/zendframework/Component_ZendConfig) to manage configurations. In this case, __phystrix-config.php__ is a PHP array: ```php return array( @@ -160,7 +160,7 @@ Phystrix only works with the command keys. If you have two different commands wi Sometimes, you may need to change a parameter when a command is used in a particular context: ```php -use Zend\Config\Config; +use Laminas\Config\Config; $myCommand = $phystrix->getCommand('MyCommand', 'Alex'); $myCommand->setConfig(new Config(array('requestCache' => array('enabled' => false)))); $result = $myCommand->execute(); @@ -258,7 +258,7 @@ where “timeout” is a custom parameter which Phystrix does not make any use o } ``` -where the client might be a 3rd library you downloaded, or an instance of http client from a framework such as Zend Framework or Symfony or something you wrote yourself. +where the client might be a 3rd library you downloaded, or an instance of http client from a framework such as Laminas Framework or Symfony or something you wrote yourself. Of course, having to add this into each command would be suboptimal. Normally, you will have a set of abstract commands, specific to your use cases. E.g. you might have __GenericCurlCommand__ or __GenericGoogleApiCommand__ and __MyCommand__ would extend one of those. @@ -270,10 +270,10 @@ One way would be to extend the __Odesk\Phystrix\CommandFactory__, create your ow Alternatively, configure the locator instance that __Odesk\Phystrix\CommandFactory__ accepts in the constructor. -The service locator can be anything, implementing the very basic [Zend\Di\LocatorInterface](https://github.com/zendframework/zf2/blob/master/library/Zend/Di/LocatorInterface.php). You can inject an IoC container that will lazily instantiate instance as they are needed, or you can use a simpler, preconfigured, instance of __Zend\Di\ServiceLocator__: +The service locator can be anything, implementing the very basic [Laminas\Di\LocatorInterface](https://github.com/zendframework/zf2/blob/master/library/Zend/Di/LocatorInterface.php). You can inject an IoC container that will lazily instantiate instance as they are needed, or you can use a simpler, preconfigured, instance of __Laminas\Di\ServiceLocator__: ```php -$serviceLocator = \Zend\Di\ServiceLocator(); +$serviceLocator = \Laminas\Di\ServiceLocator(); $googleApiRemoteService = new GoogleApi(...); $serviceLocator->set('googleApi', $googleApiRemoteService); diff --git a/composer.json b/composer.json index 8ce84eb..e8935d0 100644 --- a/composer.json +++ b/composer.json @@ -12,12 +12,13 @@ } }, "require": { - "php": ">=5.3.3", - "zendframework/zend-config": "~2.2", - "zendframework/zend-di": "~2.2" + "php": ">=7.4", + "laminas/laminas-config": "~2.2", + "laminas/laminas-di": "~2.2" }, "require-dev": { - "phpunit/phpunit": "~4.2" + "roave/security-advisories": "dev-latest", + "phpunit/phpunit": "^9.5" }, "extra": { "branch-alias": { diff --git a/library/Odesk/Phystrix/AbstractCommand.php b/library/Odesk/Phystrix/AbstractCommand.php index 96f0430..4eb5a59 100644 --- a/library/Odesk/Phystrix/AbstractCommand.php +++ b/library/Odesk/Phystrix/AbstractCommand.php @@ -21,8 +21,8 @@ use Odesk\Phystrix\Exception\BadRequestException; use Odesk\Phystrix\Exception\FallbackNotAvailableException; use Odesk\Phystrix\Exception\RuntimeException; -use Zend\Di\LocatorInterface; -use Zend\Config\Config; +use Laminas\Di\LocatorInterface; +use Laminas\Config\Config; use Exception; /** @@ -95,7 +95,7 @@ abstract class AbstractCommand /** * Exception thrown if there was one * - * @var \Exception + * @var Exception */ private $executionException; @@ -108,17 +108,15 @@ abstract class AbstractCommand /** * Determines and returns command key, used for circuit breaker grouping and metrics tracking - * - * @return string */ - public function getCommandKey() + public function getCommandKey(): string { if ($this->commandKey) { return $this->commandKey; - } else { - // If the command key hasn't been defined in the class we use the current class name - return get_class($this); } + + // If the command key hasn't been defined in the class we use the current class name + return get_class($this); } /** @@ -168,13 +166,13 @@ public function setRequestLog(RequestLog $requestLog) */ public function initializeConfig(Config $phystrixConfig) { - $commandKey = $this->getCommandKey(); - $config = new Config($phystrixConfig->get('default')->toArray(), true); - if ($phystrixConfig->__isset($commandKey)) { - $commandConfig = $phystrixConfig->get($commandKey); - $config->merge($commandConfig); + $key = $this->getCommandKey(); + $configuration = new Config($phystrixConfig->get('default')->toArray(), true); + if ($phystrixConfig->__isset($key)) { + $commandConfig = $phystrixConfig->get($key); + $configuration->merge($commandConfig); } - $this->config = $config; + $this->config = $configuration; } /** @@ -192,6 +190,11 @@ public function setConfig(Config $config, $merge = true) } } + public function getConfig(): Config + { + return $this->config; + } + /** * Determines whether request caching is enabled for this command * diff --git a/library/Odesk/Phystrix/ApcStateStorage.php b/library/Odesk/Phystrix/ApcStateStorage.php index 15de4ab..58dfcac 100644 --- a/library/Odesk/Phystrix/ApcStateStorage.php +++ b/library/Odesk/Phystrix/ApcStateStorage.php @@ -128,7 +128,7 @@ public function allowSingleTest($commandKey, $sleepingWindowInMilliseconds) // using 'add' enforces thread safety. $sleepingWindowInSeconds = ceil($sleepingWindowInMilliseconds / 1000); // another APC limitation is that within the current request variables will never expire. - return (boolean) apc_add($singleTestFlagKey, true, $sleepingWindowInSeconds); + return apc_add($singleTestFlagKey, true, $sleepingWindowInSeconds); } /** diff --git a/library/Odesk/Phystrix/ArrayStateStorage.php b/library/Odesk/Phystrix/ArrayStateStorage.php index 0921486..1012df6 100644 --- a/library/Odesk/Phystrix/ArrayStateStorage.php +++ b/library/Odesk/Phystrix/ArrayStateStorage.php @@ -50,9 +50,7 @@ class ArrayStateStorage implements StateStorageInterface */ public function getBucket($commandKey, $type, $index) { - return isset($this->buckets[$commandKey][$type][$index]) - ? $this->buckets[$commandKey][$type][$index] - : null; + return $this->buckets[$commandKey][$type][$index] ?? null; } /** diff --git a/library/Odesk/Phystrix/CircuitBreaker.php b/library/Odesk/Phystrix/CircuitBreaker.php index 7fb0177..8b167d9 100644 --- a/library/Odesk/Phystrix/CircuitBreaker.php +++ b/library/Odesk/Phystrix/CircuitBreaker.php @@ -18,7 +18,7 @@ */ namespace Odesk\Phystrix; -use Zend\Config\Config; +use Laminas\Config\Config; /** * Circuit-breaker logic that is hooked into AbstractCommand execution and will stop allowing executions @@ -29,40 +29,17 @@ */ class CircuitBreaker implements CircuitBreakerInterface { - /** - * @var CommandMetrics - */ - private $metrics; - - /** - * Phystrix config - * - * @var Config - */ - private $config; - - /** - * @var StateStorageInterface - */ - private $stateStorage; + private CommandMetrics $metrics; + private Config $config; + private StateStorageInterface $stateStorage; /** * String identifier of the group of commands this circuit breaker is responsible for - * - * @var string */ - private $commandKey; + private string $commandKey; - /** - * Constructor - * - * @param string $commandKey - * @param CommandMetrics $metrics - * @param Config $commandConfig - * @param StateStorageInterface $stateStorage - */ public function __construct( - $commandKey, + string $commandKey, CommandMetrics $metrics, Config $commandConfig, StateStorageInterface $stateStorage @@ -73,6 +50,16 @@ public function __construct( $this->stateStorage = $stateStorage; } + public function getConfig(): Config + { + return $this->config; + } + + public function getCommandKey(): string + { + return $this->commandKey; + } + /** * Whether the circuit is open * @@ -96,21 +83,19 @@ public function isOpen() $allowedErrorPercentage = $this->config->get('circuitBreaker')->get('errorThresholdPercentage'); if ($healthCounts->getErrorPercentage() < $allowedErrorPercentage) { return false; - } else { - $this->stateStorage->openCircuit( - $this->commandKey, - $this->config->get('circuitBreaker')->get('sleepWindowInMilliseconds') - ); - return true; } + + $this->stateStorage->openCircuit( + $this->commandKey, + $this->config->get('circuitBreaker')->get('sleepWindowInMilliseconds') + ); + return true; } /** * Whether a single test is allowed now - * - * @return boolean */ - public function allowSingleTest() + public function allowSingleTest(): bool { return $this->stateStorage->allowSingleTest( $this->commandKey, @@ -120,10 +105,8 @@ public function allowSingleTest() /** * Whether the request is allowed - * - * @return boolean */ - public function allowRequest() + public function allowRequest(): bool { if ($this->config->get('circuitBreaker')->get('forceOpen')) { return false; diff --git a/library/Odesk/Phystrix/CircuitBreakerFactory.php b/library/Odesk/Phystrix/CircuitBreakerFactory.php index a6cb241..0ec2f95 100644 --- a/library/Odesk/Phystrix/CircuitBreakerFactory.php +++ b/library/Odesk/Phystrix/CircuitBreakerFactory.php @@ -18,28 +18,16 @@ */ namespace Odesk\Phystrix; -use Zend\Config\Config; +use Laminas\Config\Config; /** * Factory to keep track of and instantiate new circuit breakers when needed */ class CircuitBreakerFactory { - /** - * @var array - */ - protected $circuitBreakersByCommand = array(); - - /** - * @var StateStorageInterface - */ - protected $stateStorage; + protected array $circuitBreakersByCommand = []; + protected StateStorageInterface $stateStorage; - /** - * Constructor - * - * @param StateStorageInterface $stateStorage - */ public function __construct(StateStorageInterface $stateStorage) { $this->stateStorage = $stateStorage; diff --git a/library/Odesk/Phystrix/CommandFactory.php b/library/Odesk/Phystrix/CommandFactory.php index f650f96..823a2e4 100644 --- a/library/Odesk/Phystrix/CommandFactory.php +++ b/library/Odesk/Phystrix/CommandFactory.php @@ -19,8 +19,8 @@ namespace Odesk\Phystrix; use ReflectionClass; -use Zend\Config\Config; -use Zend\Di\LocatorInterface; +use Laminas\Config\Config; +use Laminas\Di\LocatorInterface; /** * All commands must be created through this factory. diff --git a/library/Odesk/Phystrix/CommandMetrics.php b/library/Odesk/Phystrix/CommandMetrics.php index 466f40e..01e9a78 100644 --- a/library/Odesk/Phystrix/CommandMetrics.php +++ b/library/Odesk/Phystrix/CommandMetrics.php @@ -27,15 +27,8 @@ */ class CommandMetrics { - /** - * @var MetricsCounter - */ - private $counter; - - /** - * @var integer - */ - private $healthSnapshotIntervalInMilliseconds = 1000; + private MetricsCounter $counter; + private int $healthSnapshotIntervalInMilliseconds = 1000; /** * @var HealthCountsSnapshot @@ -54,10 +47,15 @@ public function __construct(MetricsCounter $counter, $snapshotInterval) $this->healthSnapshotIntervalInMilliseconds = $snapshotInterval; } + public function getHealthSnapshotIntervalInMilliseconds(): int + { + return $this->healthSnapshotIntervalInMilliseconds; + } + /** * Increments success counter */ - public function markSuccess() + public function markSuccess(): void { $this->counter->add(MetricsCounter::SUCCESS); } @@ -65,7 +63,7 @@ public function markSuccess() /** * Increments from cache counter */ - public function markResponseFromCache() + public function markResponseFromCache(): void { $this->counter->add(MetricsCounter::RESPONSE_FROM_CACHE); } @@ -73,7 +71,7 @@ public function markResponseFromCache() /** * Increments failure counter */ - public function markFailure() + public function markFailure(): void { $this->counter->add(MetricsCounter::FAILURE); } @@ -81,7 +79,7 @@ public function markFailure() /** * Increments fallback success counter */ - public function markFallbackSuccess() + public function markFallbackSuccess(): void { $this->counter->add(MetricsCounter::FALLBACK_SUCCESS); } @@ -89,7 +87,7 @@ public function markFallbackSuccess() /** * Increments fallback failure counter */ - public function markFallbackFailure() + public function markFallbackFailure(): void { $this->counter->add(MetricsCounter::FALLBACK_FAILURE); } @@ -97,7 +95,7 @@ public function markFallbackFailure() /** * Increments exception thrown counter */ - public function markExceptionThrown() + public function markExceptionThrown(): void { $this->counter->add(MetricsCounter::EXCEPTION_THROWN); } @@ -105,7 +103,7 @@ public function markExceptionThrown() /** * Increments short circuited counter */ - public function markShortCircuited() + public function markShortCircuited(): void { $this->counter->add(MetricsCounter::SHORT_CIRCUITED); } @@ -114,7 +112,7 @@ public function markShortCircuited() * Resets counters for all metrics * may cause some stats to be removed from reporting, see http://goo.gl/dtHN34 */ - public function resetCounter() + public function resetCounter(): void { $this->counter->reset(); } diff --git a/library/Odesk/Phystrix/CommandMetricsFactory.php b/library/Odesk/Phystrix/CommandMetricsFactory.php index 093c29b..533f9da 100644 --- a/library/Odesk/Phystrix/CommandMetricsFactory.php +++ b/library/Odesk/Phystrix/CommandMetricsFactory.php @@ -18,28 +18,17 @@ */ namespace Odesk\Phystrix; -use Zend\Config\Config; +use Laminas\Config\Config; /** * Factory to keep track of and instantiate new command metrics objects when needed */ class CommandMetricsFactory { - /** - * @var array - */ - protected $commandMetricsByCommand = array(); + protected array $commandMetricsByCommand = []; - /** - * @var StateStorageInterface - */ - protected $stateStorage; + protected StateStorageInterface $stateStorage; - /** - * Constructor - * - * @param StateStorageInterface $stateStorage - */ public function __construct(StateStorageInterface $stateStorage) { $this->stateStorage = $stateStorage; diff --git a/library/Odesk/Phystrix/Exception/ApcNotLoadedException.php b/library/Odesk/Phystrix/Exception/ApcNotLoadedException.php index 06b9d77..8877d52 100644 --- a/library/Odesk/Phystrix/Exception/ApcNotLoadedException.php +++ b/library/Odesk/Phystrix/Exception/ApcNotLoadedException.php @@ -18,9 +18,11 @@ */ namespace Odesk\Phystrix\Exception; +use Exception; + /** * Throw when APC extension is not loaded. APC is required for Phystrix to work. */ -class ApcNotLoadedException extends \Exception +class ApcNotLoadedException extends Exception { } diff --git a/library/Odesk/Phystrix/Exception/BadRequestException.php b/library/Odesk/Phystrix/Exception/BadRequestException.php index ee8a400..9c2f94f 100644 --- a/library/Odesk/Phystrix/Exception/BadRequestException.php +++ b/library/Odesk/Phystrix/Exception/BadRequestException.php @@ -18,9 +18,11 @@ */ namespace Odesk\Phystrix\Exception; +use LogicException; + /** * This exception is treated differently and allows to propagate without any stats tracking or fallback logic */ -class BadRequestException extends \LogicException +class BadRequestException extends LogicException { } diff --git a/library/Odesk/Phystrix/Exception/RuntimeException.php b/library/Odesk/Phystrix/Exception/RuntimeException.php index 38254d6..bf00787 100644 --- a/library/Odesk/Phystrix/Exception/RuntimeException.php +++ b/library/Odesk/Phystrix/Exception/RuntimeException.php @@ -18,6 +18,8 @@ */ namespace Odesk\Phystrix\Exception; +use Exception; + /** * General Phystrix exception */ @@ -26,7 +28,7 @@ class RuntimeException extends \RuntimeException /** * Exception while retrieving the fallback, if enabled * - * @var \Exception + * @var Exception */ private $fallbackException; @@ -42,14 +44,14 @@ class RuntimeException extends \RuntimeException * * @param string $message * @param int $commandClass - * @param \Exception $originalException (Optional) Original exception. May be null if short-circuited - * @param \Exception $fallbackException (Optional) Exception thrown while retrieving fallback + * @param Exception $originalException (Optional) Original exception. May be null if short-circuited + * @param Exception $fallbackException (Optional) Exception thrown while retrieving fallback */ public function __construct( $message, $commandClass, - \Exception $originalException = null, - \Exception $fallbackException = null + Exception $originalException = null, + Exception $fallbackException = null ) { parent::__construct($message, 0, $originalException); $this->fallbackException = $fallbackException; @@ -69,7 +71,7 @@ public function getCommandClass() /** * Returns fallback exception if available * - * @return \Exception + * @return Exception */ public function getFallbackException() { diff --git a/library/Odesk/Phystrix/RequestLog.php b/library/Odesk/Phystrix/RequestLog.php index af45fc5..ab8b195 100644 --- a/library/Odesk/Phystrix/RequestLog.php +++ b/library/Odesk/Phystrix/RequestLog.php @@ -89,7 +89,7 @@ public function getExecutedCommandsAsString() $aggregatedCommandsExecuted[$outputForExecutedCommand] = 0; } - $aggregatedCommandsExecuted[$outputForExecutedCommand] = $aggregatedCommandsExecuted[$outputForExecutedCommand] + 1; + ++$aggregatedCommandsExecuted[$outputForExecutedCommand]; $executionTime = $executedCommand->getExecutionTimeInMilliseconds(); @@ -98,7 +98,7 @@ public function getExecutedCommandsAsString() } if (isset($aggregatedCommandExecutionTime[$outputForExecutedCommand]) && $executionTime > 0) { - $aggregatedCommandExecutionTime[$outputForExecutedCommand] = $aggregatedCommandExecutionTime[$outputForExecutedCommand] + $executionTime; + $aggregatedCommandExecutionTime[$outputForExecutedCommand] += $executionTime; } else { $aggregatedCommandExecutionTime[$outputForExecutedCommand] = $executionTime; } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4f4d326..1601b74 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,7 +7,6 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" forceCoversAnnotation="false" - mapTestClassNameToCoveredClassName="false" processIsolation="false" stopOnError="false" stopOnFailure="false" @@ -23,14 +22,13 @@ - - + + + ./library + + ./vendor ./tests/ - - - ./library - - - + + diff --git a/tests/Tests/Odesk/Phystrix/ArrayStateStorageTest.php b/tests/Tests/Odesk/Phystrix/ArrayStateStorageTest.php index 76a047a..64365d0 100644 --- a/tests/Tests/Odesk/Phystrix/ArrayStateStorageTest.php +++ b/tests/Tests/Odesk/Phystrix/ArrayStateStorageTest.php @@ -19,15 +19,16 @@ namespace Tests\Odesk\Phystrix; use Odesk\Phystrix\ArrayStateStorage; +use PHPUnit\Framework\TestCase; -class ArrayStateStorageTest extends \PHPUnit_Framework_TestCase +class ArrayStateStorageTest extends TestCase { /** * @var ArrayStateStorage */ protected $storage; - protected function setUp() + protected function setUp(): void { $this->storage = new ArrayStateStorage(); } diff --git a/tests/Tests/Odesk/Phystrix/CircuitBreakerFactoryTest.php b/tests/Tests/Odesk/Phystrix/CircuitBreakerFactoryTest.php index 7603a91..989f1fb 100644 --- a/tests/Tests/Odesk/Phystrix/CircuitBreakerFactoryTest.php +++ b/tests/Tests/Odesk/Phystrix/CircuitBreakerFactoryTest.php @@ -20,60 +20,58 @@ use Odesk\Phystrix\CircuitBreakerFactory; use Odesk\Phystrix\CommandMetrics; +use Odesk\Phystrix\StateStorageInterface; +use Odesk\Phystrix\NoOpCircuitBreaker; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; -class CircuitBreakerFactoryTest extends \PHPUnit_Framework_TestCase +class CircuitBreakerFactoryTest extends TestCase { /** - * @var CircuitBreakerFactory - */ - protected $factory; - - /** - * @var CommandMetrics + * @var MockObject|CommandMetrics */ protected $metrics; + protected CircuitBreakerFactory $factory; - protected static $baseConfig = array( - 'circuitBreaker' => array( + protected static array $baseConfig = [ + 'circuitBreaker' => [ 'enabled' => true, - ) - ); + ] + ]; - protected function setUp() + protected function setUp(): void { - $this->factory = new CircuitBreakerFactory($this->getMock('Odesk\Phystrix\StateStorageInterface')); - $this->metrics = $this->getMock('Odesk\Phystrix\CommandMetrics', array(), array(), '', false); + $this->factory = new CircuitBreakerFactory($this->createMock(StateStorageInterface::class)); + $this->metrics = $this->createMock(CommandMetrics::class); } - public function testGetNoOpCircuitBreaker() + public function testGetNoOpCircuitBreaker(): void { $config = self::$baseConfig; $config['circuitBreaker']['enabled'] = false; - $config = new \Zend\Config\Config($config); + $config = new \Laminas\Config\Config($config); $circuitBreaker = $this->factory->get('TestCommand', $config, $this->metrics); - $this->assertInstanceOf('Odesk\Phystrix\NoOpCircuitBreaker', $circuitBreaker); + $this->assertInstanceOf(NoOpCircuitBreaker::class, $circuitBreaker); } - public function testGetInstantiatesOnce() + public function testGetInstantiatesOnce(): void { $config = self::$baseConfig; $config['circuitBreaker']['enabled'] = false; - $config = new \Zend\Config\Config($config); + $config = new \Laminas\Config\Config($config); // this will be a NoOpCircuitBreaker $circuitBreaker = $this->factory->get('TestCommand', $config, $this->metrics); // now trying to get the same circuit breaker with a different config $circuitBreakerB - = $this->factory->get('TestCommand', new \Zend\Config\Config(self::$baseConfig), $this->metrics); + = $this->factory->get('TestCommand', new \Laminas\Config\Config(self::$baseConfig), $this->metrics); $this->assertEquals($circuitBreaker, $circuitBreakerB); } - public function testGetInjectsParameters() + public function testGetInjectsParameters(): void { - $config = new \Zend\Config\Config(self::$baseConfig); + $config = new \Laminas\Config\Config(self::$baseConfig); $circuitBreaker = $this->factory->get('TestCommand', $config, $this->metrics); - $this->assertAttributeEquals('TestCommand', 'commandKey', $circuitBreaker); - $this->assertAttributeEquals($config, 'config', $circuitBreaker); - $this->assertAttributeInstanceOf('Odesk\Phystrix\CommandMetrics', 'metrics', $circuitBreaker); - $this->assertAttributeInstanceOf('Odesk\Phystrix\StateStorageInterface', 'stateStorage', $circuitBreaker); + $this->assertSame('TestCommand', $circuitBreaker->getCommandKey()); + $this->assertEquals($config, $circuitBreaker->getConfig()); } } diff --git a/tests/Tests/Odesk/Phystrix/CircuitBreakerTest.php b/tests/Tests/Odesk/Phystrix/CircuitBreakerTest.php index 89c5120..7149ff9 100644 --- a/tests/Tests/Odesk/Phystrix/CircuitBreakerTest.php +++ b/tests/Tests/Odesk/Phystrix/CircuitBreakerTest.php @@ -18,55 +18,61 @@ */ namespace Tests\Odesk\Phystrix; +use Laminas\Config\Config; use Odesk\Phystrix\CircuitBreaker; +use Odesk\Phystrix\CommandMetrics; +use Odesk\Phystrix\StateStorageInterface; +use Odesk\Phystrix\HealthCountsSnapshot; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; -class CircuitBreakerTest extends \PHPUnit_Framework_TestCase +class CircuitBreakerTest extends TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MockObject|CommandMetrics */ protected $metrics; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MockObject|StateStorageInterface */ protected $stateStorage; - protected function setUp() + protected function setUp(): void { - $this->metrics = $this->getMock('Odesk\Phystrix\CommandMetrics', array(), array(), '', false); - $this->stateStorage = $this->getMock('Odesk\Phystrix\StateStorageInterface'); + $this->metrics = $this->createMock(CommandMetrics::class); + $this->stateStorage = $this->createMock(StateStorageInterface::class); } - protected function getCircuitBreaker($config = array()) + protected function getCircuitBreaker($config = []): CircuitBreaker { - $commandConfig = new \Zend\Config\Config(array( - 'circuitBreaker' => array( + $commandConfig = new Config([ + 'circuitBreaker' => [ 'enabled' => true, 'errorThresholdPercentage' => 50, 'forceOpen' => false, 'forceClosed' => false, 'requestVolumeThreshold' => 50, 'sleepWindowInMilliseconds' => 5000, - 'metrics' => array( + 'metrics' => [ 'healthSnapshotIntervalInMilliseconds' => 1000, 'rollingStatisticalWindowInMilliseconds' => 10000, 'rollingStatisticalWindowBuckets' => 10, - ) - ), - ), true); - $commandConfig->merge(new \Zend\Config\Config($config, true)); + ] + ], + ], true); + $commandConfig->merge(new Config($config, true)); return new CircuitBreaker('TestCommand', $this->metrics, $commandConfig, $this->stateStorage); } - public function testIsOpenReturnsTrueImmediately() + public function testIsOpenReturnsTrueImmediately(): void { $this->stateStorage->expects($this->once()) ->method('isCircuitOpen') ->with($this->equalTo('TestCommand')) - ->will($this->returnValue(true)); + ->willReturn(true); $this->metrics->expects($this->never()) ->method('getHealthCounts'); @@ -74,55 +80,55 @@ public function testIsOpenReturnsTrueImmediately() $this->assertTrue($this->getCircuitBreaker()->isOpen()); } - public function testIsOpenNotPastTheThreshold() + public function testIsOpenNotPastTheThreshold(): void { $this->stateStorage->expects($this->once()) ->method('isCircuitOpen') ->with($this->equalTo('TestCommand')) - ->will($this->returnValue(false)); + ->willReturn(false); - $healthCounts = $this->getMock('Odesk\Phystrix\HealthCountsSnapshot', array(), array(), '', false); + $healthCounts = $this->createMock(HealthCountsSnapshot::class); $healthCounts->expects($this->once()) ->method('getTotal') - ->will($this->returnValue(47)); // total is 47, threshold is set to 50. + ->willReturn(47); // total is 47, threshold is set to 50. $healthCounts->expects($this->never()) ->method('getErrorPercentage'); // making sure it doesn't make it to error percentage checking logic $this->metrics->expects($this->once()) ->method('getHealthCounts') - ->will($this->returnValue($healthCounts)); + ->willReturn($healthCounts); $this->assertFalse($this->getCircuitBreaker()->isOpen()); } - public function testIsOpenErrorPercentageNotBigEnough() + public function testIsOpenErrorPercentageNotBigEnough(): void { - $healthCounts = $this->getMock('Odesk\Phystrix\HealthCountsSnapshot', array(), array(), '', false); + $healthCounts = $this->createMock(HealthCountsSnapshot::class); $healthCounts->expects($this->once()) ->method('getTotal') - ->will($this->returnValue(60)); // total is 60, threshold is set to 50. + ->willReturn(60); // total is 60, threshold is set to 50. $healthCounts->expects($this->once()) ->method('getErrorPercentage') - ->will($this->returnValue(49)); // error percentage threshold is set to 50. 49 should not open the circuit + ->willReturn(49); // error percentage threshold is set to 50. 49 should not open the circuit $this->metrics->expects($this->once()) ->method('getHealthCounts') - ->will($this->returnValue($healthCounts)); + ->willReturn($healthCounts); $this->assertFalse($this->getCircuitBreaker()->isOpen()); } - public function testIsOpenOpensCircuit() + public function testIsOpenOpensCircuit(): void { - $healthCounts = $this->getMock('Odesk\Phystrix\HealthCountsSnapshot', array(), array(), '', false); + $healthCounts = $this->createMock(HealthCountsSnapshot::class); $healthCounts->expects($this->once()) ->method('getTotal') - ->will($this->returnValue(60)); // total is 60, threshold is set to 50. + ->willReturn(60); // total is 60, threshold is set to 50. $healthCounts->expects($this->once()) ->method('getErrorPercentage') - ->will($this->returnValue(51)); // error percentage threshold is set to 50. 51 should open the circuit + ->willReturn(51); // error percentage threshold is set to 50. 51 should open the circuit $this->metrics->expects($this->once()) ->method('getHealthCounts') - ->will($this->returnValue($healthCounts)); + ->willReturn($healthCounts); $this->stateStorage->expects($this->once()) ->method('openCircuit') @@ -131,44 +137,39 @@ public function testIsOpenOpensCircuit() $this->assertTrue($this->getCircuitBreaker()->isOpen()); } - public function testAllowSingleTest() + public function testAllowSingleTest(): void { - $this->stateStorage->expects($this->at(0)) + $this->stateStorage ->method('allowSingleTest') - ->with($this->equalTo('TestCommand'), $this->equalTo(5000)) - ->will($this->returnValue(false)); - - $this->stateStorage->expects($this->at(1)) - ->method('allowSingleTest') - ->with($this->equalTo('TestCommand'), $this->equalTo(5000)) - ->will($this->returnValue(true)); + ->withConsecutive(['TestCommand', 5000], ['TestCommand', 5000]) + ->willReturnOnConsecutiveCalls(false, true); $this->assertFalse($this->getCircuitBreaker()->allowSingleTest()); $this->assertTrue($this->getCircuitBreaker()->allowSingleTest()); } - public function testAllowRequestForceOpen() + public function testAllowRequestForceOpen(): void { $this->stateStorage->expects($this->never()) ->method('isCircuitOpen'); // making sure it doesn't get to checking if the circuit is open - $circuitBreaker = $this->getCircuitBreaker(array('circuitBreaker' => array('forceOpen' => true))); + $circuitBreaker = $this->getCircuitBreaker(['circuitBreaker' => ['forceOpen' => true]]); $this->assertFalse($circuitBreaker->allowRequest()); } - public function testAllowRequestForceClose() + public function testAllowRequestForceClose(): void { $this->stateStorage->expects($this->never()) ->method('isCircuitOpen'); // making sure it doesn't get to checking if the circuit is open - $circuitBreaker = $this->getCircuitBreaker(array('circuitBreaker' => array('forceClosed' => true))); + $circuitBreaker = $this->getCircuitBreaker(['circuitBreaker' => ['forceClosed' => true]]); $this->assertTrue($circuitBreaker->allowRequest()); } - public function testMarkSuccessClosesCircuitIfOpenAndResetCounter() + public function testMarkSuccessClosesCircuitIfOpenAndResetCounter(): void { $this->stateStorage->expects($this->once()) ->method('isCircuitOpen') ->with($this->equalTo('TestCommand')) - ->will($this->returnValue(true)); + ->willReturn(true); $this->stateStorage->expects($this->once()) ->method('closeCircuit') ->with($this->equalTo('TestCommand')); diff --git a/tests/Tests/Odesk/Phystrix/CommandFactoryTest.php b/tests/Tests/Odesk/Phystrix/CommandFactoryTest.php index c263031..17e6a14 100644 --- a/tests/Tests/Odesk/Phystrix/CommandFactoryTest.php +++ b/tests/Tests/Odesk/Phystrix/CommandFactoryTest.php @@ -23,19 +23,22 @@ use Odesk\Phystrix\CommandMetricsFactory; use Odesk\Phystrix\RequestCache; use Odesk\Phystrix\RequestLog; -use Zend\Di\ServiceLocator; +use Laminas\Di\ServiceLocator; +use Odesk\Phystrix\StateStorageInterface; +use PHPUnit\Framework\TestCase; +use Tests\Odesk\Phystrix\FactoryCommandMock; -class CommandFactoryTest extends \PHPUnit_Framework_TestCase +class CommandFactoryTest extends TestCase { - public function testGetCommand() + public function testGetCommand(): void { - $config = new \Zend\Config\Config(array( - 'default' => array( - 'fallback' => array('enabled' => true) - ) - )); + $config = new \Laminas\Config\Config([ + 'default' => [ + 'fallback' => ['enabled' => true] + ] + ]); $serviceLocator = new ServiceLocator(); - $stateStorage = $this->getMock('Odesk\Phystrix\StateStorageInterface'); + $stateStorage = $this->createMock(StateStorageInterface::class); $circuitBreakerFactory = new CircuitBreakerFactory($stateStorage); $commandMetricsFactory = new CommandMetricsFactory($stateStorage); $requestCache = new RequestCache(); @@ -49,35 +52,31 @@ public function testGetCommand() $requestLog ); /** @var FactoryCommandMock $command */ - $command = $commandFactory->getCommand('Tests\Odesk\Phystrix\FactoryCommandMock', 'test', 'hello'); + $command = $commandFactory->getCommand(FactoryCommandMock::class, 'test', 'hello'); // injects constructor parameters $this->assertEquals('test', $command->a); $this->assertEquals('hello', $command->b); // injects the infrastructure components - $expectedDefaultConfig = new \Zend\Config\Config(array( + $expectedDefaultConfig = new \Laminas\Config\Config(array( 'fallback' => array('enabled' => true) ), true); - $this->assertAttributeEquals($expectedDefaultConfig, 'config', $command); - $this->assertAttributeEquals($circuitBreakerFactory, 'circuitBreakerFactory', $command); - $this->assertAttributeEquals($serviceLocator, 'serviceLocator', $command); - $this->assertAttributeEquals($requestCache, 'requestCache', $command); - $this->assertAttributeEquals($requestLog, 'requestLog', $command); + $this->assertEquals($expectedDefaultConfig, $command->getConfig()); } - public function testGetCommandMergesConfig() + public function testGetCommandMergesConfig(): void { - $config = new \Zend\Config\Config(array( - 'default' => array( - 'fallback' => array('enabled' => true), + $config = new \Laminas\Config\Config([ + 'default' => [ + 'fallback' => ['enabled' => true], 'customData' => 12345 - ), - 'Tests\Odesk\Phystrix\FactoryCommandMock' => array( - 'fallback' => array('enabled' => false), - 'circuitBreaker' => array('enabled' => false) - ) - )); + ], + FactoryCommandMock::class => [ + 'fallback' => ['enabled' => false], + 'circuitBreaker' => ['enabled' => false] + ] + ]); $serviceLocator = new ServiceLocator(); - $stateStorage = $this->getMock('Odesk\Phystrix\StateStorageInterface'); + $stateStorage = $this->createMock(StateStorageInterface::class); $circuitBreakerFactory = new CircuitBreakerFactory($stateStorage); $commandMetricsFactory = new CommandMetricsFactory($stateStorage); $commandFactory = new CommandFactory( @@ -89,12 +88,12 @@ public function testGetCommandMergesConfig() new RequestLog() ); /** @var FactoryCommandMock $command */ - $command = $commandFactory->getCommand('Tests\Odesk\Phystrix\FactoryCommandMock', 'test', 'hello'); - $expectedConfig = new \Zend\Config\Config(array( + $command = $commandFactory->getCommand(FactoryCommandMock::class, 'test', 'hello'); + $expectedConfig = new \Laminas\Config\Config(array( 'fallback' => array('enabled' => false), 'circuitBreaker' => array('enabled' => false), 'customData' => 12345 ), true); - $this->assertAttributeEquals($expectedConfig, 'config', $command); + $this->assertEquals($expectedConfig, $command->getConfig()); } } diff --git a/tests/Tests/Odesk/Phystrix/CommandMetricsFactoryTest.php b/tests/Tests/Odesk/Phystrix/CommandMetricsFactoryTest.php index da7cae1..1b1ecf1 100644 --- a/tests/Tests/Odesk/Phystrix/CommandMetricsFactoryTest.php +++ b/tests/Tests/Odesk/Phystrix/CommandMetricsFactoryTest.php @@ -20,12 +20,15 @@ use Odesk\Phystrix\ArrayStateStorage; use Odesk\Phystrix\CommandMetricsFactory; +use PHPUnit\Framework\TestCase; +use Odesk\Phystrix\CommandMetrics; +use ReflectionClass; -class CommandMetricsFactoryTest extends \PHPUnit_Framework_TestCase +class CommandMetricsFactoryTest extends TestCase { - public function testGet() + public function testGet(): void { - $config = new \Zend\Config\Config(array( + $config = new \Laminas\Config\Config(array( 'metrics' => array( 'rollingStatisticalWindowInMilliseconds' => 10000, 'rollingStatisticalWindowBuckets' => 10, @@ -34,15 +37,6 @@ public function testGet() )); $factory = new CommandMetricsFactory(new ArrayStateStorage()); $metrics = $factory->get('TestCommand', $config); - $this->assertAttributeEquals(2000, 'healthSnapshotIntervalInMilliseconds', $metrics); - - $reflection = new \ReflectionClass('Odesk\Phystrix\CommandMetrics'); - $property = $reflection->getProperty('counter'); - $property->setAccessible(true); - $counter = $property->getValue($metrics); - $this->assertAttributeEquals('TestCommand', 'commandKey', $counter); - $this->assertAttributeEquals(10000, 'rollingStatisticalWindowInMilliseconds', $counter); - $this->assertAttributeEquals(10, 'rollingStatisticalWindowBuckets', $counter); - $this->assertAttributeEquals(1000, 'bucketInMilliseconds', $counter); // 10000 / 10 = 1000 + $this->assertSame(2000, $metrics->getHealthSnapshotIntervalInMilliseconds()); } } diff --git a/tests/Tests/Odesk/Phystrix/CommandMetricsTest.php b/tests/Tests/Odesk/Phystrix/CommandMetricsTest.php index 1403f07..4e1e898 100644 --- a/tests/Tests/Odesk/Phystrix/CommandMetricsTest.php +++ b/tests/Tests/Odesk/Phystrix/CommandMetricsTest.php @@ -21,29 +21,28 @@ use Odesk\Phystrix\CommandMetrics; use Odesk\Phystrix\HealthCountsSnapshot; use Odesk\Phystrix\MetricsCounter; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; -class CommandMetricsTest extends \PHPUnit_Framework_TestCase +class CommandMetricsTest extends TestCase { - /** - * @var CommandMetrics - */ - protected $metrics; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MockObject|MetricsCounter */ protected $counter; + protected CommandMetrics $metrics; - protected function setUp() + protected function setUp(): void { - $this->counter = $this->getMock('Odesk\Phystrix\MetricsCounter', array(), array(), '', false); + $this->counter = $this->createMock(MetricsCounter::class); $this->metrics = new CommandMetrics($this->counter, 1000); // microtime is fixed global $globalUnitTestPhystrixMicroTime; $globalUnitTestPhystrixMicroTime = 1369861562.1266; } - protected function tearDown() + protected function tearDown(): void { // making microtime to fallback to the default behavior global $globalUnitTestPhystrixMicroTime; @@ -98,20 +97,20 @@ public function testResetCounter() $this->metrics->resetCounter(); } - public function testGetRollingCount() + public function testGetRollingCount(): void { $this->counter->expects($this->once())->method('get')->with(1); $this->metrics->getRollingCount(1); } - public function testGetHealthCountsInitialSnapshot() + public function testGetHealthCountsInitialSnapshot(): void { $this->counter->expects($this->exactly(2)) ->method('get') - ->will($this->returnValueMap(array( - array(MetricsCounter::FAILURE, 22), - array(MetricsCounter::SUCCESS, 33), - ))); + ->willReturnMap([ + [MetricsCounter::FAILURE, 22], + [MetricsCounter::SUCCESS, 33], + ]); $snapshot = $this->metrics->getHealthCounts(); $this->assertEquals(22, $snapshot->getFailure()); diff --git a/tests/Tests/Odesk/Phystrix/CommandTest.php b/tests/Tests/Odesk/Phystrix/CommandTest.php index 55a44e8..24d5807 100644 --- a/tests/Tests/Odesk/Phystrix/CommandTest.php +++ b/tests/Tests/Odesk/Phystrix/CommandTest.php @@ -22,49 +22,45 @@ use Odesk\Phystrix\Exception\RuntimeException; use Odesk\Phystrix\RequestCache; use Odesk\Phystrix\RequestLog; -use Zend\Config\Config; - -class CommandTest extends \PHPUnit_Framework_TestCase +use Laminas\Config\Config; +use Odesk\Phystrix\CircuitBreakerFactory; +use Odesk\Phystrix\CircuitBreakerInterface; +use Odesk\Phystrix\CommandMetricsFactory; +use Odesk\Phystrix\CommandMetrics; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Odesk\Phystrix\Exception\BadRequestException; + +class CommandTest extends TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MockObject */ protected $circuitBreakerFactory; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MockObject|CircuitBreakerInterface */ protected $circuitBreaker; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MockObject|CommandMetricsFactory */ protected $commandMetricsFactory; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MockObject|CommandMetrics */ protected $commandMetrics; + protected CommandMock $command; + protected RequestLog $requestLog; - /** - * @var CommandMock - */ - protected $command; - - /** - * @var RequestLog - */ - protected $requestLog; - - protected function setUp() + protected function setUp(): void { - $this->circuitBreakerFactory - = $this->getMock('Odesk\Phystrix\CircuitBreakerFactory', array(), array(), '', false); - $this->circuitBreaker - = $this->getMock('Odesk\Phystrix\CircuitBreakerInterface', array(), array(), '', false); - $this->commandMetricsFactory - = $this->getMock('Odesk\Phystrix\CommandMetricsFactory', array(), array(), '', false); - $this->commandMetrics = $this->getMock('Odesk\Phystrix\CommandMetrics', array(), array(), '', false); + $this->circuitBreakerFactory = $this->createMock(CircuitBreakerFactory::class); + $this->circuitBreaker = $this->createMock(CircuitBreakerInterface::class); + $this->commandMetricsFactory = $this->createMock(CommandMetricsFactory::class); + $this->commandMetrics = $this->createMock(CommandMetrics::class); $this->command = new CommandMock(); $this->command->setCommandMetricsFactory($this->commandMetricsFactory); @@ -72,17 +68,17 @@ protected function setUp() $this->requestLog = new RequestLog(); $this->command->setRequestLog($this->requestLog); $this->command->setCircuitBreakerFactory($this->circuitBreakerFactory); - $this->command->setConfig(new Config(array( - 'fallback' => array( + $this->command->setConfig(new Config([ + 'fallback' => [ 'enabled' => true, - ), - 'requestCache' => array( + ], + 'requestCache' => [ 'enabled' => true, - ), - 'requestLog' => array( + ], + 'requestLog' => [ 'enabled' => true, - ), - ), true)); + ], + ], true)); } /** @@ -116,14 +112,15 @@ protected function setUpExecutionDelayExpectations() $this->command->simulateDelay = true; } - public function testSetTestMergesConfig() + public function testSetTestMergesConfig(): void { $command = new CommandMock(); - $command->setConfig(new Config(array('a' => 1), true)); - $command->setConfig(new Config(array('b' => 2), true)); - $this->assertAttributeEquals(new Config(array('a' => 1, 'b' => 2), true), 'config', $command); - $command->setConfig(new Config(array('c' => 3), true), false); // false to skip merge - $this->assertAttributeEquals(new Config(array('c' => 3), true), 'config', $command); + $command->setConfig(new Config(['a' => 1], true)); + $command->setConfig(new Config(['b' => 2], true)); + $this->assertEquals(new Config(['a' => 1, 'b' => 2], true), $command->getConfig()); + + $command->setConfig(new Config(['c' => 3], true), false); // false to skip merge + $this->assertEquals(new Config(['c' => 3], true), $command->getConfig()); } public function testExecuteDefaultCommandKey() @@ -195,7 +192,7 @@ public function testRequestLogOff() * * @param bool $logEnabled whether config is set to use request log */ - public function testRequestLogNotInjected($logEnabled) + public function testRequestLogNotInjected($logEnabled): void { // Duplicate some of the class setup in order to bypass requestLog generation $command = new CommandMock(); @@ -240,19 +237,15 @@ public function testRequestCacheNotInjected($cacheEnabled) $this->assertEquals('run result', $this->command->execute()); } - - /** - * @return array - */ - public function configBoolProvider() + public function configBoolProvider(): array { - return array( - 'config enabled' => array(true), - 'config disabled' => array(false), - ); + return [ + 'config enabled' => [true], + 'config disabled' => [false], + ]; } - public function testExecuteRequestNotAllowed() + public function testExecuteRequestNotAllowed(): void { $this->setUpCommonExpectations(false); @@ -354,7 +347,8 @@ public function testBadRequestExceptionTracksNoMetrics() $this->commandMetrics->expects($this->never()) ->method('markFailure'); $this->command->throwBadRequestException = true; - $this->setExpectedException('Odesk\Phystrix\Exception\BadRequestException', 'special treatment'); + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('special treatment'); $this->command->execute(); // no events logged in this case $this->assertEquals(array(), $this->command->getExecutionEvents()); @@ -362,7 +356,7 @@ public function testBadRequestExceptionTracksNoMetrics() $this->assertEquals(null, $this->command->getExecutionTimeInMilliseconds()); } - public function testShortCircuitedExceptionMessage() + public function testShortCircuitedExceptionMessage(): void { $this->setUpCommonExpectations(false); $this->command->throwException = true; @@ -411,7 +405,7 @@ public function testRequestCacheHit() ->with('Tests\Odesk\Phystrix\CommandMock') ->will($this->returnValue($this->commandMetrics)); /** @var RequestCache|\PHPUnit_Framework_MockObject_MockObject $requestCache */ - $requestCache = $this->getMock('Odesk\Phystrix\RequestCache'); + $requestCache = $this->createMock(RequestCache::class); $requestCache->expects($this->once()) ->method('exists') ->with('Tests\Odesk\Phystrix\CommandMock', 'test-cache-key') @@ -435,7 +429,7 @@ public function testRequestCacheMiss() { $this->setUpCommonExpectations(); /** @var RequestCache|\PHPUnit_Framework_MockObject_MockObject $requestCache */ - $requestCache = $this->getMock('Odesk\Phystrix\RequestCache'); + $requestCache = $this->createMock(RequestCache::class); $requestCache->expects($this->once()) ->method('exists') ->with('Tests\Odesk\Phystrix\CommandMock', 'test-cache-key') @@ -456,7 +450,7 @@ public function testSavesResultToCache() { $this->setUpCommonExpectations(); /** @var RequestCache|\PHPUnit_Framework_MockObject_MockObject $requestCache */ - $requestCache = $this->getMock('Odesk\Phystrix\RequestCache'); + $requestCache = $this->createMock(RequestCache::class); $requestCache->expects($this->once()) ->method('put') ->with('Tests\Odesk\Phystrix\CommandMock', 'test-cache-key', 'run result'); @@ -470,7 +464,7 @@ public function testRequestCacheDisabled() $this->setUpCommonExpectations(); $this->command->setConfig(new Config(array('requestCache' => array('enabled' => false)))); /** @var RequestCache|\PHPUnit_Framework_MockObject_MockObject $requestCache */ - $requestCache = $this->getMock('Odesk\Phystrix\RequestCache'); + $requestCache = $this->createMock(RequestCache::class); $requestCache->expects($this->never()) ->method('get'); $requestCache->expects($this->never()) @@ -485,7 +479,7 @@ public function testRequestCacheGetCacheKeyNotImplemented() { $this->setUpCommonExpectations(); /** @var RequestCache|\PHPUnit_Framework_MockObject_MockObject $requestCache */ - $requestCache = $this->getMock('Odesk\Phystrix\RequestCache'); + $requestCache = $this->createMock(RequestCache::class); $requestCache->expects($this->never()) ->method('get'); $requestCache->expects($this->never()) diff --git a/tests/Tests/Odesk/Phystrix/Exception/RuntimeExceptionTest.php b/tests/Tests/Odesk/Phystrix/Exception/RuntimeExceptionTest.php index 672d176..df043a2 100644 --- a/tests/Tests/Odesk/Phystrix/Exception/RuntimeExceptionTest.php +++ b/tests/Tests/Odesk/Phystrix/Exception/RuntimeExceptionTest.php @@ -16,11 +16,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -namespace Tests\Odesk\Phystrix; +namespace Tests\Odesk\Phystrix\Exception; use Odesk\Phystrix\Exception\RuntimeException; +use PHPUnit\Framework\TestCase; -class RuntimeExceptionTest extends \PHPUnit_Framework_TestCase +class RuntimeExceptionTest extends TestCase { public function testGetCommandClass() { diff --git a/tests/Tests/Odesk/Phystrix/HealthCountsSnapshotTest.php b/tests/Tests/Odesk/Phystrix/HealthCountsSnapshotTest.php index 69b9b82..85d3d9b 100644 --- a/tests/Tests/Odesk/Phystrix/HealthCountsSnapshotTest.php +++ b/tests/Tests/Odesk/Phystrix/HealthCountsSnapshotTest.php @@ -19,47 +19,46 @@ namespace Tests\Odesk\Phystrix; use Odesk\Phystrix\HealthCountsSnapshot; +use PHPUnit\Framework\TestCase; -class HealthCountsSnapshotTest extends \PHPUnit_Framework_TestCase +class HealthCountsSnapshotTest extends TestCase { - /** - * @var HealthCountsSnapshot - */ - protected $snapshot; - protected function setUp() + protected HealthCountsSnapshot $snapshot; + + protected function setUp(): void { $this->snapshot = new HealthCountsSnapshot(1369760400, 12, 24); } - public function testConstruct() + public function testConstruct(): void { - $this->assertAttributeEquals(12, 'successful', $this->snapshot); - $this->assertAttributeEquals(24, 'failure', $this->snapshot); - $this->assertAttributeEquals(1369760400, 'time', $this->snapshot); + $this->assertSame(12, $this->snapshot->getSuccessful()); + $this->assertSame(24, $this->snapshot->getFailure()); + $this->assertSame(1369760400, $this->snapshot->getTime()); } - public function testGetTime() + public function testGetTime(): void { $this->assertEquals(1369760400, $this->snapshot->getTime()); } - public function testGetFailure() + public function testGetFailure(): void { $this->assertEquals(24, $this->snapshot->getFailure()); } - public function testGetSuccessful() + public function testGetSuccessful(): void { $this->assertEquals(12, $this->snapshot->getSuccessful()); } - public function testGetTotal() + public function testGetTotal(): void { $this->assertEquals(36, $this->snapshot->getTotal()); } - public function testGetErrorPercentage() + public function testGetErrorPercentage(): void { $this->assertEquals(66, (integer) $this->snapshot->getErrorPercentage()); } diff --git a/tests/Tests/Odesk/Phystrix/MetricsCounterTest.php b/tests/Tests/Odesk/Phystrix/MetricsCounterTest.php index 0bc860b..722d046 100644 --- a/tests/Tests/Odesk/Phystrix/MetricsCounterTest.php +++ b/tests/Tests/Odesk/Phystrix/MetricsCounterTest.php @@ -16,25 +16,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + namespace Tests\Odesk\Phystrix; use Odesk\Phystrix\MetricsCounter; +use Odesk\Phystrix\StateStorageInterface; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; -class MetricsCounterTest extends \PHPUnit_Framework_TestCase +class MetricsCounterTest extends TestCase { - /** - * @var MetricsCounter - */ - protected $counter; + protected MetricsCounter $counter; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MockObject|StateStorageInterface */ protected $stateStorageMock; - protected function setUp() + protected function setUp(): void { - $this->stateStorageMock = $this->getMock('Odesk\Phystrix\StateStorageInterface'); + $this->stateStorageMock = $this->createMock(StateStorageInterface::class); // 10 seconds statistical window, 10 buckets. $this->counter = new MetricsCounter('TestCommand', $this->stateStorageMock, 10000, 10); // microtime is fixed @@ -42,7 +43,7 @@ protected function setUp() $globalUnitTestPhystrixMicroTime = 1369861562.1266; } - protected function tearDown() + protected function tearDown(): void { // making microtime to fallback to the default behavior global $globalUnitTestPhystrixMicroTime; @@ -55,7 +56,7 @@ protected function getExpectedBucketIndex($bucketNumber) return floor(($timeInMilliseconds - $bucketNumber * 1000) / 1000); } - public function testAdd() + public function testAdd(): void { // current bucket (0) index is calculated as follows, using the value from microtime: // floor((1369861562.126 - 0 * 1000) / 10000) = 1369861562 @@ -71,19 +72,18 @@ public function testAdd() $this->counter->add(MetricsCounter::SUCCESS); } - public function testGet() + + public function testGet(): void { // going through each bucket, making sure the value for it is requested from the storage - for ($bucketNumber = 0; $bucketNumber < 10; $bucketNumber++) { - $this->stateStorageMock - ->expects($this->at($bucketNumber)) - ->method('getBucket') - ->with( - $this->equalTo('TestCommand'), - $this->equalTo(MetricsCounter::SUCCESS), - $this->equalTo($this->getExpectedBucketIndex($bucketNumber)) - ); - } + $this->stateStorageMock + ->expects($this->exactly(10)) + ->method('getBucket') + ->with( + $this->equalTo('TestCommand'), + $this->equalTo(MetricsCounter::SUCCESS), + ); + $this->counter->get(MetricsCounter::SUCCESS); } } diff --git a/tests/Tests/Odesk/Phystrix/NoOpCircuitBreakerTest.php b/tests/Tests/Odesk/Phystrix/NoOpCircuitBreakerTest.php index 39ee670..1d5778f 100644 --- a/tests/Tests/Odesk/Phystrix/NoOpCircuitBreakerTest.php +++ b/tests/Tests/Odesk/Phystrix/NoOpCircuitBreakerTest.php @@ -19,15 +19,13 @@ namespace Tests\Odesk\Phystrix; use Odesk\Phystrix\NoOpCircuitBreaker; +use PHPUnit\Framework\TestCase; -class NoOpCircuitBreakerTest extends \PHPUnit_Framework_TestCase +class NoOpCircuitBreakerTest extends TestCase { - /** - * @var NoOpCircuitBreaker - */ - protected $circuitBreaker; + protected NoOpCircuitBreaker $circuitBreaker; - protected function setUp() + protected function setUp(): void { $this->circuitBreaker = new NoOpCircuitBreaker(); } diff --git a/tests/Tests/Odesk/Phystrix/RequestCacheTest.php b/tests/Tests/Odesk/Phystrix/RequestCacheTest.php index fe71104..9d9ec49 100644 --- a/tests/Tests/Odesk/Phystrix/RequestCacheTest.php +++ b/tests/Tests/Odesk/Phystrix/RequestCacheTest.php @@ -19,15 +19,13 @@ namespace Tests\Odesk\Phystrix; use Odesk\Phystrix\RequestCache; +use PHPUnit\Framework\TestCase; -class RequestCacheTest extends \PHPUnit_Framework_TestCase +class RequestCacheTest extends TestCase { - /** - * @var RequestCache - */ - protected $requestCache; + protected RequestCache $requestCache; - protected function setUp() + protected function setUp(): void { $this->requestCache = new RequestCache(); } diff --git a/tests/Tests/Odesk/Phystrix/RequestLogTest.php b/tests/Tests/Odesk/Phystrix/RequestLogTest.php index a838545..f17e3af 100644 --- a/tests/Tests/Odesk/Phystrix/RequestLogTest.php +++ b/tests/Tests/Odesk/Phystrix/RequestLogTest.php @@ -20,52 +20,47 @@ use Odesk\Phystrix\AbstractCommand; use Odesk\Phystrix\RequestLog; +use PHPUnit\Framework\TestCase; -class RequestLogTest extends \PHPUnit_Framework_TestCase +class RequestLogTest extends TestCase { - /** - * @var RequestLog - */ - protected $requestLog; + protected RequestLog $requestLog; - protected function setUp() + protected function setUp(): void { $this->requestLog = new RequestLog(); } - public function testAddAndGet() + public function testAddAndGet(): void { - $commandA = $this->getMock('Odesk\Phystrix\AbstractCommand', array('run')); - $commandB = $this->getMock('Odesk\Phystrix\AbstractCommand', array('run')); + $commandA = $this->createMock(AbstractCommand::class); + $commandB = $this->createMock(AbstractCommand::class); $this->assertEmpty($this->requestLog->getExecutedCommands()); $this->requestLog->addExecutedCommand($commandA); $this->requestLog->addExecutedCommand($commandB); $this->assertEquals(array($commandA, $commandB), $this->requestLog->getExecutedCommands()); } - public function testReadableEmptyLog() + public function testReadableEmptyLog(): void { $this->assertSame('', $this->requestLog->getExecutedCommandsAsString()); } - public function testReadableLogWithExecutedCommands() + public function testReadableLogWithExecutedCommands(): void { - $this->addExecutedCommand('commandA', 100, array(AbstractCommand::EVENT_FAILURE)); - $this->addExecutedCommand('commandA', 50, array(AbstractCommand::EVENT_SUCCESS)); - $this->addExecutedCommand('commandA', 15, array(AbstractCommand::EVENT_SUCCESS)); - $this->addExecutedCommand('commandB', -1, array()); + $this->addExecutedCommand('commandA', 100, [AbstractCommand::EVENT_FAILURE]); + $this->addExecutedCommand('commandA', 50, [AbstractCommand::EVENT_SUCCESS]); + $this->addExecutedCommand('commandA', 15, [AbstractCommand::EVENT_SUCCESS]); + $this->addExecutedCommand('commandB', -1, []); $this->assertSame( 'commandA[FAILURE][100ms], commandA[SUCCESS][65ms]x2, commandB[Executed][0ms]', $this->requestLog->getExecutedCommandsAsString() ); } - protected function addExecutedCommand($commandKey, $executionTime, array $events) + protected function addExecutedCommand($commandKey, $executionTime, array $events): void { - $command = $this->getMock( - 'Odesk\Phystrix\AbstractCommand', - array('run', 'getCommandKey', 'getExecutionEvents', 'getExecutionTimeInMilliseconds') - ); + $command = $this->createMock(AbstractCommand::class); $command->expects($this->once()) ->method('getCommandKey') ->willReturn($commandKey); diff --git a/vendor/.gitignore b/vendor/.gitignore deleted file mode 100644 index 593bcf0..0000000 --- a/vendor/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -!.gitignore -*