Skip to content

Commit b9e0a15

Browse files
committed
Added Behat tests
1 parent aba4f86 commit b9e0a15

File tree

14 files changed

+190
-50
lines changed

14 files changed

+190
-50
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ install:
4646
- composer require phpunit/phpunit:${PHPUNIT_VERSION:-"8.*"} --update-with-all-dependencies
4747

4848
script:
49-
- cp tests/Functional/parameters.yml.dist tests/Functional/parameters.yml
49+
- cp tests/Functional/app/parameters.yml.dist tests/Functional/app/parameters.yml
5050
- rm -rf tests/Functional/cache
5151
- if [[ ${SKIP_CS_FIXER} != "1" ]]; then make php_cs_fixer_check; fi
5252
- make phpstan
@@ -55,6 +55,7 @@ script:
5555
elif [[ ${PHPUNIT_VERSION} == "7.*" ]]; then vendor/bin/phpunit -c tests/phpunit7.xml tests/;
5656
else vendor/bin/phpunit -c tests/ tests/;
5757
fi
58+
- vendor/bin/behat -c tests/behat.yml
5859

5960
after_success:
6061
- if [[ ${TEST_COVERAGE} ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
2222
"symfony/yaml": "^3.4 || ^4.4 || ^5.1",
2323
"symfony/phpunit-bridge": "^5.1",
24+
"symfony/process": "^3.4 || ^4.3 || ^5.1",
2425
"phpstan/phpstan": "^0.12"
2526
},
26-
"conflict": {
27-
"behat/behat": "<3.0"
28-
},
2927
"autoload": {
3028
"psr-4": {
3129
"DAMA\\DoctrineTestBundle\\": "src/DAMA/DoctrineTestBundle"
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Tests\Functional;
4+
5+
use Doctrine\DBAL\Connection;
6+
use PHPUnit\Framework\Assert;
7+
use Symfony\Component\HttpKernel\KernelInterface;
8+
use Tests\Functional\app\AppKernel;
9+
10+
trait FunctionalTestTrait
11+
{
12+
/**
13+
* @var KernelInterface
14+
*/
15+
private $kernel;
16+
17+
/**
18+
* @var Connection
19+
*/
20+
protected $connection;
21+
22+
/**
23+
* @BeforeScenario
24+
*/
25+
public function setUp(): void
26+
{
27+
$this->kernel = new AppKernel('test', true);
28+
$this->kernel->boot();
29+
$this->connection = $this->kernel->getContainer()->get('doctrine.dbal.default_connection');
30+
}
31+
32+
/**
33+
* @AfterScenario
34+
*/
35+
public function tearDown(): void
36+
{
37+
$this->kernel->shutdown();
38+
}
39+
40+
/**
41+
* @Then there are :count rows
42+
* @Then there is :count row
43+
*/
44+
public function assertRowCount($count): void
45+
{
46+
Assert::assertEquals($count, $this->connection->fetchColumn('SELECT COUNT(*) FROM test'));
47+
}
48+
49+
/**
50+
* @When I insert a new row
51+
*/
52+
public function insertRow(): void
53+
{
54+
$this->connection->insert('test', [
55+
'test' => 'foo',
56+
]);
57+
}
58+
59+
/**
60+
* @When I begin a transaction
61+
*/
62+
public function beginTransaction(): void
63+
{
64+
$this->connection->beginTransaction();
65+
}
66+
67+
/**
68+
* @When I rollback the transaction
69+
*/
70+
public function rollbackTransaction(): void
71+
{
72+
$this->connection->rollBack();
73+
}
74+
75+
/**
76+
* @When I commit the transaction
77+
*/
78+
public function commitTransaction(): void
79+
{
80+
$this->connection->commit();
81+
}
82+
83+
/**
84+
* @When I create a savepoint named :name
85+
*/
86+
public function createSavepoint(string $name): void
87+
{
88+
$this->connection->createSavepoint($name);
89+
}
90+
91+
/**
92+
* @When I rollback the savepoint named :name
93+
*/
94+
public function rollbackSavepoint(string $name): void
95+
{
96+
$this->connection->rollbackSavepoint($name);
97+
}
98+
}

tests/Functional/FunctionalTest.php renamed to tests/Functional/PhpunitTest.php

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,12 @@
22

33
namespace Tests\Functional;
44

5-
use Doctrine\DBAL\Connection;
65
use Doctrine\DBAL\Exception\TableNotFoundException;
76
use PHPUnit\Framework\TestCase;
8-
use Symfony\Component\HttpKernel\KernelInterface;
97

10-
class FunctionalTest extends TestCase
8+
class PhpunitTest extends TestCase
119
{
12-
/**
13-
* @var KernelInterface
14-
*/
15-
private $kernel;
16-
17-
/**
18-
* @var Connection
19-
*/
20-
private $connection;
21-
22-
protected function setUp(): void
23-
{
24-
$this->kernel = new AppKernel('test', true);
25-
$this->kernel->boot();
26-
$this->connection = $this->kernel->getContainer()->get('doctrine.dbal.default_connection');
27-
}
28-
29-
protected function tearDown(): void
30-
{
31-
$this->kernel->shutdown();
32-
}
33-
34-
private function assertRowCount($count): void
35-
{
36-
$this->assertEquals($count, $this->connection->fetchColumn('SELECT COUNT(*) FROM test'));
37-
}
38-
39-
private function insertRow(): void
40-
{
41-
$this->connection->insert('test', [
42-
'test' => 'foo',
43-
]);
44-
}
10+
use FunctionalTestTrait;
4511

4612
public function testChangeDbState(): void
4713
{
@@ -50,6 +16,9 @@ public function testChangeDbState(): void
5016
$this->assertRowCount(1);
5117
}
5218

19+
/**
20+
* @depends testChangeDbState
21+
*/
5322
public function testPreviousChangesAreRolledBack(): void
5423
{
5524
$this->assertRowCount(0);
@@ -59,18 +28,21 @@ public function testChangeDbStateWithinTransaction(): void
5928
{
6029
$this->assertRowCount(0);
6130

62-
$this->connection->beginTransaction();
31+
$this->beginTransaction();
6332
$this->insertRow();
6433
$this->assertRowCount(1);
65-
$this->connection->rollBack();
34+
$this->rollbackTransaction();
6635
$this->assertRowCount(0);
6736

68-
$this->connection->beginTransaction();
37+
$this->beginTransaction();
6938
$this->insertRow();
70-
$this->connection->commit();
39+
$this->commitTransaction();
7140
$this->assertRowCount(1);
7241
}
7342

43+
/**
44+
* @depends testChangeDbStateWithinTransaction
45+
*/
7446
public function testPreviousChangesAreRolledBackAfterTransaction(): void
7547
{
7648
$this->assertRowCount(0);
@@ -79,14 +51,17 @@ public function testPreviousChangesAreRolledBackAfterTransaction(): void
7951
public function testChangeDbStateWithSavePoint(): void
8052
{
8153
$this->assertRowCount(0);
82-
$this->connection->createSavepoint('foo');
54+
$this->createSavepoint('foo');
8355
$this->insertRow();
8456
$this->assertRowCount(1);
85-
$this->connection->rollbackSavepoint('foo');
57+
$this->rollbackSavepoint('foo');
8658
$this->assertRowCount(0);
8759
$this->insertRow();
8860
}
8961

62+
/**
63+
* @depends testChangeDbStateWithSavePoint
64+
*/
9065
public function testPreviousChangesAreRolledBackAfterUsingSavePoint(): void
9166
{
9267
$this->assertRowCount(0);

tests/Functional/AppKernel.php renamed to tests/Functional/app/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Functional;
3+
namespace Tests\Functional\app;
44

55
use Symfony\Component\Config\Loader\LoaderInterface;
66
use Symfony\Component\HttpKernel\Kernel;
File renamed without changes.
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
db_host: 127.0.0.1
3+
db_name: doctrine_test_bundle
4+
db_user: root
5+
db_password: ~
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Feature:: Behat integration
2+
In order to improve database usage in behat tests
3+
developers need to be able to use DamaDoctrineTestBundle with Behat
4+
5+
Scenario: Changing DB state
6+
Given there are 0 rows
7+
When I insert a new row
8+
Then there is 1 row
9+
10+
Scenario: Change db state within rolled back transaction
11+
Given there are 0 rows
12+
When I begin a transaction
13+
And I insert a new row
14+
Then there is 1 row
15+
When I rollback the transaction
16+
Then there are 0 rows
17+
18+
Scenario: Change db state within committed transaction
19+
Given there are 0 rows
20+
When I begin a transaction
21+
And I insert a new row
22+
And I commit the transaction
23+
Then there is 1 row
24+
25+
Scenario: Change db state with savepoint
26+
Given there are 0 rows
27+
When I create a savepoint named "foo"
28+
And I insert a new row
29+
Then there is 1 row
30+
When I rollback the savepoint named "foo"
31+
Then there are 0 rows
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Behat\Behat\Context\Context;
4+
use Symfony\Component\Process\PhpExecutableFinder;
5+
use Symfony\Component\Process\Process;
6+
use Tests\Functional\FunctionalTestTrait;
7+
8+
class FeatureContext implements Context
9+
{
10+
use FunctionalTestTrait;
11+
12+
/**
13+
* @BeforeSuite
14+
*/
15+
public static function bootstrap(): void
16+
{
17+
$executableFinder = new PhpExecutableFinder();
18+
$php = $executableFinder->find(false);
19+
20+
(new Process([$php, __DIR__.'/../../../bootstrap.php']))->mustRun();
21+
}
22+
}

0 commit comments

Comments
 (0)