From 6c1278267bc0865ea2b750fa9a39e65de5c4282e Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Sun, 4 Oct 2020 16:25:48 +0200 Subject: [PATCH] Added Behat tests --- .travis.yml | 3 +- composer.json | 4 +- tests/Functional/FunctionalTestTrait.php | 98 +++++++++++++++++++ .../{FunctionalTest.php => PhpunitTest.php} | 59 ++++------- tests/Functional/{ => app}/AppKernel.php | 2 +- tests/Functional/{ => app}/config.yml | 0 .../parameters.yml} | 0 tests/Functional/app/parameters.yml.dist | 5 + .../features/behat_integration.feature | 31 ++++++ .../features/bootstrap/FeatureContext.php | 22 +++++ tests/behat.yml | 10 ++ .../{phpunit.bootstrap.php => bootstrap.php} | 2 +- tests/phpunit.xml | 2 +- tests/phpunit7.xml | 2 +- 14 files changed, 190 insertions(+), 50 deletions(-) create mode 100644 tests/Functional/FunctionalTestTrait.php rename tests/Functional/{FunctionalTest.php => PhpunitTest.php} (54%) rename tests/Functional/{ => app}/AppKernel.php (94%) rename tests/Functional/{ => app}/config.yml (100%) rename tests/Functional/{parameters.yml.dist => app/parameters.yml} (100%) create mode 100644 tests/Functional/app/parameters.yml.dist create mode 100644 tests/Functional/features/behat_integration.feature create mode 100644 tests/Functional/features/bootstrap/FeatureContext.php create mode 100644 tests/behat.yml rename tests/{phpunit.bootstrap.php => bootstrap.php} (92%) diff --git a/.travis.yml b/.travis.yml index 716cd99..5122bfb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,7 +40,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 - make phpstan php_cs_fixer_check - | @@ -48,6 +48,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 after_success: - if [[ ${TEST_COVERAGE} ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi diff --git a/composer.json b/composer.json index b965c97..4fb38d9 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/tests/Functional/FunctionalTestTrait.php b/tests/Functional/FunctionalTestTrait.php new file mode 100644 index 0000000..9016867 --- /dev/null +++ b/tests/Functional/FunctionalTestTrait.php @@ -0,0 +1,98 @@ +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); + } +} diff --git a/tests/Functional/FunctionalTest.php b/tests/Functional/PhpunitTest.php similarity index 54% rename from tests/Functional/FunctionalTest.php rename to tests/Functional/PhpunitTest.php index 5fadf39..af51b15 100644 --- a/tests/Functional/FunctionalTest.php +++ b/tests/Functional/PhpunitTest.php @@ -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 { @@ -49,6 +15,9 @@ public function testChangeDbState(): void $this->assertRowCount(1); } + /** + * @depends testChangeDbState + */ public function testPreviousChangesAreRolledBack(): void { $this->assertRowCount(0); @@ -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); @@ -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); diff --git a/tests/Functional/AppKernel.php b/tests/Functional/app/AppKernel.php similarity index 94% rename from tests/Functional/AppKernel.php rename to tests/Functional/app/AppKernel.php index b893515..b003b83 100644 --- a/tests/Functional/AppKernel.php +++ b/tests/Functional/app/AppKernel.php @@ -1,6 +1,6 @@ find(false); + + (new Process([$php, __DIR__.'/../../../bootstrap.php']))->mustRun(); + } +} diff --git a/tests/behat.yml b/tests/behat.yml new file mode 100644 index 0000000..28039e1 --- /dev/null +++ b/tests/behat.yml @@ -0,0 +1,10 @@ +default: + autoload: + '': '%paths.base%/Functional/features/bootstrap' + + suites: + functional: + paths: ['%paths.base%/Functional/features'] + + extensions: + DAMA\DoctrineTestBundle\Behat\ServiceContainer\DoctrineExtension: ~ diff --git a/tests/phpunit.bootstrap.php b/tests/bootstrap.php similarity index 92% rename from tests/phpunit.bootstrap.php rename to tests/bootstrap.php index db0e0d7..134ce60 100644 --- a/tests/phpunit.bootstrap.php +++ b/tests/bootstrap.php @@ -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); diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 89f965d..3a2224c 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -9,7 +9,7 @@ convertWarningsToExceptions="true" processIsolation="false" stderr="true" - bootstrap="./phpunit.bootstrap.php" + bootstrap="./bootstrap.php" > diff --git a/tests/phpunit7.xml b/tests/phpunit7.xml index 9dd0754..7a13cc5 100644 --- a/tests/phpunit7.xml +++ b/tests/phpunit7.xml @@ -9,7 +9,7 @@ convertWarningsToExceptions="true" processIsolation="false" stderr="true" - bootstrap="./phpunit.bootstrap.php" + bootstrap="./bootstrap.php" >