Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:

- name: Set composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT"

- name: Cache composer
uses: actions/cache@v4
Expand Down
117 changes: 9 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,121 +1,22 @@
# Behat Api Context Bundle

| Version | Build Status | Code Coverage | Latest Release |
|:---------:|:---------------------------------------------------------:|:------------------------------------------------------------------------:|:-----------------:|
| `master` | [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] | ![Latest Release] |
| Version | Build Status | Code Coverage | Latest Release |
| :-------: | :-------------------------------------------------------: | :----------------------------------------------------------------------: | :---------------: |
| `master` | [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] | ![Latest Release] |
| `develop` | [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] | - |

## ⚠️ Deprecation Notice

> The `ORMContext` has been **deprecated** and **removed** from this package.
> Please use the standalone package [`macpaw/behat-orm-context`](https://github.com/macpaw/behat-orm-context) instead.

---

## Installation

### Step 1: Install the Bundle

Run the following command in your project directory to install the bundle as a development dependency:

```bash
composer require --dev macpaw/behat-api-context
```

> If you are using Symfony Flex, the bundle will be registered automatically.
> Otherwise, follow Step 2 to register the bundle manually.
### Step 2: Register the Bundle

If your project does **not** use Symfony Flex or the bundle does not provide a recipe, manually register it in `config/bundles.php`:

```php
<?php
// config/bundles.php

return [
// ...
BehatApiContext\BehatApiContextBundle::class => ['test' => true],
];
```

> ℹ️ The bundle should only be enabled in the `test` environment.

### Step 3: Configure Behat

Update your `behat.yml`:

```yaml
default:
suites:
default:
contexts:
- BehatApiContext\Context\ApiContext
- BehatApiContext\Context\ORMContext
```

> If you also want to use `ORMContext`, install [macpaw/behat-orm-context](https://github.com/macpaw/behat-orm-context) and follow its setup instructions.

> 📄 **Migration Notice:** `OrmContext` will be removed from `behat-api-context` in the next major release.
> Please migrate to [`behat-orm-context`](https://github.com/macpaw/behat-orm-context) to avoid test failures.
> See the full [ORMContext Migration Plan](./docs/ormcontext-migration.md) for step-by-step instructions.


---

## Configuration

By default, the bundle provides the following configuration:
> This bundle does not yet include a Symfony recipe to automatically create the configuration file.
> If you need a specific configuration, you have to add it manually.
> [Recipe in progress](https://github.com/MacPaw/BehatRedisContext/issues/2)

```yaml
behat_api_context:
kernel_reset_managers: []
```

You can also add your own reset manager by overriding the configuration manually in `config/packages/test/behat_api_context.yaml`:

```yaml
behat_api_context:
kernel_reset_managers:
- BehatApiContext\Service\ResetManager\DoctrineResetManager
```
Behat API Context provides a set of Behat steps for testing RESTful APIs with support for dynamic request parameters, context persistence, and Symfony integration.

---

## Usage

### Runnable request parameters

Main use case when tests need to use the current date.
Instead of static data in some `.feature` file like this:

```gherkin
"""
{
"dateTo": 1680360081,
"dateFrom": 1680532881
}
"""
```

You can use dynamic expressions:

```gherkin
"""
{
"dateTo": "<(new DateTimeImmutable())->add(new DateInterval('P6D'))->getTimestamp()>",
"dateFrom": "<(new DateTimeImmutable())->add(new DateInterval('P2D'))->getTimestamp()>"
}
"""
```
## 📄 Documentation

#### To achieve this, several conditions must be met:
- Runnable code must be a string and placed inside `<>`.
- Do not add `return` keyword at the beginning, otherwise a `RuntimeException` will be thrown.
- Do not add a semicolon (`;`) at the end of the expression, otherwise a `RuntimeException` will be thrown.
- Avoid code that returns `null`, otherwise a `RuntimeException` will be thrown.
- [Installation & Configuration](docs/install.md)
- [Available Steps](docs/steps.md)
- [Usage Examples](docs/examples.md)
- [Runnable Parameters](docs/runnable-parameters.md)

---

Expand Down
18 changes: 0 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
"slevomat/coding-standard": "^7.0",
"squizlabs/php_codesniffer": "^3.6"
},
"suggest": {
"doctrine/orm": "^2.0"
},
"autoload": {
"psr-4": {
"BehatApiContext\\": "src"
Expand All @@ -55,12 +52,6 @@
"BehatApiContext\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"deprecated": "The ORMContext has been removed from this package. Please use macpaw/behat-orm-context instead."
},
"scripts": {
"composer-validate": "composer validate",
"phpstan": "./vendor/bin/phpstan analyse -l max",
Expand All @@ -73,15 +64,6 @@
"@phpstan",
"@code-style",
"@phpunit"
],
"post-install-cmd": [
"@show-deprecation"
],
"post-update-cmd": [
"@show-deprecation"
],
"show-deprecation": [
"@php -r \"echo '[DEPRECATED] ORMContext has been removed from this package. Use macpaw/behat-orm-context instead.';\""
]
},
"config": {
Expand Down
68 changes: 68 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Usage Examples

Real-world `.feature` examples using the API Context.

---

## 🔐 Example: Successful Login

```gherkin
Feature: Login API

Scenario: Successful login returns token
Given the request contains params:
"""
{
"email": "test@example.com",
"password": "securepassword"
}
"""
When I send "POST" request to "api_v1_sign_in" route
Then response status code should be 200
And response should be JSON with variable fields "token":
"""
{
"token": "abc123"
}
"""
```

---

## ❌ Example: Invalid Credentials

```gherkin
Scenario: Login with wrong password
Given the request contains params:
"""
{
"email": "test@example.com",
"password": "wrongpassword"
}
"""
When I send "POST" request to "api_v1_sign_in" route
Then response status code should be 401
And response should be JSON:
"""
{
"error": "Invalid credentials"
}
"""
```

---

## 🕓 Example: Relative Dates in Request

```gherkin
Scenario: Create report with dynamic date range
Given the request contains params:
"""
{
"dateFrom": "<(new DateTimeImmutable('-7 days'))->format('Y-m-d')>",
"dateTo": "<(new DateTimeImmutable())->format('Y-m-d')>"
}
"""
When I send a "POST" request to "api_v1_generate_reports"
Then response status code should be 201
```
44 changes: 44 additions & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Installation & Configuration

## 1. Install the package

```bash
composer require --dev macpaw/behat-api-context
```

> If you're using Symfony Flex, the bundle will be registered automatically.

## 2. Register the bundle manually (if not using Flex)

```php
// config/bundles.php
return [
BehatApiContext\BehatApiContextBundle::class => ['test' => true],
];
```

## 3. Configure Behat
By default, the bundle provides the following configuration:
> This bundle does not yet include a Symfony recipe to automatically create the configuration file.
> If you need a specific configuration, you have to add it manually.
> [Recipe in progress](https://github.com/MacPaw/BehatRedisContext/issues/2)

```yaml
# behat.yml
default:
suites:
default:
contexts:
- BehatApiContext\Context\ApiContext
```

## 4. Optional configuration
You can also add your own reset manager by overriding the configuration manually in `config/packages/behat_api_context.yaml`:

```yaml
# config/packages/behat_api_context.yaml
when@test:
behat_api_context:
kernel_reset_managers:
- BehatApiContext\Service\ResetManager\DoctrineResetManager
```
31 changes: 0 additions & 31 deletions docs/ormcontext-migration.md

This file was deleted.

42 changes: 42 additions & 0 deletions docs/runnable-parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Runnable Parameters

Behat API Context supports inline PHP expressions in request payloads using angle brackets `<>`.

## ✨ Example

```gherkin
Given the request contains params:
"""
{
"timestamp": "<(new DateTimeImmutable())->getTimestamp()>",
"uuid": "<Ramsey\\Uuid\\Uuid::uuid4()>"
}
"""
```

## ✅ Rules

- Expressions must be wrapped in `<>`
- Do **not** use `return` statements
- Do **not** end expressions with a semicolon
- Expressions **must not** return `null`

## 💡 Common Use Cases

| Use Case | Example |
|---------------|--------------------------------------------------------|
| Timestamps | `<(new DateTimeImmutable())->getTimestamp()>` |
| UUIDs | `<Ramsey\\Uuid\\Uuid::uuid4()>` |
| Relative Time | `<(new DateTimeImmutable('+1 day'))->format('Y-m-d')>` |
| Random value | `<bin2hex(random_bytes(8))>` |

## 🔥 Pro Tip

You can mix static and dynamic parameters:

```json
{
"start_date": "2024-01-01",
"end_date": "<(new DateTimeImmutable('+7 days'))->format('Y-m-d')>"
}
```
Loading