Skip to content

Commit de2f926

Browse files
authored
Merge pull request #42 from MacPaw/develop
Release
2 parents 012fb2a + ca03ba8 commit de2f926

File tree

11 files changed

+229
-75
lines changed

11 files changed

+229
-75
lines changed

README.md

Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
Behat Api Context Bundle
2-
=================================
1+
# Behat Api Context Bundle
32

43
| Version | Build Status | Code Coverage |
54
|:---------:|:-------------:|:-----:|
6-
| `master`| [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] |
7-
| `develop`| [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] |
5+
| `master` | [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] |
6+
| `develop` | [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] |
87

9-
Installation
10-
============
8+
## Installation
9+
10+
### Step 1: Download the Bundle
1111

12-
Step 1: Download the Bundle
13-
----------------------------------
1412
Open a command console, enter your project directory and execute:
1513

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

18-
```console
19-
$ composer require --dev macpaw/behat-api-context
16+
```bash
17+
composer require --dev macpaw/behat-api-context
2018
```
2119

22-
### Applications that don't use Symfony Flex
20+
#### Applications that don't use Symfony Flex
2321

24-
Open a command console, enter your project directory and execute the
25-
following command to download the latest stable version of this bundle:
22+
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
2623

27-
```console
28-
$ composer require --dev macpaw/behat-api-context
24+
```bash
25+
composer require --dev macpaw/behat-api-context
2926
```
3027

