forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseConnectionFactoryTest.php
executable file
·170 lines (139 loc) · 5.9 KB
/
DatabaseConnectionFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Connectors\ConnectionFactory;
use InvalidArgumentException;
use Mockery as m;
use PDO;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
class DatabaseConnectionFactoryTest extends TestCase
{
protected $db;
protected function setUp(): void
{
$this->db = new DB;
$this->db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$this->db->addConnection([
'url' => 'sqlite:///:memory:',
], 'url');
$this->db->addConnection([
'driver' => 'sqlite',
'read' => [
'database' => ':memory:',
],
'write' => [
'database' => ':memory:',
],
], 'read_write');
$this->db->setAsGlobal();
}
protected function tearDown(): void
{
m::close();
}
public function testConnectionCanBeCreated()
{
$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());
}
public function testSingleConnectionNotCreatedUntilNeeded()
{
$connection = $this->db->getConnection();
$pdo = new ReflectionProperty(get_class($connection), 'pdo');
$readPdo = new ReflectionProperty(get_class($connection), 'readPdo');
$this->assertNotInstanceOf(PDO::class, $pdo->getValue($connection));
$this->assertNotInstanceOf(PDO::class, $readPdo->getValue($connection));
}
public function testReadWriteConnectionsNotCreatedUntilNeeded()
{
$connection = $this->db->getConnection('read_write');
$pdo = new ReflectionProperty(get_class($connection), 'pdo');
$readPdo = new ReflectionProperty(get_class($connection), 'readPdo');
$this->assertNotInstanceOf(PDO::class, $pdo->getValue($connection));
$this->assertNotInstanceOf(PDO::class, $readPdo->getValue($connection));
}
public function testIfDriverIsntSetExceptionIsThrown()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('A driver must be specified.');
$factory = new ConnectionFactory($container = m::mock(Container::class));
$factory->createConnector(['foo']);
}
public function testExceptionIsThrownOnUnsupportedDriver()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported driver [foo]');
$factory = new ConnectionFactory($container = m::mock(Container::class));
$container->shouldReceive('bound')->once()->andReturn(false);
$factory->createConnector(['driver' => 'foo']);
}
public function testCustomConnectorsCanBeResolvedViaContainer()
{
$factory = new ConnectionFactory($container = m::mock(Container::class));
$container->shouldReceive('bound')->once()->with('db.connector.foo')->andReturn(true);
$container->shouldReceive('make')->once()->with('db.connector.foo')->andReturn('connector');
$this->assertSame('connector', $factory->createConnector(['driver' => 'foo']));
}
public function testSqliteForeignKeyConstraints()
{
$this->db->addConnection([
'url' => 'sqlite:///:memory:?foreign_key_constraints=true',
], 'constraints_set');
$this->assertEquals(0, $this->db->getConnection()->select('PRAGMA foreign_keys')[0]->foreign_keys);
$this->assertEquals(1, $this->db->getConnection('constraints_set')->select('PRAGMA foreign_keys')[0]->foreign_keys);
}
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);
}
}