Version | Build Status | Code Coverage |
---|---|---|
master |
||
develop |
Open a command console, enter your project directory and execute:
Applications that use Symfony Flex in progress
composer require --dev macpaw/behat-api-context
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
composer require --dev macpaw/behat-api-context
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
BehatApiContext\BehatApiContextBundle::class => ['test' => true],
);
// ...
}
// ...
}
Go to behat.yml
:
# ...
contexts:
- BehatApiContext\Context\ApiContext
# ...
If you want to use ORMContext
, you need to have doctrine/orm
installed:
composer require --dev doctrine/orm
Then, update your behat.yml
:
# ...
contexts:
- BehatApiContext\Context\ORMContext
# ...
By default, the bundle has the following configuration:
behat_api_context:
kernel_reset_managers: []
use_orm_context: true
ORM Installed | Default use_orm_context |
---|---|
Yes | true |
No | false |
You can override it manually in your config/packages/test/behat_api_context.yaml
:
behat_api_context:
use_orm_context: false
or
behat_api_context:
use_orm_context: true
depending on your needs.
Main use case when tests need to use the current date.
Instead of static data in some .feature
file like this:
"""
{
"dateTo": 1680360081,
"dateFrom": 1680532881
}
"""
You can use dynamic expressions:
"""
{
"dateTo": "<(new DateTimeImmutable())->add(new DateInterval('P6D'))->getTimeStamp()>",
"dateFrom": "<(new DateTimeImmutable())->add(new DateInterval('P2D'))->getTimeStamp()>"
}
"""
- Runnable code must be a string and placed inside
<>
. - Do not add
return
keyword at the beginning, otherwise aRuntimeException
will be thrown. - Do not add
;
at the end of the expression, otherwise aRuntimeException
will be thrown. - Avoid code that returns
null
, otherwise aRuntimeException
will be thrown.