Skip to content

Commit 6718ae6

Browse files
committed
Merge branch 'model-support-for-database-assertions' into 8.x
2 parents d456f60 + f8bf46b commit 6718ae6

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ trait InteractsWithDatabase
1717
/**
1818
* Assert that a given where condition exists in the database.
1919
*
20-
* @param string $table
20+
* @param \Illuminate\Database\Eloquent\Model|string $table
2121
* @param array $data
2222
* @param string|null $connection
2323
* @return $this
2424
*/
2525
protected function assertDatabaseHas($table, array $data, $connection = null)
2626
{
2727
$this->assertThat(
28-
$table, new HasInDatabase($this->getConnection($connection), $data)
28+
$this->getTable($table), new HasInDatabase($this->getConnection($connection), $data)
2929
);
3030

3131
return $this;
@@ -34,7 +34,7 @@ protected function assertDatabaseHas($table, array $data, $connection = null)
3434
/**
3535
* Assert that a given where condition does not exist in the database.
3636
*
37-
* @param string $table
37+
* @param \Illuminate\Database\Eloquent\Model|string $table
3838
* @param array $data
3939
* @param string|null $connection
4040
* @return $this
@@ -45,23 +45,23 @@ protected function assertDatabaseMissing($table, array $data, $connection = null
4545
new HasInDatabase($this->getConnection($connection), $data)
4646
);
4747

48-
$this->assertThat($table, $constraint);
48+
$this->assertThat($this->getTable($table), $constraint);
4949

5050
return $this;
5151
}
5252

5353
/**
5454
* Assert the count of table entries.
5555
*
56-
* @param string $table
56+
* @param \Illuminate\Database\Eloquent\Model|string $table
5757
* @param int $count
5858
* @param string|null $connection
5959
* @return $this
6060
*/
6161
protected function assertDatabaseCount($table, int $count, $connection = null)
6262
{
6363
$this->assertThat(
64-
$table, new CountInDatabase($this->getConnection($connection), $count)
64+
$this->getTable($table), new CountInDatabase($this->getConnection($connection), $count)
6565
);
6666

6767
return $this;
@@ -81,7 +81,7 @@ protected function assertDeleted($table, array $data = [], $connection = null)
8181
return $this->assertDatabaseMissing($table->getTable(), [$table->getKeyName() => $table->getKey()], $table->getConnectionName());
8282
}
8383

84-
$this->assertDatabaseMissing($table, $data, $connection);
84+
$this->assertDatabaseMissing($this->getTable($table), $data, $connection);
8585

8686
return $this;
8787
}
@@ -102,7 +102,7 @@ protected function assertSoftDeleted($table, array $data = [], $connection = nul
102102
}
103103

104104
$this->assertThat(
105-
$table, new SoftDeletedInDatabase($this->getConnection($connection), $data, $deletedAtColumn)
105+
$this->getTable($table), new SoftDeletedInDatabase($this->getConnection($connection), $data, $deletedAtColumn)
106106
);
107107

108108
return $this;
@@ -152,6 +152,17 @@ protected function getConnection($connection = null)
152152
return $database->connection($connection);
153153
}
154154

155+
/**
156+
* Get the table name from the given model or string.
157+
*
158+
* @param \Illuminate\Database\Eloquent\Model|string $table
159+
* @return string
160+
*/
161+
protected function getTable($table)
162+
{
163+
return is_subclass_of($table, Model::class) ? (new $table)->getTable() : $table;
164+
}
165+
155166
/**
156167
* Seed a given database connection.
157168
*

tests/Foundation/FoundationInteractsWithDatabaseTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public function testSeeInDatabaseFindsResults()
4141
$this->assertDatabaseHas($this->table, $this->data);
4242
}
4343

44+
public function testAssertDatabaseHasSupportModels()
45+
{
46+
$this->mockCountBuilder(1);
47+
48+
$this->assertDatabaseHas(ProductStub::class, $this->data);
49+
$this->assertDatabaseHas(new ProductStub, $this->data);
50+
}
51+
4452
public function testSeeInDatabaseDoesNotFindResults()
4553
{
4654
$this->expectException(ExpectationFailedException::class);
@@ -91,6 +99,14 @@ public function testDontSeeInDatabaseDoesNotFindResults()
9199
$this->assertDatabaseMissing($this->table, $this->data);
92100
}
93101

102+
public function testAssertDatabaseMissingSupportModels()
103+
{
104+
$this->mockCountBuilder(0);
105+
106+
$this->assertDatabaseMissing(ProductStub::class, $this->data);
107+
$this->assertDatabaseMissing(new ProductStub, $this->data);
108+
}
109+
94110
public function testDontSeeInDatabaseFindsResults()
95111
{
96112
$this->expectException(ExpectationFailedException::class);
@@ -110,6 +126,14 @@ public function testAssertTableEntriesCount()
110126
$this->assertDatabaseCount($this->table, 1);
111127
}
112128

129+
public function testAssertDatabaseCountSupportModels()
130+
{
131+
$this->mockCountBuilder(1);
132+
133+
$this->assertDatabaseCount(ProductStub::class, 1);
134+
$this->assertDatabaseCount(new ProductStub, 1);
135+
}
136+
113137
public function testAssertTableEntriesCountWrong()
114138
{
115139
$this->expectException(ExpectationFailedException::class);
@@ -168,6 +192,13 @@ public function testAssertSoftDeletedInDatabaseFindsResults()
168192
$this->assertSoftDeleted($this->table, $this->data);
169193
}
170194

195+
public function testAssertSoftDeletedSupportModelStrings()
196+
{
197+
$this->mockCountBuilder(1);
198+
199+
$this->assertSoftDeleted(ProductStub::class, $this->data);
200+
}
201+
171202
public function testAssertSoftDeletedInDatabaseDoesNotFindResults()
172203
{
173204
$this->expectException(ExpectationFailedException::class);
@@ -208,6 +239,13 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnR
208239
$this->assertSoftDeleted(new CustomProductStub($this->data));
209240
}
210241

242+
public function testGetTableNameFromModel()
243+
{
244+
$this->assertEquals($this->table, $this->getTable(ProductStub::class));
245+
$this->assertEquals($this->table, $this->getTable(new ProductStub));
246+
$this->assertEquals($this->table, $this->getTable($this->table));
247+
}
248+
211249
protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at')
212250
{
213251
$builder = m::mock(Builder::class);

0 commit comments

Comments
 (0)