Documentation
- Installation
- Controller
- Views
- Database access
- Modules
- Autoloading
- Config management
- Localization
- Logging
- Environment
- Helpers
- Exceptions
- CLI interface
- Events
- Commands
- Authentication
- Caching
- Testing
- mail() wrapper
- SMTP mailer
- HTML helpers
- npm/webpack
Testing
Asatru PHP utilizes PHPUnit in order to perform tests. Tests shall be located in the app/tests directory. A test file has to have the postfix ‚Test‘. For example if you have the test „Example“ then the file must be named „ExampleTest.php“ and the class of the test case must be named „ExampleTest“.
A test case class should be derived from Asatru\Testing\Test class. This way your test is well suited for testing your application so you can also test your routes.
For testing the .env is not parsed, but instead a .env.testing which you have to create. The APP_DEBUG is ignored because for testing it is forced to true.
If you want to test a route you can simply call
Asatru\Testing\Test::request($type, $url, $data)
Type is the type of request, e.g. POST or GET. The following types are supported:
- Asatru\Testing\Test::REQUEST_GET
- Asatru\Testing\Test::REQUEST_POST
- Asatru\Testing\Test::REQUEST_PUT
- Asatru\Testing\Test::REQUEST_PATCH
- Asatru\Testing\Test::REQUEST_DELETE
The URL is the same as someone would type it in your browser (without domain names, port or protocol). Data is an array where you can set GET and POST data. For instance:
array(
"GET" => array("somevar" => somevalue),
"POST" => array("somevar" => somevalue)
)
Example
$result = Asatru\Testing\Test::request(Asatru\Testing\Test::REQUEST_GET, '/my/route', array(
'GET' => array('value' => 'Hello World'))
);
//Now you can do assertions with $result. It contains the data that
//has been returned by the controller action.
/*The following methods are available to deal with controller route tests:
- request($method, $route, $data = array()): Simulates a request to a controller route
- getResponse(): Returns the response of the previous controller route request
- getDatatype(): Returns the data type of the previous controller route request
response. If it is a class then the actuall full qualified class name will be returned
- getDebugInfo(): Returns the debug info for the previous controller route request
response. Internally uses print_r to generate the debug info
- getTimeDiff(): Returns a float that holds the execution time for the previous
controller route test workflow.*/
You can use the Asatru CLI to create a new test case:
php asatru make:test example
This will generate the following testcase:
<?php
/*
Testcase for Test Example
*/
use PHPUnit\Framework\TestCase;
/**
* This class holds your test methods
*/
class ExampleTest extends Asatru\Testing\Test
{
//
}
You can then add your test methods:
class ExampleTest extends Asatru\Testing\Test
{
public function testExample()
{
//Just an example
$this->assertTrue(true);
}
}
Refer to the documentation of PHPUnit in order to get to know how to make assertions.