From 2bceb0d75774300b2357bad1dfb71528ad9a5324 Mon Sep 17 00:00:00 2001 From: Vasily Belosludcev Date: Sat, 19 Aug 2017 01:52:02 +0500 Subject: [PATCH] more tests --- src/Options/ModuleOptions.php | 2 +- src/Service/QueueService.php | 5 ++- test/AppTrait.php | 3 +- test/Options/ModuleOptionsTest.php | 49 ++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 test/Options/ModuleOptionsTest.php diff --git a/src/Options/ModuleOptions.php b/src/Options/ModuleOptions.php index 5251f24..a5dd693 100644 --- a/src/Options/ModuleOptions.php +++ b/src/Options/ModuleOptions.php @@ -32,7 +32,7 @@ public function getOneTimeLimit(): int public function setErrorLimit(int $errorLimit): ModuleOptions { - $this->errorLimit = $errorLimit; + $this->errorLimit = $errorLimit >= 0 ? $errorLimit : 0; return $this; } diff --git a/src/Service/QueueService.php b/src/Service/QueueService.php index 7e6b7a4..69c7aac 100644 --- a/src/Service/QueueService.php +++ b/src/Service/QueueService.php @@ -68,7 +68,10 @@ protected function executeTask(TaskEntity $entity): void $entity->setStatusId(TaskEntity::STATUS_OK); } else { $entity->incNumberErrors(); - if ($entity->getNumberErrors() >= $this->config->getErrorLimit()) { + if ( + $this->config->getErrorLimit() !== 0 + && $entity->getNumberErrors() >= $this->config->getErrorLimit() + ) { $entity->setStatusId(TaskEntity::STATUS_IMPOSSIBLE); } else { $entity->setStatusId(TaskEntity::STATUS_ERROR); diff --git a/test/AppTrait.php b/test/AppTrait.php index 2ac3e4b..9958f91 100644 --- a/test/AppTrait.php +++ b/test/AppTrait.php @@ -10,7 +10,7 @@ */ trait AppTrait { - protected function getSm(): ServiceManager + protected function getSm(array $config = []): ServiceManager { $moduleLoader = new ModuleLoader([ 'modules' => [ @@ -22,6 +22,7 @@ protected function getSm(): ServiceManager __DIR__ . '/../config/module.config.php', __DIR__ . '/config/module.config.php', ], + 'extra_config' => $config, 'module_paths' => [], 'config_cache_enabled' => false, 'module_map_cache_enabled' => false, diff --git a/test/Options/ModuleOptionsTest.php b/test/Options/ModuleOptionsTest.php new file mode 100644 index 0000000..8a01967 --- /dev/null +++ b/test/Options/ModuleOptionsTest.php @@ -0,0 +1,49 @@ + + */ +class ModuleOptionsTest extends TestCase +{ + use AppTrait; + + public function testInstance() + { + $options = $this->getSm()->get('Bupy7\Queue\Options\ModuleOptions'); + $this->assertInstanceOf(ModuleOptions::class, $options); + } + + public function testGet() + { + $options = $this->getSm()->get('Bupy7\Queue\Options\ModuleOptions'); + $this->assertEquals(100, $options->getOneTimeLimit()); + $this->assertEquals(5, $options->getErrorLimit()); + } + + public function testSet() + { + $options = $this->getSm()->get('Bupy7\Queue\Options\ModuleOptions'); + $options->setOneTimeLimit(-50); + $this->assertEquals(0, $options->getOneTimeLimit()); + $options->setErrorLimit(-10); + $this->assertEquals(0, $options->getErrorLimit()); + } + + public function testConfiguration() + { + $config = [ + 'one_time_limit' => 32, + 'error_limit' => 10, + ]; + $options = $this->getSm(['queue' => $config])->get('Bupy7\Queue\Options\ModuleOptions'); + $this->assertEquals(32, $options->getOneTimeLimit()); + $this->assertEquals(10, $options->getErrorLimit()); + } +}