Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Behat integration #128

Merged
merged 3 commits into from
Oct 30, 2020
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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ install:
- composer require phpunit/phpunit:${PHPUNIT_VERSION:-"8.*"} --update-with-all-dependencies

script:
- cp tests/Functional/parameters.yml.dist tests/Functional/parameters.yml
- cp tests/Functional/app/parameters.yml.dist tests/Functional/app/parameters.yml
- rm -rf tests/Functional/cache
- if [[ ${SKIP_CS_FIXER} != "1" ]]; then make php_cs_fixer_check; fi
- make phpstan
Expand All @@ -55,6 +55,7 @@ script:
elif [[ ${PHPUNIT_VERSION} == "7.*" ]]; then vendor/bin/phpunit -c tests/phpunit7.xml tests/;
else vendor/bin/phpunit -c tests/ tests/;
fi
- vendor/bin/behat -c tests/behat.yml -fprogress

after_success:
- if [[ ${TEST_COVERAGE} ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi
Expand Down
65 changes: 20 additions & 45 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,26 @@ 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`)
wouterj marked this conversation as resolved.
Show resolved Hide resolved

```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.

Please note that this is only works if the tests are executed in the same process as Behat. This means it cannot work when using e.g. Selenium to call your application.

### Configuration

Expand Down Expand Up @@ -109,48 +126,6 @@ public function testMyTestCaseThatINeedToDebug()
}
```

### Behat

It is possible to use this bundle in a Behat test suite if scenarios are executed in the same process as Behat. This will not work if the Behat tests invoke the application via HTTP requests.

To use the bundle follow the installation instructions and add the following methods to your `FeatureContext` class:

```php
/**
* @BeforeSuite
*/
public static function beforeSuite()
{
StaticDriver::setKeepStaticConnections(true);
}

/**
* @BeforeScenario
*/
public function beforeScenario()
{
StaticDriver::beginTransaction();
}

/**
* @AfterScenario
*/
public function afterScenario()
{
StaticDriver::rollBack();
}

/**
* @AfterSuite
*/
public static function afterSuite()
{
StaticDriver::setKeepStaticConnections(false);
}
```

See [dmaicher/symfony-flex-behat-test](https://github.com/dmaicher/symfony-flex-behat-test) for a complete example.

### Troubleshooting

In case you are running (maybe without knowing it) queries during your tests that are implicitly committing any open transaction
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
"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.4 || ^5.1",
"symfony/phpunit-bridge": "^5.1",
"symfony/process": "^3.4 || ^4.4 || ^5.1",
"phpstan/phpstan": "^0.12"
},
"autoload": {
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,37 @@
<?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
{
}
}
98 changes: 98 additions & 0 deletions tests/Functional/FunctionalTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Tests\Functional;

use Doctrine\DBAL\Connection;
use PHPUnit\Framework\Assert;
use Symfony\Component\HttpKernel\KernelInterface;
use Tests\Functional\app\AppKernel;

trait FunctionalTestTrait
{
/**
* @var KernelInterface
*/
private $kernel;

/**
* @var Connection
*/
protected $connection;

/**
* @BeforeScenario
*/
public function setUp(): void
{
$this->kernel = new AppKernel('test', true);
$this->kernel->boot();
$this->connection = $this->kernel->getContainer()->get('doctrine.dbal.default_connection');
}

/**
* @AfterScenario
*/
public function tearDown(): void
{
$this->kernel->shutdown();
}

/**
* @Then there are :count rows
* @Then there is :count row
*/
public function assertRowCount($count): void
{
Assert::assertEquals($count, $this->connection->fetchColumn('SELECT COUNT(*) FROM test'));
}

/**
* @When I insert a new row
*/
public function insertRow(): void
{
$this->connection->insert('test', [
'test' => 'foo',
]);
}

/**
* @When I begin a transaction
*/
public function beginTransaction(): void
{
$this->connection->beginTransaction();
}

/**
* @When I rollback the transaction
*/
public function rollbackTransaction(): void
{
$this->connection->rollBack();
}

/**
* @When I commit the transaction
*/
public function commitTransaction(): void
{
$this->connection->commit();
}

/**
* @When I create a savepoint named :name
*/
public function createSavepoint(string $name): void
{
$this->connection->createSavepoint($name);
}

/**
* @When I rollback the savepoint named :name
*/
public function rollbackSavepoint(string $name): void
{
$this->connection->rollbackSavepoint($name);
}
}
Loading