diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index 49c0e35ed755..141504fda314 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -106,7 +106,12 @@ protected function assertSoftDeleted($table, array $data = [], $connection = nul } $this->assertThat( - $this->getTable($table), new SoftDeletedInDatabase($this->getConnection($connection, $table), $data, $deletedAtColumn) + $this->getTable($table), + new SoftDeletedInDatabase( + $this->getConnection($connection, $table), + $data, + $this->getDeletedAtColumn($table, $deletedAtColumn) + ) ); return $this; @@ -133,7 +138,12 @@ protected function assertNotSoftDeleted($table, array $data = [], $connection = } $this->assertThat( - $this->getTable($table), new NotSoftDeletedInDatabase($this->getConnection($connection, $table), $data, $deletedAtColumn) + $this->getTable($table), + new NotSoftDeletedInDatabase( + $this->getConnection($connection, $table), + $data, + $this->getDeletedAtColumn($table, $deletedAtColumn) + ) ); return $this; @@ -270,6 +280,18 @@ protected function getTableConnection($table) return $this->newModelFor($table)?->getConnectionName(); } + /** + * Get the table column name used for soft deletes. + * + * @param string $table + * @param string $defaultColumnName + * @return string + */ + protected function getDeletedAtColumn($table, $defaultColumnName = 'deleted_at') + { + return $this->newModelFor($table)?->getDeletedAtColumn() ?: $defaultColumnName; + } + /** * Get the model entity from the given model or string. * diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 474b02456795..8662178aaff3 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -238,6 +238,21 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnR $this->assertSoftDeleted($model, ['name' => 'Tailwind']); } + public function testAssertSoftDeletedInDatabaseDoesNotFindModePassedViaFcnWithCustomColumnResults() + { + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('The table is empty.'); + + $model = new CustomProductStub(['id' => 1, 'name' => 'Laravel']); + $this->data = ['id' => 1]; + + $builder = $this->mockCountBuilder(0, 'trashed_at'); + + $builder->shouldReceive('get')->andReturn(collect()); + + $this->assertSoftDeleted(CustomProductStub::class, ['id' => $model->id]); + } + public function testAssertNotSoftDeletedInDatabaseFindsResults() { $this->mockCountBuilder(1); @@ -305,6 +320,21 @@ public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelWithCustomColu $this->assertNotSoftDeleted($model, ['name' => 'Tailwind']); } + public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelPassedViaFcnWithCustomColumnResults() + { + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('The table is empty.'); + + $model = new CustomProductStub(['id' => 1, 'name' => 'Laravel']); + $this->data = ['id' => 1]; + + $builder = $this->mockCountBuilder(0, 'trashed_at'); + + $builder->shouldReceive('get')->andReturn(collect()); + + $this->assertNotSoftDeleted(CustomProductStub::class, ['id' => $model->id]); + } + public function testAssertExistsPassesWhenFindsResults() { $this->data = ['id' => 1]; @@ -323,6 +353,12 @@ public function testGetTableNameFromModel() $this->assertEquals($this->table, $this->getTable($this->table)); } + public function testGetTableCustomizedDeletedAtColumnName() + { + $this->assertEquals('trashed_at', $this->getDeletedAtColumn(CustomProductStub::class)); + $this->assertEquals('trashed_at', $this->getDeletedAtColumn(new CustomProductStub())); + } + public function testExpectsDatabaseQueryCount() { $case = new class('foo') extends TestingTestCase