diff --git a/README.md b/README.md index 6b1035a..3ec1f9f 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ File validation checks: 2. Enable diagnostic tests in [your application configuration](https://github.com/liip/LiipMonitorBundle/blob/master/README.md). 3. In your console type `./app/console monitor:health` to run diagnostics. +## Using diagnostics with PSR-7 middleware + +Install the [rstgroup/diagnostics-middleware](https://github.com/rstgroup/diagnostics-middleware). + ## Using diagnostics in plain PHP 1. Add ZendDiagnostics component to your application @@ -547,7 +551,7 @@ Validate that a Redis service is running. 1) { - $failureString .= 'The following paths are not valid directories: ' . join(', ', $nonDirs).' '; + $failureString .= sprintf('The following paths are not valid directories: %s. ', join(', ', $nonDirs)); } elseif (count($nonDirs) == 1) { - $failureString .= current($nonDirs) . ' is not a valid directory. '; + $failureString .= sprintf('%s is not a valid directory. ', current($nonDirs)); } if (count($unwritable) > 1) { - $failureString .= 'The following directories are not writable: ' . join(', ', $unwritable); + $failureString .= sprintf('The following directories are not writable: %s. ', join(', ', $unwritable)); } elseif (count($unwritable) == 1) { - $failureString .= current($unwritable) . ' directory is not writable.'; + $failureString .= sprintf('%s directory is not writable. ', current($unwritable)); } // Return success or failure @@ -83,7 +83,7 @@ public function check() return new Failure(trim($failureString), array('nonDirs' => $nonDirs, 'unwritable' => $unwritable)); } else { return new Success( - count($this->dir) > 1 ? 'All paths are writable directories.' : 'The path is a a writable directory.', + count($this->dir) > 1 ? 'All paths are writable directories.' : 'The path is a writable directory.', $this->dir ); } diff --git a/src/ZendDiagnostics/Check/HttpService.php b/src/ZendDiagnostics/Check/HttpService.php index 617cff5..c55424c 100644 --- a/src/ZendDiagnostics/Check/HttpService.php +++ b/src/ZendDiagnostics/Check/HttpService.php @@ -43,7 +43,7 @@ class HttpService extends AbstractCheck * @param int $port Port to connect to (defaults to 80) * @param string $path The path to retrieve (defaults to /) * @param int $statusCode (optional) Expected status code - * @param null $content (optional) Expected substring to match agains the page content. + * @param null $content (optional) Expected substring to match against the page content. */ public function __construct($host, $port = 80, $path = '/', $statusCode = null, $content = null) { diff --git a/src/ZendDiagnostics/Check/Redis.php b/src/ZendDiagnostics/Check/Redis.php index 4d51d6f..9c7c6f5 100644 --- a/src/ZendDiagnostics/Check/Redis.php +++ b/src/ZendDiagnostics/Check/Redis.php @@ -14,6 +14,11 @@ */ class Redis extends AbstractCheck { + /** + * @var string|null + */ + protected $auth; + /** * @var string */ @@ -25,13 +30,15 @@ class Redis extends AbstractCheck protected $port; /** - * @param string $host + * @param string $host * @param int $port + * @param string|null $auth */ - public function __construct($host = 'localhost', $port = 6379) + public function __construct($host = 'localhost', $port = 6379, $auth = null) { $this->host = $host; $this->port = $port; + $this->auth = $auth; } /** @@ -49,22 +56,33 @@ public function check() /** * @return PredisClient|RedisExtensionClient * + * @throws \RedisException * @throws \RuntimeException */ private function createClient() { if (class_exists('\Redis')) { $client = new RedisExtensionClient(); - $client->connect($this->host); + $client->connect($this->host, $this->port); + + if ($this->auth && false === $client->auth($this->auth)) { + throw new \RedisException('Failed to AUTH connection'); + } return $client; } if (class_exists('Predis\Client')) { - return new PredisClient(array( + $parameters = array( 'host' => $this->host, 'port' => $this->port, - )); + ); + + if ($this->auth) { + $parameters['password'] = $this->auth; + } + + return new PredisClient($parameters); } throw new \RuntimeException('Neither the PHP Redis extension or Predis are installed'); diff --git a/tests/ZendDiagnosticsTest/BasicConsoleReporterTest.php b/tests/ZendDiagnosticsTest/BasicConsoleReporterTest.php index 43158fe..2995739 100644 --- a/tests/ZendDiagnosticsTest/BasicConsoleReporterTest.php +++ b/tests/ZendDiagnosticsTest/BasicConsoleReporterTest.php @@ -86,7 +86,6 @@ public function testUnknownSymbols() $this->reporter->onStart($checks, array()); ob_clean(); - ob_start(); foreach ($checks as $alias => $check) { $result = new Unknown(); $this->reporter->onAfterRun($check, $result, $alias); @@ -104,7 +103,6 @@ public function testProgressDotsNoGutter() $this->reporter->onStart($checks, array()); ob_clean(); - ob_start(); foreach ($checks as $alias => $check) { $result = new Success(); $this->reporter->onAfterRun($check, $result, $alias); @@ -122,7 +120,6 @@ public function testProgressOverflow() $this->reporter->onStart($checks, array()); ob_clean(); - ob_start(); foreach ($checks as $alias => $check) { $result = new Success(); $this->reporter->onAfterRun($check, $result, $alias); @@ -145,7 +142,6 @@ public function testProgressOverflowMatch() $this->reporter->onStart($checks, array()); ob_clean(); - ob_start(); foreach ($checks as $alias => $check) { $result = new Success(); $this->reporter->onAfterRun($check, $result, $alias); diff --git a/tests/ZendDiagnosticsTest/ChecksTest.php b/tests/ZendDiagnosticsTest/ChecksTest.php index 528f2f8..fafda2b 100644 --- a/tests/ZendDiagnosticsTest/ChecksTest.php +++ b/tests/ZendDiagnosticsTest/ChecksTest.php @@ -392,7 +392,7 @@ public function testDirWritable() $result = $check->check(); $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result, 'Non-dir path'); $this->assertStringMatchesFormat('%s' . $path1 . '%s', $result->getMessage()); - $this->assertStringMatchesFormat('%s' . $path2, $result->getMessage()); + $this->assertStringMatchesFormat('%s' . $path2 . '%s', $result->getMessage()); // create a barrage of unwritable directories $tmpDir = sys_get_temp_dir(); @@ -453,7 +453,7 @@ public function testDirWritable() $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result); $this->assertStringMatchesFormat('%s' . $dir1 . '%s', $result->getMessage()); $this->assertStringMatchesFormat('%s' . $dir2 . '%s', $result->getMessage()); - $this->assertStringMatchesFormat('%simprobabledir999999999999', $result->getMessage()); + $this->assertStringMatchesFormat('%simprobabledir999999999999%s', $result->getMessage()); chmod($dir1, 0777); chmod($dir2, 0777); diff --git a/tests/ZendDiagnosticsTest/DirWritableTest.php b/tests/ZendDiagnosticsTest/DirWritableTest.php new file mode 100644 index 0000000..657f718 --- /dev/null +++ b/tests/ZendDiagnosticsTest/DirWritableTest.php @@ -0,0 +1,95 @@ +assertInstanceOf( + 'ZendDiagnostics\Check\CheckInterface', + $this->prophesize($this->checkClass)->reveal() + ); + } + + /** + * @dataProvider providerValidConstructorArguments + */ + public function testConstructor($arguments) + { + $object = new DirWritable($arguments); + } + + public function providerValidConstructorArguments() + { + return array( + array(__DIR__), + array(vfsStream::setup()->url()), + array(array(__DIR__, vfsStream::setup()->url())) + ); + } + + public function testCheckSuccessSinglePath() + { + $object = new DirWritable(vfsStream::setup()->url()); + $r = $object->check(); + $this->assertInstanceOf('ZendDiagnostics\Result\Success', $r); + $this->assertEquals('The path is a writable directory.', $r->getMessage()); + } + + public function testCheckSuccessMultiplePaths() + { + $object = new DirWritable(array(__DIR__, vfsStream::setup()->url())); + $r = $object->check(); + $this->assertInstanceOf('ZendDiagnostics\Result\Success', $r); + $this->assertEquals('All paths are writable directories.', $r->getMessage()); + } + + public function testCheckFailureSingleInvalidDir() + { + $object = new DirWritable('notadir'); + $r = $object->check(); + $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $r); + $this->assertContains('notadir is not a valid directory.', $r->getMessage()); + } + + public function testCheckFailureMultipleInvalidDirs() + { + $object = new DirWritable(array('notadir1', 'notadir2')); + $r = $object->check(); + $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $r); + $this->assertContains('The following paths are not valid directories: notadir1, notadir2.', $r->getMessage()); + } + + public function testCheckFailureSingleUnwritableDir() + { + $root = vfsStream::setup(); + $unwritableDir = vfsStream::newDirectory('unwritabledir', 000)->at($root); + $object = new DirWritable($unwritableDir->url()); + $r = $object->check(); + $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $r); + $this->assertEquals('vfs://root/unwritabledir directory is not writable.', $r->getMessage()); + } + + public function testCheckFailureMultipleUnwritableDirs() + { + $root = vfsStream::setup(); + $unwritableDir1 = vfsStream::newDirectory('unwritabledir1', 000)->at($root); + $unwritableDir2 = vfsStream::newDirectory('unwritabledir2', 000)->at($root); + + $object = new DirWritable(array($unwritableDir1->url(), $unwritableDir2->url())); + $r = $object->check(); + $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $r); + $this->assertEquals('The following directories are not writable: vfs://root/unwritabledir1, vfs://root/unwritabledir2.', $r->getMessage()); + } +}