1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace TBolier\RethinkQL\UnitTest\Connection; |
5
|
|
|
|
6
|
|
|
use TBolier\RethinkQL\Connection\Connection; |
7
|
|
|
use TBolier\RethinkQL\Connection\Options; |
8
|
|
|
use TBolier\RethinkQL\Connection\Registry; |
9
|
|
|
use TBolier\RethinkQL\UnitTest\BaseUnitTestCase; |
10
|
|
|
|
11
|
|
|
class RegistryTest extends BaseUnitTestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return void |
15
|
|
|
* @throws \Exception |
16
|
|
|
* @throws \TBolier\RethinkQL\Connection\ConnectionException |
17
|
|
|
*/ |
18
|
|
|
public function testIfRegistryGetsConstructedWithConnections(): void |
19
|
|
|
{ |
20
|
|
|
$optionsConfig = [ |
21
|
|
|
'dbname' => 'foo', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
$options = new Options($optionsConfig); |
25
|
|
|
|
26
|
|
|
$options2Config = [ |
27
|
|
|
'dbname' => 'bar', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
$options2 = new Options($options2Config); |
31
|
|
|
|
32
|
|
|
$registry = new Registry( |
33
|
|
|
[ |
34
|
|
|
'fooConnection' => $options, |
35
|
|
|
'barConnection' => $options2, |
36
|
|
|
'bazConnection' => [], |
37
|
|
|
] |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$this->assertTrue($registry->hasConnection('fooConnection')); |
41
|
|
|
$this->assertTrue($registry->hasConnection('barConnection')); |
42
|
|
|
$this->assertFalse($registry->hasConnection('bazConnection')); |
43
|
|
|
|
44
|
|
|
$this->assertInstanceOf(Connection::class, $registry->getConnection('fooConnection')); |
45
|
|
|
$this->assertInstanceOf(Connection::class, $registry->getConnection('barConnection')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
50
|
|
|
* @expectedExceptionMessage The connection fooConnection has already been added |
51
|
|
|
* @expectedExceptionCode 400 |
52
|
|
|
* @return void |
53
|
|
|
* @throws \TBolier\RethinkQL\Connection\ConnectionException |
54
|
|
|
*/ |
55
|
|
|
public function testIfExceptionThrownOnDuplicateConnection(): void |
56
|
|
|
{ |
57
|
|
|
$optionsConfig = [ |
58
|
|
|
'dbname' => 'foo', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$options = new Options($optionsConfig); |
62
|
|
|
|
63
|
|
|
$registry = new Registry( |
64
|
|
|
[ |
65
|
|
|
'fooConnection' => $options, |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$registry->addConnection('fooConnection', $options); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
74
|
|
|
* @expectedExceptionMessage The connection fooConnection does not exist |
75
|
|
|
* @expectedExceptionCode 400 |
76
|
|
|
* @return void |
77
|
|
|
* @throws \TBolier\RethinkQL\Connection\ConnectionException |
78
|
|
|
*/ |
79
|
|
|
public function testIfExceptionThrownOnMissingConnection(): void |
80
|
|
|
{ |
81
|
|
|
$registry = new Registry([]); |
82
|
|
|
|
83
|
|
|
$registry->getConnection('fooConnection'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|