Skip to content

Commit 1d544e9

Browse files
committed
Introduce DB URL as configuration option #10
1 parent 8c4609a commit 1d544e9

7 files changed

+123
-70
lines changed

phpunit-integration.xml.dist

+15-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
<phpunit
2-
bootstrap="tests/bootstrap.dist.php"
3-
backupGlobals="false"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
>
9-
<php>
10-
<const name="WpTestsStarter\Test\WpIntegration\Db\USER" value="db" />
11-
<const name="WpTestsStarter\Test\WpIntegration\Db\NAME" value="test" />
12-
<const name="WpTestsStarter\Test\WpIntegration\Db\PASSWORD" value="db" />
13-
<const name="WpTestsStarter\Test\WpIntegration\Db\HOST" value="db" />
14-
<const name="WpTestsStarter\Test\WpIntegration\Db\CHARSET" value="utf8" />
15-
<const name="WpTestsStarter\Test\WpIntegration\Db\COLLATE" value="" />
16-
<const name="WpTestsStarter\Test\WpIntegration\Db\TABLE_PREFIX" value="wp_test_" />
17-
</php>
18-
<testsuites>
19-
<testsuite name="WpIntegrationTests">
20-
<directory suffix="Test.php">./tests/WpIntegration</directory>
21-
</testsuite>
22-
</testsuites>
2+
bootstrap="tests/bootstrap.dist.php"
3+
backupGlobals="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
>
9+
<php>
10+
<env name="WPTS_DB_URL" value="mysql://db:db@db/test?table_prefix=wp_test_&amp;charset=utf8" />
11+
</php>
12+
<testsuites>
13+
<testsuite name="WpIntegrationTests">
14+
<directory suffix="Test.php">./tests/WpIntegration</directory>
15+
</testsuite>
16+
</testsuites>
2317
</phpunit>

phpunit.xml.dist

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<phpunit
2-
bootstrap="tests/bootstrap.dist.php"
3-
backupGlobals="false"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
>
9-
<testsuites>
10-
<testsuite name="UnitTests">
11-
<directory suffix="Test.php">./tests/Unit</directory>
12-
</testsuite>
13-
</testsuites>
2+
bootstrap="tests/bootstrap.dist.php"
3+
backupGlobals="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
>
9+
<testsuites>
10+
<testsuite name="UnitTests">
11+
<directory suffix="Test.php">./tests/Unit</directory>
12+
</testsuite>
13+
</testsuites>
1414
</phpunit>

src/WpTestsStarter/Helper/DbUrlParser.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DbUrlParser
1010
{
1111

1212
/**
13-
* Parses a URL like mysql://user:password@host/database?table_prefix=wp_&charset=utf8mb4
13+
* Parses a URL like mysql://user:password@host/database?table_prefix=wp_&charset=utf8mb4&collation=utf8_general_ci
1414
* all parts are optional
1515
*
1616
* @return array{
@@ -19,7 +19,8 @@ class DbUrlParser
1919
* password: ?string,
2020
* db: ?string,
2121
* table_prefix: ?string,
22-
* charset: ?string
22+
* charset: ?string,
23+
* collation: ?string
2324
* }
2425
*/
2526
public function parse(string $dbUrl): array
@@ -31,6 +32,7 @@ public function parse(string $dbUrl): array
3132
'db' => null,
3233
'table_prefix' => null,
3334
'charset' => null,
35+
'collation' => null,
3436
];
3537

3638
$parts = parse_url($dbUrl);
@@ -56,6 +58,7 @@ public function parse(string $dbUrl): array
5658

5759
array_key_exists('table_prefix', $query) and $credentials['table_prefix'] = $query['table_prefix'];
5860
array_key_exists('charset', $query) and $credentials['charset'] = $query['charset'];
61+
array_key_exists('collation', $query) and $credentials['collation'] = $query['collation'];
5962

6063
return $credentials;
6164
}

src/WpTestsStarter/WpTestsStarter.php

+29-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WpTestsStarter;
66

7+
use WpTestsStarter\Helper\DbUrlParser;
78
use WpTestsStarter\Helper\SaltGenerator;
89

910
class WpTestsStarter
@@ -12,6 +13,8 @@ class WpTestsStarter
1213

1314
private SaltGenerator $saltGenerator;
1415

