diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index 3c791acbceb4..692dfdee2f67 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -1646,6 +1646,23 @@ public function withTablePrefix(Grammar $grammar) return $grammar; } + /** + * Execute the given callback without table prefix. + * + * @param \Closure $callback + * @return void + */ + public function withoutTablePrefix(Closure $callback): void + { + $tablePrefix = $this->getTablePrefix(); + + $this->setTablePrefix(''); + + $callback($this); + + $this->setTablePrefix($tablePrefix); + } + /** * Get the server version for the connection. * diff --git a/src/Illuminate/Foundation/Testing/DatabaseTruncation.php b/src/Illuminate/Foundation/Testing/DatabaseTruncation.php index 3f2c95cd637e..2ef1701917a0 100644 --- a/src/Illuminate/Foundation/Testing/DatabaseTruncation.php +++ b/src/Illuminate/Foundation/Testing/DatabaseTruncation.php @@ -4,7 +4,6 @@ use Illuminate\Contracts\Console\Kernel; use Illuminate\Database\ConnectionInterface; -use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\PostgresBuilder; use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands; use Illuminate\Support\Collection; @@ -99,13 +98,15 @@ function (Collection $tables) use ($connection, $name) { } ) ->each(function (array $table) use ($connection) { - $table = $connection->table( - new Expression($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']) - ); - - if ($table->exists()) { - $table->truncate(); - } + $connection->withoutTablePrefix(function ($connection) use ($table) { + $table = $connection->table( + $table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name'] + ); + + if ($table->exists()) { + $table->truncate(); + } + }); }); $connection->setEventDispatcher($dispatcher); diff --git a/tests/Foundation/Testing/DatabaseTruncationTest.php b/tests/Foundation/Testing/DatabaseTruncationTest.php index b1daeabcb201..8b28d69cab76 100644 --- a/tests/Foundation/Testing/DatabaseTruncationTest.php +++ b/tests/Foundation/Testing/DatabaseTruncationTest.php @@ -5,8 +5,6 @@ use Illuminate\Config\Repository; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Connection; -use Illuminate\Database\Grammar; -use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\PostgresBuilder; use Illuminate\Foundation\Testing\DatabaseTruncation; @@ -189,18 +187,18 @@ private function arrangeConnection( $schema->shouldReceive('getSchemas')->once()->andReturn($schemas); } - $grammar = m::mock(Grammar::class); - $connection = m::mock(Connection::class); $connection->shouldReceive('getTablePrefix')->andReturn($prefix); $connection->shouldReceive('getEventDispatcher')->once()->andReturn($dispatcher = m::mock(Dispatcher::class)); $connection->shouldReceive('unsetEventDispatcher')->once(); $connection->shouldReceive('setEventDispatcher')->once()->with($dispatcher); $connection->shouldReceive('getSchemaBuilder')->once()->andReturn($schema); + $connection->shouldReceive('withoutTablePrefix')->andReturnUsing(function ($callback) use ($connection) { + $callback($connection); + }); $connection->shouldReceive('table') - ->with(m::type(Expression::class)) - ->andReturnUsing(function (Expression $expression) use (&$actual, $grammar) { - $actual[] = $expression->getValue($grammar); + ->andReturnUsing(function (string $tableName) use (&$actual) { + $actual[] = $tableName; $table = m::mock(); $table->shouldReceive('exists')->andReturnTrue(); diff --git a/tests/Integration/Database/DatabaseConnectionsTest.php b/tests/Integration/Database/DatabaseConnectionsTest.php index f12bb0fe209f..ffb2a419058e 100644 --- a/tests/Integration/Database/DatabaseConnectionsTest.php +++ b/tests/Integration/Database/DatabaseConnectionsTest.php @@ -8,6 +8,7 @@ use Illuminate\Database\Events\ConnectionEstablished; use Illuminate\Database\SQLiteConnection; use Illuminate\Events\Dispatcher; +use Illuminate\Support\Facades\DB; use RuntimeException; class DatabaseConnectionsTest extends DatabaseTestCase @@ -118,4 +119,19 @@ public function testEstablishingAConnectionWillDispatchAnEvent() self::assertSame('my-phpunit-connection', $event->connectionName); } + + public function testTablePrefix() + { + DB::setTablePrefix('prefix_'); + $this->assertSame('prefix_', DB::getTablePrefix()); + + DB::withoutTablePrefix(function ($connection) { + $this->assertSame('', $connection->getTablePrefix()); + }); + + $this->assertSame('prefix_', DB::getTablePrefix()); + + DB::setTablePrefix(''); + $this->assertSame('', DB::getTablePrefix()); + } }