Skip to content

Commit e2a8c7c

Browse files
authored
Merge pull request #49 from MacPaw/develop
Release
2 parents 3250999 + 8e938c5 commit e2a8c7c

22 files changed

+484
-530
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262

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

6767
- name: Cache composer
6868
uses: actions/cache@v4

README.md

Lines changed: 9 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,22 @@
11
# Behat Api Context Bundle
22

3-
| Version | Build Status | Code Coverage | Latest Release |
4-
|:---------:|:---------------------------------------------------------:|:------------------------------------------------------------------------:|:-----------------:|
5-
| `master` | [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] | ![Latest Release] |
3+
| Version | Build Status | Code Coverage | Latest Release |
4+
| :-------: | :-------------------------------------------------------: | :----------------------------------------------------------------------: | :---------------: |
5+
| `master` | [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] | ![Latest Release] |
66
| `develop` | [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] | - |
77

8-
## ⚠️ Deprecation Notice
9-
10-
> The `ORMContext` has been **deprecated** and **removed** from this package.
11-
> Please use the standalone package [`macpaw/behat-orm-context`](https://github.com/macpaw/behat-orm-context) instead.
12-
13-
---
14-
15-
## Installation
16-
17-
### Step 1: Install the Bundle
18-
19-
Run the following command in your project directory to install the bundle as a development dependency:
20-
21-
```bash
22-
composer require --dev macpaw/behat-api-context
23-
```
24-
25-
> If you are using Symfony Flex, the bundle will be registered automatically.
26-
> Otherwise, follow Step 2 to register the bundle manually.
27-
### Step 2: Register the Bundle
28-
29-
If your project does **not** use Symfony Flex or the bundle does not provide a recipe, manually register it in `config/bundles.php`:
30-
31-
```php
32-
<?php
33-
// config/bundles.php
34-
35-
return [
36-
// ...
37-
BehatApiContext\BehatApiContextBundle::class => ['test' => true],
38-
];
39-
```
40-
41-
> ℹ️ The bundle should only be enabled in the `test` environment.
42-
43-
### Step 3: Configure Behat
44-
45-
Update your `behat.yml`:
46-
47-
```yaml
48-
default:
49-
suites:
50-
default:
51-
contexts:
52-
- BehatApiContext\Context\ApiContext
53-
- BehatApiContext\Context\ORMContext
54-
```
55-
56-
> If you also want to use `ORMContext`, install [macpaw/behat-orm-context](https://github.com/macpaw/behat-orm-context) and follow its setup instructions.
57-
58-
> 📄 **Migration Notice:** `OrmContext` will be removed from `behat-api-context` in the next major release.
59-
> Please migrate to [`behat-orm-context`](https://github.com/macpaw/behat-orm-context) to avoid test failures.
60-
> See the full [ORMContext Migration Plan](./docs/ormcontext-migration.md) for step-by-step instructions.
61-
62-
638
---
649

65-
## Configuration
66-
67-
By default, the bundle provides the following configuration:
68-
> This bundle does not yet include a Symfony recipe to automatically create the configuration file.
69-
> If you need a specific configuration, you have to add it manually.
70-
> [Recipe in progress](https://github.com/MacPaw/BehatRedisContext/issues/2)
71-
72-
```yaml
73-
behat_api_context:
74-
kernel_reset_managers: []
75-
```
76-
77-
You can also add your own reset manager by overriding the configuration manually in `config/packages/test/behat_api_context.yaml`:
78-
79-
```yaml
80-
behat_api_context:
81-
kernel_reset_managers:
82-
- BehatApiContext\Service\ResetManager\DoctrineResetManager
83-
```
10+
Behat API Context provides a set of Behat steps for testing RESTful APIs with support for dynamic request parameters, context persistence, and Symfony integration.
8411

8512
---
8613

87-
## Usage
88-
89-
### Runnable request parameters
90-
91-
Main use case when tests need to use the current date.
92-
Instead of static data in some `.feature` file like this:
93-
94-
```gherkin
95-
"""
96-
{
97-
"dateTo": 1680360081,
98-
"dateFrom": 1680532881
99-
}
100-
"""
101-
```
102-
103-
You can use dynamic expressions:
104-
105-
```gherkin
106-
"""
107-
{
108-
"dateTo": "<(new DateTimeImmutable())->add(new DateInterval('P6D'))->getTimestamp()>",
109-
"dateFrom": "<(new DateTimeImmutable())->add(new DateInterval('P2D'))->getTimestamp()>"
110-
}
111-
"""
112-
```
14+
## 📄 Documentation
11315

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

12021
---
12122

composer.json

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
"slevomat/coding-standard": "^7.0",
4343
"squizlabs/php_codesniffer": "^3.6"
4444
},
45-
"suggest": {
46-
"doctrine/orm": "^2.0"
47-
},
4845
"autoload": {
4946
"psr-4": {
5047
"BehatApiContext\\": "src"
@@ -55,12 +52,6 @@
5552
"BehatApiContext\\Tests\\": "tests"
5653
}
5754
},
58-
"extra": {
59-
"branch-alias": {
60-
"dev-master": "1.x-dev"
61-
},
62-
"deprecated": "The ORMContext has been removed from this package. Please use macpaw/behat-orm-context instead."
63-
},
6455
"scripts": {
6556
"composer-validate": "composer validate",
6657
"phpstan": "./vendor/bin/phpstan analyse -l max",
@@ -73,15 +64,6 @@
7364
"@phpstan",
7465
"@code-style",
7566
"@phpunit"
76-
],
77-
"post-install-cmd": [
78-
"@show-deprecation"
79-
],
80-
"post-update-cmd": [
81-
"@show-deprecation"
82-
],
83-
"show-deprecation": [
84-
"@php -r \"echo '[DEPRECATED] ORMContext has been removed from this package. Use macpaw/behat-orm-context instead.';\""
8567
]
8668
},
8769
"config": {

docs/examples.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Usage Examples
2+
3+
Real-world `.feature` examples using the API Context.
4+
5+
---
6+
7+
## 🔐 Example: Successful Login
8+
9+
```gherkin
10+
Feature: Login API
11+
12+
Scenario: Successful login returns token
13+
Given the request contains params:
14+
"""
15+
{
16+
"email": "test@example.com",
17+
"password": "securepassword"
18+
}
19+
"""
20+
When I send "POST" request to "api_v1_sign_in" route
21+
Then response status code should be 200
22+
And response should be JSON with variable fields "token":
23+
"""
24+
{
25+
"token": "abc123"
26+
}
27+
"""
28+
```
29+
30+
---
31+
32+
## ❌ Example: Invalid Credentials
33+
34+
```gherkin
35+
Scenario: Login with wrong password
36+
Given the request contains params:
37+
"""
38+
{
39+
"email": "test@example.com",
40+
"password": "wrongpassword"
41+
}
42+
"""
43+
When I send "POST" request to "api_v1_sign_in" route
44+
Then response status code should be 401
45+
And response should be JSON:
46+
"""
47+
{
48+
"error": "Invalid credentials"
49+
}
50+
"""
51+
```
52+
53+
---
54+
55+
## 🕓 Example: Relative Dates in Request
56+
57+
```gherkin
58+
Scenario: Create report with dynamic date range
59+
Given the request contains params:
60+
"""
61+
{
62+
"dateFrom": "<(new DateTimeImmutable('-7 days'))->format('Y-m-d')>",
63+
"dateTo": "<(new DateTimeImmutable())->format('Y-m-d')>"
64+
}
65+
"""
66+
When I send a "POST" request to "api_v1_generate_reports"
67+
Then response status code should be 201
68+
```

docs/install.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Installation & Configuration
2+
3+
## 1. Install the package
4+
5+
```bash
6+
composer require --dev macpaw/behat-api-context
7+
```
8+
9+
> If you're using Symfony Flex, the bundle will be registered automatically.
10+
11+
## 2. Register the bundle manually (if not using Flex)
12+
13+
```php
14+
// config/bundles.php
15+
return [
16+
BehatApiContext\BehatApiContextBundle::class => ['test' => true],
17+
];
18+
```
19+
20+
## 3. Configure Behat
21+
By default, the bundle provides the following configuration:
22+
> This bundle does not yet include a Symfony recipe to automatically create the configuration file.
23+
> If you need a specific configuration, you have to add it manually.
24+
> [Recipe in progress](https://github.com/MacPaw/BehatRedisContext/issues/2)
25+
26+
```yaml
27+
# behat.yml
28+
default:
29+
suites:
30+
default:
31+
contexts:
32+
- BehatApiContext\Context\ApiContext
33+
```
34+
35+
## 4. Optional configuration
36+
You can also add your own reset manager by overriding the configuration manually in `config/packages/behat_api_context.yaml`:
37+
38+
```yaml
39+
# config/packages/behat_api_context.yaml
40+
when@test:
41+
behat_api_context:
42+
kernel_reset_managers:
43+
- BehatApiContext\Service\ResetManager\DoctrineResetManager
44+
```

docs/ormcontext-migration.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

docs/runnable-parameters.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Runnable Parameters
2+
3+
Behat API Context supports inline PHP expressions in request payloads using angle brackets `<>`.
4+
5+
## ✨ Example
6+
7+
```gherkin
8+
Given the request contains params:
9+
"""
10+
{
11+
"timestamp": "<(new DateTimeImmutable())->getTimestamp()>",
12+
"uuid": "<Ramsey\\Uuid\\Uuid::uuid4()>"
13+
}
14+
"""
15+
```
16+
17+
## ✅ Rules
18+
19+
- Expressions must be wrapped in `<>`
20+
- Do **not** use `return` statements
21+
- Do **not** end expressions with a semicolon
22+
- Expressions **must not** return `null`
23+
24+
## 💡 Common Use Cases
25+
26+
| Use Case | Example |
27+
|---------------|--------------------------------------------------------|
28+
| Timestamps | `<(new DateTimeImmutable())->getTimestamp()>` |
29+
| UUIDs | `<Ramsey\\Uuid\\Uuid::uuid4()>` |
30+
| Relative Time | `<(new DateTimeImmutable('+1 day'))->format('Y-m-d')>` |
31+
| Random value | `<bin2hex(random_bytes(8))>` |
32+
33+
## 🔥 Pro Tip
34+
35+
You can mix static and dynamic parameters:
36+
37+
```json
38+
{
39+
"start_date": "2024-01-01",
40+
"end_date": "<(new DateTimeImmutable('+7 days'))->format('Y-m-d')>"
41+
}
42+
```

0 commit comments

Comments
 (0)