16+
private DbUrlParser $dbUrlParser;
17+
1518
/**
1619
* @var callable[]
1720
*/
@@ -22,10 +25,15 @@ class WpTestsStarter
2225
/**
2326
* @param string $baseDir Absolute path to the wordpress-develop repository
2427
*/
25-
public function __construct(string $baseDir, ?SaltGenerator $saltGenerator = null)
26-
{
28+
public function __construct(
29+
string $baseDir,
30+
?string $dbUrl = '',
31+
?SaltGenerator $saltGenerator = null,
32+
?DbUrlParser $dbUrlParser = null
33+
) {
2734
$this->baseDir = rtrim($baseDir, '\\/');
2835
$this->saltGenerator = $saltGenerator ?? new SaltGenerator();
36+
$this->dbUrlParser = $dbUrlParser ?? new DbUrlParser();
2937

3038
// set some common defaults
3139
$this->useDbHost('localhost')
@@ -34,8 +42,10 @@ public function __construct(string $baseDir, ?SaltGenerator $saltGenerator = nul
3442
->useSiteTitle('Wp Tests Starter')
3543
->usePhpBinary('/usr/bin/php')
3644
->useTablePrefix('wp_tests_')
37-
->useAbspath($this->baseDir . '/src/')
45+
->useAbsPath($this->baseDir . '/src/')
3846
->generateSalts();
47+
48+
$dbUrl and $this->useDbUrl($dbUrl);
3949
}
4050

4151
/**
@@ -67,7 +77,22 @@ public function useGlobalVar(string $var, $value): self
6777
return $this;
6878
}
6979

70-
public function useAbspath(?string $abspath = null): self
80+
public function useDbUrl(string $dbUrl): self
81+
{
82+
$credentials = $this->dbUrlParser->parse($dbUrl);
83+
84+
$credentials['host'] and $this->useDbHost($credentials['host']);
85+
$credentials['user'] and $this->useDbUser($credentials['user']);
86+
$credentials['password'] and $this->useDbPassword($credentials['password']);
87+
$credentials['db'] and $this->useDbName($credentials['db']);
88+
$credentials['table_prefix'] and $this->useTablePrefix($credentials['table_prefix']);
89+
$credentials['charset'] and $this->useDbCharset($credentials['charset']);
90+
$credentials['collation'] and $this->useDbCollation($credentials['collation']);
91+
92+
return $this;
93+
}
94+
95+
public function useAbsPath(?string $abspath = null): self
7196
{
7297
return $this->useConst('ABSPATH', $abspath);
7398
}

tests/Unit/Helper/DbUrlParserTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ public function parseData(): iterable
3030
{
3131

3232
yield 'Test entire parameter set' => [
33-
'dbUrl' => 'mysql://john:T0p3ecr3t@localhost/wordpress?table_prefix=wp_tests_&charset=iso88591',
33+
'dbUrl' => 'mysql://john:T0p3ecr3t@localhost/wordpress?table_prefix=wp_tests_&charset=iso88591&collation=latin1_swedish_ci',
3434
'expected' => [
3535
'user' => 'john',
3636
'host' => 'localhost',
3737
'password' => 'T0p3ecr3t',
3838
'db' => 'wordpress',
3939
'table_prefix' => 'wp_tests_',
4040
'charset' => 'iso88591',
41+
'collation' => 'latin1_swedish_ci'
4142
]
4243
];
4344

@@ -50,6 +51,7 @@ public function parseData(): iterable
5051
'db' => null,
5152
'table_prefix' => null,
5253
'charset' => null,
54+
'collation' => null,
5355
]
5456
];
5557

@@ -62,6 +64,7 @@ public function parseData(): iterable
6264
'db' => null,
6365
'table_prefix' => null,
6466
'charset' => null,
67+
'collation' => null,
6568
]
6669
];
6770
}

tests/WpIntegration/WpTestsStarterDefaultConstantsTest.php

+54-12
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,75 @@
55
namespace WpTestsStarter\Test\WpIntegration;
66

77
use PHPUnit\Framework\TestCase;
8+
use WpTestsStarter\Helper\DbUrlParser;
89
use WpTestsStarter\WpTestsStarter;
910

1011
class WpTestsStarterDefaultConstantsTest extends TestCase
1112
{
1213
/**
1314
* @runInSeparateProcess
1415
*/
15-
public function testBoostrapWithDefaultConstants(): void
16+
public function testBoostrapWithDbUrl(): void
1617
{
1718
$baseDir = dirname(dirname(__DIR__)) . '/vendor/wordpress/wordpress';
1819
$wpTestConfig = $baseDir . '/wp-tests-config.php';
19-
$testee = new WpTestsStarter($baseDir);
20+
$testee = new WpTestsStarter(
21+
$baseDir,
22+
getenv('WPTS_DB_URL')
23+
);
24+
$testee->bootstrap();
2025

21-
// defined in phpunit-integration.xml
22-
$testee->useDbName(Db\NAME)
23-
->useDbUser(Db\USER)
24-
->useDbCharset(Db\USER)
25-
->useDbPassword(Db\PASSWORD)
26-
->useDbHost(Db\HOST)
27-
->useDbCharset(Db\CHARSET)
28-
->useDbCollation(Db\COLLATE)
29-
->useTablePrefix(Db\TABLE_PREFIX);
26+
$this->assertFileExists($wpTestConfig);
27+
$config_data = file_get_contents($wpTestConfig);
3028

29+
self::assertMatchesRegularExpression(
30+
'~define\(\s\'ABSPATH\',\s\'[^\']+\'~',
31+
$config_data
32+
);
33+
self::assertInstanceOf(
34+
\wpdb::class,
35+
$GLOBALS['wpdb']
36+
);
37+
self::assertTrue(
38+
$GLOBALS['wpdb']->check_connection()
39+
);
40+
}
41+
42+
/**
43+
* @runInSeparateProcess
44+
*/
45+
public function testBoostrapWithSetters(): void
46+
{
47+
$baseDir = dirname(dirname(__DIR__)) . '/vendor/wordpress/wordpress';
48+
$wpTestConfig = $baseDir . '/wp-tests-config.php';
49+
50+
$credentials = (new DbUrlParser())
51+
->parse(getenv('WPTS_DB_URL'));
52+
53+
$testee = new WpTestsStarter($baseDir);
54+
55+
$credentials['host'] and $testee->useDbHost($credentials['host']);
56+
$credentials['user'] and $testee->useDbUser($credentials['user']);
57+
$credentials['password'] and $testee->useDbPassword($credentials['password']);
58+
$credentials['db'] and $testee->useDbName($credentials['db']);
59+
$credentials['table_prefix'] and $testee->useTablePrefix($credentials['table_prefix']);
60+
$credentials['charset'] and $testee->useDbCharset($credentials['charset']);
61+
$credentials['collation'] and $testee->useDbCollation($credentials['collation']);
3162
$testee->bootstrap();
3263

3364
$this->assertFileExists($wpTestConfig);
3465
$config_data = file_get_contents($wpTestConfig);
35-
self::assertMatchesRegularExpression('~define\(\s\'ABSPATH\',\s\'[^\']+\'~', $config_data);
66+
67+
self::assertMatchesRegularExpression(
68+
'~define\(\s\'ABSPATH\',\s\'[^\']+\'~',
69+
$config_data
70+
);
71+
self::assertInstanceOf(
72+
\wpdb::class,
73+
$GLOBALS['wpdb']
74+
);
75+
self::assertTrue(
76+
$GLOBALS['wpdb']->check_connection()
77+
);
3678
}
3779
}

tests/WpIntegration/WpTestsStarterTest.php

+4-18
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
use PHPUnit\Framework\TestCase;
88
use WpTestsStarter\WpTestsStarter;
99

10-
/**
11-
* Class WpTestsStarterTest
12-
*
13-
* This test has to be in a completely different environment as the Constants are alredy
14-
* defined by during unit tests
15-
*
16-
* @package WpTestsStarter\Test\Integration
17-
*/
1810
class WpTestsStarterTest extends TestCase
1911
{
2012
private static string $baseDir;
@@ -24,16 +16,10 @@ class WpTestsStarterTest extends TestCase
2416
public static function setUpBeforeClass(): void
2517
{
2618
self::$baseDir = dirname(dirname(__DIR__)) . '/vendor/wordpress/wordpress';
27-
self::$testee = new WpTestsStarter(self::$baseDir);
28-
29-
// defined in phpunit-integration.xml
30-
self::$testee->useDbName(Db\NAME);
31-
self::$testee->useDbUser(Db\USER);
32-
self::$testee->useDbPassword(Db\PASSWORD);
33-
self::$testee->useDbHost(Db\HOST);
34-
self::$testee->useDbCharset(Db\CHARSET);
35-
self::$testee->useDbCollation(Db\COLLATE);
36-
self::$testee->useTablePrefix(Db\TABLE_PREFIX);
19+
self::$testee = new WpTestsStarter(
20+
self::$baseDir,
21+
getenv('WPTS_DB_URL')
22+
);
3723

3824
// test plugin loading
3925
$plugin_test_dir = dirname(__DIR__) . '/tmp';

0 commit comments

Comments
 (0)