Skip to content

Commit

Permalink
Added Behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Oct 4, 2020
1 parent 6882bb0 commit 6c12782
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ 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
- make phpstan php_cs_fixer_check
- |
if [[ ${TEST_COVERAGE} ]]; then phpdbg -qrr vendor/bin/phpunit -c tests/ --coverage-clover ./build/logs/clover.xml;
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

after_success:
- if [[ ${TEST_COVERAGE} ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi
Expand Down
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
"symfony/yaml": "^3.4 || ^4.3 || ^5.0",
"symfony/phpunit-bridge": "^4.3 || ^5.0",
"symfony/process": "^3.4 || ^4.3 || ^5.0",
"phpstan/phpstan": "^0.12"
},
"conflict": {
"behat/behat": "<3.0"
},
"autoload": {
"psr-4": {
"DAMA\\DoctrineTestBundle\\": "src/DAMA/DoctrineTestBundle"
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,11 @@

namespace Tests\Functional;

use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;

class FunctionalTest extends TestCase
class PhpunitTest extends TestCase
{
/**
* @var KernelInterface
*/
private $kernel;

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

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

protected function tearDown(): void
{
$this->kernel->shutdown();
}

private function assertRowCount($count): void
{
$this->assertEquals($count, $this->connection->fetchColumn('SELECT COUNT(*) FROM test'));
}

private function insertRow(): void
{
$this->connection->insert('test', [
'test' => 'foo',
]);
}
use FunctionalTestTrait;

public function testChangeDbState(): void
{
Expand All @@ -49,6 +15,9 @@ public function testChangeDbState(): void
$this->assertRowCount(1);
}

/**
* @depends testChangeDbState
*/
public function testPreviousChangesAreRolledBack(): void
{
$this->assertRowCount(0);
Expand All @@ -58,18 +27,21 @@ public function testChangeDbStateWithinTransaction(): void
{
$this->assertRowCount(0);

$this->connection->beginTransaction();
$this->beginTransaction();
$this->insertRow();
$this->assertRowCount(1);
$this->connection->rollBack();
$this->rollbackTransaction();
$this->assertRowCount(0);

$this->connection->beginTransaction();
$this->beginTransaction();
$this->insertRow();
$this->connection->commit();
$this->commitTransaction();
$this->assertRowCount(1);
}

/**
* @depends testChangeDbStateWithinTransaction
*/
public function testPreviousChangesAreRolledBackAfterTransaction(): void
{
$this->assertRowCount(0);
Expand All @@ -78,14 +50,17 @@ public function testPreviousChangesAreRolledBackAfterTransaction(): void
public function testChangeDbStateWithSavePoint(): void
{
$this->assertRowCount(0);
$this->connection->createSavepoint('foo');
$this->createSavepoint('foo');
$this->insertRow();
$this->assertRowCount(1);
$this->connection->rollbackSavepoint('foo');
$this->rollbackSavepoint('foo');
$this->assertRowCount(0);
$this->insertRow();
}

/**
* @depends testChangeDbStateWithSavePoint
*/
public function testPreviousChangesAreRolledBackAfterUsingSavePoint(): void
{
$this->assertRowCount(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Functional;
namespace Tests\Functional\app;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/Functional/app/parameters.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
db_host: 127.0.0.1
db_name: doctrine_test_bundle
db_user: root
db_password: ~
31 changes: 31 additions & 0 deletions tests/Functional/features/behat_integration.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feature:: Behat integration
In order to improve database usage in behat tests
developers need to be able to use DamaDoctrineTestBundle with Behat

Scenario: Changing DB state
Given there are 0 rows
When I insert a new row
Then there is 1 row

Scenario: Change db state within rolled back transaction
Given there are 0 rows
When I begin a transaction
And I insert a new row
Then there is 1 row
When I rollback the transaction
Then there are 0 rows

Scenario: Change db state within committed transaction
Given there are 0 rows
When I begin a transaction
And I insert a new row
And I commit the transaction
Then there is 1 row

Scenario: Change db state with savepoint
Given there are 0 rows
When I create a savepoint named "foo"
And I insert a new row
Then there is 1 row
When I rollback the savepoint named "foo"
Then there are 0 rows
22 changes: 22 additions & 0 deletions tests/Functional/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Behat\Behat\Context\Context;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use Tests\Functional\FunctionalTestTrait;

class FeatureContext implements Context
{
use FunctionalTestTrait;

/**
* @BeforeSuite
*/
public static function bootstrap(): void
{
$executableFinder = new PhpExecutableFinder();
$php = $executableFinder->find(false);

(new Process([$php, __DIR__.'/../../../bootstrap.php']))->mustRun();
}
}
10 changes: 10 additions & 0 deletions tests/behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
default:
autoload:
'': '%paths.base%/Functional/features/bootstrap'

suites:
functional:
paths: ['%paths.base%/Functional/features']

extensions:
DAMA\DoctrineTestBundle\Behat\ServiceContainer\DoctrineExtension: ~
2 changes: 1 addition & 1 deletion tests/phpunit.bootstrap.php → tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function bootstrap(): void
{
$kernel = new \Tests\Functional\AppKernel('test', true);
$kernel = new \Tests\Functional\app\AppKernel('test', true);
$kernel->boot();

$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stderr="true"
bootstrap="./phpunit.bootstrap.php"
bootstrap="./bootstrap.php"
>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0" />
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit7.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stderr="true"
bootstrap="./phpunit.bootstrap.php"
bootstrap="./bootstrap.php"
>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0" />
Expand Down

0 comments on commit 6c12782

Please sign in to comment.