/** * Test d'un objet response */ public function testResponseRedirect() { $_SERVER['TEST_RESPONSE'] = true; $response = Response::create(); $response->redirect('/'); Response::redirect('/'); }
/** * Permet de tester la création de réponse via la factory */ public function testCreateResponse() { $response = Response::create(); $this->assertInstanceOf(ResponseContent::class, $response); $response = Response::string('view'); $this->assertInstanceOf(StringMessage::class, $response->view); $response = Response::json(['id' => 1]); $this->assertInstanceOf(JsonMessage::class, $response->view); $response = Response::text(''); $this->assertInstanceOf(TextMessage::class, $response->view); $response = Response::html(''); $this->assertInstanceOf(HtmlMessage::class, $response->view); }
public function testRender() { Config::add(__DIR__ . '/../Test/test.php', 'app'); $string = Response::string('view'); $this->assertEquals('view', $string->view->render()); $json = Response::json(['id' => 1]); $this->assertEquals('{"id":1}', $json->view->render()); $text = Response::html('test', 'a', ['id' => 1], ['b.js']); $this->assertEquals('<h1>a</h1><p>1</p><script src="b.js"></script>', $text->view->render()); $html = Response::text('salut'); $catched = false; try { $html->view->render(); } catch (InvalidArgumentException $e) { $this->assertEquals('View [salut] not found.', $e->getMessage()); $catched = true; } $this->assertTrue($catched); }
<?php use PHQ\Routing\Router; Router::get('/', function () { return \PHQ\Http\Response::string('Welcome'); });
/** * @return ResponseContent La réponse final */ public function __invoke() { $result = null; if (is_string($this->callable)) { // Version avec une class controller $callables = explode('@', $this->callable, 2); if (count($callables) != 2) { throw new InvalidArgumentException('Callable string use not a valid format'); } $controllerName = $callables[0]; $controllerName = str_replace(':', '\\', $controllerName); if (mb_strpos($controllerName, 'Controller', 1, 'UTF-8') === false) { $controllerName .= 'Controller'; } $controllerName = "App\\Controllers\\{$controllerName}"; $refClass = new ReflectionClass($controllerName); if ($refClass->isInstantiable()) { $instance = $refClass->newInstance(); $refMethod = $refClass->getMethod($callables[1]); if ($refMethod) { $parameters = $refMethod->getParameters(); $callParameters = $this->getCallParameters($parameters); if (empty($callParameters)) { $result = $refMethod->invoke($instance); } else { $result = $refMethod->invokeArgs($instance, $callParameters); } } } } else { $refFunction = new ReflectionFunction($this->callable); $parameters = $refFunction->getParameters(); $callParameters = $this->getCallParameters($parameters); if (empty($callParameters)) { $result = $refFunction->invoke(); } else { $result = $refFunction->invokeArgs($callParameters); } } if ($result instanceof ResponseContent) { return $result; } elseif (is_string($result)) { return Response::string($result); } throw new InvalidArgumentException('Return value is not valid'); }