/** * @param string $destination fully qualified presenter name (module:module:presenter) * @param array $params provided to the presenter usually via URL * @param array $post provided to the presenter via POST * * @return \Nette\Application\IResponse * @throws \Exception */ protected function check($destination, $params = [], $post = []) { $destination = ltrim($destination, ':'); $pos = strrpos($destination, ':'); $presenter = substr($destination, 0, $pos); $action = substr($destination, $pos + 1) ?: 'default'; $container = \Testbench\ContainerFactory::create(FALSE); $container->removeService('httpRequest'); $headers = $this->__testbench_ajaxMode ? ['X-Requested-With' => 'XMLHttpRequest'] : []; $url = new \Nette\Http\UrlScript($container->parameters['testbench']['url']); $container->addService('httpRequest', new Mocks\HttpRequestMock($url, $params, $post, [], [], $headers)); $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); $this->__testbench_presenter = $presenterFactory->createPresenter($presenter); $this->__testbench_presenter->autoCanonicalize = FALSE; $this->__testbench_presenter->invalidLinkMode = \Nette\Application\UI\Presenter::INVALID_LINK_EXCEPTION; $postCopy = $post; if (isset($params['do'])) { foreach ($post as $key => $field) { if (is_array($field) && array_key_exists(\Nette\Forms\Form::REQUIRED, $field)) { $post[$key] = $field[0]; } } } /** @var \Kdyby\FakeSession\Session $session */ $session = $this->__testbench_presenter->getSession(); $session->setFakeId('testbench.fakeId'); $session->getSection('Nette\\Forms\\Controls\\CsrfProtection')->token = 'testbench.fakeToken'; $post = $post + ['_token_' => 'goVdCQ1jk0UQuVArz15RzkW6vpDU9YqTRILjE=']; //CSRF magic! ¯\_(ツ)_/¯ $request = new Mocks\ApplicationRequestMock($presenter, $post ? 'POST' : 'GET', ['action' => $action] + $params, $post); try { $this->__testbench_httpCode = 200; $this->__testbench_exception = NULL; $response = $this->__testbench_presenter->run($request); if (isset($params['do'])) { if (preg_match('~(.+)-submit$~', $params['do'], $matches)) { /** @var \Nette\Application\UI\Form $form */ $form = $this->__testbench_presenter->getComponent($matches[1]); foreach ($form->getControls() as $control) { if (array_key_exists($control->getName(), $postCopy)) { $subvalues = $postCopy[$control->getName()]; $rq = \Nette\Forms\Form::REQUIRED; if (is_array($subvalues) && array_key_exists($rq, $subvalues) && $subvalues[$rq]) { if ($control->isRequired() !== TRUE) { Assert::fail("field '{$control->name}' should be defined as required, but it's not"); } } } if ($control->hasErrors()) { $errors = ''; $counter = 1; foreach ($control->getErrors() as $error) { $errors .= " - {$error}\n"; $counter++; } Assert::fail("field '{$control->name}' returned this error(s):\n{$errors}"); } } foreach ($form->getErrors() as $error) { Assert::fail($error); } } } return $response; } catch (\Exception $exc) { $this->__testbench_exception = $exc; $this->__testbench_httpCode = $exc->getCode(); throw $exc; } }