1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Tools\Console\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\Tools\Console\Command\PingCommand; |
7
|
|
|
use Doctrine\DBAL\Tools\Console\ConsoleRunner; |
8
|
|
|
use PDOException; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Symfony\Component\Console\Application; |
12
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
13
|
|
|
|
14
|
|
|
class PingCommandTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** @var CommandTester */ |
17
|
|
|
private $commandTester; |
18
|
|
|
/** @var PingCommand */ |
19
|
|
|
private $command; |
20
|
|
|
|
21
|
|
|
/** @var Connection */ |
22
|
|
|
private $connectionMock; |
23
|
|
|
|
24
|
|
|
protected function setUp() : void |
25
|
|
|
{ |
26
|
|
|
$application = new Application(); |
27
|
|
|
$application->add(new PingCommand()); |
28
|
|
|
|
29
|
|
|
$this->command = $application->find('dbal:ping'); |
30
|
|
|
$this->commandTester = new CommandTester($this->command); |
31
|
|
|
|
32
|
|
|
$this->connectionMock = $this->createMock(Connection::class); |
33
|
|
|
|
34
|
|
|
$helperSet = ConsoleRunner::createHelperSet($this->connectionMock); |
35
|
|
|
$this->command->setHelperSet($helperSet); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testConnectionWorking() : void |
39
|
|
|
{ |
40
|
|
|
$this->connectionMock |
41
|
|
|
->expects($this->once()) |
42
|
|
|
->method('ping') |
43
|
|
|
->willReturn(true); |
44
|
|
|
|
45
|
|
|
$this->commandTester->execute([]); |
46
|
|
|
|
47
|
|
|
self::assertSame(0, $this->commandTester->getStatusCode()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testConnectionNotWorking() : void |
51
|
|
|
{ |
52
|
|
|
$this->connectionMock |
53
|
|
|
->expects($this->once()) |
54
|
|
|
->method('ping') |
55
|
|
|
->willReturn(false); |
56
|
|
|
|
57
|
|
|
$this->commandTester->execute([]); |
58
|
|
|
|
59
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
60
|
|
|
self::assertSame("Ping failed\n", $this->commandTester->getDisplay()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testConnectionErrors() : void |
64
|
|
|
{ |
65
|
|
|
$this->connectionMock |
66
|
|
|
->expects($this->once()) |
67
|
|
|
->method('ping') |
68
|
|
|
->willThrowException(new PDOException('Connection failed')); |
69
|
|
|
|
70
|
|
|
$this->commandTester->execute([]); |
71
|
|
|
|
72
|
|
|
self::assertSame(2, $this->commandTester->getStatusCode()); |
73
|
|
|
self::assertSame("Connection error: Connection failed\n", $this->commandTester->getDisplay()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testConnectionNotWorkingLoop() : void |
77
|
|
|
{ |
78
|
|
|
$this->connectionMock |
79
|
|
|
->expects($this->exactly(3)) |
80
|
|
|
->method('ping') |
81
|
|
|
->willReturn(false); |
82
|
|
|
|
83
|
|
|
$this->commandTester->execute([ |
84
|
|
|
'--limit' => '3', |
85
|
|
|
'--sleep' => '0', |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
89
|
|
|
self::assertSame("Ping failed\nPing failed\nPing failed\n", $this->commandTester->getDisplay()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testConnectionStartsWorking() : void |
93
|
|
|
{ |
94
|
|
|
$this->connectionMock |
95
|
|
|
->expects($this->exactly(3)) |
96
|
|
|
->method('ping') |
97
|
|
|
->willReturnOnConsecutiveCalls(false, false, true); |
98
|
|
|
|
99
|
|
|
$this->commandTester->execute([ |
100
|
|
|
'--limit' => '5', |
101
|
|
|
'--sleep' => '0', |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
self::assertSame(0, $this->commandTester->getStatusCode()); |
105
|
|
|
self::assertSame("Ping failed\nPing failed\n", $this->commandTester->getDisplay()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testInvalidLimit(): void |
109
|
|
|
{ |
110
|
|
|
$this->expectException(RuntimeException::class); |
111
|
|
|
$this->expectExceptionMessage('Option ""limit" must contain a positive integer value'); |
112
|
|
|
|
113
|
|
|
$this->commandTester->execute([ |
114
|
|
|
'--limit' => '-1', |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testInvalidLimitNum(): void |
121
|
|
|
{ |
122
|
|
|
$this->expectException(RuntimeException::class); |
123
|
|
|
$this->expectExceptionMessage('Option ""limit" must contain a positive integer value'); |
124
|
|
|
|
125
|
|
|
$this->commandTester->execute([ |
126
|
|
|
'--limit' => 'foo', |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testInvalidSleep(): void |
133
|
|
|
{ |
134
|
|
|
$this->expectException(RuntimeException::class); |
135
|
|
|
$this->expectExceptionMessage('Option "sleep" must contain a positive integer value'); |
136
|
|
|
|
137
|
|
|
$this->commandTester->execute([ |
138
|
|
|
'--sleep' => '-1', |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testInvalidSleepNum(): void |
145
|
|
|
{ |
146
|
|
|
$this->expectException(RuntimeException::class); |
147
|
|
|
$this->expectExceptionMessage('Option "sleep" must contain a positive integer value'); |
148
|
|
|
|
149
|
|
|
$this->commandTester->execute([ |
150
|
|
|
'--sleep' => 'foo', |
151
|
|
|
]); |
152
|
|
|
|
153
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|