3128
This command requires you to have Composer installed globally, as explained
@@ -57,9 +54,11 @@ class AppKernel extends Kernel
5754
}
5855
```
5956

60-
Step 2: Configure Behat
61-
=============
62-
Go to `behat.yml`
57+
---
58+
59+
## Step 2: Configure Behat
60+
61+
Go to `behat.yml`:
6362

6463
```yaml
6564
# ...
@@ -68,45 +67,92 @@ Go to `behat.yml`
6867
# ...
6968
```
7069

71-
If you want use orm context add to `behat.yml`
70+
### Optional: Enable ORMContext
71+
72+
If you want to use `ORMContext`, you need to have `doctrine/orm` installed:
73+
74+
```bash
75+
composer require --dev doctrine/orm
76+
```
77+
78+
Then, update your `behat.yml`:
79+
7280
```yaml
7381
# ...
7482
contexts:
7583
- BehatApiContext\Context\ORMContext
7684
# ...
7785
```
7886

79-
Usage
80-
=============
87+
---
88+
89+
## Configuration
90+
91+
By default, the bundle has the following configuration:
92+
93+
```yaml
94+
behat_api_context:
95+
kernel_reset_managers: []
96+
use_orm_context: true
97+
```
98+
99+
| ORM Installed | Default `use_orm_context` |
100+
|:-------------:|:-------------------------:|
101+
| Yes | `true` |
102+
| No | `false` |
103+
104+
You can override it manually in your `config/packages/test/behat_api_context.yaml`:
81105

82-
Runnable request parameters
83-
----------------------------------
84-
Main use case when tests need to use current date.
85-
Instead of static data in some `testCaseName.feature`, like this:
86-
```feature
106+
```yaml
107+
behat_api_context:
108+
use_orm_context: false
109+
```
110+
111+
or
112+
113+
```yaml
114+
behat_api_context:
115+
use_orm_context: true
116+
```
117+
118+
depending on your needs.
119+
120+
---
121+
122+
# Usage
123+
124+
### Runnable request parameters
125+
126+
Main use case when tests need to use the current date.
127+
Instead of static data in some `.feature` file like this:
128+
129+
```gherkin
87130
"""
88131
{
89132
"dateTo": 1680360081,
90-
"dateFrom": 1680532881,
133+
"dateFrom": 1680532881
91134
}
92135
"""
93136
```
94-
Can use, for example:
95-
```feature
137+
138+
You can use dynamic expressions:
139+
140+
```gherkin
96141
"""
97142
{
98143
"dateTo": "<(new DateTimeImmutable())->add(new DateInterval('P6D'))->getTimeStamp()>",
99-
"dateFrom": "<(new DateTimeImmutable())->add(new DateInterval('P2D'))->getTimeStamp()>",
144+
"dateFrom": "<(new DateTimeImmutable())->add(new DateInterval('P2D'))->getTimeStamp()>"
100145
}
101146
"""
102147
```
103148

104-
#### To accomplish this, several conditions must be met:
105-
- Runnable code must be a string and placed in `<>`
106-
- Should not add `return` keyword at the beginning, otherwise will get RuntimeException
107-
- Should not add `;` keyword at the end, otherwise will get RuntimeException
108-
- Should not use the code that returns `null`, otherwise will get RuntimeException
149+
#### To achieve this, several conditions must be met:
150+
- Runnable code must be a string and placed inside `<>`.
151+
- Do not add `return` keyword at the beginning, otherwise a `RuntimeException` will be thrown.
152+
- Do not add `;` at the end of the expression, otherwise a `RuntimeException` will be thrown.
153+
- Avoid code that returns `null`, otherwise a `RuntimeException` will be thrown.
109154

155+
---
110156

111157
[master Build Status]: https://github.com/macpaw/behat-api-context/actions?query=workflow%3ACI+branch%3Amaster
112158
[master Build Status Image]: https://github.com/macpaw/behat-api-context/workflows/CI/badge.svg?branch=master

phpstan.neon.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ parameters:
33
paths:
44
- src
55
level: max
6-
checkMissingIterableValueType: false
76
checkExplicitMixed: false
87
ignoreErrors:
98
-
@@ -34,3 +33,5 @@ parameters:
3433
message: '#Call to an undefined method object::getConnection().*#'
3534
count: 1
3635
path: ./src/Service/ResetManager/DoctrineResetManager
36+
-
37+
identifier: missingType.iterableValue

src/DependencyInjection/BehatApiContextExtension.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace BehatApiContext\DependencyInjection;
66

7-
use BehatApiContext\Context\ApiContext;
87
use BehatApiContext\Context\ORMContext;
98
use Symfony\Component\Config\FileLocator;
109
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -26,7 +25,7 @@ public function load(array $configs, ContainerBuilder $container): void
2625
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
2726

2827
$this->loadApiContext($config, $loader, $container);
29-
$this->disableOrmContext($config, $container);
28+
$this->loadOrmContext($config, $loader, $container);
3029
}
3130

3231
/**
@@ -37,29 +36,50 @@ private function loadApiContext(
3736
XmlFileLoader $loader,
3837
ContainerBuilder $container
3938
): void {
40-
$loader->load('api_context.xml');
41-
42-
if (isset($config['kernel_reset_managers'])) {
43-
$apiContextDefinition = $container->findDefinition(ApiContext::class);
44-
foreach ($config['kernel_reset_managers'] as $resetManager) {
45-
$resetManagerDefinition = $container->findDefinition($resetManager);
39+
$this->safeLoad($loader, 'api_context.xml');
40+
}
4641

47-
$apiContextDefinition->addMethodCall(
48-
'addKernelResetManager',
49-
[$resetManagerDefinition],
50-
);
51-
}
42+
/**
43+
* @param array<array> $config
44+
*/
45+
private function loadOrmContext(
46+
array $config,
47+
XmlFileLoader $loader,
48+
ContainerBuilder $container
49+
): void {
50+
if (!($config['use_orm_context'] ?? true)) {
51+
return;
5252
}
53-
}
5453

55-
private function disableOrmContext(array $config, ContainerBuilder $container): void
56-
{
57-
$config['use_orm_context'] = $config['use_orm_context'] ?? true;
54+
$this->safeLoad($loader, 'orm_context.xml');
55+
56+
$this->configureKernelResetManagers(
57+
$config,
58+
$container,
59+
ORMContext::class
60+
);
61+
}
5862

59-
if ($config['use_orm_context']) {
63+
private function configureKernelResetManagers(
64+
array $config,
65+
ContainerBuilder $container,
66+
string $contextClass
67+
): void {
68+
if (empty($config['kernel_reset_managers'])) {
6069
return;
6170
}
6271

63-
$container->removeDefinition(ORMContext::class);
72+
$contextDefinition = $container->findDefinition($contextClass);
73+
74+
foreach ($config['kernel_reset_managers'] as $resetManager) {
75+
$resetManagerDefinition = $container->findDefinition($resetManager);
76+
77+
$contextDefinition->addMethodCall('addKernelResetManager', [$resetManagerDefinition]);
78+
}
79+
}
80+
81+
private function safeLoad(XmlFileLoader $loader, string $file): void
82+
{
83+
$loader->load($file);
6484
}
6585
}

src/DependencyInjection/Configuration.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace BehatApiContext\DependencyInjection;
66

7+
use Composer\InstalledVersions;
78
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
89
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
910
use Symfony\Component\Config\Definition\ConfigurationInterface;
@@ -28,8 +29,13 @@ private function addKernelResetManagersSection(NodeBuilder $builder): void
2829
->end()
2930

3031
->booleanNode('use_orm_context')
31-
->defaultValue(true)
32+
->defaultValue($this->checkOrmContextDefValue())
3233
->end()
3334
->end();
3435
}
36+
37+
private function checkOrmContextDefValue(): bool
38+
{
39+
return InstalledVersions::isInstalled('doctrine/orm');
40+
}
3541
}

src/Resources/config/api_context.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<container xmlns="http://symfony.com/schema/dic/services"
3-
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
4-
xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
4+
xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
55

66
<services>
77
<service public="true" autowire="true" id="BehatApiContext\Context\ApiContext" class="BehatApiContext\Context\ApiContext"/>
8-
<service public="true" autowire="true" id="BehatApiContext\Context\ORMContext" class="BehatApiContext\Context\ORMContext"/>
9-
10-
<service public="false" id="BehatApiContext\Service\ResetManager\DoctrineResetManager" class="BehatApiContext\Service\ResetManager\DoctrineResetManager"/>
118
</services>
129
</container>

src/Resources/config/orm_context.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
4+
xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service public="true" autowire="true" id="BehatApiContext\Context\ORMContext" class="BehatApiContext\Context\ORMContext"/>
8+
<service public="false" id="BehatApiContext\Service\ResetManager\DoctrineResetManager" class="BehatApiContext\Service\ResetManager\DoctrineResetManager"/>
9+
</services>
10+
</container>

0 commit comments

Comments
 (0)