Permalink
Browse files

Added base framework configuration

In order to remove useless ifs on configuration, the framework merge user
configuration with a predefined configuration.
  • Loading branch information...
wdalmut committed Dec 24, 2013
1 parent e286749 commit e4ad845909b778d6880954995d865feb3694b12c
View
@@ -25,13 +25,29 @@ class App
public function __construct(array $configs)
{
$conf = [];
$conf = $this->getAnEmptyConf();
foreach ($configs as $confFile) {
$conf = array_replace_recursive($conf, $confFile);
}
$this->conf = $conf;
}
private function getAnEmptyConf()
{
return [
"router" => [],
"services" => [
"invokables" => [
"UpCloo\\Renderer\\Jsonp" => "UpCloo\\Renderer\\Jsonp"
],
"aliases" => [
"renderer" => "UpCloo\\Renderer\\Jsonp",
]
],
"listeners" => []
];
}
/**
*Prepare the application
*
@@ -57,30 +73,34 @@ public function bootstrap()
$action = $match->getParam("action");
$this->hydrate($this, $controller);
$this->events()->attach("execute", array($controller, $action));
$this->events()->attach("execute", [$controller, $action]);
}
return $match;
});
if ($this->services()->has("renderer")) {
$renderer = $this->services()->get("renderer", "UpCloo\\Renderer\\Jsonp");
$this->events()->attach("renderer", array($renderer, "render"));
$renderer = $this->services()->get("renderer", "UpCloo\\Renderer\\Jsonp");
if ($this->isNotARenderer($renderer)) {
throw new \InvalidArgumentException("The 'renderer' alias must implement Renderizable interface");
}
$this->events()->attach("renderer", [$renderer, "render"]);
$this->events()->attach("send.response", function($event) {
$this->response()->send();
});
return $this;
}
private function registerRouter()
private function isNotARenderer($renderer)
{
if (!array_key_exists("router", $this->conf)) {
$this->conf["router"] = array();
}
return (!($renderer instanceof Renderer\Renderizable));
}
private function registerRouter()
{
$this->router = TreeRouteStack::factory($this->conf["router"]);
}
@@ -89,10 +109,6 @@ private function registerRouter()
*/
private function registerServices()
{
if (!array_key_exists("services", $this->conf)) {
$this->conf["services"] = array();
}
$services = $this->conf["services"];
$config = new ServiceManagerConfig($services);
$serviceManager = new ServiceManager($config);
@@ -102,10 +118,8 @@ private function registerServices()
private function registerListeners()
{
if (array_key_exists("listeners", $this->conf)) {
foreach ($this->conf["listeners"] as $eventName => $callables) {
$this->registerCallbacks($eventName, $callables);
}
foreach ($this->conf["listeners"] as $eventName => $callables) {
$this->registerCallbacks($eventName, $callables);
}
}
@@ -1,9 +1,11 @@
<?php
namespace UpCloo\Renderer;
class Json
use Zend\EventManager\Event;
class Json implements Renderizable
{
public function render($event)
public function render(Event $event)
{
$dataPack = $this->getDataPack($event);
$response = $event->getParam("response");
@@ -3,9 +3,11 @@
use Zend\Http\Exception\InvalidArgumentException;
use Zend\EventManager\Event;
class Jsonp extends Json
{
public function render($event)
public function render(Event $event)
{
parent::render($event);
@@ -0,0 +1,9 @@
<?php
namespace UpCloo\Renderer;
use Zend\EventManager\Event;
interface Renderizable
{
public function render(Event $event);
}
View
@@ -1,6 +1,8 @@
<?php
namespace UpCloo;
use Zend\EventManager\EventManager;
class AppTest extends Test\WebTestCase
{
private $app;
@@ -36,6 +38,53 @@ public function setUp()
$this->setApp($app);
}
public function testAppFlowWorks()
{
$this->dispatch("/walter");
$this->assertJsonStringEqualsJsonString(json_encode(["ok" => true]), $this->getApp()->response()->getContent());
}
public function testConfigurationOverwrite()
{
$myConf = [
"services" => [
"invokables" => [
"UpCloo\\Renderer\\Json" => "UpCloo\\Renderer\\Json"
],
"aliases" => [
"renderer" => "UpCloo\\Renderer\\Json",
]
]
];
$app = new App([$myConf]);
$renderer = $app->services()->get("renderer");
$this->assertInstanceOf("UpCloo\\Renderer\\Json", $renderer);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The 'renderer' alias must implement Renderizable interface
*/
public function testInvalidRenderer()
{
$conf = [
"services" => [
"factories" => [
"UpCloo\\Renderer\\Invalid" => function(\Zend\ServiceManager\ServiceLocatorInterface $sl) {
return "FAIL";
},
],
"aliases" => [
"renderer" => "UpCloo\\Renderer\\Invalid",
],
]
];
$app = new App([$conf]);
$app->bootstrap();
}
public function testRouteEventIsFired()
{
$routeIsFired = false;
@@ -228,11 +277,11 @@ public function testResponseIsSentToBrowser()
$app = new App([]);
$this->setApp($app, false);
$responseStub = $this->getMock("Zend\\Http\\PhpEnvironment\\Response");
$responseStub->expects($this->once())
$responseMock = $this->getMock("Zend\\Http\\PhpEnvironment\\Response", ["send"]);
$responseMock->expects($this->once())
->method("send")
->will($this->returnValue(true));
$app->setResponse($responseStub);
$app->setResponse($responseMock);
$this->dispatch("/a-page");
}
View
@@ -1,4 +1,10 @@
<phpunit bootstrap="./bootstrap.php">
<phpunit
bootstrap="./bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true">
<testsuites>
<testsuite name="UpCloo_Unit">
<directory>.</directory>

0 comments on commit e4ad845

Please sign in to comment.