Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tools:
external_code_coverage: true
build:
nodes:
analysis:
tests:
override:
- php-scrutinizer-run
filter:
excluded_paths:
- "tests/_support/_generated"
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ 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
- travis_retry composer update --prefer-dist --no-interaction

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
5 changes: 5 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ paths:
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
coverage:
enabled: true
local: true
include:
- src/*.php
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
87 changes: 87 additions & 0 deletions src/Codeception/Lib/Driver/Pheanstalk4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);

namespace Codeception\Lib\Driver;


use Codeception\Lib\Interfaces\Queue;
use Pheanstalk\Contract\ResponseInterface;
use Pheanstalk\Pheanstalk;

class Pheanstalk4 implements Queue
{
/**
* @var Pheanstalk
*/
private $queue;
/**
* @inheritDoc
*/
public function openConnection($config)
{
$this->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'];
}
}
8 changes: 7 additions & 1 deletion src/Codeception/Module/Queue.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Codeception\Module;

use Codeception\Lib\Driver\Pheanstalk4;
use Codeception\Module as CodeceptionModule;
use Codeception\TestInterface;
use Codeception\Exception\ModuleConfigException;
Expand Down Expand Up @@ -167,7 +168,12 @@ protected function createQueueDriver()
case 'beanstalk':
case 'beanstalkd':
case 'beanstalkq':
return new Beanstalk();
// Account for different versions of Pheanstalk.
if (interface_exists(\Pheanstalk\Contract\JobIdInterface::class)) {
return new Pheanstalk4();
} else {
return new Beanstalk();
}
default:
throw new ModuleConfigException(
__CLASS__,
Expand Down
53 changes: 8 additions & 45 deletions tests/unit/Codeception/Module/BeanstalkdTest.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
<?php

use Codeception\Util\Stub;
use Pheanstalk\Exception\ConnectionException;

class BeanstalkdTest extends \Codeception\PHPUnit\TestCase
class BeanstalkdTest extends QueueTest
{
protected $config = array(
'type' => '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'
]]
];
}
}
47 changes: 47 additions & 0 deletions tests/unit/Codeception/Module/QueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Codeception\Lib\ModuleContainer;
use Codeception\Module\Queue;
use \Codeception\PHPUnit\TestCase;
use Codeception\TestInterface;
use Codeception\Util\Stub;

abstract class QueueTest extends TestCase
{

abstract public function configProvider();

/**
* @dataProvider configProvider
*/
public function testFlow($config)
{
/** @var ModuleContainer $container */
$container = Stub::make(ModuleContainer::class);
$module = new Queue($container);
$module->_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();
}
}