|
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(true)); |
|
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("Ping failed: Connection failed\n", $this->commandTester->getDisplay(true)); |
|
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(true)); |
|
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(true)); |
|
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(['--limit' => '-1']); |
|
114
|
|
|
|
|
115
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testInvalidLimitNum() : void |
|
119
|
|
|
{ |
|
120
|
|
|
$this->expectException(RuntimeException::class); |
|
121
|
|
|
$this->expectExceptionMessage('Option "limit" must contain a positive integer value'); |
|
122
|
|
|
|
|
123
|
|
|
$this->commandTester->execute(['--limit' => 'foo']); |
|
124
|
|
|
|
|
125
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testInvalidSleep() : void |
|
129
|
|
|
{ |
|
130
|
|
|
$this->expectException(RuntimeException::class); |
|
131
|
|
|
$this->expectExceptionMessage('Option "sleep" must contain a positive integer value'); |
|
132
|
|
|
|
|
133
|
|
|
$this->commandTester->execute(['--sleep' => '-1']); |
|
134
|
|
|
|
|
135
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testInvalidSleepNum() : void |
|
139
|
|
|
{ |
|
140
|
|
|
$this->expectException(RuntimeException::class); |
|
141
|
|
|
$this->expectExceptionMessage('Option "sleep" must contain a positive integer value'); |
|
142
|
|
|
|
|
143
|
|
|
$this->commandTester->execute(['--sleep' => 'foo']); |
|
144
|
|
|
|
|
145
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|