Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

add a health-check controller #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"zendframework/zend-loader": ">=2.2.2",
"zendframework/zend-log": ">=2.2.2",
"zendframework/zend-modulemanager": ">=2.2.2",
"zendframework/zend-mvc": ">=2.2.2",
"zendframework/zend-mvc": "^2.2.2",
"zendframework/zend-serializer": ">=2.2.2",
"zendframework/zend-servicemanager": ">=2.2.2",
"zendframework/zend-servicemanager": "^2.2.2",
"zendframework/zend-stdlib": ">=2.2.2",
"zendframework/zend-text": ">=2.2.2",
"zendframework/zend-version": ">=2.2.2 ",
Expand Down
1 change: 1 addition & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'ZFTool\Controller\Create' => 'ZFTool\Controller\CreateController',
'ZFTool\Controller\Install' => 'ZFTool\Controller\InstallController',
'ZFTool\Controller\Diagnostics' => 'ZFTool\Controller\DiagnosticsController',
'ZFTool\Controller\HealthCheck' => 'ZFTool\Controller\HealthCheckController',
),
),

Expand Down
12 changes: 11 additions & 1 deletion config/zftool.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ return array(
'action' => 'run'
)
)
)
),
'zftool-health-check' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/health-check',
'defaults' => array(
'controller' => 'ZFTool\Controller\HealthCheck',
'action' => 'run',
),
),
),
)
),
*/
Expand Down
77 changes: 77 additions & 0 deletions src/ZFTool/Controller/HealthCheckController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace ZFTool\Controller;

use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\Controller\Plugin\Forward;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use ZendDiagnostics\Result\Collection;

class HealthCheckController extends AbstractActionController
{
/** @var int */
private $healthyStatusCode = 204;

/** @var int */
private $unhealthyStatusCode = 500;

/**
* @return Response
* @throws \Zend\Http\Exception\InvalidArgumentException
* @throws \UnexpectedValueException
* @throws \Zend\Mvc\Exception\DomainException
*/
public function runAction()
{
/** @var Forward $forward */
$forward = $this->forward();

$diagnosticsResponse = $forward->dispatch('ZFTool\Controller\Diagnostics', array(
'action' => 'run',
));

if (!$diagnosticsResponse instanceof ViewModel) {
throw new \UnexpectedValueException(
'Response should be an instance of `\Zend\View\Model\ViewModel`.'
);
}

if ($diagnosticsResponse instanceof JsonModel) {
$failureCount = $diagnosticsResponse->getVariable('failure', null);
if (!is_numeric($failureCount)) {
throw new \UnexpectedValueException('Response should have a numeric `failure` variable.');
}
} else {
$results = $diagnosticsResponse->getVariable('results');
if (!$results instanceof Collection) {
throw new \UnexpectedValueException(
'Response should have a `results` variable of type `\ZendDiagnostics\Result\Collection`.'
);
}

$failureCount = $results->getFailureCount();
}

$response = new Response();
$response->setStatusCode($failureCount < 1 ? $this->healthyStatusCode : $this->unhealthyStatusCode);
return $response;
}

/**
* @param int $healthyStatusCode
*/
public function setHealthyStatusCode($healthyStatusCode)
{
$this->healthyStatusCode = $healthyStatusCode;
}

/**
* @param int $unhealthyStatusCode
*/
public function setUnhealthyStatusCode($unhealthyStatusCode)
{
$this->unhealthyStatusCode = $unhealthyStatusCode;
}
}
143 changes: 143 additions & 0 deletions tests/ZFToolTest/Controller/HealthCheckControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace ZFToolTest\Controller;

use Zend\Mvc\Controller\Plugin\Forward;
use Zend\Mvc\Controller\PluginManager;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use ZendDiagnostics\Result\Collection;
use ZendDiagnostics\Result\Failure;
use ZFTool\Controller\HealthCheckController;

