Skip to content

Commit

Permalink
Test DatabaseConnection class
Browse files Browse the repository at this point in the history
  • Loading branch information
A5hleyRich committed Oct 9, 2017
1 parent 86f5a94 commit 0398fd8
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 10 deletions.
37 changes: 29 additions & 8 deletions src/WP_Queue/Connections/DatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,46 +89,67 @@ public function pop() {
* Delete a job from the queue.
*
* @param Job $job
*
* @return bool
*/
public function delete( $job ) {
$where = array(
'id' => $job->id(),
);

$this->database->delete( $this->jobs_table, $where );
if ( $this->database->delete( $this->jobs_table, $where ) ) {
return true;
}

return false;
}

/**
* Release a job back onto the queue.
*
* @param Job $job
*
* @return bool
*/
public function release( $job ) {
$data = array(
$data = array(
'job' => serialize( $job ),
'attempts' => $job->attempts(),
'reserved_at' => null,
);

$this->database->update( $this->jobs_table, $data, array(
$where = array(
'id' => $job->id(),
) );
);

if ( $this->database->update( $this->jobs_table, $data, $where ) ) {
return true;
}

return false;
}

/**
* Push a job onto the failure queue.
*
* @param Job $job
* @param Exception $exception
*
* @return bool
*/
public function failure( $job, Exception $exception ) {
$this->database->insert( $this->failures_table, array(
$insert = $this->database->insert( $this->failures_table, array(
'job' => serialize( $job ),
'error' => $this->format_exception( $exception ),
'failed_at' => $this->datetime(),
) );

$this->delete( $job );
if ( $insert ) {
$this->delete( $job );

return true;
}

return false;
}

/**
Expand Down Expand Up @@ -194,7 +215,7 @@ protected function vitalize_job( $raw_job ) {

$job->set_id( $raw_job->id );
$job->set_attempts( $raw_job->attempts );
$job->set_reserved_at( new Carbon( $raw_job->reserved_at ) );
$job->set_reserved_at( empty( $raw_job->reserved_at ) ? null : new Carbon( $raw_job->reserved_at ) );
$job->set_available_at( new Carbon( $raw_job->available_at ) );
$job->set_created_at( new Carbon( $raw_job->created_at ) );

Expand Down
4 changes: 2 additions & 2 deletions src/WP_Queue/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public function reserved_at() {
/**
* Set reserved at date.
*
* @param Carbon $reserved_at
* @param null|Carbon $reserved_at
*/
public function set_reserved_at( Carbon $reserved_at ) {
public function set_reserved_at( $reserved_at ) {
$this->reserved_at = $reserved_at;
}

Expand Down
137 changes: 137 additions & 0 deletions tests/TestDatabaseConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use WP_Queue\Connections\DatabaseConnection;
use WP_Queue\Job;

class TestDatabaseConnection extends TestCase {

protected $wpdb;

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

$this->wpdb = Mockery::spy( 'WPDB' );;
$this->wpdb->prefix = "wp_";
}

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

public function test_push_success() {
$insert_id = 12345;
$this->wpdb->shouldReceive( 'insert' )->once()->andReturn( 1 );
$this->wpdb->insert_id = $insert_id;

$instance = new DatabaseConnection( $this->wpdb );

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

public function test_push_fail() {
$this->wpdb->shouldReceive( 'insert' )->once()->andReturn( false );

$instance = new DatabaseConnection( $this->wpdb );

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

public function test_pop_success() {
$this->wpdb->shouldReceive( 'get_row' )->once()->andReturn( (object) array(
'id' => 12345,
'job' => serialize( new TestJob() ),
'attempts' => 0,
'reserved_at' => null,
'available_at' => '2017-10-09 00:00:00',
'created_at' => '2017-10-09 00:00:00',
) );
$instance = new DatabaseConnection( $this->wpdb );
$job = $instance->pop();

$this->assertInstanceOf( TestJob::class, $job );
$this->assertEquals( 12345, $job->id() );
$this->assertEquals( 0, $job->attempts() );
$this->assertNull( $job->reserved_at() );
$this->assertInstanceOf( Carbon::class, $job->available_at() );
$this->assertInstanceOf( Carbon::class, $job->created_at() );
}

public function test_pop_fail() {
$this->wpdb->shouldReceive( 'get_row' )->once()->andReturn( null );
$instance = new DatabaseConnection( $this->wpdb );

$this->assertFalse( $instance->pop() );
}

public function test_delete_success() {
$this->wpdb->shouldReceive( 'delete' )->once()->andReturn( 1 );

$instance = new DatabaseConnection( $this->wpdb );

$this->assertTrue( $instance->delete( new TestJob() ) );
}

public function test_delete_fail() {
$this->wpdb->shouldReceive( 'delete' )->once()->andReturn( false );

$instance = new DatabaseConnection( $this->wpdb );

$this->assertFalse( $instance->delete( new TestJob() ) );
}

public function test_release_success() {
$this->wpdb->shouldReceive( 'update' )->once()->andReturn( 1 );

$instance = new DatabaseConnection( $this->wpdb );

$this->assertTrue( $instance->release( new TestJob() ) );
}

public function test_release_fail() {
$this->wpdb->shouldReceive( 'update' )->once()->andReturn( false );

$instance = new DatabaseConnection( $this->wpdb );

$this->assertFalse( $instance->release( new TestJob() ) );
}

public function test_failure_success() {
$this->wpdb->shouldReceive( 'insert' )->once()->andReturn( 1 );

$instance = new DatabaseConnection( $this->wpdb );

$this->assertTrue( $instance->failure( new TestJob(), new Exception() ) );
}

public function test_failure_fail() {
$this->wpdb->shouldReceive( 'insert' )->once()->andReturn( false );

$instance = new DatabaseConnection( $this->wpdb );

$this->assertFalse( $instance->failure( new TestJob(), new Exception() ) );
}

public function test_jobs() {
$count = rand( 1, 100 );
$this->wpdb->shouldReceive( 'get_var' )->once()->andReturn( $count );

$instance = new DatabaseConnection( $this->wpdb );

$this->assertEquals( $count, $instance->jobs() );
}

public function test_failed_jobs() {
$count = rand( 1, 100 );
$this->wpdb->shouldReceive( 'get_var' )->once()->andReturn( $count );

$instance = new DatabaseConnection( $this->wpdb );

$this->assertEquals( $count, $instance->failed_jobs() );
}
}

class TestJob extends Job {
public function handle() {}
}

0 comments on commit 0398fd8

Please sign in to comment.