Download the PHP package geckoboom/whirlwind-application-testing without Composer
On this page you can find all versions of the php package geckoboom/whirlwind-application-testing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download geckoboom/whirlwind-application-testing
More information about geckoboom/whirlwind-application-testing
Files in geckoboom/whirlwind-application-testing
Package whirlwind-application-testing
Short Description Functional testing for whirlwind micro framework
License MIT
Informations about the package whirlwind-application-testing
Whirlwind Application Tests
Application tests check the integration of all layers of the application (from routing to the responses). They are based on PHPUnit framework and have a special workflow:
- Make a request
- Test the response
Requirements
PHP 7.4
or higher.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require-dev section of your composer.json file.
Defining a TestCase
To define your REST tests infrastructure you have to create a TestCase class that extends
RestTestCase
and implement realization for createApplication()
method.
The following code defines a whirlwind application TestCase
In the above example, we have minimum configuration for making application tests
Now you can make REST tests. Just create new test and extend created earlier TestCase
In the above example, the test add Bearer authorization token and validates if HTTP request was successful, have 200 status code and contains appropriate header and body fragment in response.
Making Requests
The TestCase simulates an HTTP client like a browser and makes requests into your Whirlwind application. By default you can access to the next HTTP methods:
get($uri, $headers = [])
getJson($uri, $headers = [])
post($uri, $data = [], $headers = [])
postJson($uri, $data = [], $headers = [])
put($uri, $data = [], $headers = [])
putJson($uri, $data = [], $headers = [])
patch($uri, $data = [], $headers = [])
patchJson($uri, $data = [], $headers = [])
delete($uri, $data = [], $headers = [])
deleteJson($uri, $data = [], $headers = [])
options($uri, $data = [], $headers = [])
optionsJson($uri, $data = [], $headers = [])
If required HTTP method absent in the list above you can make HTTP request via call()
or json()
method.
The full signature of the call()
and json()
methods
Available Response Assertions
assertResponseIsSuccessful()
check if status code in range [200, 300)assertResponseCodeIs(int $status)
check if status code equal to$status
assertResponseCodeIsOk()
check if status code is 200assertResponseCodeIsCreated()
check if status code is 201assertResponseCodeIsAccepted()
check if status code is 202assertResponseCodeIsNoContent()
check if status code is 204assertResponseCodeIsBadRequest()
check if status code is 400assertResponseCodeIsUnauthorized()
check if status code is 401assertResponseCodeIsForbidden()
check if status code is 403assertResponseCodeIsNotFound()
check if status code is 404assertResponseCodeIsNotAllowed()
check if status code is 405assertResponseCodeIsUnprocessable()
check if status code is 422assertHeader(string $name, $value = null)
check if response has header$name
with value$value
(if not null)assertResponseIsJson()
check if response contains valid jsonassertResponseContains(string $needle, bool $escape = true)
check if response json contains stringassertResponseNotContains(string $needle, bool $escape = true)
check if response json not contains stringassertHeaderMissing(string $name)
check if response has no header with name$name
assertContainsInResponsePath(string $path, $expected)
assertResponseContainsExactJson(array $data)
assertResponseContainsJsonFragment(array $data)
check if response contains$data
fragmentassertResponseNotContainsExactJson(array $data)
assertResponseMatchesJsonType(array $jsonSchema, ?string $jsonPath = null)
See justinrainbow/json-schemaassertResponseCount(int $count, ?string $jsonPath = null)
Fixtures
Fixtures are used to load "fake" set of data into a database for testing purpose. Fixture can depend on other fixtures
(WhirlwindApplicationTesting\Fixture\Fixture::$depends
property).
Defining a Fixture
To define a fixture, just create a new class that implements WhirlwindApplicationTesting\Fixture\FixtureInterface
.
Tip: Each EntityFixture is about preparing a DB table for testing purpose. You may specify appropriate realization of TableGatewayInterface in constructor as well as a Hydrator realization property. The table name encapsulates in table gateway, while hydrator uses for mapping raw results in WhirlwindApplicationTesting\Fixture\EntityFixture::$entityClass objects
The fixture data for an EntityFixture fixture is usually provided in a file located at data
directory where fixture
class is declared. The data file should return an array of data rows to be inserted into the table. For example:
Dependable fixture can be created by specifying WhirlwindApplicationTesting\Fixture\Fixture::$depends
property, like
the following
The dependency allows you to load and unload fixtures in well-defined order. In the above example UserFixture
will
always be loaded before UserProfileFixture
.
Using Fixtures
If you are using fixtures in your test code, you need to add WhirlwindApplicationTesting\Traits\InteractWithFixtures
trait and implement fixtures()
method
The fixtures listed in the fixtures()
method will be automatically loaded before a test is executed.
All versions of whirlwind-application-testing with dependencies
ext-json Version *
phpunit/phpunit Version ^9.0
mockery/mockery Version ^1.4
justinrainbow/json-schema Version ^5.2
ext-mbstring Version *
softcreatr/jsonpath Version ^0.8.2