rest-certain / rest-certain
PHP DSL for easy testing of REST services, with a nod to the Java DSL, REST Assured
Requires
- php: ~8.3.0 || ~8.4.0
- dflydev/fig-cookies: ^3.1
- league/uri: ^7.5
- league/uri-components: ^7.5
- loilo/jsonpath: ^0.2.0
- mtdowling/jmespath.php: ^2.8
- neoncitylights/media-type: ^3.1
- opis/json-schema: ^2.4
- php-http/discovery: ^1.20
- phpunit/phpunit: ^12.0
- psr/http-client: ^1.0
- psr/http-client-implementation: *
- psr/http-factory: ^1.1
- psr/http-factory-implementation: *
- psr/http-message: ^2.0
- rest-certain/hamcrest-phpunit: ^0.2.0
- symfony/filesystem: ^7.2
Requires (Dev)
- captainhook/captainhook: ^5.25
- captainhook/plugin-composer: ^5.3
- ciareis/bypass: ^2.1.1
- ergebnis/composer-normalize: ^2.47
- laminas/laminas-diactoros: ^3.6
- mockery/mockery: ^1.6
- nyholm/psr7: ^1.8
- phly/keep-a-changelog: ^2.13
- php-parallel-lint/php-console-highlighter: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.4
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-mockery: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- ramsey/coding-standard: ^2.3
- ramsey/composer-repl: ^1.5
- ramsey/conventional-commits: ^1.6
- roave/security-advisories: dev-latest
- symfony/http-client: ^7.2
README
REST Certain
PHP DSL for easy testing of REST services, with a nod to the Java DSL, REST Assured
About
REST Certain is a port of REST Assured to the PHP programming language. It provides a DSL that aims to simplify and ease the testing of REST services.
This project adheres to a code of conduct. By participating in this project and its community, you are expected to uphold this code.
Installation
Install this package as a development dependency using Composer.
composer require --dev rest-certain/rest-certain
Usage
Borrowing from the REST Assured project's examples, here's an example of how to
use REST Certain to make a GET
request and validate a JSON response.
Given the following JSON response body:
{ "lotto":{ "lottoId": 5, "winning-numbers": [2, 45, 34, 23, 7, 5, 3], "winners":[{ "winnerId": 23, "numbers": [2, 45, 34, 23, 3, 5] },{ "winnerId": 54, "numbers": [52, 3, 12, 11, 18, 22] }] } }
We can use JMESPath query language syntax to assert that
lottoId
is equal to 5
:
get('/lotto')->then()->assertThat()->path('lotto.lottoId', is(equalTo(5)));
We can also verify all the winner IDs:
get('/lotto')->then()->assertThat() ->path('lotto.winners[*].winnerId', hasItems(54, 23));
Tip
REST Certain supports both JMESPath and
JSONPath. If the path query begins
with a dollar symbol ($
), REST Certain assumes the query syntax is JSONPath.
Otherwise, it assumes the query syntax is JMESPath.
We can also get a lot more complex and expressive with the HTTP requests and assertions we make. For example:
given() ->accept('application/json') ->queryParam('foo', 'bar') ->and()->body(['name' => 'Something Cool']) ->when() ->put('/something/{id}', ['id' => 123]) ->then() ->statusCode(200) ->and()->header('content-type', 'application/json') ->and()->cookie('baz', 'qux') ->and()->path('id', 123);
REST Certain supports any HTTP method but has explicit support for POST
, GET
,
PUT
, DELETE
, OPTIONS
, PATCH
, and HEAD
and includes specifying and
validating parameters, headers, cookies, and body easily.
Contributing
Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.
Coordinated Disclosure
Keeping user information safe and secure is a top priority, and we welcome the contribution of external security researchers. If you believe you've found a security issue in software that is maintained in this repository, please read SECURITY.md for instructions on submitting a vulnerability report.
Copyright and License
REST Certain is copyright © REST Certain Contributors and licensed for use under the terms of the GNU Lesser General Public License (LGPL-3.0-or-later) as published by the Free Software Foundation. Please see COPYING.LESSER, COPYING, and NOTICE for more information.