public function testBeginRoute() { $app = new Jarvis(); $response = $app->run(Request::create('/hello/jarvis')); $this->assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode()); $app['router']->beginRoute()->setPattern('/hello/{name}')->setHandler(function ($name) { return "Hello {$name}!"; })->end()->beginRoute('with_params')->setPattern('/hello/{name:\\w+}/{id:\\d+}')->setHandler(function ($name, $id) { return "{$name} ({$id})"; })->end(); $response = $app->run(Request::create('/hello/jarvis')); $this->assertSame(Response::HTTP_OK, $response->getStatusCode()); $this->assertSame('Hello jarvis!', $response->getContent()); $response = $app->run(Request::create('/hello/jarvis/123')); $this->assertSame(Response::HTTP_OK, $response->getStatusCode()); $this->assertSame('jarvis (123)', $response->getContent()); }
public function testControllerSmartTypeHint() { $app = new Jarvis(['debug' => true]); $app['router']->beginRoute()->setHandler(function (\DateTime $datetime) { return str_replace(':', 'h', $datetime->format('d/m/Y H:i')); })->end(); $response = $app->run(); $this->assertSame(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode()); $app['current_datetime'] = function () { return new \DateTime('1988-06-08 00:00:00'); }; $response = $app->run(); $this->assertSame(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode()); $app['current_datetime'] = function () : \DateTime { return new \DateTime('1988-06-08 00:00:00'); }; $response = $app->run(); $this->assertSame(Response::HTTP_OK, $response->getStatusCode()); $this->assertSame('08/06/1988 00h00', $response->getContent()); $this->assertTrue(isset($app['DateTime'])); }
public function testBroadcastOfRunEventAndControllerEventAndResponseEventDuringRunExecution() { $app = new Jarvis(); $receiver = new FakeReceiver(); $app->on(BroadcasterInterface::RUN_EVENT, [$receiver, 'onRunEvent']); $app->on(BroadcasterInterface::CONTROLLER_EVENT, [$receiver, 'onControllerEvent']); $app->on(BroadcasterInterface::RESPONSE_EVENT, [$receiver, 'onResponseEvent']); $app['router']->beginRoute()->setHandler(function () { return 'Hello, world!'; })->end(); $this->assertNull($receiver->runEvent); $this->assertNull($receiver->controllerEvent); $this->assertNull($receiver->responseEvent); $response = $app->run(); $this->assertInstanceOf(RunEvent::class, $receiver->runEvent); $this->assertInstanceOf(ControllerEvent::class, $receiver->controllerEvent); $this->assertInstanceOf(ResponseEvent::class, $receiver->responseEvent); }
public function testRunWillConvertToSymfonyResponseIfRouteCallbackReturnString() { $app = new Jarvis(); $str = 'hello world'; $app['router']->beginRoute()->setHandler(function () use($str) { return $str; })->end(); $this->assertInstanceOf(Response::class, $response = $app->run()); $this->assertSame($str, $response->getContent()); }