PHPackages                             allan-simon/functionnal-test-helpers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. allan-simon/functionnal-test-helpers

ActiveLibrary[Testing &amp; Quality](/categories/testing)

allan-simon/functionnal-test-helpers
====================================

A suite of helpers traits to help you writing functionnal tests with phpunit for symfony2

v0.0.8(9y ago)11.5k3[3 issues](https://github.com/allan-simon/symfony2-test-helpers/issues)MITPHPPHP &gt;=5.4.0

Since May 24Pushed 9y ago1 watchersCompare

[ Source](https://github.com/allan-simon/symfony2-test-helpers)[ Packagist](https://packagist.org/packages/allan-simon/functionnal-test-helpers)[ RSS](/packages/allan-simon-functionnal-test-helpers/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (1)Versions (8)Used By (0)

Symfony2 functionnal test helpers
---------------------------------

[](#symfony2-functionnal-test-helpers)

This library offer you a set of Traits to ease the creation of functionnal tests for both REST api and normal HTML controllers of your symfony2 application. It also take care as much as possible to close the database connections after the tests are done (which by default phpunit seems not to do)

Installation
------------

[](#installation)

add in your `composer.json` :

```
    "require-dev": {
        ...
        "allan-simon/functionnal-test-helpers": "*",
        "liip/functional-test-bundle": "~1.0",
        ...
    }
```

Then the classes are accessible from the namespace `AllanSimon\TestHelpers`

Documentation:
--------------

[](#documentation)

A more complete documentation is to come, waiting for that one can directly check the code, which has been made to be as clear as possible, if you got any specific question, you can open a ticket, we're quite fast to answer.

### For ApiHelpersTrait

[](#for-apihelperstrait)

For the moment the Api go with the assumption that except for binary data (images etc.), you want to send and receive JSON encoded data

#### Perform requests

[](#perform-requests)

all these methods perform the request using `$this->client` and it's your responsability (for the moment) to create it to fit your needs

Once the Request is performed, the raw response is assigned in `$this->response` if the content is json, the decoded content will in `$this->responseJson`

- `performGET(string $uri)` GET request to $uri
- `performDELETE($string $uri)` DELETE request to $uri
- `performPOSt(string $uri, array $data)` json\_encode the $data and POST it to the URI
- `performPUT(string $uri, array $data)` json\_encode the $data and PUT it to the URI
- `performPATCH(string $uri, array $data)` json\_encode the $data and PATCH it to the URI

#### Methods to play with data fixtures

[](#methods-to-play-with-data-fixtures)

- `given(string $fixtureName)` , load the entity referenced by $fixtureName and set it in $this-&gt;entity
- `refreshEntiy()`, resync/refresh the entity in `$this->entity` with the database

#### Assert HTTP status code

[](#assert-http-status-code)

`

- `assertBadRequestError()` =&gt; 400
- `assertPermissionError()` =&gt; 401
- `assertPermissionDenied()` =&gt; 403
- `assertNotFoundError()` =&gt; 404
- `assertResponseUnprocessableEntity()` =&gt; 422
- `assertOkSuccess()` =&gt; 200
- `assertCreatedSuccess()` =&gt; 201
- `assertNoContentResponse()` =&gt; 203

#### Assert JSON returned

[](#assert-json-returned)

all these assets use the property `$this->responseJson`, which is populated by

- `assertEmptyList` , check the json returned by a `perform*` is a json Array with 0 element
- `assertNotEmptyList` , check the json returned by a `perform*`is a json Array with 1+ element
- `assertResponseHasFields(array $fields)` check that the json returned is a json object made of at least the fields given in parameters
- `assertListElementsHaveFields(array $fields)` check the json is an array for which every single element contains at least the given fields

Usage For REST Api
------------------

[](#usage-for-rest-api)

```
