diff --git a/src/Application.php b/src/Application.php index 931a287..4282945 100755 --- a/src/Application.php +++ b/src/Application.php @@ -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); @@ -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') @@ -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 diff --git a/tests/unit/ApplicationTest.php b/tests/unit/ApplicationTest.php index 593a870..15ca3e1 100755 --- a/tests/unit/ApplicationTest.php +++ b/tests/unit/ApplicationTest.php @@ -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; @@ -119,7 +122,6 @@ public function testMakeRequest() $requestEndCalled = false; $requestEndApplication = null; - $app = Application::init([ 'components' => [ 'Basic', @@ -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); + } }