|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoctrineORMModuleTest\Diagnostics; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Connections\MasterSlaveConnection; |
|
7
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use DoctrineORMModule\Diagnostics\CheckConnection; |
|
10
|
|
|
use Zend\ServiceManager\ServiceManager; |
|
11
|
|
|
use ZendDiagnostics\Result\Failure; |
|
12
|
|
|
use ZendDiagnostics\Result\Success; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @covers \DoctrineORMModule\Diagnostics\CheckConnection |
|
16
|
|
|
*/ |
|
17
|
|
|
class CheckConnectionTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|PoolingShardConnection */ |
|
20
|
|
|
private $poolingShardConnection; |
|
21
|
|
|
|
|
22
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|Connection */ |
|
23
|
|
|
private $connection; |
|
24
|
|
|
|
|
25
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|MasterSlaveConnection */ |
|
26
|
|
|
private $masterSlaveConnection; |
|
27
|
|
|
|
|
28
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface */ |
|
29
|
|
|
private $entityManager; |
|
30
|
|
|
|
|
31
|
|
|
/** @var ServiceManager */ |
|
32
|
|
|
private $serviceLocator; |
|
33
|
|
|
|
|
34
|
|
|
protected function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::setUp(); |
|
37
|
|
|
|
|
38
|
|
|
$this->serviceLocator = new ServiceManager(); |
|
39
|
|
|
|
|
40
|
|
|
$this->entityManager = $this->getMock(EntityManagerInterface::class); |
|
41
|
|
|
|
|
42
|
|
|
$this->poolingShardConnection = $this->getMockBuilder(PoolingShardConnection::class) |
|
43
|
|
|
->disableOriginalConstructor() |
|
44
|
|
|
->getMock(); |
|
45
|
|
|
|
|
46
|
|
|
$this->masterSlaveConnection = $this->getMockBuilder(MasterSlaveConnection::class) |
|
47
|
|
|
->disableOriginalConstructor() |
|
48
|
|
|
->getMock(); |
|
49
|
|
|
|
|
50
|
|
|
$this->connection = $this->getMockBuilder(Connection::class) |
|
51
|
|
|
->disableOriginalConstructor() |
|
52
|
|
|
->getMock(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testCheckMasterSlaveConnectionSuccessful() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->masterSlaveConnection->method('ping')->willReturnOnConsecutiveCalls(true, true); |
|
58
|
|
|
|
|
59
|
|
|
$actual = (new CheckConnection($this->masterSlaveConnection))->check(); |
|
60
|
|
|
|
|
61
|
|
|
self::assertInstanceOf(Success::class, $actual); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testCheckMasterSlaveConnectionFailedFirstPing() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->masterSlaveConnection->method('ping')->willReturnOnConsecutiveCalls(false, true); |
|
67
|
|
|
|
|
68
|
|
|
$actual = (new CheckConnection($this->masterSlaveConnection))->check(); |
|
69
|
|
|
|
|
70
|
|
|
self::assertInstanceOf(Failure::class, $actual); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testCheckMasterSlaveConnectionFailedSecondPing() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->masterSlaveConnection->method('ping')->willReturnOnConsecutiveCalls(true, false); |
|
76
|
|
|
|
|
77
|
|
|
$actual = (new CheckConnection($this->masterSlaveConnection))->check(); |
|
78
|
|
|
|
|
79
|
|
|
self::assertInstanceOf(Failure::class, $actual); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testCheckConnectionSuccessful() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->connection->method('ping')->willReturn(true); |
|
85
|
|
|
|
|
86
|
|
|
$actual = (new CheckConnection($this->connection))->check(); |
|
87
|
|
|
|
|
88
|
|
|
self::assertInstanceOf(Success::class, $actual); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testCheckConnectionFailed() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->connection->method('ping')->willReturn(false); |
|
94
|
|
|
|
|
95
|
|
|
$actual = (new CheckConnection($this->connection))->check(); |
|
96
|
|
|
|
|
97
|
|
|
self::assertInstanceOf(Failure::class, $actual); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testCheckPoolingShardConnectionSuccessful() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->poolingShardConnection->method('ping')->willReturn(true); |
|
103
|
|
|
|
|
104
|
|
|
$actual = (new CheckConnection($this->poolingShardConnection))->check(); |
|
105
|
|
|
|
|
106
|
|
|
self::assertInstanceOf(Success::class, $actual); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testCheckPoolingShardConnectionFailed() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->poolingShardConnection->method('ping')->willReturn(false); |
|
112
|
|
|
|
|
113
|
|
|
$actual = (new CheckConnection($this->poolingShardConnection))->check(); |
|
114
|
|
|
|
|
115
|
|
|
self::assertInstanceOf(Failure::class, $actual); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|