Skip to content

Commit

Permalink
Include $request and $response to config callbacks (#485)
Browse files Browse the repository at this point in the history
* Include $request and $response to config callbacks

* Remove unnecessary quoting of \
  • Loading branch information
matslindh authored and christeredvartsen committed Jul 15, 2016
1 parent f3bb57f commit 54a2b40
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
14 changes: 7 additions & 7 deletions library/Imbo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function run(array $config) {
$database = $config['database'];

if (is_callable($database) && !($database instanceof DatabaseInterface)) {
$database = $database();
$database = $database($request, $response);
}

if (!$database instanceof DatabaseInterface) {
Expand All @@ -60,7 +60,7 @@ public function run(array $config) {
$storage = $config['storage'];

if (is_callable($storage) && !($storage instanceof StorageInterface)) {
$storage = $storage();
$storage = $storage($request, $response);
}

if (!$storage instanceof StorageInterface) {
Expand All @@ -71,7 +71,7 @@ public function run(array $config) {
$accessControl = $config['accessControl'];

if (is_callable($accessControl) && !($accessControl instanceof AccessControlInterface)) {
$accessControl = $accessControl();
$accessControl = $accessControl($request, $response);
}

if (!$accessControl instanceof AccessControlInterface) {
Expand Down Expand Up @@ -190,7 +190,7 @@ public function run(array $config) {

if (is_callable($definition) && !($definition instanceof ListenerInterface)) {
// Callable piece of code which is not an implementation of the listener interface
$definition = $definition();
$definition = $definition($request, $response);
}

if ($definition instanceof ListenerInterface) {
Expand All @@ -205,7 +205,7 @@ public function run(array $config) {
$users = isset($definition['users']) ? $definition['users'] : [];

if (is_callable($listener) && !($listener instanceof ListenerInterface)) {
$listener = $listener();
$listener = $listener($request, $response);
}

if (!is_string($listener) && !($listener instanceof ListenerInterface)) {
Expand Down Expand Up @@ -246,7 +246,7 @@ public function run(array $config) {
// Custom resources
foreach ($config['resources'] as $name => $resource) {
if (is_callable($resource)) {
$resource = $resource();
$resource = $resource($request, $response);
}

$eventManager->addEventHandler($name, $resource)
Expand All @@ -266,7 +266,7 @@ public function run(array $config) {
$resource = $config['resources'][$routeName];

if (is_callable($resource)) {
$resource = $resource();
$resource = $resource($request, $response);
}

if (is_string($resource)) {
Expand Down
76 changes: 75 additions & 1 deletion tests/phpunit/ImboUnitTest/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

use Imbo\Application,
Imbo\Version,
Imbo\Http\Request\Request;
Imbo\EventListener\ListenerInterface,
Imbo\Http\Request\Request,
Imbo\Resource\ResourceInterface;

/**
* @covers Imbo\Application
Expand Down Expand Up @@ -103,6 +105,62 @@ public function testApplicationSetsTrustedProxies() {
$this->assertSame(['10.0.0.77'], Request::getTrustedProxies());
}

/**
* @covers Imbo\Application::run
*/
public function testApplicationPassesRequestAndResponseToCallbacks() {
// We just want to swallow the output, since we're testing it explicitly below.
$this->expectOutputRegex('|.*}|');

$default = require __DIR__ . '/../../../config/config.default.php';
$test = array(
'database' => function ($request, $response) {
$this->assertInstanceOf('Imbo\Http\Request\Request', $request);
$this->assertInstanceOf('Imbo\Http\Response\Response', $response);

return $this->getMock('Imbo\Database\DatabaseInterface');
},
'storage' => function ($request, $response) {
$this->assertInstanceOf('Imbo\Http\Request\Request', $request);
$this->assertInstanceOf('Imbo\Http\Response\Response', $response);

return $this->getMock('Imbo\Storage\StorageInterface');
},
'accessControl' => function ($request, $response) {
$this->assertInstanceOf('Imbo\Http\Request\Request', $request);
$this->assertInstanceOf('Imbo\Http\Response\Response', $response);

return $this->getMock('Imbo\Auth\AccessControl\Adapter\AdapterInterface');
},
'eventListeners' => [
'test' => function ($request, $response) {
$this->assertInstanceOf('Imbo\Http\Request\Request', $request);
$this->assertInstanceOf('Imbo\Http\Response\Response', $response);

return new TestListener();
},
'testSubelement' => [
'listener' => function ($request, $response) {
$this->assertInstanceOf('Imbo\Http\Request\Request', $request);
$this->assertInstanceOf('Imbo\Http\Response\Response', $response);

return new TestListener();
},
],
],
'resources' => [
'test' => function ($request, $response) {
$this->assertInstanceOf('Imbo\Http\Request\Request', $request);
$this->assertInstanceOf('Imbo\Http\Response\Response', $response);

return new TestResource();
},
],
);

$this->application->run(array_merge($default, $test));
}

/**
* @covers Imbo\Application::run
*/
Expand All @@ -111,3 +169,19 @@ public function testCanRunWithDefaultConfiguration() {
$this->application->run(require __DIR__ . '/../../../config/config.default.php');
}
}

class TestListener implements ListenerInterface {
public static function getSubscribedEvents() {
return [];
}
}

class TestResource implements ListenerInterface, ResourceInterface {
public static function getSubscribedEvents() {
return [];
}

public function getAllowedMethods() {
return [];
}
}

0 comments on commit 54a2b40

Please sign in to comment.