Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Soft delete assertion #18328

Merged
merged 3 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\HasSoftDeletedInDatabase;

trait InteractsWithDatabase
{
Expand Down Expand Up @@ -43,6 +44,23 @@ protected function assertDatabaseMissing($table, array $data, $connection = null
return $this;
}

/**
* Assert that a given where condition exists in the database.
*
* @param string $table
* @param array $data
* @param string $connection
* @return $this
*/
protected function assertDatabaseHasSoftDelete($table, array $data, $connection = null)
{
$this->assertThat(
$table, new HasSoftDeletedInDatabase($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,102 @@
<?php

namespace Illuminate\Foundation\Testing\Constraints;

use PHPUnit_Framework_Constraint;
use Illuminate\Database\Connection;

class HasSoftDeletedInDatabase 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(
"a 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->assertDatabaseHasSoftDelete($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->assertDatabaseHasSoftDelete($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