Skip to content

Commit a85bd9d

Browse files
committed
Create IntegrationTestPHPUnitExtension.php
Signed-off-by: Nathanael Esayeas <[email protected]>
1 parent d6860fb commit a85bd9d

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaminasIntegrationTest\Db;
6+
7+
use LaminasIntegrationTest\Db\Platform\MysqlFixtureLoader;
8+
use LaminasIntegrationTest\Db\Platform\PgsqlFixtureLoader;
9+
use LaminasIntegrationTest\Db\Platform\SqlServerFixtureLoader;
10+
use PHPUnit\Runner\AfterLastTestHook;
11+
use PHPUnit\Runner\BeforeFirstTestHook;
12+
use PHPUnit\Runner\TestHook;
13+
14+
use function array_search;
15+
use function getenv;
16+
use function printf;
17+
use function trim;
18+
19+
class IntegrationTestPHPUnitExtension implements TestHook, BeforeFirstTestHook, AfterLastTestHook
20+
{
21+
private array $fixtureLoaders = [];
22+
23+
public function executeBeforeFirstTest(): void
24+
{
25+
if ($this->getPhpUnitParameter("testsuite") !== 'integration test') {
26+
return;
27+
}
28+
29+
if (getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL')) {
30+
$this->fixtureLoaders[] = new MysqlFixtureLoader();
31+
}
32+
33+
if (getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL')) {
34+
$this->fixtureLoaders[] = new PgsqlFixtureLoader();
35+
}
36+
37+
if (getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_SQLSRV')) {
38+
$this->fixtureLoaders[] = new SqlServerFixtureLoader();
39+
}
40+
41+
if (empty($this->fixtureLoaders)) {
42+
return;
43+
}
44+
45+
printf("\nIntegration test started.\n");
46+
47+
foreach ($this->fixtureLoaders as $fixtureLoader) {
48+
$fixtureLoader->createDatabase();
49+
}
50+
}
51+
52+
public function executeAfterLastTest(): void
53+
{
54+
if (
55+
$this->getPhpUnitParameter("testsuite") === 'integration test' ||
56+
$this->fixtureLoaders === []
57+
) {
58+
return;
59+
}
60+
61+
printf("\nIntegration test ended.\n");
62+
63+
foreach ($this->fixtureLoaders as $fixtureLoader) {
64+
$fixtureLoader->dropDatabase();
65+
}
66+
}
67+
68+
/**
69+
* Resolves the parameters passed in to PHPUnit.
70+
*
71+
* eg. "phpunit --testsuite Unit --filter FirstTest"
72+
*
73+
* $this->getPhpUnitParameter("filter"); // Unit
74+
* $this->getPhpUnitParameter("testsuite"); // FirstTest
75+
*/
76+
private function getPhpUnitParameter(string $paramName): ?string
77+
{
78+
global $argv;
79+
80+
if ($offset = array_search("--$paramName", $argv) === false) {
81+
return null;
82+
}
83+
84+
return trim($argv[$offset + 1]);
85+
}
86+
}

0 commit comments

Comments
 (0)