diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..8849d54 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,11 @@ +tools: + external_code_coverage: true +build: + nodes: + analysis: + tests: + override: + - php-scrutinizer-run +filter: + excluded_paths: + - "tests/_support/_generated" \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 4e34745..e214a66 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,10 @@ php: # faster builds on new travis setup not using sudo sudo: false - +addons: + apt: + packages: + - beanstalkd install: - '[[ -z "$CI_USER_TOKEN" ]] || composer config github-oauth.github.com ${CI_USER_TOKEN};' - travis_retry composer self-update && composer --version @@ -17,3 +20,6 @@ install: script: - php ./vendor/bin/codecept run + - if [[ "$TRAVIS_PHP_VERSION" == "7.3" ]]; then php ./vendor/bin/codecept run --coverage-xml; fi +after_script: + - if [[ "$TRAVIS_PHP_VERSION" == "7.3" ]]; then wget https://scrutinizer-ci.com/ocular.phar; php ocular.phar code-coverage:upload --format=php-clover tests/_output/coverage.xml; fi diff --git a/codeception.yml b/codeception.yml index 779d777..8a4432c 100644 --- a/codeception.yml +++ b/codeception.yml @@ -5,3 +5,8 @@ paths: support: tests/_support envs: tests/_envs actor_suffix: Tester +coverage: + enabled: true + local: true + include: + - src/*.php diff --git a/composer.json b/composer.json index 50ff7cc..ad65e0e 100644 --- a/composer.json +++ b/composer.json @@ -17,17 +17,20 @@ "codeception/codeception": "4.0.x-dev | ^4.0" }, "require-dev": { - "pda/pheanstalk": "~3.0", + "pda/pheanstalk": "^3.0 | ^4.0", "codeception/util-robohelpers": "dev-master" }, "suggest": { "aws/aws-sdk-php": "For Amazon SQS", - "pda/pheanstalk": "For Beanstalkd (v4 is not supported)", + "pda/pheanstalk": "For Beanstalkd", "iron-io/iron_mq": "For Iron MQ (v4 is not supported)" }, "autoload":{ "classmap": ["src/"] }, + "autoload-dev": { + "classmap": ["tests/"] + }, "config": { "classmap-authoritative": true } diff --git a/src/Codeception/Lib/Driver/Pheanstalk4.php b/src/Codeception/Lib/Driver/Pheanstalk4.php new file mode 100644 index 0000000..429cc6c --- /dev/null +++ b/src/Codeception/Lib/Driver/Pheanstalk4.php @@ -0,0 +1,87 @@ +queue = Pheanstalk::create($config['host'], $config['port'], $config['timeout']); + } + + /** + * @inheritDoc + */ + public function addMessageToQueue($message, $queue) + { + $this->queue->useTube($queue); + $this->queue->put($message); + } + + /** + * @inheritDoc + */ + public function getQueues() + { + return $this->queue->listTubes(); + } + + /** + * @inheritDoc + */ + public function getMessagesCurrentCountOnQueue($queue) + { + $response = $this->queue->statsTube($queue); + return $response->getResponseName() !== ResponseInterface::RESPONSE_NOT_FOUND + ? $response['current-jobs-ready'] + : 0; + } + + /** + * @inheritDoc + */ + public function getMessagesTotalCountOnQueue($queue) + { + $response = $this->queue->statsTube($queue); + return $response->getResponseName() !== ResponseInterface::RESPONSE_NOT_FOUND + ? $response['total-jobs'] + : 0; + } + + public function clearQueue($queue) + { + $this->queue->useTube($queue); + while (null !== $job = $this->queue->peekBuried()) { + $this->queue->delete($job); + } + while (null !== $job = $this->queue->peekDelayed()) { + $this->queue->delete($job); + } + while (null !== $job = $this->queue->peekReady()) { + $this->queue->delete($job); + } + } + + public function getRequiredConfig() + { + return []; + } + + public function getDefaultConfig() + { + return ['port' => 11300, 'timeout' => 90, 'host' => 'localhost']; + } +} \ No newline at end of file diff --git a/src/Codeception/Module/Queue.php b/src/Codeception/Module/Queue.php index 8c04a3e..29dca2d 100644 --- a/src/Codeception/Module/Queue.php +++ b/src/Codeception/Module/Queue.php @@ -1,6 +1,7 @@ 'beanstalkq', - 'host' => 'localhost' - ); - - /** - * @var \Codeception\Module\Queue - */ - protected $module = null; - - public function _setUp() - { - $container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer'); - $this->module = new \Codeception\Module\Queue($container); - $this->module->_setConfig($this->config); - $this->module->_before(Stub::makeEmpty('\Codeception\TestInterface')); - try { - $this->module->clearQueue('default'); - } catch (ConnectionException $e) { - $this->markTestSkipped("Beanstalk is not running"); - } - } - - /** @test */ - public function flow() + public function configProvider() { - $this->module->addMessageToQueue('hello world - ' . date('d-m-y'), 'default'); - $this->module->clearQueue('default'); - - $this->module->seeQueueExists('default'); - $this->module->dontSeeQueueExists('fake_queue'); - - $this->module->seeEmptyQueue('default'); - $this->module->addMessageToQueue('hello world - ' . date('d-m-y'), 'default'); - $this->module->dontSeeEmptyQueue('default'); - - $this->module->seeQueueHasTotalCount('default', 2); - - $this->module->seeQueueHasCurrentCount('default', 1); - $this->module->dontSeeQueueHasCurrentCount('default', 9999); - - $this->module->grabQueues(); + return [ + [[ + 'type' => 'beanstalkd', + 'host' => 'localhost' + ]] + ]; } } diff --git a/tests/unit/Codeception/Module/QueueTest.php b/tests/unit/Codeception/Module/QueueTest.php new file mode 100644 index 0000000..dfc2562 --- /dev/null +++ b/tests/unit/Codeception/Module/QueueTest.php @@ -0,0 +1,47 @@ +_setConfig($config); + $module->_before(Stub::makeEmpty(TestInterface::class)); + try { + $module->clearQueue('default'); + } catch (\Throwable $t) { + $this->markTestSkipped("Connection failed for: " . print_r($config, true)); + } + $initialCount = $module->grabQueueTotalCount('default'); + $module->addMessageToQueue('hello world - ' . date('d-m-y'), 'default'); + $module->clearQueue('default'); + + $module->seeQueueExists('default'); + $module->dontSeeQueueExists('fake_queue'); + + $module->seeEmptyQueue('default'); + $module->addMessageToQueue('hello world - ' . date('d-m-y'), 'default'); + $module->dontSeeEmptyQueue('default'); + + $module->seeQueueHasTotalCount('default', $initialCount + 2); + + $module->seeQueueHasCurrentCount('default', 1); + $module->dontSeeQueueHasCurrentCount('default', 9999); + + $module->grabQueues(); + } +} \ No newline at end of file