Testing tool for Nette presenter with easy to use API.
The recommended way to install is via Composer:
composer require mangoweb/presenter-tester
It requires PHP version 7.1.
If you are using power of Nette DI Container in your tests, you can use Presenter Tester in your current testing environment. All you need is to register PresenterTester service in
.neon configuration for tests.
services: - Mangoweb\Tester\PresenterTester\PresenterTester(baseUrl: "http://my-app.dev")
You can also specify list of listeners:
services: - Mangoweb\Tester\PresenterTester\PresenterTester( baseUrl: "http://my-app.dev" listeners: [ MyListener() ] )
Other way is to use Presenter Tester together with mangoweb/tester-infrastructure. In that case you have to register DI extension in infrastructure
extensions: mango.presenterTester: Mangoweb\Tester\PresenterTester\Bridges\Infrastructure\PresenterTesterExtension
In configuration of the extension you can set a base url and custom identity factory.
mango.presenterTester: baseUrl: http://my-app.dev identityFactory: MyIdentityFactory()
In Mango Tester Infrastructure environment, service
PresenterTester is available in infrastructure container. When you get the service, you can start testing your presenters:
$testRequest = $presenterTester->createRequest('Admin:Article') ->withParameters([ 'action' => 'edit', 'id' => 1, ]); $testResult = $presenterTester->execute($testRequet); $testResult->assertRenders('%A%Hello world article editation%A%');
As you can see, you first create a
createRequest method on
PresenterTester. You pass a presenter name (without an action) and later you configure the test request. You can set additional request parameters like
action or your own application parameters. There are many other things you can configure on the request, like form values or headers.
After the test request is configured, you pass it to
execute method, which performs presenter execution and returns
TestPresenterResult, which wraps
Nette\Application\IResponse with some additional data collected during execution.
TestPresenterResult contains many useful assert functions like render check or form validity check. In our example there is
assertRenders method, which asserts that presenter returns
TextResponse and that the text contains given pattern. You probably already know the pattern format from Tester\Assert::match() function.
TestPresenterRequest is immutable object.
Set application request parameters.
Add form submission data to request. You have to specify full component tree path to in
Presenter Tester supports forms with CSRF protection, but since it uses session, it is recommended to install mangoweb/tester-http-mocks package.
With Presenter Tester, you can also easily test signal method. The componentClass is only required in the case you are using
nextras/secured-links (which you should). It is also recommended to install mangoweb/tester-http-mocks package.
(Not only) signals often uses AJAX, which you can enable using this method.
Change the HTTP method. The default is
GET. You don't have to explicitly set a method for forms.
Pass additional HTTP headers.
Change identity of User, which is executing given request. This is useful when login is required to perform the action. You can implement identity factory, which provides a default identity for each request.
It is a result of test execution. It wraps
Nette\Application\IResponse and adds few methods to check the response easily.
Checks that response is
TextResponse. Also, you can provide a
$match parameter to check that response contains some text. You can either pass pattern or an array plain strings.
Checks that given pattern or strings were not rendered.
Checks that response is JSON. You can optionally pass expected payload.
Checks that requests terminates with bad request exception (e.g. 404 not found).
Checks that request redirects to given presenter. You can also pass parameters to check. Extra parameters in redirect request are ignored.
Also, there are methods like
getPresenter to access original data a perform some custom checks.
You can hook to some events by implementing
Mangoweb\Tester\PresenterTester\IPresenterTesterListener interface. Then you can e.g. modify test request or execute some implicit result checks.
To register a listener, simply register it as a service in DI container (infrastructure container if you are using Mango Tester Infrastructure).
Using identity factory you can implement a factory which creates a default identity. The factory is a simple PHP callback, which accepts
PresenterTestRequest and returns