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