diff --git a/src/WP_Queue/Connections/DatabaseConnection.php b/src/WP_Queue/Connections/DatabaseConnection.php index 3607feb..6f3b61d 100644 --- a/src/WP_Queue/Connections/DatabaseConnection.php +++ b/src/WP_Queue/Connections/DatabaseConnection.php @@ -89,30 +89,43 @@ 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; } /** @@ -120,15 +133,23 @@ public function release( $job ) { * * @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; } /** @@ -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 ) ); diff --git a/src/WP_Queue/Job.php b/src/WP_Queue/Job.php index d10e7fe..e160b56 100644 --- a/src/WP_Queue/Job.php +++ b/src/WP_Queue/Job.php @@ -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; } diff --git a/tests/TestDatabaseConnection.php b/tests/TestDatabaseConnection.php new file mode 100644 index 0000000..35cfb3e --- /dev/null +++ b/tests/TestDatabaseConnection.php @@ -0,0 +1,137 @@ +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() {} +} \ No newline at end of file