public function __destruct() { // Shift off anything else that's on the stack. This can happen if something throws // an exception that causes a premature TestSession::__destruct() call while (Controller::has_curr() && Controller::curr() !== $this->controller) { Controller::curr()->popCurrent(); } if (Controller::has_curr()) { $this->controller->popCurrent(); } }
public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model) { $headerName = Config::inst()->get('ApiKeyRequestFilter', 'header_name'); if ($key = $request->getHeader($headerName)) { try { $matchingKey = MemberApiKey::findByKey($key); } catch (LogicException $e) { } if ($matchingKey) { // Log-in can't have session injected, we need to to push $session into the global state $controller = new Controller(); $controller->setSession($session); $controller->pushCurrent(); $matchingKey->Member()->logIn(); // Undo our global state manipulation $controller->popCurrent(); $matchingKey->markUsed(); } else { throw new SS_HTTPResponse_Exception("Bad X-API-Key", 400); } } return true; }
/** * Push a controller onto the stack to mock a particular request * * @param Controller $controller * @param callback $callback */ public function withController(Controller $controller, $callback) { $controller->pushCurrent(); try { // Ensure failed test don't break state $callback($this); } catch (Exception $ex) { } $controller->popCurrent(); if (!empty($ex)) { throw $ex; } }
function testPullRegionWithRenderContext() { // this is a dirty dirty mess. sorry. $req = new SS_HTTPRequest('GET', '/test1'); $req->addHeader(AjaxHTTPResponse::PULL_HEADER, 'TestProductGroupItem:BUYABLE'); $req->addHeader('X-Requested-With', 'XMLHttpRequest'); $ctrl = new Controller(); $ctrl->pushCurrent(); $ctrl->setRequest($req); $ctrl->setDataModel(DataModel::inst()); $ctrl->setURLParams(array()); $ctrl->init(); $response = $ctrl->getAjaxResponse(); $response->addRenderContext('BUYABLE', new ArrayData(array('Title' => 'Test Product', 'Link' => '/test-product', 'Price' => 29.99))); $data = json_decode($response->getBody(), true); $ctrl->popCurrent(); $this->assertNotEmpty($data[AjaxHTTPResponse::REGIONS_KEY]['TestProductGroupItem']); }