Skip to content
This repository was archived by the owner on Feb 15, 2023. It is now read-only.

Commit 3579f34

Browse files
committed
Improve error handling thanks to tests.
1 parent 5b45395 commit 3579f34

File tree

2 files changed

+107
-33
lines changed

2 files changed

+107
-33
lines changed

platformsh-flex-env.php

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,43 @@ function mapPlatformShEnvironment() : void
3535

3636
function mapPlatformShDatabase() : void
3737
{
38-
3938
$dbRelationshipName = 'database';
4039

4140
// Set the DATABASE_URL for Doctrine, if necessary.
4241
# "mysql://[email protected]:3306/symfony?charset=utf8mb4&serverVersion=5.7";
4342
if (getenv('PLATFORM_RELATIONSHIPS')) {
44-
$relationships = json_decode(base64_decode(getenv('PLATFORM_RELATIONSHIPS'), true));
45-
foreach ($relationships[$dbRelationshipName] as $endpoint) {
46-
if (empty($endpoint['query']['is_master'])) {
47-
continue;
48-
}
49-
50-
$dbUrl = sprintf(
51-
'%s://%s:%s@%s:%d/%s',
52-
$endpoint['scheme'],
53-
$endpoint['username'],
54-
$endpoint['password'],
55-
$endpoint['host'],
56-
$endpoint['port'],
57-
$endpoint['path']
58-
);
59-
60-
switch ($endpoint['scheme']) {
61-
case 'mysql':
62-
// Defaults to the latest MariaDB version
63-
$dbUrl .= '?serverVersion=10.2&charset=utf8mb4';
64-
break;
65-
66-
case 'pgsql':
67-
// Postgres 9.6 is the latest supported version on Platform.sh
68-
$dbUrl .= '?serverVersion=9.6';
43+
$relationships = json_decode(base64_decode(getenv('PLATFORM_RELATIONSHIPS'), true), true);
44+
if (isset($relationships[$dbRelationshipName])) {
45+
foreach ($relationships[$dbRelationshipName] as $endpoint) {
46+
if (empty($endpoint['query']['is_master'])) {
47+
continue;
48+
}
49+
50+
$dbUrl = sprintf(
51+
'%s://%s:%s@%s:%d/%s',
52+
$endpoint['scheme'],
53+
$endpoint['username'],
54+
$endpoint['password'],
55+
$endpoint['host'],
56+
$endpoint['port'],
57+
$endpoint['path']
58+
);
59+
60+
switch ($endpoint['scheme']) {
61+
case 'mysql':
62+
// Defaults to the latest MariaDB version
63+
$dbUrl .= '?serverVersion=10.2&charset=utf8mb4';
64+
break;
65+
66+
case 'pgsql':
67+
// Postgres 9.6 is the latest supported version on Platform.sh
68+
$dbUrl .= '?serverVersion=9.6';
69+
}
70+
71+
$_SERVER['DATABASE_URL'] = $dbUrl;
72+
return;
6973
}
70-
71-
$_SERVER['DATABASE_URL'] = $dbUrl;
72-
73-
return;
7474
}
75-
76-
return;
7775
}
7876

7977
// Hack the Doctrine URL to be syntactically valid in a build hook, even
@@ -89,5 +87,4 @@ function mapPlatformShDatabase() : void
8987
);
9088

9189
$_SERVER['DATABASE_URL'] = $dbUrl;
92-
9390
}

tests/FlexBridgeDatabaseTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Platformsh\FlexBridge\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
8+
class FlexBridgeDatabaseTest extends TestCase
9+
{
10+
11+
protected $relationships;
12+
13+
protected $defaultDbUrl;
14+
15+
public function setUp()
16+
{
17+
parent::setUp();
18+
19+
$this->relationships = [
20+
'database' => [
21+
'scheme' => 'mysql',
22+
'username' => 'user',
23+
'password' => '',
24+
'host' => 'database.internal',
25+
'port' => '3306',
26+
'path' => 'main',
27+
'query' => ['is_master' => true],
28+
]
29+
];
30+
31+
$this->defaultDbUrl = sprintf(
32+
'%s://%s:%s@%s:%s/%s?charset=utf8mb4&serverVersion=10.2',
33+
'mysql',
34+
'',
35+
'',
36+
'localhost',
37+
3306,
38+
''
39+
);
40+
}
41+
42+
public function testNotOnPlatformshDoesNotSetDatabase()
43+
{
44+
mapPlatformShEnvironment();
45+
46+
$this->assertArrayNotHasKey('DATABASE_URL', $_SERVER);
47+
}
48+
49+
public function testNoRelationships()
50+
{
51+
// We assume no relationships array, but a PLATFORM_APPLICATION env var,
52+
// means we're in a build hook.
53+
54+
putenv('PLATFORM_APPLICATION=test');
55+
56+
//putenv(sprintf('PLATFORM_RELATIONSHIPS=%s', base64_encode(json_encode($this->relationships))));
57+
58+
mapPlatformShEnvironment();
59+
60+
$this->assertEquals($this->defaultDbUrl, $_SERVER['DATABASE_URL']);
61+
}
62+
63+
public function testNoDatabaseRelationship()
64+
{
65+
putenv('PLATFORM_APPLICATION=test');
66+
67+
$rels = $this->relationships;
68+
unset($rels['database']);
69+
70+
putenv(sprintf('PLATFORM_RELATIONSHIPS=%s', base64_encode(json_encode($this->relationships))));
71+
72+
mapPlatformShEnvironment();
73+
74+
$this->assertEquals($this->defaultDbUrl, $_SERVER['DATABASE_URL']);
75+
}
76+
77+
}

0 commit comments

Comments
 (0)