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
127 changes: 53 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,112 +1,90 @@
# Behat Api Context Bundle

| Version | Build Status | Code Coverage |
|:---------:|:-------------:|:-----:|
| `master` | [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] |
| `develop` | [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] |
| 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] | - |

## Installation

### Step 1: Download the Bundle
## ⚠️ Deprecation Notice

Open a command console, enter your project directory and execute:
> 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.

#### Applications that use Symfony Flex [in progress](https://github.com/MacPaw/BehatRedisContext/issues/2)
---

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

#### Applications that don't use Symfony Flex
### Step 1: Install the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this 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
composer require --dev macpaw/behat-api-context
```

This command requires you to have Composer installed globally, as explained
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.
> 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


Then, enable the bundle by adding it to the list of registered bundles
in the `app/AppKernel.php` file of your project:
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
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
BehatApiContext\BehatApiContextBundle::class => ['test' => true],
);

// ...
}
// config/bundles.php

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

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

## Step 2: Configure Behat
### Step 3: Configure Behat

Go to `behat.yml`:
Update your `behat.yml`:

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

### Optional: Enable 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.

If you want to use `ORMContext`, you need to have `doctrine/orm` installed:
> 📄 **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.

```bash
composer require --dev doctrine/orm
```

Then, update your `behat.yml`:

```yaml
# ...
contexts:
- BehatApiContext\Context\ORMContext
# ...
```

---

## Configuration

By default, the bundle has the following 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:
- BehatApiContext\Service\ResetManager\DoctrineResetManager
kernel_reset_managers: []
```

The `use_orm_context` parameter is no longer configurable manually. Its value is determined automatically based on whether the Doctrine ORM is installed:
> **Important:** This logic is applied internally and cannot be overridden via configuration.
You can also add your own reset manager by overriding the configuration manually in `config/packages/test/behat_api_context.yaml`:

| ORM Installed | Default `use_orm_context` |
|:-------------:|:-------------------------:|
| Yes | `true` |
| No | `false` |
```yaml
behat_api_context:
kernel_reset_managers:
- BehatApiContext\Service\ResetManager\DoctrineResetManager
```

---

# Usage
## Usage

### Runnable request parameters

Expand All @@ -127,25 +105,26 @@ You can use dynamic expressions:
```gherkin
"""
{
"dateTo": "<(new DateTimeImmutable())->add(new DateInterval('P6D'))->getTimeStamp()>",
"dateFrom": "<(new DateTimeImmutable())->add(new DateInterval('P2D'))->getTimeStamp()>"
"dateTo": "<(new DateTimeImmutable())->add(new DateInterval('P6D'))->getTimestamp()>",
"dateFrom": "<(new DateTimeImmutable())->add(new DateInterval('P2D'))->getTimestamp()>"
}
"""
```

#### 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 `;` at the end of the expression, 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.

---

[master Build Status]: https://github.com/macpaw/behat-api-context/actions?query=workflow%3ACI+branch%3Amaster
[master Build Status Image]: https://github.com/macpaw/behat-api-context/workflows/CI/badge.svg?branch=master
[develop Build Status]: https://github.com/macpaw/behat-api-context/actions?query=workflow%3ACI+branch%3Adevelop
[develop Build Status Image]: https://github.com/macpaw/behat-api-context/workflows/CI/badge.svg?branch=develop
[master Build Status]: https://github.com/MacPaw/behat-api-context/actions/workflows/ci.yaml
[master Build Status Image]: https://github.com/MacPaw/behat-api-context/actions/workflows/ci.yaml/badge.svg?branch=master
[develop Build Status]: https://github.com/MacPaw/behat-api-context/actions/workflows/ci.yaml
[develop Build Status Image]: https://github.com/MacPaw/behat-api-context/actions/workflows/ci.yaml/badge.svg?branch=develop
[master Code Coverage]: https://codecov.io/gh/macpaw/behat-api-context/branch/master
[master Code Coverage Image]: https://img.shields.io/codecov/c/github/macpaw/behat-api-context/master?logo=codecov
[develop Code Coverage]: https://codecov.io/gh/macpaw/behat-api-context/branch/develop
[develop Code Coverage Image]: https://img.shields.io/codecov/c/github/macpaw/behat-api-context/develop?logo=codecov
[Latest Release]: https://img.shields.io/github/v/release/macpaw/behat-api-context
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
"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 @@ -67,6 +73,15 @@
"@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
31 changes: 31 additions & 0 deletions docs/ormcontext-migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Migration plan for using ORMContext

### Step 1: 📦 Add `behat-orm-context` to `composer.json`
```bash
composer require --dev macpaw/behat-orm-context
```

### Step 2: 🧩 Register `OrmContext` in `behat.yml`
```yaml
default:
suites:
default:
contexts:
- BehatOrmContext\Context\OrmContext

```
> If you previously used `BehatApiContext\Context\OrmContext`, replace it with `BehatOrmContext\Context\OrmContext`

### Step 3: 🧪 Verify Scenarios
1. Run Behat tests:
```bash
vendor/bin/behat
```

2. Ensure that all steps previously relying on OrmContext still work correctly after the migration.

### 🚨 Backward Compatibility Notice
> `OrmContext` will be removed from `behat-api-context` in a future major release. If you don't migrate, your tests will break after updating dependencies.

### 🔔 Recommendation
Perform the migration before the next major release of `behat-api-context` to avoid CI/CD disruptions and unexpected test failures in your production pipeline.
9 changes: 9 additions & 0 deletions src/Context/ORMContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@
use Doctrine\ORM\NoResultException;
use RuntimeException;

/**
* @deprecated since 1.4.0, use macpaw/behat-orm-context instead: https://github.com/MacPaw/behat-orm-context
*/
final class ORMContext implements Context
{
private EntityManagerInterface $manager;

public function __construct(EntityManagerInterface $manager)
{
trigger_error(
'BehatApiContext\ORMContext is deprecated since version 1.4.0 and will be removed in 2.0. '
. 'Please use macpaw/behat-orm-context instead: https://github.com/MacPaw/behat-orm-context',
E_USER_DEPRECATED
);

$this->manager = $manager;
}

Expand Down
Loading