Skip to content

Commit

Permalink
Merge branch 'browner12-soft-delete-assertion' into 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 16, 2017
2 parents d8ba110 + f89f917 commit 3f8c21a
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Illuminate\Foundation\Testing\Constraints;

use PHPUnit_Framework_Constraint;
use Illuminate\Database\Connection;

class SoftDeletedInDatabase extends PHPUnit_Framework_Constraint
{
/**
* Number of records that will be shown in the console in case of failure.
*
* @var int
*/
protected $show = 3;

/**
* The database connection.
*
* @var \Illuminate\Database\Connection
*/
protected $database;

/**
* The data that will be used to narrow the search in the database table.
*
* @var array
*/
protected $data;

/**
* Create a new constraint instance.
*
* @param \Illuminate\Database\Connection $database
* @param array $data
* @return void
*/
public function __construct(Connection $database, array $data)
{
$this->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);
}
}
22 changes: 22 additions & 0 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 3f8c21a

Please sign in to comment.