/** * Run the router * * @param string $method * @param string $url */ public function run($method = null, $url = null) { // Prepare the session loading. Http::getInstance()->session()->init(); // Begin parsing request. if ($method === null) { $method = $_SERVER['REQUEST_METHOD']; } if ($url === null) { $url = $_SERVER['REQUEST_URI']; // Strip query from url if (strstr($url, '?')) { $parts = explode('?', $url, 2); $url = $parts[0]; } } // Compile $this->compiled = strtoupper($method) . '~' . $url; // Try to match foreach ($this->routes as $route) { if ($route->match($this->compiled)) { $this->executeRoute($route, $method); break; } } }
/** * Get spoofed request class instance * * @param string $url * @param string $method * @param array $post * @return \Arvici\Heart\Http\Request */ private function spoofedRequest($url, $method = 'get', $post = array()) { TestUtils::clear(); //$url = '/get/test?param1=1¶m2=yes¶m3[]=1¶m3[]=2¶m3[]=3'; $urlparts = parse_url($url); parse_str($urlparts['query'], $get); TestUtils::spoofUrl($url, $method, $get, $post); return Http::getInstance()->request(); }
/** * Create validation class with input. * * @param array|null $input Array of input, null or leave undefined for all request parameters/values. */ public function __construct($input = null) { // Default we validate $_POST and $_GET data together. if ($input === null) { $input = Http::getInstance()->request()->params(); } $this->input = $input; $this->set = array(); }
/** * Get fresh clean response class instance. * * @return \Arvici\Heart\Http\Response */ private function getResponse() { TestUtils::clear(); return Http::getInstance()->response(); }
/** * Render all views in the stack. * * @param bool $return Return the rendered output? * * @return mixed|void * * @throws RendererException */ public function run($return = false) { $output = ""; foreach ($this->stack as $view) { /** @var View $view */ $data = $view->getData(); if (!is_array($data)) { $data = array(); } if ($view->getType() === View::PART_BODY_PLACEHOLDER) { throw new RendererException("The body placeholder isn't replaced!"); // @codeCoverageIgnore } if (!empty($this->globalData)) { $data = array_merge($data, $this->globalData); } $engineClass = $view->getEngine(); /** @var RendererInterface $engine */ $engine = $engineClass->newInstance(); if (!$engine instanceof RendererInterface) { // @codeCoverageIgnore throw new RendererException("Engine is not instance of the RendererInterface."); // @codeCoverageIgnore } // @codeCoverageIgnore // Render it! $output .= $engine->render($view, $data); } // If we need to return. Then just return it. if ($return) { return $output; } // Instead, push it into the response object and send. Http::getInstance()->response()->body($output)->send(200); // @codeCoverageIgnore return; // @codeCoverageIgnore }
/** * @covers \Arvici\Heart\Router\Router * @covers \Arvici\Heart\Router\Route * @covers \Arvici\Heart\Http\Http * @covers \Arvici\Heart\Http\Request * @covers \Arvici\Heart\Http\Response */ public function testQueryParameters() { TestUtils::clear(); $router = Router::getInstance(); $router->addRoute(new Route('/test/get/controller/query', 'GET', 'App\\Controller\\TestQuery::basicQuery')); $this->spoof('/test/get/controller/query?test=yes', 'GET', array('test' => 'yes')); try { $router->run(); $this->assertTrue(false); } catch (\Exception $e) { $this->assertEquals(999, $e->getCode()); } // Test via request object (right here) $request = Http::getInstance()->request(); $this->assertEquals(array('test' => 'yes'), $request->get()->all()); // More testing on REQUEST is done in a separate case }
/** * Controller constructor, will prepare the request and response objects. */ public function __construct() { $this->request = Http::getInstance()->request(); $this->response = Http::getInstance()->response(); }
public static function clearHttp() { Http::clearInstance(); }