Skip to content

Commit de17104

Browse files
committed
Added Behat integration
1 parent a364cfe commit de17104

4 files changed

Lines changed: 102 additions & 3 deletions

File tree

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ It also includes a `StaticArrayCache` that will be automatically configured as m
3737
3838
Note: if you are using symfony flex and you are allowing contrib recipes (`extra.symfony.allow-contrib=true`) then the bundle will be automatically enabled for the `'test'` environment. See https://github.com/symfony/recipes-contrib/tree/master/dama/doctrine-test-bundle
3939
40-
3. For PHPUnit version >= 7.5 add the extension in your xml config (e.g. `app/phpunit.xml`)
40+
#### Using the Bundle with PHPUnit
41+
42+
1. For PHPUnit version >= 7.5 add the extension in your xml config (e.g. `app/phpunit.xml`)
4143
4244
```xml
4345
<phpunit>
@@ -59,11 +61,24 @@ It also includes a `StaticArrayCache` that will be automatically configured as m
5961
</phpunit>
6062
```
6163
62-
4. Make sure you also have `phpunit/phpunit` available as a `dev` dependency (**versions 7, 8 and 9 are supported with the built in listener/extension**) to run your tests.
64+
2. Make sure you also have `phpunit/phpunit` available as a `dev` dependency (**versions 7, 8 and 9 are supported with the built in listener/extension**) to run your tests.
6365
Alternatively this bundle is also compatible with `symfony/phpunit-bridge` and its `simple-phpunit` script.
6466
(Note: you may need to make sure the phpunit-bridge requires the correct PHPUnit 7+ Version using the environment variable `SYMFONY_PHPUNIT_VERSION`).
6567
66-
5. That's it! From now on whatever changes you do to the database within each single testcase (be it a `WebTestCase` or a `KernelTestCase` or any custom test) are automatically rolled back for you :blush:
68+
3. That's it! From now on whatever changes you do to the database within each single testcase (be it a `WebTestCase` or a `KernelTestCase` or any custom test) are automatically rolled back for you :blush:
69+
70+
#### Using the Bundle with Behat
71+
72+
Enable the extension in your Behat config (e.g. `behat.yml`)
73+
74+
```yaml
75+
default:
76+
# ...
77+
extensions:
78+
DAMA\DoctrineTestBundle\Behat\ServiceContainer\DoctrineExtension: ~
79+
```
80+
81+
That's it! From now on whatever changes you do to the database within each scenario are automatically rolled back for you.
6782
6883
### Configuration
6984

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
"doctrine/doctrine-bundle": "^1.11 || ^2.0"
1818
},
1919
"require-dev": {
20+
"behat/behat": "^3.0",
2021
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
2122
"symfony/yaml": "^3.4 || ^4.3 || ^5.0",
2223
"symfony/phpunit-bridge": "^4.3 || ^5.0",
2324
"phpstan/phpstan": "^0.12"
2425
},
26+
"conflict": {
27+
"behat/behat": "<3.0"
28+
},
2529
"autoload": {
2630
"psr-4": {
2731
"DAMA\\DoctrineTestBundle\\": "src/DAMA/DoctrineTestBundle"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace DAMA\DoctrineTestBundle\Behat;
4+
5+
use Behat\Behat\EventDispatcher\Event\ExampleTested;
6+
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
7+
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
8+
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
11+
class BehatListener implements EventSubscriberInterface
12+
{
13+
public static function getSubscribedEvents(): array
14+
{
15+
return [
16+
ExerciseCompleted::BEFORE => 'enableStaticConnection',
17+
ExerciseCompleted::AFTER => 'disableStaticConnection',
18+
ScenarioTested::BEFORE => ['beginTransaction', 255],
19+
ExampleTested::BEFORE => ['beginTransaction', 255],
20+
ScenarioTested::AFTER => ['rollBack', -255],
21+
ExampleTested::AFTER => ['rollBack', -255],
22+
];
23+
}
24+
25+
public function enableStaticConnection(): void
26+
{
27+
StaticDriver::setKeepStaticConnections(true);
28+
}
29+
30+
public function disableStaticConnection(): void
31+
{
32+
StaticDriver::setKeepStaticConnections(false);
33+
}
34+
35+
public function beginTransaction(): void
36+
{
37+
StaticDriver::beginTransaction();
38+
}
39+
40+
public function rollBack(): void
41+
{
42+
StaticDriver::rollBack();
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace DAMA\DoctrineTestBundle\Behat\ServiceContainer;
4+
5+
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
6+
use Behat\Testwork\ServiceContainer\Extension;
7+
use Behat\Testwork\ServiceContainer\ExtensionManager;
8+
use DAMA\DoctrineTestBundle\Behat\BehatListener;
9+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
10+
use Symfony\Component\DependencyInjection\ContainerBuilder;
11+
12+
class DoctrineExtension implements Extension
13+
{
14+
public function getConfigKey(): string
15+
{
16+
return 'dama_doctrine';
17+
}
18+
19+
public function initialize(ExtensionManager $extensionManager): void
20+
{
21+
}
22+
23+
public function configure(ArrayNodeDefinition $builder): void
24+
{
25+
}
26+
27+
public function load(ContainerBuilder $container, array $config): void
28+
{
29+
$container->register('dama_doctrine_test.listener', BehatListener::class)
30+
->addTag(EventDispatcherExtension::SUBSCRIBER_TAG);
31+
}
32+
33+
public function process(ContainerBuilder $container): void
34+
{
35+
}
36+
}

0 commit comments

Comments
 (0)