Skip to content

Commit 451bc21

Browse files
[11.x] Constrain key when asserting database has a model (#52464)
* Constrain key when asserting database has against a model * Use already constrained database has methods * Using a model instance that isn't saved to the database should fail
1 parent f0de069 commit 451bc21

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ trait InteractsWithDatabase
2424
* @param string|null $connection
2525
* @return $this
2626
*/
27-
protected function assertDatabaseHas($table, array $data, $connection = null)
27+
protected function assertDatabaseHas($table, array $data = [], $connection = null)
2828
{
29+
if ($table instanceof Model) {
30+
$data = [
31+
$table->getKeyName() => $table->getKey(),
32+
...$data,
33+
];
34+
}
35+
2936
$this->assertThat(
3037
$this->getTable($table), new HasInDatabase($this->getConnection($connection, $table), $data)
3138
);
@@ -41,8 +48,15 @@ protected function assertDatabaseHas($table, array $data, $connection = null)
4148
* @param string|null $connection
4249
* @return $this
4350
*/
44-
protected function assertDatabaseMissing($table, array $data, $connection = null)
51+
protected function assertDatabaseMissing($table, array $data = [], $connection = null)
4552
{
53+
if ($table instanceof Model) {
54+
$data = [
55+
$table->getKeyName() => $table->getKey(),
56+
...$data,
57+
];
58+
}
59+
4660
$constraint = new ReverseConstraint(
4761
new HasInDatabase($this->getConnection($connection, $table), $data)
4862
);
@@ -157,11 +171,7 @@ protected function assertNotSoftDeleted($table, array $data = [], $connection =
157171
*/
158172
protected function assertModelExists($model)
159173
{
160-
return $this->assertDatabaseHas(
161-
$model->getTable(),
162-
[$model->getKeyName() => $model->getKey()],
163-
$model->getConnectionName()
164-
);
174+
return $this->assertDatabaseHas($model);
165175
}
166176

167177
/**
@@ -172,11 +182,7 @@ protected function assertModelExists($model)
172182
*/
173183
protected function assertModelMissing($model)
174184
{
175-
return $this->assertDatabaseMissing(
176-
$model->getTable(),
177-
[$model->getKeyName() => $model->getKey()],
178-
$model->getConnectionName()
179-
);
185+
return $this->assertDatabaseMissing($model);
180186
}
181187

182188
/**

tests/Foundation/FoundationInteractsWithDatabaseTest.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,25 @@ public function testSeeInDatabaseFindsResults()
4444
$this->assertDatabaseHas($this->table, $this->data);
4545
}
4646

47-
public function testAssertDatabaseHasSupportModels()
47+
public function testAssertDatabaseHasSupportsModelClass()
4848
{
4949
$this->mockCountBuilder(1);
5050

5151
$this->assertDatabaseHas(ProductStub::class, $this->data);
52-
$this->assertDatabaseHas(new ProductStub, $this->data);
52+
}
53+
54+
public function testAssertDatabaseHasConstrainsToModel()
55+
{
56+
$data = $this->data;
57+
58+
$this->data = [
59+
'id' => 1,
60+
...$this->data,
61+
];
62+
63+
$this->mockCountBuilder(1);
64+
65+
$this->assertDatabaseHas(new ProductStub(['id' => 1]), $data);
5366
}
5467

5568
public function testSeeInDatabaseDoesNotFindResults()
@@ -102,12 +115,25 @@ public function testDontSeeInDatabaseDoesNotFindResults()
102115
$this->assertDatabaseMissing($this->table, $this->data);
103116
}
104117

105-
public function testAssertDatabaseMissingSupportModels()
118+
public function testAssertDatabaseMissingSupportsModelClass()
106119
{
107120
$this->mockCountBuilder(0);
108121

109122
$this->assertDatabaseMissing(ProductStub::class, $this->data);
110-
$this->assertDatabaseMissing(new ProductStub, $this->data);
123+
}
124+
125+
public function testAssertDatabaseMissingConstrainsToModel()
126+
{
127+
$data = $this->data;
128+
129+
$this->data = [
130+
'id' => 1,
131+
...$this->data,
132+
];
133+
134+
$this->mockCountBuilder(0);
135+
136+
$this->assertDatabaseMissing(new ProductStub(['id' => 1]), $data);
111137
}
112138

113139
public function testDontSeeInDatabaseFindsResults()

0 commit comments

Comments
 (0)