Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Application no longer for response echoing. Closes #27
Browse files Browse the repository at this point in the history
  • Loading branch information
stevewest committed Apr 18, 2017
1 parent 6004dec commit 5c60626
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/Application.php
Expand Up @@ -136,7 +136,7 @@ protected function initDependencyContainer(array $config, ContainerInterface $de
$this->dependencyContainer->addServiceProvider(new ApplicationServicesProvider());
}

public function run()
public function run() : ResponseInterface
{
$request = $this->dependencyContainer->get('fuel.application.request');
$response = $this->performRequest($request);
Expand All @@ -146,9 +146,6 @@ public function run()
->get('fuel.application.event')
->emit(new ResponseStarted($this));

http_response_code($response->getStatusCode());
echo $response->getBody();

// send shutdown event
$this->dependencyContainer
->get('fuel.application.event')
Expand All @@ -157,6 +154,8 @@ public function run()
$this->dependencyContainer
->get('fuel.application.event')
->emit(new AppShutdown($this));

return $response;
}

public function performRequest(RequestInterface $request) : ResponseInterface
Expand Down
64 changes: 63 additions & 1 deletion tests/unit/ApplicationTest.php
Expand Up @@ -14,9 +14,12 @@

use Codeception\TestCase\Test;
use Fuel\Foundation\Application;
use Fuel\Foundation\Event\AppShutdown;
use Fuel\Foundation\Event\AppStarted;
use Fuel\Foundation\Event\RequestFinished;
use Fuel\Foundation\Event\RequestStarted;
use Fuel\Foundation\Event\ResponseFinished;
use Fuel\Foundation\Event\ResponseStarted;
use Fuel\Foundation\Request\Http as HttpRequest;
use Zend\Diactoros\Request;

Expand Down Expand Up @@ -119,7 +122,6 @@ public function testMakeRequest()
$requestEndCalled = false;
$requestEndApplication = null;


$app = Application::init([
'components' => [
'Basic',
Expand Down Expand Up @@ -154,4 +156,64 @@ public function testMakeRequest()
$this->assertTrue($requestEndCalled);
$this->assertSame($app, $requestEndApplication);
}

public function testRun()
{
$responseStartCalled = false;
$responseStartApplication = null;

$responseEndCalled = false;
$responseEndApplication = null;

$appShutdownCalled = false;
$appShutdownApplication = null;

$app = Application::init([
'components' => [
'Basic',
],
'events' => [
[
'name' => 'fuel.response.started',
'listener' => function (ResponseStarted $responseStarted) use (&$responseStartCalled, &$responseStartApplication) {
$responseStartCalled = true;
$responseStartApplication = $responseStarted->getApplication();
}
],
[
'name' => 'fuel.response.finished',
'listener' => function (ResponseFinished $responseFinished) use (&$responseEndCalled, &$responseEndApplication) {
$responseEndCalled = true;
$responseEndApplication = $responseFinished->getApplication();
}
],
[
'name' => 'fuel.application.shutdown',
'listener' => function (AppShutdown $appShutdown) use (&$appShutdownCalled, &$appShutdownApplication) {
$appShutdownCalled = true;
$appShutdownApplication = $appShutdown->getApplication();
}
],
],
]);

// Set up a custom request and inject that
$request = new HttpRequest([], [], '/testroute');
$app->getDependencyContainer()->add('fuel.application.request', $request);

$response = $app->run();

// Fire off the request and see if the expected events are fired.
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('found me', (string) $response->getBody());

$this->assertTrue($responseStartCalled);
$this->assertSame($app, $responseStartApplication);

$this->assertTrue($responseEndCalled);
$this->assertSame($app, $responseEndApplication);

$this->assertTrue($appShutdownCalled);
$this->assertSame($app, $appShutdownApplication);
}
}

0 comments on commit 5c60626

Please sign in to comment.