Skip to content

Commit 3f8c21a

Browse files
committed
Merge branch 'browner12-soft-delete-assertion' into 5.4
2 parents d8ba110 + f89f917 commit 3f8c21a

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-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\SoftDeletedInDatabase;
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 the given record has been deleted.
49+
*
50+
* @param string $table
51+
* @param array $data
52+
* @param string $connection
53+
* @return $this
54+
*/
55+
protected function assertSoftDeleted($table, array $data, $connection = null)
56+
{
57+
$this->assertThat(
58+
$table, new SoftDeletedInDatabase($this->getConnection($connection), $data)
59+
);
60+
61+
return $this;
62+
}
63+
4664
/**
4765
* Get the database connection.
4866
*
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Testing\Constraints;
4+
5+
use PHPUnit_Framework_Constraint;
6+
use Illuminate\Database\Connection;
7+
8+
class SoftDeletedInDatabase 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)
54+
->where($this->data)->whereNotNull('deleted_at')->count() > 0;
55+
}
56+
57+
/**
58+
* Get the description of the failure.
59+
*
60+
* @param string $table
61+
* @return string
62+
*/
63+
public function failureDescription($table)
64+
{
65+
return sprintf(
66+
"any soft deleted row in the table [%s] matches the attributes %s.\n\n%s",
67+
$table, $this->toString(), $this->getAdditionalInfo($table)
68+
);
69+
}
70+
71+
/**
72+
* Get additional info about the records found in the database table.
73+
*
74+
* @param string $table
75+
* @return string
76+
*/
77+
protected function getAdditionalInfo($table)
78+
{
79+
$results = $this->database->table($table)->get();
80+
81+
if ($results->isEmpty()) {
82+
return 'The table is empty';
83+
}
84+
85+
$description = 'Found: '.json_encode($results->take($this->show), JSON_PRETTY_PRINT);
86+
87+
if ($results->count() > $this->show) {
88+
$description .= sprintf(' and %s others', $results->count() - $this->show);
89+
}
90+
91+
return $description;
92+
}
93+
94+
/**
95+
* Get a string representation of the object.
96+
*
97+
* @return string
98+
*/
99+
public function toString()
100+
{
101+
return json_encode($this->data);
102+
}
103+
}

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->assertSoftDeleted($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->assertSoftDeleted($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)