Download the PHP package lchrusciel/api-test-case without Composer
On this page you can find all versions of the php package lchrusciel/api-test-case. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package api-test-case
ApiTestCase
ApiTestCase is a PHPUnit TestCase that will make your life as a Symfony API developer much easier. It extends basic Symfony WebTestCase with some cool features.
Thanks to PHP-Matcher you can, according to its readme, "write expected json responses like a gangster". We definitely agree.
It also uses Alice for easy Doctrine fixtures loading.
Features:
- Clear TDD workflow for API development with Symfony;
- JSON/XML matching with clear error messages;
- Fixtures loading with Alice (optional);
Installation
Assuming you already have Composer installed globally:
And it's done! ApiTestCase is working with the default configuration.
Usage
We provide two base classes for your test cases: JsonApiTestCase and the XmlApiTestCase. Choose one based on the format of the API you want to create.
Json Example
The basic TDD workflow is the following:
- Write a test case that sends the request and use
assertResponse
assertion method to check if response contents are matching your expectations. You need a name for the response file; - Create the file with name that you picked in step 1. and put expected response contents there. It should be put in
src/AppBundle/Tests/Responses/Expected/hello_world.json
for example. - Make it red.
- Make it green.
- Refactor.
Let's see a simple example! Write the following test:
Now define the expected response file:
Run your tests:
Your test should fail with some errors, you are probably missing the controller and routing, so go ahead and define them! As soon as you implement your Controller and configure appropriate routing, you can run your tests again:
If the response contents will match our expectations, console will present a simple message:
Otherwise it will present diff of received messages:
Firstly, function assertResponse
will check the response code (200 is a default response code), then it will check if header of response contains application/json
content type. At the end it will check if the response contents matches the expectation.
Sometimes you can't predict some values in the response, for example autogenerated date or id from the database. No magic is needed here because PHP-Matcher comes with a helping hand. These are just a few examples of available patterns:
@string@
@integer@
@boolean@
@array@
Check for more on PHP-Matcher's documentation.
With these patterns your expected response will look like this:
With this in place, any string under key message
will match the pattern. More complicated expected response could look like this:
And will match the following list of products:
Testing With Database Fixtures
ApiTestCase is integrated with nelmio/alice
. Thanks to this nice library you can easily load your fixtures when you need them. You have to define your fixtures and place them in an appropriate directory.
Here is some example how to define your fixtures and use case. For more information how to define your fixtures check Alice's documentation.
In order to use Alice with Doctrine, you should enable two additional bundles:
Symfony 4.0+
Now, let's say you have a mapped Doctrine entity called Book in your application:
To load fixtures for the test, you need to define a simple YAML
file in src/AppBundle/Tests/DataFixtures/ORM/books.yml
:
Finally, to use these fixtures in a test, just call a proper method:
Configuration Reference
To customize your test suite configuration you can add a few more options to phpunit.xml:
KERNEL_CLASS
allows you to specify exactly which class should be used in order to setup the Kernel.ERESPONSE_DIR
variable contain paths to folder with expected responses. It is used when API result is compared with existing json file. If this value isn't set, ApiTestCase will try to guess location of responses, looking for the responses in a folder: '../Responses' relatively located to your controller test class.FIXTURES_DIR
variable contains a path to folder with your data fixtures. By default if this variable isn't set it will search for../DataFixtures/ORM/
relatively located to your test class . ApiTestCase throws RunTimeException if folder doesn't exist or there won't be any files to load.OPEN_ERROR_IN_BROWSER
is a flag which turns on displaying error in a browser window. The default value is false.OPEN_BROWSER_COMMAND
is a command which will be used to open browser with an exception.IS_DOCTRINE_ORM_SUPPORTED
is a flag which turns on doctrine support includes handy data fixtures loader and database purger.TMP_DIR
variable contains a path to temporary folder, where the log files will be stored.ESCAPE_JSON
is a flag which turns on escaping (unicode characters and slashes) of your JSON output before comparing it to expected data. The default value is false. This flag only exists for backwards compatibility with previous versions of ApiTestCase (when turned on) and will be removed in a future version.
Sample Project
In the test/
directory, you can find sample Symfony project with minimal configuration required to use this library.
Testing
In order to run our PHPUnit tests suite, execute following commands:
Bug Tracking and Suggestions
If you have found a bug or have a great idea for improvement, please open an issue on this repository.
Versioning
Releases will be numbered with the format major.minor.patch
.
And constructed with the following guidelines.
- Breaking backwards compatibility bumps the major.
- New additions without breaking backwards compatibility bumps the minor.
- Bug fixes and misc changes bump the patch.
For more information on SemVer, please visit semver.org website.
MIT License
License can be found here.
Authors
The library was originally created by:
at Lakion company under https://github.com/Lakion/ApiTestCase repository.
See the list of contributors.
All versions of api-test-case with dependencies
ext-json Version *
coduo/php-matcher Version ^6.0
openlss/lib-array2xml Version ^1.0
doctrine/data-fixtures Version ^1.2
doctrine/doctrine-bundle Version ^2.0
doctrine/orm Version ^2.5 || ^3.0
nelmio/alice Version ^3.6
phpspec/php-diff Version ^1.1
phpunit/phpunit Version ^9.0 || ^10.0 || ^11.0
symfony/browser-kit Version ^6.4 || ^7.0
symfony/finder Version ^6.4 || ^7.0
symfony/framework-bundle Version ^6.4 || ^7.0
theofidry/alice-data-fixtures Version ^1.0