1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Abacaphiliac\DoctrineORMDiagnosticsModuleTest; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\DoctrineORMDiagnosticsModule\CheckConnection; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\Connections\MasterSlaveConnection; |
8
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
10
|
|
|
use Zend\ServiceManager\ServiceManager; |
11
|
|
|
use ZendDiagnostics\Result\Failure; |
12
|
|
|
use ZendDiagnostics\Result\Success; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers \Abacaphiliac\DoctrineORMDiagnosticsModule\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->getMockBuilder(EntityManagerInterface::class)->getMock(); |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.