Permalink
Browse files

Covered UpCloo\App with new tests

In order to improve this base class I need tests thats covers the whole application
dispatching.
  • Loading branch information...
wdalmut committed Dec 24, 2013
1 parent c70375e commit a3fb91b42a20b3335758eb1c24a4a27916dbaa30
View
@@ -39,10 +39,11 @@ public function __construct(array $configs)
*/
public function bootstrap()
{
$this->registerRouter();
$this->registerServices();
$this->registerListeners();
$closure = function($event){
$this->events()->attach("route", function($event){
$request = $event->getParam('request');
$match = $this->getRouter()->match($request);
@@ -60,11 +61,7 @@ public function bootstrap()
}
return $match;
};
$this->router = TreeRouteStack::factory($this->conf["router"]);
$closure->bindTo($this);
$this->events()->attach("route", $closure);
});
if ($this->services()->has("renderer")) {
$renderer = $this->services()->get("renderer", "UpCloo\\Renderer\\Jsonp");
@@ -78,6 +75,15 @@ public function bootstrap()
return $this;
}
private function registerRouter()
{
if (!array_key_exists("router", $this->conf)) {
$this->conf["router"] = array();
}
$this->router = TreeRouteStack::factory($this->conf["router"]);
}
/**
* Register services into the ServiceManager
*/
@@ -120,9 +126,9 @@ public function getRouter()
return $this->router;
}
public function setEventManager(EventManagerInterface $em)
public function setEventManager(EventManagerInterface $eventManager)
{
$this->events = $em;
$this->events = $eventManager;
}
public function event()
@@ -11,11 +11,13 @@ class WebTestCase extends \PHPUnit_Framework_TestCase
{
private $app;
public function setApp($app)
public function setApp($app, $disableRenderer = true)
{
$app->events()->attach("send.response", function($event) {
$event->stopPropagation(true);
}, 100);
if ($disableRenderer) {
$app->events()->attach("send.response", function($event) {
$event->stopPropagation(true);
}, 100);
}
$this->app = $app;
}
@@ -25,6 +27,9 @@ public function getApp()
return $this->app;
}
/**
* @todo needs refactor
*/
public function dispatch($url, $method = "GET", array $params = array())
{
$request = new HttpRequest();
@@ -0,0 +1,25 @@
<?php
namespace UpCloo\Test;
class BaseController
{
public static $call = false;
public $nonStaticProperty = false;
public function indexAction()
{
return ["ok" => true];
}
public function nonStaticMethod()
{
$this->nonStaticProperty = true;
}
public static function aStaticListener()
{
self::$call = true;
}
}
@@ -0,0 +1,18 @@
<?php
namespace UpCloo\Test;
use UpCloo\Exception\HaltException;
class Haltcontroller
{
public function haltMe()
{
throw new HaltException("Stop it!");
}
public function genericException()
{
throw new \RuntimeException("A runtime exception");
}
}
@@ -1,11 +1,9 @@
<?php
namespace UpCloo;
use Zend\Http\Request;
use Zend\Mvc\Router\Http\TreeRouteStack;
class AppTest extends \PHPUnit_Framework_TestCase
class AppTest extends Test\WebTestCase
{
private $object;
private $app;
public function setUp()
{
@@ -17,39 +15,227 @@ public function setUp()
"options" => array(
"route" => "/walter",
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'index',
'controller' => 'UpCloo\\Test\\BaseController',
'action' => 'indexAction',
)
),
'may_terminate' => true
)
)
),
"services" => array(
"invokables" => array(
"UpCloo\\Renderer\\Json" => "UpCloo\\Renderer\\Json",
),
"aliases" => array(
"renderer" => "UpCloo\\Renderer\\Json",
)
)
);
$this->object = new App([$conf]);
$this->object->bootstrap();
$app = new App([$conf]);
$this->setApp($app);
}
public function testRoutes()
public function testRouteEventIsFired()
{
$router = $this->object->getRouter();
$routeIsFired = false;
$this->getApp()->events()->attach("route", function() use (&$routeIsFired) {
$routeIsFired = true;
});
$this->dispatch("/walter");
$this->assertInstanceOf("\\Zend\\Mvc\\Router\\Http\\TreeRouteStack", $router);
$request = Request::fromString(<<<EOS
POST /walter HTTP/1.1
\r\n
HeaderField1: header-field-value1
HeaderField2: header-field-value2
\r\n\r\n
foo=bar&
EOS
);
$this->assertTrue($routeIsFired);
}
public function testRequestedRouteAreCorrectlyParsed()
{
$event = null;
$this->getApp()->events()->attach("execute", function($e) use (&$event) {
$event = $e;
$e->stopPropagation(true);
}, 100);
$this->dispatch("/walter");
$this->assertInstanceOf("Zend\\EventManager\\Event", $event);
$this->assertInstanceOf("Zend\Mvc\Router\Http\RouteMatch", $event->getTarget());
$routeMatch = $event->getTarget();
$this->assertEquals("UpCloo\\Test\\BaseController", $routeMatch->getParam("controller"));
$this->assertEquals("indexAction", $routeMatch->getParam("action"));
}
public function testMissingPage()
{
$this->dispatch("/missing-page");
$response = $this->getApp()->response();
$this->assertEquals(404, $response->getStatusCode());
}
public function testAppWorksAlsoWithEmptyConf()
{
$app = new App([]);
$this->setApp($app);
$this->dispatch("/a-page");
$this->assertEquals(404, $app->response()->getStatusCode());
}
public function testRendererEventIsFired()
{
$rendererIsFired = false;
$this->getApp()->events()->attach("renderer", function() use (&$rendererIsFired) {
$rendererIsFired = true;
});
$this->dispatch("/walter");
$this->assertTrue($rendererIsFired);
}
public function testListenersAreAttachedToInternalEvents()
{
$attached = false;
$app = new App([[
"listeners" => [
"begin" => [
function () use (&$attached) {
$attached = true;
}
]
]
]]);
$this->setApp($app);
$this->dispatch("/a-page");
$this->assertTrue($attached);
}
public function testAttachAStaticListener()
{
$app = new App([[
"listeners" => [
"404" => [
["UpCloo\\Test\\BaseController", "aStaticListener"]
]
]
]]);
$this->setApp($app);
$this->dispatch("/missing-page");
$this->assertTrue(Test\BaseController::$call);
}
public function testAttachServicesToListeners()
{
$app = new App([[
"services" => [
"invokables" => [
"UpCloo\\Test\\BaseController" => "UpCloo\\Test\\BaseController"
]
],
"listeners" => [
"404" => [
["UpCloo\\Test\\BaseController", "nonStaticMethod"]
]
]
]]);
$this->setApp($app);
$this->dispatch("/a-page");
$baseController = $app->services()->get("UpCloo\\Test\\BaseController");
$this->assertTrue($baseController->nonStaticProperty);
}
public function testHaltEventIsFired()
{
$app = new App([[
"router" => [
"routes" => [
"elb" => [
"type" => "Literal",
"options" => [
"route" => "/halt",
"defaults" => [
"controller" => "UpCloo\\Test\\HaltController",
"action" => "haltMe",
]
],
"may_terminate" => true,
],
]
]
]]);
$this->setApp($app);
$isFired = false;
$app->events()->attach("halt", function($e) use (&$isFired) {
$isFired = true;
});
$this->dispatch("/halt");
$this->assertTrue($isFired);
$this->assertEquals(200, $app->response()->getStatusCode());
}
public function testApplicationErrorEventIsFired()
{
$app = new App([[
"router" => [
"routes" => [
"elb" => [
"type" => "Literal",
"options" => [
"route" => "/halt",
"defaults" => [
"controller" => "UpCloo\\Test\\HaltController",
"action" => "genericException",
]
],
"may_terminate" => true,
],
]
]
]]);
$this->setApp($app);
$isFired = false;
$app->events()->attach("500", function($e) use (&$isFired) {
$isFired = true;
});
$this->dispatch("/halt");
$match = $router->match($request);
$this->assertNotNull($match);
$this->assertTrue($isFired);
$this->assertEquals(500, $app->response()->getStatusCode());
}
public function testGetEmptyServiceManagerOnMissingConfiguration()
{
$app = new App([]);
$params = $match->getParams();
$this->assertEquals("Application\\Controller\\IndexController", $params["controller"]);
$this->assertEquals("index", $params["action"]);
$this->assertInstanceOf("Zend\\ServiceManager\\ServiceManager", $app->services());
}
public function testResponseIsSentToBrowser()
{
$app = new App([]);
$this->setApp($app, false);
$responseStub = $this->getMock("Zend\\Http\\PhpEnvironment\\Response");
$responseStub->expects($this->once())
->method("send")
->will($this->returnValue(true));
$app->setResponse($responseStub);
$this->dispatch("/a-page");
}
}

0 comments on commit a3fb91b

Please sign in to comment.