2

I want to test the sign in presenter action with form submit and correct response in redirect. E.g. I want to test that after correct login, user is redirected somewhere and flash message with text "login successful" is displayed.

In all the examples the only way to test correct form behavior is to test that I get RedirectResponse (see below). Isn't that too little? How to do the test I describe? Is it even possible?

function testSignUpSuccess()
{
    $post = [
        'identity' => 'john.doe@gmail.com',
        'password' => 'superSecret123',
    ];

    $pt = $this->getPresenterTester()
        ->setPresenter('Sign')
        ->setAction('up')
        ->setHandle('signUpForm-submit')
        ->setPost($post);

    $response = $pt->run();

    Assert::true($response instanceof Nette\Application\Responses\RedirectResponse);

    //TODO test that flashMessage: 'login successful' is displayed on final page
}

Note: This example uses PresenterTester tool to get the response but the important part is about working with that response so it doesn't matter whether you get it by native means or by this tool.

Josef Sábl
  • 7,538
  • 9
  • 54
  • 66
  • 1
    have you considered using [codeception](http://codeception.com/) for integration testing? there is [nette driver](https://github.com/Arachne/Codeception) which does exactly the same think as PresenterTester :) – Northys Dec 12 '15 at 21:05

1 Answers1

1

No, it's not posiible, because Nette uses session for storing flash messages. You don't have session on console. But you can use Tester\DomQuery to test if required content is on the page (for example logged in user name).

$dom = Tester\DomQuery::fromHtml($html);

Assert::true( $dom->has('form#registration') );
Assert::true( $dom->has('input[name="username"]') );
Assert::true( $dom->has('input[name="password"]') );
Assert::true( $dom->has('input[type="submit"]') );

You will probably need to switch off flash messages in tests to avoid session errors. You can do it in BasePresenter.

abstract class BasePresenter extends Nette\Application\UI\Presenter
{
    /**
     * @var bool
     */
    public $allowFlashMessages = TRUE;

    /**
     * Saves the message to template, that can be displayed after redirect.
     *
     * @param  string
     * @param  string
     *
     * @return \stdClass
     */
    public function flashMessage($message, $type = 'info')
    {
        if ($this->allowFlashMessages) {
            return parent::flashMessage($message, $type);
        }
    }
}

You can then turn it off in you tests.

isset($presenter->allowFlashMessages) && $presenter->allowFlashMessages = FALSE;
Tomáš Jacík
  • 645
  • 4
  • 12