diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index b41df5468f15..a2e60e7916f8 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -4,6 +4,7 @@ use PHPUnit_Framework_Constraint_Not as ReverseConstraint; use Illuminate\Foundation\Testing\Constraints\HasInDatabase; +use Illuminate\Foundation\Testing\Constraints\SoftDeletedInDatabase; trait InteractsWithDatabase { @@ -43,6 +44,23 @@ protected function assertDatabaseMissing($table, array $data, $connection = null return $this; } + /** + * Assert the given record has been deleted. + * + * @param string $table + * @param array $data + * @param string $connection + * @return $this + */ + protected function assertSoftDeleted($table, array $data, $connection = null) + { + $this->assertThat( + $table, new SoftDeletedInDatabase($this->getConnection($connection), $data) + ); + + return $this; + } + /** * Get the database connection. * diff --git a/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php b/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php new file mode 100644 index 000000000000..8d74d4f6d490 --- /dev/null +++ b/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php @@ -0,0 +1,103 @@ +data = $data; + + $this->database = $database; + } + + /** + * Check if the data is found in the given table. + * + * @param string $table + * @return bool + */ + public function matches($table) + { + return $this->database->table($table) + ->where($this->data)->whereNotNull('deleted_at')->count() > 0; + } + + /** + * Get the description of the failure. + * + * @param string $table + * @return string + */ + public function failureDescription($table) + { + return sprintf( + "any soft deleted row in the table [%s] matches the attributes %s.\n\n%s", + $table, $this->toString(), $this->getAdditionalInfo($table) + ); + } + + /** + * Get additional info about the records found in the database table. + * + * @param string $table + * @return string + */ + protected function getAdditionalInfo($table) + { + $results = $this->database->table($table)->get(); + + if ($results->isEmpty()) { + return 'The table is empty'; + } + + $description = 'Found: '.json_encode($results->take($this->show), JSON_PRETTY_PRINT); + + if ($results->count() > $this->show) { + $description .= sprintf(' and %s others', $results->count() - $this->show); + } + + return $description; + } + + /** + * Get a string representation of the object. + * + * @return string + */ + public function toString() + { + return json_encode($this->data); + } +} diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 3f9303cf4b4c..fd0d7ad95ef3 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -101,12 +101,34 @@ public function testDontSeeInDatabaseFindsResults() $this->assertDatabaseMissing($this->table, $this->data); } + public function testSeeSoftDeletedInDatabaseFindsResults() + { + $this->mockCountBuilder(1); + + $this->assertSoftDeleted($this->table, $this->data); + } + + /** + * @expectedException \PHPUnit_Framework_ExpectationFailedException + * @expectedExceptionMessage The table is empty. + */ + public function testSeeSoftDeletedInDatabaseDoesNotFindResults() + { + $builder = $this->mockCountBuilder(0); + + $builder->shouldReceive('get')->andReturn(collect()); + + $this->assertSoftDeleted($this->table, $this->data); + } + protected function mockCountBuilder($countResult) { $builder = m::mock(Builder::class); $builder->shouldReceive('where')->with($this->data)->andReturnSelf(); + $builder->shouldReceive('whereNotNull')->with('deleted_at')->andReturnSelf(); + $builder->shouldReceive('count')->andReturn($countResult); $this->connection->shouldReceive('table')