diff --git a/tests/Facade/ConstantBackOffTest.php b/tests/Facade/ConstantBackOffTest.php index bb091bb..22456d4 100644 --- a/tests/Facade/ConstantBackOffTest.php +++ b/tests/Facade/ConstantBackOffTest.php @@ -3,7 +3,6 @@ namespace Orangesoft\BackOff\Tests\Facade; use PHPUnit\Framework\TestCase; -use Orangesoft\BackOff\BackOffInterface; use Orangesoft\BackOff\Facade\ConstantBackOff; use Orangesoft\BackOff\Jitter\DummyJitter; use Orangesoft\BackOff\Sleeper\Sleeper; @@ -20,8 +19,6 @@ public function testBackOff(): void $backOff = new ConstantBackOff($maxAttempts, $baseTimeMs, $capTimeMs, $jitter, $sleeper); - $this->assertInstanceOf(BackOffInterface::class, $backOff); - $this->expectException(\RuntimeException::class); $backOff->backOff(0, new \RuntimeException()); diff --git a/tests/Facade/DecorrelationJitterBackOffTest.php b/tests/Facade/DecorrelationJitterBackOffTest.php index b7a447f..dec2406 100644 --- a/tests/Facade/DecorrelationJitterBackOffTest.php +++ b/tests/Facade/DecorrelationJitterBackOffTest.php @@ -3,7 +3,6 @@ namespace Orangesoft\BackOff\Tests\Facade; use PHPUnit\Framework\TestCase; -use Orangesoft\BackOff\BackOffInterface; use Orangesoft\BackOff\Facade\DecorrelationJitterBackOff; use Orangesoft\BackOff\Sleeper\Sleeper; @@ -19,8 +18,6 @@ public function testBackOff(): void $backOff = new DecorrelationJitterBackOff($maxAttempts, $baseTimeMs, $capTimeMs, $multiplier, $sleeper); - $this->assertInstanceOf(BackOffInterface::class, $backOff); - $this->expectException(\RuntimeException::class); $backOff->backOff(0, new \RuntimeException()); diff --git a/tests/Facade/ExponentialBackOffTest.php b/tests/Facade/ExponentialBackOffTest.php index 9ff6a4a..fc7948e 100644 --- a/tests/Facade/ExponentialBackOffTest.php +++ b/tests/Facade/ExponentialBackOffTest.php @@ -3,7 +3,6 @@ namespace Orangesoft\BackOff\Tests\Facade; use PHPUnit\Framework\TestCase; -use Orangesoft\BackOff\BackOffInterface; use Orangesoft\BackOff\Facade\ExponentialBackOff; use Orangesoft\BackOff\Jitter\DummyJitter; use Orangesoft\BackOff\Sleeper\Sleeper; @@ -21,8 +20,6 @@ public function testBackOff(): void $backOff = new ExponentialBackOff($maxAttempts, $baseTimeMs, $capTimeMs, $multiplier, $jitter, $sleeper); - $this->assertInstanceOf(BackOffInterface::class, $backOff); - $this->expectException(\RuntimeException::class); $backOff->backOff(0, new \RuntimeException()); diff --git a/tests/Facade/LinearBackOffTest.php b/tests/Facade/LinearBackOffTest.php index d28cb1e..5ddee96 100644 --- a/tests/Facade/LinearBackOffTest.php +++ b/tests/Facade/LinearBackOffTest.php @@ -3,7 +3,6 @@ namespace Orangesoft\BackOff\Tests\Facade; use PHPUnit\Framework\TestCase; -use Orangesoft\BackOff\BackOffInterface; use Orangesoft\BackOff\Facade\LinearBackOff; use Orangesoft\BackOff\Jitter\DummyJitter; use Orangesoft\BackOff\Sleeper\Sleeper; @@ -20,8 +19,6 @@ public function testBackOff(): void $backOff = new LinearBackOff($maxAttempts, $baseTimeMs, $capTimeMs, $jitter, $sleeper); - $this->assertInstanceOf(BackOffInterface::class, $backOff); - $this->expectException(\RuntimeException::class); $backOff->backOff(0, new \RuntimeException()); diff --git a/tests/Generator/GeneratorBuilderTest.php b/tests/Generator/GeneratorBuilderTest.php index 5f9f58b..4f925e6 100644 --- a/tests/Generator/GeneratorBuilderTest.php +++ b/tests/Generator/GeneratorBuilderTest.php @@ -6,80 +6,77 @@ use Orangesoft\BackOff\Generator\Generator; use Orangesoft\BackOff\Generator\GeneratorBuilder; use Orangesoft\BackOff\Duration\Milliseconds; -use Orangesoft\BackOff\Duration\DurationInterface; -use Orangesoft\BackOff\Strategy\ConstantStrategy; +use Orangesoft\BackOff\Strategy\ExponentialStrategy; use Orangesoft\BackOff\Strategy\LinearStrategy; -use Orangesoft\BackOff\Strategy\StrategyInterface; use Orangesoft\BackOff\Jitter\DummyJitter; use Orangesoft\BackOff\Jitter\FullJitter; -use Orangesoft\BackOff\Jitter\JitterInterface; class GeneratorBuilderTest extends TestCase { public function testCreate(): void { - $builder = GeneratorBuilder::create(); + $generatorBuilder = GeneratorBuilder::create(); - $this->assertInstanceOf(GeneratorBuilder::class, $builder); + $this->assertInstanceOf(GeneratorBuilder::class, $generatorBuilder); } - public function testDefaults(): void + public function testDefault(): void { - $builder = GeneratorBuilder::create(); + $generatorBuilder = GeneratorBuilder::create(); - $this->assertSame(INF, $builder->getMaxAttempts()); - $this->assertEquals(1000, $builder->getBaseTime()->asMilliseconds()); - $this->assertEquals(60 * 1000, $builder->getCapTime()->asMilliseconds()); - $this->assertInstanceOf(ConstantStrategy::class, $builder->getStrategy()); - $this->assertInstanceOf(DummyJitter::class, $builder->getJitter()); + $this->assertSame(INF, $generatorBuilder->getMaxAttempts()); + $this->assertEquals(1000, $generatorBuilder->getBaseTime()->asMilliseconds()); + $this->assertEquals(60 * 1000, $generatorBuilder->getCapTime()->asMilliseconds()); + $this->assertInstanceOf(ExponentialStrategy::class, $generatorBuilder->getStrategy()); + $this->assertInstanceOf(DummyJitter::class, $generatorBuilder->getJitter()); } public function testMaxAttempts(): void { - $builder = GeneratorBuilder::create()->setMaxAttempts(3); + $generatorBuilder = GeneratorBuilder::create()->setMaxAttempts(3); - $this->assertEquals(3, $builder->getMaxAttempts()); + $this->assertEquals(3, $generatorBuilder->getMaxAttempts()); } public function testMaxAttemptsInf(): void { - $builder = GeneratorBuilder::create()->setMaxAttempts(INF); + $generatorBuilder = GeneratorBuilder::create()->setMaxAttempts(INF); - $this->assertEquals(INF, $builder->getMaxAttempts()); + $this->assertEquals(INF, $generatorBuilder->getMaxAttempts()); } public function testBaseTime(): void { - $builder = GeneratorBuilder::create()->setBaseTime(new Milliseconds(1000)); + $generatorBuilder = GeneratorBuilder::create()->setBaseTime(new Milliseconds(1000)); - $this->assertInstanceOf(DurationInterface::class, $builder->getBaseTime()); + $this->assertInstanceOf(Milliseconds::class, $generatorBuilder->getBaseTime()); } public function testCapTime(): void { - $builder = GeneratorBuilder::create()->setCapTime(new Milliseconds(1000)); + $generatorBuilder = GeneratorBuilder::create()->setCapTime(new Milliseconds(1000)); - $this->assertInstanceOf(DurationInterface::class, $builder->getCapTime()); + $this->assertInstanceOf(Milliseconds::class, $generatorBuilder->getCapTime()); } public function testStrategy(): void { - $builder = GeneratorBuilder::create()->setStrategy(new LinearStrategy()); + $generatorBuilder = GeneratorBuilder::create()->setStrategy(new LinearStrategy()); - $this->assertInstanceOf(StrategyInterface::class, $builder->getStrategy()); + $this->assertInstanceOf(LinearStrategy::class, $generatorBuilder->getStrategy()); } public function testJitter(): void { - $builder = GeneratorBuilder::create()->setJitter(new FullJitter()); + $generatorBuilder = GeneratorBuilder::create()->setJitter(new FullJitter()); - $this->assertInstanceOf(JitterInterface::class, $builder->getJitter()); + $this->assertInstanceOf(FullJitter::class, $generatorBuilder->getJitter()); } public function testBuild(): void { - $generator = GeneratorBuilder::create()->build(); + $generatorBuilder = GeneratorBuilder::create()->build(); - $this->assertInstanceOf(Generator::class, $generator); + $this->assertInstanceOf(Generator::class, $generatorBuilder); } } diff --git a/tests/Generator/GeneratorTest.php b/tests/Generator/GeneratorTest.php index ebf0bc0..c8a85dd 100644 --- a/tests/Generator/GeneratorTest.php +++ b/tests/Generator/GeneratorTest.php @@ -5,7 +5,6 @@ use PHPUnit\Framework\TestCase; use Orangesoft\BackOff\Generator\GeneratorBuilder; use Orangesoft\BackOff\Duration\Milliseconds; -use Orangesoft\BackOff\Duration\DurationInterface; use Orangesoft\BackOff\Strategy\LinearStrategy; use Orangesoft\BackOff\Generator\Exception\MaxAttemptsException; @@ -22,8 +21,6 @@ public function testGenerate(): void $duration = $generator->generate(3); - $this->assertInstanceOf(DurationInterface::class, $duration); - $this->assertEquals(4000, $duration->asMilliseconds()); } diff --git a/tests/Jitter/FullJitterTest.php b/tests/Jitter/FullJitterTest.php index b1e3911..2f0ead6 100644 --- a/tests/Jitter/FullJitterTest.php +++ b/tests/Jitter/FullJitterTest.php @@ -10,9 +10,9 @@ class FullJitterTest extends TestCase { public function testJitter(): void { - $equalJitter = new FullJitter(); + $fullJitter = new FullJitter(); - $duration = $equalJitter->jitter(new Milliseconds(1000)); + $duration = $fullJitter->jitter(new Milliseconds(1000)); $this->assertGreaterThanOrEqual(0, $duration->asMilliseconds()); $this->assertLessThanOrEqual(1000, $duration->asMilliseconds()); diff --git a/tests/Retry/BackOffCounter.php b/tests/Retry/AttemptsCounter.php similarity index 80% rename from tests/Retry/BackOffCounter.php rename to tests/Retry/AttemptsCounter.php index 9fe3884..ac47350 100644 --- a/tests/Retry/BackOffCounter.php +++ b/tests/Retry/AttemptsCounter.php @@ -4,21 +4,21 @@ use Orangesoft\BackOff\BackOffInterface; -class BackOffCounter implements BackOffInterface +class AttemptsCounter implements BackOffInterface { /** * @var int */ - private $maxAttempts; + private $lastAttempt; /** * @var int */ - private $lastAttempt; + private $maxAttempts; - public function __construct(int $maxAttempts, int $lastAttempt = 0) + public function __construct(int $maxAttempts) { + $this->lastAttempt = 0; $this->maxAttempts = $maxAttempts; - $this->lastAttempt = $lastAttempt; } /** diff --git a/tests/Retry/ExceptionClassifier/ExceptionClassifierTest.php b/tests/Retry/ExceptionClassifier/ExceptionClassifierTest.php index 6798b45..635ce5a 100644 --- a/tests/Retry/ExceptionClassifier/ExceptionClassifierTest.php +++ b/tests/Retry/ExceptionClassifier/ExceptionClassifierTest.php @@ -7,23 +7,31 @@ class ExceptionClassifierTest extends TestCase { - public function testDefaults(): void + public function testDefault(): void { - $classifier = new ExceptionClassifier(); + $exceptionClassifier = new ExceptionClassifier(); - $this->assertTrue($classifier->classify(new \Error())); - $this->assertTrue($classifier->classify(new \Exception())); + $this->assertTrue($exceptionClassifier->classify(new \Error())); + $this->assertTrue($exceptionClassifier->classify(new \Exception())); } - public function testClassify(): void + public function testInherited(): void { - $classifier = new ExceptionClassifier([ + $exceptionClassifier = new ExceptionClassifier([ \RuntimeException::class, ]); - $this->assertTrue($classifier->classify(new \RuntimeException())); - $this->assertTrue($classifier->classify(new \UnexpectedValueException())); - $this->assertFalse($classifier->classify(new \InvalidArgumentException())); - $this->assertFalse($classifier->classify(new \Exception())); + $this->assertTrue($exceptionClassifier->classify(new \RuntimeException())); + $this->assertTrue($exceptionClassifier->classify(new \UnexpectedValueException())); + } + + public function testUnsupported(): void + { + $exceptionClassifier = new ExceptionClassifier([ + \RuntimeException::class, + ]); + + $this->assertFalse($exceptionClassifier->classify(new \InvalidArgumentException())); + $this->assertFalse($exceptionClassifier->classify(new \Exception())); } } diff --git a/tests/Retry/RetryTest.php b/tests/Retry/RetryTest.php index 4bd221d..4913af3 100644 --- a/tests/Retry/RetryTest.php +++ b/tests/Retry/RetryTest.php @@ -31,11 +31,11 @@ public function testThrowException(): void { $backOff = new ConstantBackOff(0, 0); - $classifier = new ExceptionClassifier([ + $exceptionClassifier = new ExceptionClassifier([ \RuntimeException::class, ]); - $retry = new Retry($backOff, $classifier); + $retry = new Retry($backOff, $exceptionClassifier); $this->expectException(\RuntimeException::class); @@ -46,20 +46,20 @@ public function testThrowException(): void public function testAttemptsCounter(): void { - $counter = new BackOffCounter(3); + $attemptsCounter = new AttemptsCounter(3); - $classifier = new ExceptionClassifier([ + $exceptionClassifier = new ExceptionClassifier([ \RuntimeException::class, ]); - $retry = new Retry($counter, $classifier); + $retry = new Retry($attemptsCounter, $exceptionClassifier); try { $retry->call(function () { throw new \RuntimeException(); }); } catch (\RuntimeException $e) { - $this->assertSame(3, $counter->getLastAttempt()); + $this->assertSame(3, $attemptsCounter->getLastAttempt()); } } } diff --git a/tests/Strategy/ConstantStrategyTest.php b/tests/Strategy/ConstantStrategyTest.php index c6757c9..9e7300e 100644 --- a/tests/Strategy/ConstantStrategyTest.php +++ b/tests/Strategy/ConstantStrategyTest.php @@ -10,9 +10,9 @@ class ConstantStrategyTest extends TestCase { public function testCalculate(): void { - $strategy = new ConstantStrategy(); + $constantStrategy = new ConstantStrategy(); - $duration = $strategy->calculate(new Milliseconds(1000), 3); + $duration = $constantStrategy->calculate(new Milliseconds(1000), 3); $this->assertEquals(1000, $duration->asMilliseconds()); } diff --git a/tests/Strategy/DecorrelationJitterStrategyTest.php b/tests/Strategy/DecorrelationJitterStrategyTest.php index b589424..19c55dc 100644 --- a/tests/Strategy/DecorrelationJitterStrategyTest.php +++ b/tests/Strategy/DecorrelationJitterStrategyTest.php @@ -10,9 +10,9 @@ class DecorrelationJitterStrategyTest extends TestCase { public function testCalculate(): void { - $strategy = new DecorrelationJitterStrategy(3); + $decorrelationJitterStrategy = new DecorrelationJitterStrategy(3); - $duration = $strategy->calculate(new Milliseconds(1000), 0); + $duration = $decorrelationJitterStrategy->calculate(new Milliseconds(1000), 0); $this->assertGreaterThanOrEqual(1000, $duration->asMilliseconds()); $this->assertLessThanOrEqual(3000, $duration->asMilliseconds()); diff --git a/tests/Strategy/ExponentialStrategyTest.php b/tests/Strategy/ExponentialStrategyTest.php index 0874242..f29b090 100644 --- a/tests/Strategy/ExponentialStrategyTest.php +++ b/tests/Strategy/ExponentialStrategyTest.php @@ -10,9 +10,9 @@ class ExponentialStrategyTest extends TestCase { public function testCalculate(): void { - $strategy = new ExponentialStrategy(2); + $exponentialStrategy = new ExponentialStrategy(2); - $duration = $strategy->calculate(new Milliseconds(1000), 3); + $duration = $exponentialStrategy->calculate(new Milliseconds(1000), 3); $this->assertEquals(8000, $duration->asMilliseconds()); } diff --git a/tests/Strategy/LinearStrategyTest.php b/tests/Strategy/LinearStrategyTest.php index f97ec32..5de8408 100644 --- a/tests/Strategy/LinearStrategyTest.php +++ b/tests/Strategy/LinearStrategyTest.php @@ -10,9 +10,9 @@ class LinearStrategyTest extends TestCase { public function testCalculate(): void { - $strategy = new LinearStrategy(); + $linearStrategy = new LinearStrategy(); - $duration = $strategy->calculate(new Milliseconds(1000), 3); + $duration = $linearStrategy->calculate(new Milliseconds(1000), 3); $this->assertEquals(4000, $duration->asMilliseconds()); }