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 Symfony\Component\Console\Application; |
11
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
12
|
|
|
|
13
|
|
|
class PingCommandTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var CommandTester */ |
16
|
|
|
private $commandTester; |
17
|
|
|
/** @var PingCommand */ |
18
|
|
|
private $command; |
19
|
|
|
|
20
|
|
|
/** @var Connection */ |
21
|
|
|
private $connectionMock; |
22
|
|
|
|
23
|
|
|
protected function setUp() : void |
24
|
|
|
{ |
25
|
|
|
$application = new Application(); |
26
|
|
|
$application->add(new PingCommand()); |
27
|
|
|
|
28
|
|
|
$this->command = $application->find('dbal:ping'); |
29
|
|
|
$this->commandTester = new CommandTester($this->command); |
30
|
|
|
|
31
|
|
|
$this->connectionMock = $this->createMock(Connection::class); |
32
|
|
|
|
33
|
|
|
$helperSet = ConsoleRunner::createHelperSet($this->connectionMock); |
34
|
|
|
$this->command->setHelperSet($helperSet); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testConnectionWorking() : void |
38
|
|
|
{ |
39
|
|
|
$this->connectionMock |
40
|
|
|
->expects($this->once()) |
41
|
|
|
->method('ping') |
42
|
|
|
->willReturn(true); |
43
|
|
|
|
44
|
|
|
$this->commandTester->execute([]); |
45
|
|
|
|
46
|
|
|
self::assertSame(0, $this->commandTester->getStatusCode()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testConnectionNotWorking() : void |
50
|
|
|
{ |
51
|
|
|
$this->connectionMock |
52
|
|
|
->expects($this->once()) |
53
|
|
|
->method('ping') |
54
|
|
|
->willReturn(false); |
55
|
|
|
|
56
|
|
|
$this->commandTester->execute([]); |
57
|
|
|
|
58
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
59
|
|
|
self::assertSame("Ping failed\n", $this->commandTester->getDisplay()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testConnectionErrors() : void |
63
|
|
|
{ |
64
|
|
|
$this->connectionMock |
65
|
|
|
->expects($this->once()) |
66
|
|
|
->method('ping') |
67
|
|
|
->willThrowException(new PDOException('Connection failed')); |
68
|
|
|
|
69
|
|
|
$this->commandTester->execute([]); |
70
|
|
|
|
71
|
|
|
self::assertSame(2, $this->commandTester->getStatusCode()); |
72
|
|
|
self::assertSame("Connection error: Connection failed\n", $this->commandTester->getDisplay()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testConnectionNotWorkingLoop() : void |
76
|
|
|
{ |
77
|
|
|
$this->connectionMock |
78
|
|
|
->expects($this->exactly(3)) |
79
|
|
|
->method('ping') |
80
|
|
|
->willReturn(false); |
81
|
|
|
|
82
|
|
|
$this->commandTester->execute([ |
83
|
|
|
'--limit' => '3', |
84
|
|
|
'--sleep' => '0', |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
self::assertSame(1, $this->commandTester->getStatusCode()); |
88
|
|
|
self::assertSame("Ping failed\nPing failed\nPing failed\n", $this->commandTester->getDisplay()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testConnectionStartsWorking() : void |
92
|
|
|
{ |
93
|
|
|
$this->connectionMock |
94
|
|
|
->expects($this->exactly(3)) |
95
|
|
|
->method('ping') |
96
|
|
|
->willReturnOnConsecutiveCalls(false, false, true); |
97
|
|
|
|
98
|
|
|
$this->commandTester->execute([ |
99
|
|
|
'--limit' => '5', |
100
|
|
|
'--sleep' => '0', |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
self::assertSame(0, $this->commandTester->getStatusCode()); |
104
|
|
|
self::assertSame("Ping failed\nPing failed\n", $this->commandTester->getDisplay()); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|