2013-01-10 15:28:59 -06:00
|
|
|
<?php
|
|
|
|
|
|
2017-01-17 14:30:19 +00:00
|
|
|
namespace Illuminate\Tests\Database;
|
|
|
|
|
|
2018-10-03 22:05:43 +02:00
|
|
|
use Illuminate\Container\Container;
|
2016-01-06 10:20:14 -06:00
|
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
2016-08-27 13:49:54 +01:00
|
|
|
use Illuminate\Database\Connectors\ConnectionFactory;
|
2019-09-10 17:16:05 +02:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use Mockery as m;
|
|
|
|
|
use PDO;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use ReflectionProperty;
|
2013-01-10 15:28:59 -06:00
|
|
|
|
2016-12-30 21:31:11 +01:00
|
|
|
class DatabaseConnectionFactoryTest extends TestCase
|
2015-06-01 16:26:53 +01:00
|
|
|
{
|
2016-01-06 10:20:14 -06:00
|
|
|
protected $db;
|
2015-06-01 15:56:31 +01:00
|
|
|
|
2019-02-08 23:05:58 +01:00
|
|
|
protected function setUp(): void
|
2015-06-01 15:56:31 +01:00
|
|
|
{
|
2016-01-06 10:20:14 -06:00
|
|
|
$this->db = new DB;
|
|
|
|
|
|
|
|
|
|
$this->db->addConnection([
|
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
|
'database' => ':memory:',
|
|
|
|
|
]);
|
|
|
|
|
|
2019-04-23 13:10:10 +02:00
|
|
|
$this->db->addConnection([
|
|
|
|
|
'url' => 'sqlite:///:memory:',
|
|
|
|
|
], 'url');
|
|
|
|
|
|
2016-01-06 10:20:14 -06:00
|
|
|
$this->db->addConnection([
|
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
|
'read' => [
|
2021-07-05 23:30:07 +08:00
|
|
|
'database' => ':memory:',
|
2016-01-06 10:20:14 -06:00
|
|
|
],
|
|
|
|
|
'write' => [
|
2021-07-05 23:30:07 +08:00
|
|
|
'database' => ':memory:',
|
2016-01-06 11:20:43 -05:00
|
|
|
],
|
2016-01-06 10:20:14 -06:00
|
|
|
], 'read_write');
|
|
|
|
|
|
|
|
|
|
$this->db->setAsGlobal();
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-06 10:20:14 -06:00
|
|
|
public function testConnectionCanBeCreated()
|
2015-06-01 15:56:31 +01:00
|
|
|
{
|
2019-04-25 08:56:09 +02:00
|
|
|
$this->assertInstanceOf(PDO::class, $this->db->getConnection()->getPdo());
|
|
|
|
|
$this->assertInstanceOf(PDO::class, $this->db->getConnection()->getReadPdo());
|
|
|
|
|
$this->assertInstanceOf(PDO::class, $this->db->getConnection('read_write')->getPdo());
|
|
|
|
|
$this->assertInstanceOf(PDO::class, $this->db->getConnection('read_write')->getReadPdo());
|
|
|
|
|
$this->assertInstanceOf(PDO::class, $this->db->getConnection('url')->getPdo());
|
|
|
|
|
$this->assertInstanceOf(PDO::class, $this->db->getConnection('url')->getReadPdo());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConnectionFromUrlHasProperConfig()
|
|
|
|
|
{
|
|
|
|
|
$this->db->addConnection([
|
|
|
|
|
'url' => 'mysql://root:pass@db/local?strict=true',
|
|
|
|
|
'unix_socket' => '',
|
|
|
|
|
'charset' => 'utf8mb4',
|
|
|
|
|
'collation' => 'utf8mb4_unicode_ci',
|
|
|
|
|
'prefix' => '',
|
|
|
|
|
'prefix_indexes' => true,
|
|
|
|
|
'strict' => false,
|
|
|
|
|
'engine' => null,
|
|
|
|
|
], 'url-config');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
'name' => 'url-config',
|
|
|
|
|
'driver' => 'mysql',
|
|
|
|
|
'database' => 'local',
|
|
|
|
|
'host' => 'db',
|
|
|
|
|
'username' => 'root',
|
|
|
|
|
'password' => 'pass',
|
|
|
|
|
'unix_socket' => '',
|
|
|
|
|
'charset' => 'utf8mb4',
|
|
|
|
|
'collation' => 'utf8mb4_unicode_ci',
|
|
|
|
|
'prefix' => '',
|
|
|
|
|
'prefix_indexes' => true,
|
|
|
|
|
'strict' => true,
|
|
|
|
|
'engine' => null,
|
|
|
|
|
], $this->db->getConnection('url-config')->getConfig());
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-25 07:21:31 +00:00
|
|
|
public function testSingleConnectionNotCreatedUntilNeeded()
|
|
|
|
|
{
|
2019-04-25 08:56:09 +02:00
|
|
|
$connection = $this->db->getConnection();
|
2016-08-27 13:49:54 +01:00
|
|
|
$pdo = new ReflectionProperty(get_class($connection), 'pdo');
|
|
|
|
|
$readPdo = new ReflectionProperty(get_class($connection), 'readPdo');
|
2016-08-25 07:21:31 +00:00
|
|
|
|
2018-10-05 14:48:10 +02:00
|
|
|
$this->assertNotInstanceOf(PDO::class, $pdo->getValue($connection));
|
|
|
|
|
$this->assertNotInstanceOf(PDO::class, $readPdo->getValue($connection));
|
2016-08-25 07:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReadWriteConnectionsNotCreatedUntilNeeded()
|
|
|
|
|
{
|
2019-04-25 08:56:09 +02:00
|
|
|
$connection = $this->db->getConnection('read_write');
|
2016-08-27 13:49:54 +01:00
|
|
|
$pdo = new ReflectionProperty(get_class($connection), 'pdo');
|
|
|
|
|
$readPdo = new ReflectionProperty(get_class($connection), 'readPdo');
|
2016-08-25 07:21:31 +00:00
|
|
|
|
2018-10-05 14:48:10 +02:00
|
|
|
$this->assertNotInstanceOf(PDO::class, $pdo->getValue($connection));
|
|
|
|
|
$this->assertNotInstanceOf(PDO::class, $readPdo->getValue($connection));
|
2016-08-25 07:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-09 12:56:19 -03:00
|
|
|
public function testReadWriteConnectionSetsReadPdoConfig()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->db->getConnection('read_write');
|
|
|
|
|
|
|
|
|
|
$readPdoConfig = new ReflectionProperty(get_class($connection), 'readPdoConfig');
|
|
|
|
|
|
|
|
|
|
$config = $readPdoConfig->getValue($connection);
|
|
|
|
|
|
|
|
|
|
$this->assertNotEmpty($config);
|
|
|
|
|
$this->assertArrayHasKey('database', $config);
|
|
|
|
|
$this->assertSame(':memory:', $config['database']);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testIfDriverIsntSetExceptionIsThrown()
|
|
|
|
|
{
|
2019-02-07 17:04:40 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
$this->expectExceptionMessage('A driver must be specified.');
|
|
|
|
|
|
2018-10-03 22:05:43 +02:00
|
|
|
$factory = new ConnectionFactory($container = m::mock(Container::class));
|
2015-06-01 16:35:44 +01:00
|
|
|
$factory->createConnector(['foo']);
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExceptionIsThrownOnUnsupportedDriver()
|
|
|
|
|
{
|
2019-02-07 17:04:40 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
$this->expectExceptionMessage('Unsupported driver [foo]');
|
|
|
|
|
|
2018-10-03 22:05:43 +02:00
|
|
|
$factory = new ConnectionFactory($container = m::mock(Container::class));
|
2015-06-01 15:56:31 +01:00
|
|
|
$container->shouldReceive('bound')->once()->andReturn(false);
|
2015-06-01 16:35:44 +01:00
|
|
|
$factory->createConnector(['driver' => 'foo']);
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCustomConnectorsCanBeResolvedViaContainer()
|
|
|
|
|
{
|
2018-10-03 22:05:43 +02:00
|
|
|
$factory = new ConnectionFactory($container = m::mock(Container::class));
|
2015-06-01 15:56:31 +01:00
|
|
|
$container->shouldReceive('bound')->once()->with('db.connector.foo')->andReturn(true);
|
|
|
|
|
$container->shouldReceive('make')->once()->with('db.connector.foo')->andReturn('connector');
|
|
|
|
|
|
2019-08-27 14:48:17 +02:00
|
|
|
$this->assertSame('connector', $factory->createConnector(['driver' => 'foo']));
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
2018-10-29 20:43:24 +01:00
|
|
|
|
|
|
|
|
public function testSqliteForeignKeyConstraints()
|
|
|
|
|
{
|
|
|
|
|
$this->db->addConnection([
|
2019-04-23 13:10:10 +02:00
|
|
|
'url' => 'sqlite:///:memory:?foreign_key_constraints=true',
|
2018-10-29 20:43:24 +01:00
|
|
|
], 'constraints_set');
|
|
|
|
|
|
2019-04-25 08:56:09 +02:00
|
|
|
$this->assertEquals(0, $this->db->getConnection()->select('PRAGMA foreign_keys')[0]->foreign_keys);
|
2018-10-29 20:43:24 +01:00
|
|
|
|
2019-04-25 08:56:09 +02:00
|
|
|
$this->assertEquals(1, $this->db->getConnection('constraints_set')->select('PRAGMA foreign_keys')[0]->foreign_keys);
|
2018-10-29 20:43:24 +01:00
|
|
|
}
|
2024-07-10 15:49:27 -04:00
|
|
|
|
|
|
|
|
public function testSqliteBusyTimeout()
|
|
|
|
|
{
|
|
|
|
|
$this->db->addConnection([
|
|
|
|
|
'url' => 'sqlite:///:memory:?busy_timeout=1234',
|
|
|
|
|
], 'busy_timeout_set');
|
|
|
|
|
|
|
|
|
|
// Can't compare to 0, default value may be something else
|
|
|
|
|
$this->assertNotSame(1234, $this->db->getConnection()->select('PRAGMA busy_timeout')[0]->timeout);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(1234, $this->db->getConnection('busy_timeout_set')->select('PRAGMA busy_timeout')[0]->timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSqliteSynchronous()
|
|
|
|
|
{
|
|
|
|
|
$this->db->addConnection([
|
|
|
|
|
'url' => 'sqlite:///:memory:?synchronous=NORMAL',
|
|
|
|
|
], 'synchronous_set');
|
|
|
|
|
|
|
|
|
|
$this->assertSame(2, $this->db->getConnection()->select('PRAGMA synchronous')[0]->synchronous);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(1, $this->db->getConnection('synchronous_set')->select('PRAGMA synchronous')[0]->synchronous);
|
|
|
|
|
}
|
2014-02-27 19:30:59 +00:00
|
|
|
}
|