rest-certain/rest-certain

PHP DSL for easy testing of REST services, with a nod to the Java DSL, REST Assured


README

elePHPant sleeping on a globe

REST Certain

PHP DSL for easy testing of REST services, with a nod to the Java DSL, REST Assured

Source Code Download Package PHP Programming Language Read License Build Status Codecov Code Coverage

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.