-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Tools\Console\Command; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Throwable; | ||
use function is_numeric; | ||
use function sleep; | ||
use function sprintf; | ||
|
||
class PingCommand extends Command | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $defaultName = 'dbal:ping'; | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Check db is available') | ||
->addOption('limit', null, InputOption::VALUE_REQUIRED, 'Max number of pings to try', '1') | ||
->addOption('sleep', null, InputOption::VALUE_REQUIRED, 'Length of time (seconds) to sleep between pings', '1'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) : int | ||
{ | ||
$limit = $input->getOption('limit'); | ||
if (! is_numeric($limit) || $limit < 0) { | ||
throw new RuntimeException('Option ""limit" must contain a positive integer value'); | ||
} | ||
$sleep = $input->getOption('sleep'); | ||
if (! is_numeric($sleep) || $sleep < 0) { | ||
throw new RuntimeException('Option "sleep" must contain a positive integer value'); | ||
} | ||
|
||
return $this->waitForPing($this->getHelper('db')->getConnection(), (int) $limit, (int) $sleep, $output); | ||
} | ||
|
||
/** | ||
* @return int > 0 for error | ||
*/ | ||
private function waitForPing(Connection $conn, int $limit, int $sleep, OutputInterface $output) : int | ||
{ | ||
while (true) { | ||
$last = $this->ping($conn, $output); | ||
if ($last === 0 || --$limit <= 0) { | ||
break; | ||
} | ||
sleep($sleep); | ||
} | ||
|
||
return $last; | ||
} | ||
|
||
/** | ||
* @return int > 0 for error | ||
*/ | ||
private function ping(Connection $conn, OutputInterface $output) : int | ||
{ | ||
try { | ||
if ($conn->ping()) { | ||
return 0; | ||
} | ||
|
||
$output->writeln('Ping failed'); | ||
|
||
return 1; | ||
} catch (Throwable $e) { | ||
$output->writeln(sprintf('Ping failed: <error>%s</error>', $e->getMessage())); | ||
|
||
return 2; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
tests/Doctrine/Tests/DBAL/Tools/Console/Command/PingCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\DBAL\Tools\Console\Command; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Tools\Console\Command\PingCommand; | ||
use Doctrine\DBAL\Tools\Console\ConsoleRunner; | ||
use PDOException; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class PingCommandTest extends TestCase | ||
{ | ||
/** @var CommandTester */ | ||
private $commandTester; | ||
/** @var PingCommand */ | ||
private $command; | ||
|
||
/** @var Connection */ | ||
private $connectionMock; | ||
|
||
protected function setUp() : void | ||
{ | ||
$application = new Application(); | ||
$application->add(new PingCommand()); | ||
|
||
$this->command = $application->find('dbal:ping'); | ||
$this->commandTester = new CommandTester($this->command); | ||
|
||
$this->connectionMock = $this->createMock(Connection::class); | ||
|
||
$helperSet = ConsoleRunner::createHelperSet($this->connectionMock); | ||
$this->command->setHelperSet($helperSet); | ||
} | ||
|
||
public function testConnectionWorking() : void | ||
{ | ||
$this->connectionMock | ||
->expects($this->once()) | ||
->method('ping') | ||
->willReturn(true); | ||
|
||
$this->commandTester->execute([]); | ||
|
||
self::assertSame(0, $this->commandTester->getStatusCode()); | ||
} | ||
|
||
public function testConnectionNotWorking() : void | ||
{ | ||
$this->connectionMock | ||
->expects($this->once()) | ||
->method('ping') | ||
->willReturn(false); | ||
|
||
$this->commandTester->execute([]); | ||
|
||
self::assertSame(1, $this->commandTester->getStatusCode()); | ||
self::assertSame("Ping failed\n", $this->commandTester->getDisplay()); | ||
} | ||
|
||
public function testConnectionErrors() : void | ||
{ | ||
$this->connectionMock | ||
->expects($this->once()) | ||
->method('ping') | ||
->willThrowException(new PDOException('Connection failed')); | ||
|
||
$this->commandTester->execute([]); | ||
|
||
self::assertSame(2, $this->commandTester->getStatusCode()); | ||
self::assertSame("Connection error: Connection failed\n", $this->commandTester->getDisplay()); | ||
} | ||
|
||
public function testConnectionNotWorkingLoop() : void | ||
{ | ||
$this->connectionMock | ||
->expects($this->exactly(3)) | ||
->method('ping') | ||
->willReturn(false); | ||
|
||
$this->commandTester->execute([ | ||
'--limit' => '3', | ||
'--sleep' => '0', | ||
]); | ||
|
||
self::assertSame(1, $this->commandTester->getStatusCode()); | ||
self::assertSame("Ping failed\nPing failed\nPing failed\n", $this->commandTester->getDisplay()); | ||
} | ||
|
||
public function testConnectionStartsWorking() : void | ||
{ | ||
$this->connectionMock | ||
->expects($this->exactly(3)) | ||
->method('ping') | ||
->willReturnOnConsecutiveCalls(false, false, true); | ||
|
||
$this->commandTester->execute([ | ||
'--limit' => '5', | ||
'--sleep' => '0', | ||
]); | ||
|
||
self::assertSame(0, $this->commandTester->getStatusCode()); | ||
self::assertSame("Ping failed\nPing failed\n", $this->commandTester->getDisplay()); | ||
} | ||
|
||
public function testInvalidLimit(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('Option ""limit" must contain a positive integer value'); | ||
|
||
$this->commandTester->execute([ | ||
'--limit' => '-1', | ||
]); | ||
|
||
self::assertSame(1, $this->commandTester->getStatusCode()); | ||
} | ||
|
||
public function testInvalidLimitNum(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('Option ""limit" must contain a positive integer value'); | ||
|
||
$this->commandTester->execute([ | ||
'--limit' => 'foo', | ||
]); | ||
|
||
self::assertSame(1, $this->commandTester->getStatusCode()); | ||
} | ||
|
||
public function testInvalidSleep(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('Option "sleep" must contain a positive integer value'); | ||
|
||
$this->commandTester->execute([ | ||
'--sleep' => '-1', | ||
]); | ||
|
||
self::assertSame(1, $this->commandTester->getStatusCode()); | ||
} | ||
|
||
public function testInvalidSleepNum(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('Option "sleep" must contain a positive integer value'); | ||
|
||
$this->commandTester->execute([ | ||
'--sleep' => 'foo', | ||
]); | ||
|
||
self::assertSame(1, $this->commandTester->getStatusCode()); | ||
} | ||
} |