Skip to content

Commit 81187d5

Browse files
committed
Merge branch 'soft-delete-assertion' of https://github.com/browner12/framework into browner12-soft-delete-assertion
2 parents 8ebb5b8 + 29ed5d7 commit 81187d5

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit_Framework_Constraint_Not as ReverseConstraint;
66
use Illuminate\Foundation\Testing\Constraints\HasInDatabase;
7+
use Illuminate\Foundation\Testing\Constraints\HasSoftDeletedInDatabase;
78

89
trait InteractsWithDatabase
910
{
@@ -43,6 +44,23 @@ protected function assertDatabaseMissing($table, array $data, $connection = null
4344
return $this;
4445
}
4546

47+
/**
48+
* Assert that a given where condition exists in the database.
49+
*
50+
* @param string $table
51+
* @param array $data
52+
* @param string $connection
53+
* @return $this
54+
*/
55+
protected function assertDatabaseHasSoftDelete($table, array $data, $connection = null)
56+
{
57+
$this->assertThat(
58+
$table, new HasSoftDeletedInDatabase($this->getConnection($connection), $data)
59+
);
60+
61+
return $this;
62+
}
63+
4664
/**
4765
* Get the database connection.
4866
*
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Testing\Constraints;
4+
5+
use PHPUnit_Framework_Constraint;
6+
use Illuminate\Database\Connection;
7+
8+
class HasSoftDeletedInDatabase extends PHPUnit_Framework_Constraint
9+
{
10+
/**
11+
* Number of records that will be shown in the console in case of failure.
12+
*
13+
* @var int
14+
*/
15+
protected $show = 3;
16+
17+
/**
18+
* The database connection.
19+
*
20+
* @var \Illuminate\Database\Connection
21+
*/
22+
protected $database;
23+
24+
/**
25+
* The data that will be used to narrow the search in the database table.
26+
*
27+
* @var array
28+
*/
29+
protected $data;
30+
31+
/**
32+
* Create a new constraint instance.
33+
*
34+
* @param \Illuminate\Database\Connection $database
35+
* @param array $data
36+
* @return void
37+
*/
38+
public function __construct(Connection $database, array $data)
39+
{
40+
$this->data = $data;
41+
42+
$this->database = $database;
43+
}
44+
45+
/**
46+
* Check if the data is found in the given table.
47+
*
48+
* @param string $table
49+
* @return bool
50+
*/
51+
public function matches($table)
52+
{
53+
return $this->database->table($table)->where($this->data)->whereNotNull('deleted_at')->count() > 0;
54+
}
55+
56+
/**
57+
* Get the description of the failure.
58+
*
59+
* @param string $table
60+
* @return string
61+
*/
62+
public function failureDescription($table)
63+
{
64+
return sprintf(
65+
"a soft deleted row in the table [%s] matches the attributes %s.\n\n%s",
66+
$table, $this->toString(), $this->getAdditionalInfo($table)
67+
);
68+
}
69+
70+
/**
71+
* Get additional info about the records found in the database table.
72+
*
73+
* @param string $table
74+
* @return string
75+
*/
76+
protected function getAdditionalInfo($table)
77+
{
78+
$results = $this->database->table($table)->get();
79+
80+
if ($results->isEmpty()) {
81+
return 'The table is empty';
82+
}
83+
84+
$description = 'Found: '.json_encode($results->take($this->show), JSON_PRETTY_PRINT);
85+
86+
if ($results->count() > $this->show) {
87+
$description .= sprintf(' and %s others', $results->count() - $this->show);
88+
}
89+
90+
return $description;
91+
}
92+
93+
/**
94+
* Get a string representation of the object.
95+
*
96+
* @return string
97+
*/
98+
public function toString()
99+
{
100+
return json_encode($this->data);
101+
}
102+
}

tests/Foundation/FoundationInteractsWithDatabaseTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,34 @@ public function testDontSeeInDatabaseFindsResults()
101101
$this->assertDatabaseMissing($this->table, $this->data);
102102
}
103103

104+
public function testSeeSoftDeletedInDatabaseFindsResults()
105+
{
106+
$this->mockCountBuilder(1);
107+
108+
$this->assertDatabaseHasSoftDelete($this->table, $this->data);
109+
}
110+
111+
/**
112+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
113+
* @expectedExceptionMessage The table is empty.
114+
*/
115+
public function testSeeSoftDeletedInDatabaseDoesNotFindResults()
116+
{
117+
$builder = $this->mockCountBuilder(0);
118+
119+
$builder->shouldReceive('get')->andReturn(collect());
120+
121+
$this->assertDatabaseHasSoftDelete($this->table, $this->data);
122+
}
123+
104124
protected function mockCountBuilder($countResult)
105125
{
106126
$builder = m::mock(Builder::class);
107127

108128
$builder->shouldReceive('where')->with($this->data)->andReturnSelf();
109129

130+
$builder->shouldReceive('whereNotNull')->with('deleted_at')->andReturnSelf();
131+
110132
$builder->shouldReceive('count')->andReturn($countResult);
111133

112134
$this->connection->shouldReceive('table')

0 commit comments

Comments
 (0)