From 204faa000d4ff1900bfb9c7f79089a635251f605 Mon Sep 17 00:00:00 2001 From: RVxLab Date: Sat, 11 Sep 2021 19:27:07 +0200 Subject: [PATCH 1/2] [8.x] Add `assertExists` testing method --- .../Concerns/InteractsWithDatabase.php | 19 +++++++++++ .../FoundationInteractsWithDatabaseTest.php | 34 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index 269293a76590..dbcd63cc537d 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -108,6 +108,25 @@ protected function assertSoftDeleted($table, array $data = [], $connection = nul return $this; } + /** + * Assert the given record exists in the database. + * + * @param \Illuminate\Database\Eloquent\Model|string $table + * @param array $data + * @param string|null $connection + * @return $this + */ + protected function assertExists($table, array $data = [], $connection = null) + { + if ($table instanceof Model) { + return $this->assertDatabaseHas($table->getTable(), [$table->getKeyName() => $table->getKey()], $table->getConnectionName()); + } + + $this->assertDatabaseHas($this->getTable($table), $data, $connection); + + return $this; + } + /** * Determine if the argument is a soft deletable model. * diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 6b5127ce8980..1956d85a6a4d 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -239,6 +239,40 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnR $this->assertSoftDeleted(new CustomProductStub($this->data)); } + public function testAssertExistsPassesWhenFindsResults() + { + $this->data = ['id' => 1]; + + $builder = $this->mockCountBuilder(1); + + $builder->shouldReceive('get')->andReturn(collect($this->data)); + + $this->assertExists(new ProductStub($this->data)); + } + + public function testAssertExistsSupportsModelStrings() + { + $this->data = ['id' => 1]; + + $builder = $this->mockCountBuilder(1); + + $builder->shouldReceive('get')->andReturn(collect($this->data)); + + $this->assertExists(ProductStub::class, $this->data); + } + + public function testAssertExistsFailsDoesNotFindResults() + { + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('The table is empty.'); + + $builder = $this->mockCountBuilder(0); + + $builder->shouldReceive('get')->andReturn(collect()); + + $this->assertExists($this->table, $this->data); + } + public function testGetTableNameFromModel() { $this->assertEquals($this->table, $this->getTable(ProductStub::class)); From 545e35c463a7c4792a17c4710586d3ad311a7de5 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 13 Sep 2021 09:15:08 -0500 Subject: [PATCH 2/2] add methods --- .../Concerns/InteractsWithDatabase.php | 33 +++++++++++------ .../FoundationInteractsWithDatabaseTest.php | 36 +++++++------------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index dbcd63cc537d..166e5cc860a8 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -109,22 +109,33 @@ protected function assertSoftDeleted($table, array $data = [], $connection = nul } /** - * Assert the given record exists in the database. + * Assert the given model exists in the database. * - * @param \Illuminate\Database\Eloquent\Model|string $table - * @param array $data - * @param string|null $connection + * @param \Illuminate\Database\Eloquent\Model $model * @return $this */ - protected function assertExists($table, array $data = [], $connection = null) + protected function assertModelExists($model) { - if ($table instanceof Model) { - return $this->assertDatabaseHas($table->getTable(), [$table->getKeyName() => $table->getKey()], $table->getConnectionName()); - } - - $this->assertDatabaseHas($this->getTable($table), $data, $connection); + return $this->assertDatabaseHas( + $model->getTable(), + [$model->getKeyName() => $model->getKey()], + $model->getConnectionName() + ); + } - return $this; + /** + * Assert the given model does not exist in the database. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @return $this + */ + protected function assertModelMissing($model) + { + return $this->assertDatabaseMissing( + $model->getTable(), + [$model->getKeyName() => $model->getKey()], + $model->getConnectionName() + ); } /** diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 1956d85a6a4d..394110dc5f79 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -172,6 +172,17 @@ public function testAssertDeletedPassesWhenDoesNotFindModelResults() $this->assertDeleted(new ProductStub($this->data)); } + public function testAssertModelMissingPassesWhenDoesNotFindModelResults() + { + $this->data = ['id' => 1]; + + $builder = $this->mockCountBuilder(0); + + $builder->shouldReceive('get')->andReturn(collect()); + + $this->assertModelMissing(new ProductStub($this->data)); + } + public function testAssertDeletedFailsWhenFindsModelResults() { $this->expectException(ExpectationFailedException::class); @@ -247,30 +258,7 @@ public function testAssertExistsPassesWhenFindsResults() $builder->shouldReceive('get')->andReturn(collect($this->data)); - $this->assertExists(new ProductStub($this->data)); - } - - public function testAssertExistsSupportsModelStrings() - { - $this->data = ['id' => 1]; - - $builder = $this->mockCountBuilder(1); - - $builder->shouldReceive('get')->andReturn(collect($this->data)); - - $this->assertExists(ProductStub::class, $this->data); - } - - public function testAssertExistsFailsDoesNotFindResults() - { - $this->expectException(ExpectationFailedException::class); - $this->expectExceptionMessage('The table is empty.'); - - $builder = $this->mockCountBuilder(0); - - $builder->shouldReceive('get')->andReturn(collect()); - - $this->assertExists($this->table, $this->data); + $this->assertModelExists(new ProductStub($this->data)); } public function testGetTableNameFromModel()