PHPackages                             ulff/behat-rest-api-extension - 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. ulff/behat-rest-api-extension

ActiveBehat-extension

ulff/behat-rest-api-extension
=============================

Behat context for testing REST API calls

1.3.0(8y ago)1052.2k1[1 issues](https://github.com/ulff/BehatRestApiExtension/issues)MITPHPPHP &gt;=5.5

Since Apr 5Pushed 8y ago2 watchersCompare

[ Source](https://github.com/ulff/BehatRestApiExtension)[ Packagist](https://packagist.org/packages/ulff/behat-rest-api-extension)[ RSS](/packages/ulff-behat-rest-api-extension/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (5)Versions (15)Used By (0)

BehatRestApiExtension
=====================

[](#behatrestapiextension)

Behat context for testing REST API responses, extending [MinkContext](https://github.com/Behat/MinkExtension/blob/master/doc/index.rst). Currently it is supporting only JSON response types. Using that extension you can make HTTP calls to your REST API and strictly check the response status codes and contents.

Setting up
----------

[](#setting-up)

### Step 1: Install extension

[](#step-1-install-extension)

Install extenstion using [composer](https://getcomposer.org):

```
php composer.phar require "ulff/behat-rest-api-extension:^1.0"

```

### Step 2: load extension

[](#step-2-load-extension)

Add following to behat.yml:

```
default:
  extensions:
    Ulff\BehatRestApiExtension\ServiceContainer\BehatRestApiExtension: ~
```

Usage
-----

[](#usage)

Create your own context class as an extension for base `RestApiContext` class:

```
use Ulff\BehatRestApiExtension\Context\RestApiContext;

class YourContext extends RestApiContext
{
    // ...
}
```

You can list available scenario steps by command:

```
behat -di

```

Additional steps offered by extension:
--------------------------------------

[](#additional-steps-offered-by-extension)

###### When I make request :method :uri

[](#when-i-make-request-method-uri)

Make request specifying http method and uri.

Examples:

```
@When I make request "GET" "/api/v1/categories"
@When I make request "DELETE" "/api/v1/companies/{id}"
@When I make request "HEAD" "/api/v1/presentations/{id}"

```

###### When I make request :method :uri with following JSON content:

[](#when-i-make-request-method-uri-with-following-json-content)

Make request specifying http method and uri and parameters as JSON.

Examples:

```
@When I make request "POST" "/api/v1/posts" with following JSON content:
"""
{
    "user": "user-id",
    "title": "Some title"
    "number": 12
}
"""

@When I make request "PUT" "/api/v1/users/{id}" with following JSON content:
"""
{
    "education": [
        {
            "school": "A primary school",
            "address": "Some Street 10, SomeCity"
        },
        {
            "school": "High School",
            "address": "Another Street 1, SomeCity"
        }
    ],
    "workplace": {
        "name": "A company",
        "phone": "+48 111 222 333"
    }
}
"""

```

###### When I make request :method :uri with params:

[](#when-i-make-request-method-uri-with-params)

Make request specifying http method and uri and parameters as TableNode. TableNode values can be also ParameterBag params.

Examples:

```
@When I make request "POST" "/api/v1/posts" with params:
    | user      | user-id              |
    | title     | Some title           |
    | content   | Content here         |
@When I make request "PUT" "/api/v1/users/{id}" with params:
    | user  | user-id           |
    | name  | User Name Here    |
    | email | user@email.here   |

```

###### Then the response should be JSON

[](#then-the-response-should-be-json)

Checks if the response is a correct JSON.

###### Then the response JSON should be a collection

[](#then-the-response-json-should-be-a-collection)

Checks if a response JSON is a collection (array).

###### Then the response JSON collection should not be empty

[](#then-the-response-json-collection-should-not-be-empty)

Checks if a response JSON collection (array) is not empty.

###### Then the response JSON collection should be empty

[](#then-the-response-json-collection-should-be-empty)

Checks if a response JSON collection (array) is empty.

###### Then the response JSON should be a single object

[](#then-the-response-json-should-be-a-single-object)

Checks if a response JSON is a single object, not a collection (array).

###### Then the response JSON should have :property field

[](#then-the-response-json-should-have-property-field)

Checks if response JSON object has a property with given name.

Examples:

```
@Then the response JSON should have "id" field

```

###### Then the response JSON should have :property field with value :expectedValue

[](#then-the-response-json-should-have-property-field-with-value-expectedvalue)

Checks if response JSON object has a property with given name and that property has expected value.

Examples:

```
@Then the response JSON should have "name" field with value "User name"
@Then the response JSON should have "email" field with value "user@email.com"

```

###### Then the response JSON should have :property field with null value

[](#then-the-response-json-should-have-property-field-with-null-value)

Checks if response JSON object has a property with given name and that property has null value.

Examples:

```
@Then the response JSON should have "end_date" field with null value"
@Then the response JSON should have "participants" field with null value

```

###### Then the response JSON should have :property field with exact value :expectedValue

[](#then-the-response-json-should-have-property-field-with-exact-value-expectedvalue)

Checks if response JSON object has a property with given name and that property has expected exact value (including type).

Examples:

```
@Then the response JSON should have "name" field with exact value "User name"
@Then the response JSON should have "email" field with exact value "user@email.com"

```

###### Then the response JSON should have :property field with value like :expectedValueRegexp

[](#then-the-response-json-should-have-property-field-with-value-like-expectedvalueregexp)

Checks if response JSON object has a property with given name and value matching given regexp.

Examples:

```
@Then the response JSON should have "error" field with value like "Missing param: [a-z]+"
@Then the response JSON should have "zipcode" field with value like "[0-9]{2}-[0-9]{3}"

```

###### Then the response JSON should have :property field set to :expectedValue

[](#then-the-response-json-should-have-property-field-set-to-expectedvalue)

Checks if response JSON object has a property with given name and that property has expected BOOLEAN value.

Examples:

```
@Then the response JSON should have "has_access" field set to "false"
@Then the response JSON should have "is_valid" field set to "true"

```

###### Then the response JSON should have :property field with array :expectedArray as value

[](#then-the-response-json-should-have-property-field-with-array-expectedarray-as-value)

When response JSON is a single object, it checks if that object has a property with given name and that property is exact array as given.

Examples:

```
@Then the response JSON should have "colors" field with array "['red', 'green', 'blue']" as value
@Then the response JSON should have "options" field with array "array('one', 'two')" as value

```

###### Then all response collection items should have :property field

[](#then-all-response-collection-items-should-have-property-field)

When response JSON is a collection (array), it checks if ALL collection items have property with given name.

Examples:

```
@Then all response collection items should have "id" field

```

###### Then all response collection items should have :property field with value :expectedValue

[](#then-all-response-collection-items-should-have-property-field-with-value-expectedvalue)

When response JSON is a collection (array), it checks if ALL collection items have property with given name and that properties have expected value.

Examples:

```
@Then all response collection items should have "default" field with value "1"
@Then all response collection items should have "color" field with value "red"

```

###### Then all response collection items should have :property field with exact value :expectedValue

[](#then-all-response-collection-items-should-have-property-field-with-exact-value-expectedvalue)

When response JSON is a collection (array), it checks if ALL collection items have property with given name and that properties have expected exact value (including type).

Examples:

```
@Then all response collection items should have "default" field with exact value "1"
@Then all response collection items should have "color" field with exact value "red"

```

###### Then all response collection items should have nested field :property with value :expectedValue

[](#then-all-response-collection-items-should-have-nested-field-property-with-value-expectedvalue)

When response JSON is a collection (array), it checks if ALL collection items have nested property with given path and that properties have expected value. For nesting property use "-&gt;" inside expected property name.

Examples:

```
@Then all response collection items should have "owner->personal_data->name" field with value "John"
@Then all response collection items should have "root->property" field with value "1"

```

###### Then all response collection items should have nested field :property with exact value :expectedValue

[](#then-all-response-collection-items-should-have-nested-field-property-with-exact-value-expectedvalue)

When response JSON is a collection (array), it checks if ALL collection items have nested property with given path and that properties have expected exact value (including type). For nesting property use "-&gt;" inside expected property name.

Examples:

```
@Then all response collection items should have "owner->personal_data->name" field with exact value "John"
@Then all response collection items should have "root->property" field with exact value "1"

```

###### Then all response collection items should have :property field set to :expectedBoolean

[](#then-all-response-collection-items-should-have-property-field-set-to-expectedboolean)

When response JSON is a collection (array), it checks if ALL collection items have property with given name and that properties have expected BOOLEAN value.

Examples:

```
@Then all response collection items should have "is_default" field set to "true"
@Then all response collection items should have "has_access" field set to "false"

```

###### Then the response JSON :fieldName field should be a collection

[](#then-the-response-json-fieldname-field-should-be-a-collection)

When response JSON is a single object, it checks if that object has a property with given name and if that property is a collection (array).

Examples:

```
@Then the response JSON "settings" field should be a collection
@Then the response JSON "allowed_colors" field should be a collection

```

###### Then all nested :collectionFieldName collection items should have :nestedFieldName field

[](#then-all-nested-collectionfieldname-collection-items-should-have-nestedfieldname-field)

When response JSON is a single object, it checks if that object has a property with given name, and that property is a collection (array), and all of that collection items have nested field with given path.

Examples:

```
@Then all nested "owners" collection items should have "user" field
@Then all nested "themes" collection items should have "font" field

```

###### Then all nested :collectionFieldName collection items should have :nestedFieldName field set to :expectedValue

[](#then-all-nested-collectionfieldname-collection-items-should-have-nestedfieldname-field-set-to-expectedvalue)

When response JSON is a single object, it checks if that object has a property with given name, and that property is a collection (array), and all of that collection items have nested field with given path and given BOOLEAN value.

Examples:

```
@Then all nested "owners" collection items should have "has_access" field set to "false"
@Then all nested "themes" collection items should have "is_default" field set to "true"

```

###### Then all nested :collectionFieldName collection items should have :nestedFieldName field with value :expectedValue

[](#then-all-nested-collectionfieldname-collection-items-should-have-nestedfieldname-field-with-value-expectedvalue)

When response JSON is a single object, it checks if that object has a property with given name, and that property is a collection (array), and all of that collection items have nested field with given path and with given value.

Examples:

```
@Then all nested "owners" collection items should have "user" field with value "John"
@Then all nested "themes" collection items should have "font" field with value "Verdana"

```

###### Then all nested :collectionFieldName collection items should have :nestedFieldName field with exact value :expectedValue

[](#then-all-nested-collectionfieldname-collection-items-should-have-nestedfieldname-field-with-exact-value-expectedvalue)

When response JSON is a single object, it checks if that object has a property with given name, and that property is a collection (array), and all of that collection items have nested field with given path and with given exact value (including type).

Examples:

```
@Then all nested "owners" collection items should have "user" field with exact value "John"
@Then all nested "themes" collection items should have "font" field with exact value "Verdana"

```

###### Then all nested :collectionFieldName collection items should have nested :nestedFieldName field with value :expectedValue

[](#then-all-nested-collectionfieldname-collection-items-should-have-nested-nestedfieldname-field-with-value-expectedvalue)

When response JSON is a single object, it checks if that object has a property with given name, and that property is a collection (array), and all of that collection items have nested field with given path and given value. For nesting property use "-&gt;" inside expected property name.

Examples:

```
@Then all nested "owners" collection items should have nested "user->name" field with value "John"
@Then all nested "themes" collection items should have nested "font->color" field with value "Red"

```

###### Then all nested :collectionFieldName collection items should have nested :nestedFieldName field with exact value :expectedValue

[](#then-all-nested-collectionfieldname-collection-items-should-have-nested-nestedfieldname-field-with-exact-value-expectedvalue)

When response JSON is a single object, it checks if that object has a property with given name, and that property is a collection (array), and all of that collection items have nested field with given path and given exact value (including type). For nesting property use "-&gt;" inside expected property name.

Examples:

```
@Then all nested "owners" collection items should have nested "user->name" field with value "John"
@Then all nested "themes" collection items should have nested "font->color" field with value "Red"

```

###### Then exactly one nested :collectionFieldName collection items should have :nestedFieldName field with value :expectedValue

[](#then-exactly-one-nested-collectionfieldname-collection-items-should-have-nestedfieldname-field-with-value-expectedvalue)

When response JSON is a single object, it checks if that object has a property with given name, and that property is a collection (array), and exactly one of that collection items have nested field with given path and with given value.

Examples:

```
@Then exactly one nested "users" collection items should have "login" field with value "johny63"
@Then exactly one nested "members" collection items should have "position" field with value "leader"

```

###### Then at least one nested :collectionFieldName collection items should have :nestedFieldName field with value :expectedValue

[](#then-at-least-one-nested-collectionfieldname-collection-items-should-have-nestedfieldname-field-with-value-expectedvalue)

When response JSON is a single object, it checks if that object has a property with given name, and that property is a collection (array), and at least one of that collection items have nested field with given path and with given value.

Examples:

```
@Then at least one nested "users" collection items should have "firstname" field with value "John"
@Then at least one nested "members" collection items should have "position" field with value "worker"

```

###### Then the response JSON should have nested :nestedFieldName field with value :expectedValue

[](#then-the-response-json-should-have-nested-nestedfieldname-field-with-value-expectedvalue)

When response JSON is a single object, it checks if that object has a property with given path and given value. For nesting property use "-&gt;" inside expected property name.

Examples:

```
@Then the response JSON should have nested "recipient->phone_number" field with value "123456789"

```

###### Then the response JSON should have nested :nestedFieldName field with null value

[](#then-the-response-json-should-have-nested-nestedfieldname-field-with-null-value)

When response JSON is a single object, it checks if that object has a property with given path with null value. For nesting property use "-&gt;" inside expected property name.

Examples:

```
@Then the response JSON should have nested "forever_alone->friends" field with null value

```

###### Then the response collection should count :expectedValue items

[](#then-the-response-collection-should-count-expectedvalue-items)

When response JSON is a collection (array), it checks the number of items in collection.

Examples:

```
@Then the response collection should count "4" items

```

###### Then at least one of the collection items should have field :fieldName with value :expectedValue

[](#then-at-least-one-of-the-collection-items-should-have-field-fieldname-with-value-expectedvalue)

When response JSON is a collection (array), it checks if any collection item has field with given value.

Examples:

```
Then at least one of the collection items should have field "name" with value "abcdef"

```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 86.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~43 days

Recently: every ~17 days

Total

11

Last Release

3253d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a1043aa2733a63da5838cc71d7ff7631107d31f1cc11edca517e635d239f4706?d=identicon)[ulff](/maintainers/ulff)

---

Top Contributors

[![ulff](https://avatars.githubusercontent.com/u/2435762?v=4)](https://github.com/ulff "ulff (33 commits)")[![tetsuobe](https://avatars.githubusercontent.com/u/2757697?v=4)](https://github.com/tetsuobe "tetsuobe (5 commits)")

---

Tags

BehatREST APIphp5PHP7behat-extensionbehat-context

### Embed Badge

![Health badge](/badges/ulff-behat-rest-api-extension/health.svg)

```
[![Health](https://phpackages.com/badges/ulff-behat-rest-api-extension/health.svg)](https://phpackages.com/packages/ulff-behat-rest-api-extension)
```

###  Alternatives

[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k5.5M69](/packages/ifsnop-mysqldump-php)[friends-of-behat/mink-debug-extension

Debug extension for Behat

2075.3M285](/packages/friends-of-behat-mink-debug-extension)[sensiolabs/behat-page-object-extension

Page object extension for Behat

1166.5M27](/packages/sensiolabs-behat-page-object-extension)[polishsymfonycommunity/symfony2-mocker-extension

Behat extension for mocking services defined in the Symfony2 dependency injection container.

26253.1k4](/packages/polishsymfonycommunity-symfony2-mocker-extension)[devinci/devinci-behat-extension

Provides extra behat goodies.

17593.9k](/packages/devinci-devinci-behat-extension)[genesis/behat-fail-aid

Get more out of your test suite by getting it to work with you when tests fail. Screenshots and more. Works with Goutte and MinkExtension.

281.5M17](/packages/genesis-behat-fail-aid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
