/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Database/DatabaseConsoleDirectConnectionTest.php
153 строки
6 KB
Jason McCreary
[13.x] Mockery cleanup (#61117)
10 авг 2026, 20:23
Не верифицирован
10 авг 2026, 20:23
2ee40ee
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Database; use Illuminate\Config\Repository as Config; use Illuminate\Database\Connection; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Console\Concerns\InteractsWithPooledConnections; use Illuminate\Database\Console\DbCommand; use Illuminate\Foundation\Application; use Mockery; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Input\ArrayInput; class DatabaseConsoleDirectConnectionTest extends TestCase { public function testInteractsWithPooledConnectionsUsesDirectVariantWhenConfigured() { $resolver = Mockery::mock(ConnectionResolverInterface::class); $baseConnection = Mockery::mock(Connection::class); $directConnection = Mockery::mock(Connection::class); $command = new DatabaseConsoleDirectConnectionTestCommand; $resolver->expects('getDefaultConnection')->andReturn('pgsql'); $resolver->expects('connection')->with('pgsql')->andReturn($baseConnection); $baseConnection->expects('hasDirectConnection')->andReturn(true); $resolver->expects('connection')->with('pgsql::direct')->andReturn($directConnection); $this->assertSame($directConnection, $command->resolve($resolver, null)); } public function testInteractsWithPooledConnectionsPassesThroughWhenNoDirectVariantIsConfigured() { $resolver = Mockery::mock(ConnectionResolverInterface::class); $connection = Mockery::mock(Connection::class); $command = new DatabaseConsoleDirectConnectionTestCommand; $resolver->expects('connection')->with('sqlite')->andReturn($connection); $connection->expects('hasDirectConnection')->andReturn(false); $this->assertSame($connection, $command->resolve($resolver, 'sqlite')); } public function testInteractsWithPooledConnectionsPassesThroughExplicitSuffixes() { $resolver = Mockery::mock(ConnectionResolverInterface::class); $connection = Mockery::mock(Connection::class); $command = new DatabaseConsoleDirectConnectionTestCommand; $resolver->expects('connection')->with('pgsql::write')->andReturn($connection); $connection->expects('hasDirectConnection')->andReturn(true); $this->assertSame($connection, $command->resolve($resolver, 'pgsql::write')); } public function testDbCommandUsesBasePostgresConnectionWhenDirectEndpointExistsWithoutPooledMode() { $connection = $this->dbCommand()->getConnection(); $this->assertSame('pooler-host', $connection['host']); $this->assertSame('6432', $connection['port']); $this->assertArrayHasKey('direct', $connection); } public function testDbCommandDefaultsToDirectPostgresConnectionWhenPooledModeIsEnabled() { $connection = $this->dbCommand(pooled: true)->getConnection(); $this->assertSame('direct-host', $connection['host']); $this->assertSame('5432', $connection['port']); $this->assertSame('direct-user', $connection['username']); $this->assertSame('direct-password', $connection['password']); $this->assertSame('require', $connection['sslmode']); $this->assertSame('laravel', $connection['database']); $this->assertArrayNotHasKey('direct', $connection); } public function testDbCommandPooledOptionUsesBasePooledConnection() { $connection = $this->dbCommand(['--pooled' => true])->getConnection(); $this->assertSame('pooler-host', $connection['host']); $this->assertSame('6432', $connection['port']); } public function testDbCommandReadAndWriteOptionsUsePooledConnectionBranches() { $readConnection = $this->dbCommand(['--read' => true], pooled: true)->getConnection(); $writeConnection = $this->dbCommand(['--write' => true], pooled: true)->getConnection(); $this->assertSame('read-pooler-host', $readConnection['host']); $this->assertSame('6433', $readConnection['port']); $this->assertSame('write-pooler-host', $writeConnection['host']); $this->assertSame('6434', $writeConnection['port']); } protected function dbCommand(array $input = [], bool $pooled = false) { $command = new DbCommand; $command->setLaravel($this->application($pooled)); $command->setInput(new ArrayInput($input, $command->getDefinition())); return $command; } protected function application(bool $pooled = false) { $app = new Application; $app->instance('config', new Config([ 'database' => [ 'default' => 'pgsql', 'connections' => [ 'pgsql' => [ 'driver' => 'pgsql', 'host' => 'pooler-host', 'port' => '6432', 'database' => 'laravel', 'username' => 'root', 'password' => '', 'pooled' => $pooled, 'read' => [ 'host' => ['read-pooler-host', 'read-pooler-host-2'], 'port' => '6433', ], 'write' => [ 'host' => 'write-pooler-host', 'port' => '6434', ], 'direct' => [ 'host' => ['direct-host', 'direct-host-2'], 'port' => '5432', 'username' => 'direct-user', 'password' => 'direct-password', 'sslmode' => 'require', ], ], ], ], ])); return $app; } } class DatabaseConsoleDirectConnectionTestCommand { use InteractsWithPooledConnections; public function resolve($connections, $database) { return $this->resolveDirectConnectionIfPossible($connections, $database); } }