Skip to content

Commit

Permalink
Test Queue class
Browse files Browse the repository at this point in the history
  • Loading branch information
A5hleyRich committed Oct 9, 2017
1 parent 04e2c55 commit 10745e6
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 29 deletions.
45 changes: 29 additions & 16 deletions src/WP_Queue/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ class Cron {
/**
* @var string
*/
protected $identifier;
protected $id;

/**
* @var Worker
*/
protected $worker;

/**
* @var int
*/
protected $interval;

/**
* Timestamp of when processing the queue started.
*
Expand All @@ -24,20 +29,22 @@ class Cron {
/**
* Cron constructor.
*
* @param string $identifier
* @param string $id
* @param Worker $worker
* @param int $interval
*/
public function __construct( $identifier, $worker ) {
$this->identifier = $identifier;
$this->worker = $worker;
public function __construct( $id, $worker, $interval ) {
$this->id = $id;
$this->worker = $worker;
$this->interval = $interval;
}

/**
* Is the cron queue worker enabled?
*
* @return bool
*/
public function is_enabled() {
protected function is_enabled() {
if ( defined( 'DISABLE_WP_QUEUE_CRON' ) && DISABLE_WP_QUEUE_CRON ) {
return false;
}
Expand All @@ -47,30 +54,36 @@ public function is_enabled() {

/**
* Init cron class.
*
* @return bool
*/
public function init() {
if ( ! $this->is_enabled() ) {
return false;
}

add_filter( 'cron_schedules', array( $this, 'schedule_cron' ) );
add_action( "wp_queue_worker_{$this->identifier}", array( $this, 'cron_worker' ) );
add_action( "wp_queue_worker_{$this->id}", array( $this, 'cron_worker' ) );

if ( ! wp_next_scheduled( "wp_queue_worker_{$this->identifier}" ) ) {
if ( ! wp_next_scheduled( "wp_queue_worker_{$this->id}" ) ) {
// Schedule health check
wp_schedule_event( time(), 'wp_queue_cron_interval', "wp_queue_worker_{$this->identifier}" );
wp_schedule_event( time(), 'wp_queue_cron_interval', "wp_queue_worker_{$this->id}" );
}

return true;
}

/**
* Add 5 minutes to cron schedules.
* Add interval to cron schedules.
*
* @param array $schedules
*
* @return array
*/
public function schedule_cron( $schedules ) {
$interval = apply_filters( 'wp_queue_cron_interval', 5 );

$schedules['wp_queue_cron_interval'] = array(
'interval' => MINUTE_IN_SECONDS * $interval,
'display' => sprintf( __( 'Every %d Minutes' ), $interval ),
$schedules["wp_queue_cron_interval_{$this->id}"] = array(
'interval' => MINUTE_IN_SECONDS * $this->interval,
'display' => sprintf( __( 'Every %d Minutes' ), $this->interval ),
);

return $schedules;
Expand Down Expand Up @@ -121,7 +134,7 @@ protected function get_memory_limit() {
$memory_limit = '256M';
}

if ( ! $memory_limit || -1 == $memory_limit ) {
if ( ! $memory_limit || - 1 == $memory_limit ) {
// Unlimited, set to 1GB
$memory_limit = '1000M';
}
Expand Down
22 changes: 12 additions & 10 deletions src/WP_Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ class Queue {
protected $connection;

/**
* @var string
* @var Cron
*/
protected $identifier;
protected $cron;

/**
* Queue constructor.
*
* @param ConnectionInterface $connection
* @param string $identifier
*/
public function __construct( ConnectionInterface $connection, $identifier ) {
public function __construct( ConnectionInterface $connection ) {
$this->connection = $connection;
$this->identifier = $identifier;
}

/**
Expand All @@ -43,13 +41,17 @@ public function push( Job $job, $delay = 0 ) {
* Create a cron worker.
*
* @param int $attempts
* @param int $interval
*
* @return Cron
*/
public function cron( $attempts = 3 ) {
$cron = new Cron( $this->identifier, $this->worker( $attempts ) );

if ( $cron->is_enabled() ) {
$cron->init();
public function cron( $attempts = 3, $interval = 5 ) {
if ( is_null( $this->cron ) ) {
$this->cron = new Cron( get_class( $this->connection ), $this->worker( $attempts ), $interval );
$this->cron->init();
}

return $this->cron;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/WP_Queue/QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected static function build( $connection ) {
throw new ConnectionNotFoundException();
}

static::$instances[ $connection ] = new Queue( $connections[ $connection ], $connection );
static::$instances[ $connection ] = new Queue( $connections[ $connection ] );

return static::$instances[ $connection ];
}
Expand Down
6 changes: 4 additions & 2 deletions tests/TestDatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public function test_failed_jobs() {
}
}

class TestJob extends Job {
public function handle() {}
if ( ! class_exists( 'TestJob' ) ) {
class TestJob extends Job {
public function handle() {}
}
}
62 changes: 62 additions & 0 deletions tests/TestQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

use PHPUnit\Framework\TestCase;
use WP_Queue\Connections\ConnectionInterface;
use WP_Queue\Cron;
use WP_Queue\Job;
use WP_Queue\Queue;
use WP_Queue\Worker;

class TestQueue extends TestCase {

public function setUp() {
WP_Mock::setUp();
}

public function tearDown() {
WP_Mock::tearDown();
}

public function test_push_success() {
$insert_id = 12345;
$connection = Mockery::mock( ConnectionInterface::class );
$connection->shouldReceive( 'push' )->once()->andReturn( $insert_id );

$queue = new Queue( $connection );

$this->assertEquals( $insert_id, $queue->push( new TestJob() ) );
}

public function test_push_fail() {
$connection = Mockery::mock( ConnectionInterface::class );
$connection->shouldReceive( 'push' )->once()->andReturn( false );

$queue = new Queue( $connection );

$this->assertFalse( $queue->push( new TestJob() ) );
}

public function test_cron() {
$connection = Mockery::mock( ConnectionInterface::class );
$queue = new Queue( $connection );

WP_Mock::userFunction( 'wp_next_scheduled', array(
'return' => time(),
) );

$this->assertInstanceOf( Cron::class, $queue->cron() );
}

public function test_worker() {
$connection = Mockery::mock( ConnectionInterface::class );
$queue = new Queue( $connection );

$this->assertInstanceOf( Worker::class, $queue->worker( 3 ) );
}
}

if ( ! class_exists( 'TestJob' ) ) {
class TestJob extends Job {
public function handle() {}
}
}

0 comments on commit 10745e6

Please sign in to comment.