class HealthCheckControllerTest extends \PHPUnit_Framework_TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject|Forward */
private $forward;

/** @var \PHPUnit_Framework_MockObject_MockObject|PluginManager */
private $plugins;

/** @var HealthCheckController */
private $sut;

protected function setUp()
{
parent::setUp();

$this->forward = $forward = $this->getMockBuilder('\Zend\Mvc\Controller\Plugin\Forward')
->disableOriginalConstructor()
->getMock();

$this->plugins = $this->getMockBuilder('\Zend\Mvc\Controller\PluginManager')
->disableOriginalConstructor()
->getMock();

$this->plugins->method('get')->willReturnCallback(function ($name) use ($forward) {
return $name === 'forward' ? $forward : null;
});

$this->sut = new HealthCheckController();
$this->sut->setHealthyStatusCode(200);
$this->sut->setUnhealthyStatusCode(500);
$this->sut->setPluginManager($this->plugins);
}

public function testHandleJsonSuccess()
{
$diagnosticsResponse = new JsonModel();
$diagnosticsResponse->setVariable('failure', 0);

$this->forward->method('dispatch')->willReturn($diagnosticsResponse);

$actual = $this->sut->runAction();

self::assertInstanceOf('\Zend\Http\Response', $actual);
self::assertEquals(200, $actual->getStatusCode());
}

public function testHandleJsonFailure()
{
$diagnosticsResponse = new JsonModel();
$diagnosticsResponse->setVariable('failure', 1);

$this->forward->method('dispatch')->willReturn($diagnosticsResponse);

$actual = $this->sut->runAction();

self::assertInstanceOf('\Zend\Http\Response', $actual);
self::assertEquals(500, $actual->getStatusCode());
}

/**
* @expectedException \UnexpectedValueException
*/
public function testHandleJsonInvalidResponse()
{
$diagnosticsResponse = new JsonModel();

$this->forward->method('dispatch')->willReturn($diagnosticsResponse);

$this->sut->runAction();
}

public function testHandleHtmlSuccess()
{
$results = new Collection();

$diagnosticsResponse = new ViewModel();
$diagnosticsResponse->setVariable('results', $results);

$this->forward->method('dispatch')->willReturn($diagnosticsResponse);

$actual = $this->sut->runAction();

self::assertInstanceOf('\Zend\Http\Response', $actual);
self::assertEquals(200, $actual->getStatusCode());
}

public function testHandleHtmlFailure()
{
$check = $this->getMockBuilder('\ZendDiagnostics\Check\CheckInterface')->getMock();

$results = new Collection();
$results->offsetSet($check, new Failure());

$diagnosticsResponse = new ViewModel();
$diagnosticsResponse->setVariable('results', $results);

$this->forward->method('dispatch')->willReturn($diagnosticsResponse);

$actual = $this->sut->runAction();

self::assertInstanceOf('\Zend\Http\Response', $actual);
self::assertEquals(500, $actual->getStatusCode());
}

/**
* @expectedException \UnexpectedValueException
*/
public function testHandleHtmlInvalidResponse()
{
$results = null;

$diagnosticsResponse = new ViewModel();
$diagnosticsResponse->setVariable('results', $results);

$this->forward->method('dispatch')->willReturn($diagnosticsResponse);

$this->sut->runAction();
}

/**
* @expectedException \UnexpectedValueException
*/
public function testHandleInvalidResponse()
{
$diagnosticsResponse = null;

$this->forward->method('dispatch')->willReturn($diagnosticsResponse);

$this->sut->runAction();
}
}
2 changes: 2 additions & 0 deletions tests/ZFToolTest/Diagnostics/DiagnosticsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class DiagnosticsControllerTest extends \PHPUnit_Framework_TestCase

public function setup()
{
\PHPUnit_Framework_Error_Deprecated::$enabled = false;

$this->config = new ArrayObject(array(
'diagnostics' => array()
));
Expand Down