Skip to content

Commit

Permalink
Added Behat integration
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Sep 18, 2020
1 parent a364cfe commit de17104
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ It also includes a `StaticArrayCache` that will be automatically configured as m
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
3. For PHPUnit version >= 7.5 add the extension in your xml config (e.g. `app/phpunit.xml`)
#### Using the Bundle with PHPUnit
1. For PHPUnit version >= 7.5 add the extension in your xml config (e.g. `app/phpunit.xml`)
```xml
<phpunit>
Expand All @@ -59,11 +61,24 @@ It also includes a `StaticArrayCache` that will be automatically configured as m
</phpunit>
```
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.
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.
Alternatively this bundle is also compatible with `symfony/phpunit-bridge` and its `simple-phpunit` script.
(Note: you may need to make sure the phpunit-bridge requires the correct PHPUnit 7+ Version using the environment variable `SYMFONY_PHPUNIT_VERSION`).
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:
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:
#### Using the Bundle with Behat
Enable the extension in your Behat config (e.g. `behat.yml`)
```yaml
default:
# ...
extensions:
DAMA\DoctrineTestBundle\Behat\ServiceContainer\DoctrineExtension: ~
```
That's it! From now on whatever changes you do to the database within each scenario are automatically rolled back for you.
### Configuration
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
"doctrine/doctrine-bundle": "^1.11 || ^2.0"
},
"require-dev": {
"behat/behat": "^3.0",
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
"symfony/yaml": "^3.4 || ^4.3 || ^5.0",
"symfony/phpunit-bridge": "^4.3 || ^5.0",
"phpstan/phpstan": "^0.12"
},
"conflict": {
"behat/behat": "<3.0"
},
"autoload": {
"psr-4": {
"DAMA\\DoctrineTestBundle\\": "src/DAMA/DoctrineTestBundle"
Expand Down
44 changes: 44 additions & 0 deletions src/DAMA/DoctrineTestBundle/Behat/BehatListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace DAMA\DoctrineTestBundle\Behat;

use Behat\Behat\EventDispatcher\Event\ExampleTested;
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class BehatListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ExerciseCompleted::BEFORE => 'enableStaticConnection',
ExerciseCompleted::AFTER => 'disableStaticConnection',
ScenarioTested::BEFORE => ['beginTransaction', 255],
ExampleTested::BEFORE => ['beginTransaction', 255],
ScenarioTested::AFTER => ['rollBack', -255],
ExampleTested::AFTER => ['rollBack', -255],
];
}

public function enableStaticConnection(): void
{
StaticDriver::setKeepStaticConnections(true);
}

public function disableStaticConnection(): void
{
StaticDriver::setKeepStaticConnections(false);
}

public function beginTransaction(): void
{
StaticDriver::beginTransaction();
}

public function rollBack(): void
{
StaticDriver::rollBack();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace DAMA\DoctrineTestBundle\Behat\ServiceContainer;

use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
use Behat\Testwork\ServiceContainer\Extension;
use Behat\Testwork\ServiceContainer\ExtensionManager;
use DAMA\DoctrineTestBundle\Behat\BehatListener;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DoctrineExtension implements Extension
{
public function getConfigKey(): string
{
return 'dama_doctrine';
}

public function initialize(ExtensionManager $extensionManager): void
{
}

public function configure(ArrayNodeDefinition $builder): void
{
}

public function load(ContainerBuilder $container, array $config): void
{
$container->register('dama_doctrine_test.listener', BehatListener::class)
->addTag(EventDispatcherExtension::SUBSCRIBER_TAG);
}

public function process(ContainerBuilder $container): void
{
}
}

0 comments on commit de17104

Please sign in to comment.