-
Notifications
You must be signed in to change notification settings - Fork 13
Testbench\TPresenter
Testing actions should be as simple as possible. Testbench is doing only necessary tests to ensure there is no error in presenter lifecycle. Example tests of homepage can look as follows:
use \Testbench\TPresenter;
public function testHomepage()
{
$this->checkAction('Homepage:default');
}
You can try more complicated presenter destinations:
use \Testbench\TPresenter;
public function testDestinations()
{
$this->checkAction('Homepage:default');
$this->checkAction('Homepage:');
$this->checkAction(':Homepage:default');
$this->checkAction('Module:Presenter:');
$this->checkAction('Module:Presenter:default');
$this->checkAction(':Module:Presenter:default');
}
use \Testbench\TPresenter;
public function setUp()
{
$this->logIn();
// OR:
$this->logIn(1); //with user ID
$this->logIn(1, 'role'); //with user ID and role
$this->logIn(1, ['role1', 'role2']); //with user ID and roles
$this->logIn(1, ['role1', 'role2'], ['data']); //with user ID and roles and additional data
}
You can use logout as well:
use \Testbench\TPresenter;
public function tearDown()
{
$this->logOut();
}
You can use check if user is logged in:
use \Testbench\TPresenter;
public function testUser()
{
$isUserLoggedIn = $this->isUserLoggedIn();
}
use \Testbench\TPresenter;
public function testSignal()
{
$this->checkSignal('action-name', 'signal-name');
}
use \Testbench\TPresenter;
public function testSearchForm()
{
$this->checkForm('action-name', 'form-name', [
'input' => 'value',
]);
}
It's just simple stupid test. Testbench is going to help you only with basic and boring tasks. You are the tester so you can do whatever you want after this test:
use \Testbench\TPresenter;
public function testSearchForm()
{
$response = $this->checkForm('action-name', 'form-name', [
'input' => 'value',
]);
//Tester\Assert::... with $response
}
It works even with CSRF protection enabled. You don't have to care about it. Testbench is clever... :) But there is more. checkForm
is by default checking if redirect was performed. This is most common scenario. You can disable this test by fourth parameter:
use \Testbench\TPresenter;
public function testSearchForm()
{
$this->checkForm('action-name', 'form-name', [
'input' => 'value',
], FALSE);
//or test redirect to the specific URL:
$this->checkForm('action-name', 'form-name', [
'input' => 'value',
], '/x/y/z');
}
And what if you have AJAX forms? No problem, just use checkAjaxForm
method:
use \Testbench\TPresenter;
public function testSearchForm()
{
$this->checkAjaxForm('action-name', 'form-name', [
'input' => 'value',
]);
}
This tests expects JSON output and redirect is not expected. But if you want to combine classic and AJAX form, you can set fourth parameter (string - URL path). In this case two tests will be performed - classic form test with redirect to the URL and AJAX test without redirect (but with JSON output).
There is also possibility to test to test whatever is filed required or not. But be aware, that this feature is only experimental and it's api may change in the feature:
$this->checkForm('Presenter:default', 'form1', [
'a' => 'b',
'test' => [
\Nette\Forms\Form::REQUIRED => TRUE,
'value',
],
], '/x/y');
It will throw Tester\AssertException
if tested field should be defined as required, but it's not.
use \Testbench\TPresenter;
public function testRedirect()
{
$this->checkRedirect('action-name');
$this->checkRedirect('action-name', '/presenter/action'); //optional destination URL
}
Still in progress. It would be nice if you could check output using match method from Nette\Tester. But for now:
use \Testbench\TPresenter;
public function testJsonOutput()
{
$this->checkJson('json-action');
$this->checkJsonScheme('Presenter:json', [ //scheme must be the same
'string' => [
1234 => [],
],
]);
}
use \Testbench\TPresenter;
public function testRss()
{
$this->checkRss('rss');
}
public function testSitemap()
{
$this->checkSitemap('sitemap');
}
This test expects minimal template for RSS:
{contentType application/xml; charset=utf-8}
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>TITLE</title>
<link>{link //:Presenter:default}</link>
<item n:foreach="$posts as $post">
<title>{$post->title}</title>
<description>{$post->content}</description>
</item>
</channel>
</rss>
And Sitemap:
{contentType application/xml; charset=utf-8}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url n:foreach="$sitemap as $s">
<loc>{link //Homepage:default}</loc>
</url>
</urlset>