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

Commit 8f992eb

Browse files
authored
Merge pull request #5 from platformsh/fix-local
Fix local
2 parents c353813 + e8cba28 commit 8f992eb

File tree

6 files changed

+234
-36
lines changed

6 files changed

+234
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
composer.lock

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313
"autoload": {
1414
"files": ["platformsh-flex-env.php"]
1515
},
16+
"autoload-dev": {
17+
"psr-4": {
18+
"Platformsh\\FlexBridge\\Tests\\": "tests/"
19+
}
20+
},
1621
"extra": {
1722
"branch-alias": {
1823
"dev-master": "1.0.x-dev"
1924
}
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^7.0"
2028
}
2129
}

phpunit.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0/phpunit.xsd"
4+
backupGlobals="true"
5+
backupStaticAttributes="false"
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
processIsolation="true"
9+
stopOnError="false"
10+
stopOnFailure="false"
11+
stopOnIncomplete="false"
12+
stopOnSkipped="false"
13+
stopOnRisky="false"
14+
timeoutForSmallTests="1"
15+
timeoutForMediumTests="10"
16+
timeoutForLargeTests="60"
17+
verbose="false">
18+
19+
<testsuites>
20+
<testsuite name="Flex Bridge">
21+
<directory>tests</directory>
22+
</testsuite>
23+
</testsuites>
24+
</phpunit>

platformsh-flex-env.php

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,66 @@
1212
*/
1313
function mapPlatformShEnvironment() : void
1414
{
15+
// If this env var is not set then we're not on a Platform.sh
16+
// environment or in the build hook, so don't try to do anything.
17+
if (!getenv('PLATFORM_APPLICATION')) {
18+
return;
19+
}
20+
1521
// Set the application secret if it's not already set.
16-
if (!isset($_SERVER['APP_SECRET']) && isset($_SERVER['PLATFORM_PROJECT_ENTROPY'])) {
17-
$_SERVER['APP_SECRET'] = $_SERVER['PLATFORM_PROJECT_ENTROPY'];
22+
if (!isset($_SERVER['APP_SECRET']) && getenv('PLATFORM_PROJECT_ENTROPY')) {
23+
$_SERVER['APP_SECRET'] = getenv('PLATFORM_PROJECT_ENTROPY');
1824
}
1925

2026
// Default to production. You can override this value by setting
2127
// `env:APP_ENV` as a project variable, or by adding it to the
2228
// .platform.app.yaml variables block.
23-
if (!isset($_SERVER['APP_ENV'])) {
24-
$_SERVER['APP_ENV'] = 'prod';
25-
}
29+
$_SERVER['APP_ENV'] = $_SERVER['APP_ENV'] ?? (getenv('APP_ENV') ?: null) ?? 'prod';
2630

27-
if (isset($_SERVER['DATABASE_URL'])) {
28-
return;
31+
if (!isset($_SERVER['DATABASE_URL'])) {
32+
mapPlatformShDatabase();
2933
}
34+
}
35+
36+
function mapPlatformShDatabase() : void
37+
{
38+
$dbRelationshipName = 'database';
3039

3140
// Set the DATABASE_URL for Doctrine, if necessary.
3241
# "mysql://[email protected]:3306/symfony?charset=utf8mb4&serverVersion=5.7";
33-
if (isset($_SERVER['PLATFORM_RELATIONSHIPS'])) {
34-
$relationships = json_decode(base64_decode($_SERVER['PLATFORM_RELATIONSHIPS']), true);
35-
foreach ($relationships['database'] as $endpoint) {
36-
if (empty($endpoint['query']['is_master'])) {
37-
continue;
38-
}
42+
if (getenv('PLATFORM_RELATIONSHIPS')) {
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+
}
3949

40-
$dbUrl = sprintf(
41-
'%s://%s:%s@%s:%d/%s',
42-
$endpoint['scheme'],
43-
$endpoint['username'],
44-
$endpoint['password'],
45-
$endpoint['host'],
46-
$endpoint['port'],
47-
$endpoint['path']
48-
);
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+
);
4959

50-
switch ($endpoint['scheme']) {
51-
case 'mysql':
52-
// Defaults to the latest MariaDB version
53-
$dbUrl .= '?serverVersion=10.2&charset=utf8mb4';
54-
break;
55-
56-
case 'pgsql':
57-
// Postgres 9.6 is the latest supported version on Platform.sh
58-
$dbUrl .= '?serverVersion=9.6';
59-
}
60+
switch ($endpoint['scheme']) {
61+
case 'mysql':
62+
// Defaults to the latest MariaDB version
63+
$dbUrl .= '?charset=utf8mb4&serverVersion=10.2';
64+
break;
6065

61-
$_SERVER['DATABASE_URL'] = $dbUrl;
66+
case 'pgsql':
67+
// Postgres 9.6 is the latest supported version on Platform.sh
68+
$dbUrl .= '?serverVersion=9.6';
69+
}
6270

63-
return;
71+
$_SERVER['DATABASE_URL'] = $dbUrl;
72+
return;
73+
}
6474
}
65-
66-
return;
6775
}
6876

6977
// Hack the Doctrine URL to be syntactically valid in a build hook, even

tests/FlexBridgeDatabaseTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
[
22+
'scheme' => 'mysql',
23+
'username' => 'user',
24+
'password' => '',
25+
'host' => 'database.internal',
26+
'port' => '3306',
27+
'path' => 'main',
28+
'query' => ['is_master' => true],
29+
]
30+
]
31+
];
32+
33+
$this->defaultDbUrl = sprintf(
34+
'%s://%s:%s@%s:%s/%s?charset=utf8mb4&serverVersion=10.2',
35+
'mysql',
36+
'',
37+
'',
38+
'localhost',
39+
3306,
40+
''
41+
);
42+
}
43+
44+
public function testNotOnPlatformshDoesNotSetDatabase() : void
45+
{
46+
mapPlatformShEnvironment();
47+
48+
$this->assertArrayNotHasKey('DATABASE_URL', $_SERVER);
49+
}
50+
51+
public function testNoRelationships() : void
52+
{
53+
// We assume no relationships array, but a PLATFORM_APPLICATION env var,
54+
// means we're in a build hook.
55+
56+
putenv('PLATFORM_APPLICATION=test');
57+
58+
//putenv(sprintf('PLATFORM_RELATIONSHIPS=%s', base64_encode(json_encode($this->relationships))));
59+
60+
mapPlatformShEnvironment();
61+
62+
$this->assertEquals($this->defaultDbUrl, $_SERVER['DATABASE_URL']);
63+
}
64+
65+
public function testNoDatabaseRelationship() : void
66+
{
67+
putenv('PLATFORM_APPLICATION=test');
68+
69+
$rels = $this->relationships;
70+
unset($rels['database']);
71+
72+
putenv(sprintf('PLATFORM_RELATIONSHIPS=%s', base64_encode(json_encode($rels))));
73+
74+
mapPlatformShEnvironment();
75+
76+
$this->assertEquals($this->defaultDbUrl, $_SERVER['DATABASE_URL']);
77+
}
78+
79+
public function testDatabaseRelationshipSet() : void
80+
{
81+
putenv('PLATFORM_APPLICATION=test');
82+
putenv(sprintf('PLATFORM_RELATIONSHIPS=%s', base64_encode(json_encode($this->relationships))));
83+
84+
mapPlatformShEnvironment();
85+
86+
$this->assertEquals('mysql://user:@database.internal:3306/main?charset=utf8mb4&serverVersion=10.2', $_SERVER['DATABASE_URL']);
87+
}
88+
89+
}

tests/FlexBridgeTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Platformsh\FlexBridge\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
8+
class FlexBridgeTest extends TestCase
9+
{
10+
11+
public function testDoesNotRunWithoutPlatformshVariables() : void
12+
{
13+
mapPlatformShEnvironment();
14+
15+
$this->assertFalse(getenv('APP_SECRET'));
16+
}
17+
18+
public function testSetAppSecret() : void
19+
{
20+
putenv('PLATFORM_APPLICATION=test');
21+
putenv('PLATFORM_PROJECT_ENTROPY=test');
22+
23+
mapPlatformShEnvironment();
24+
25+
$this->assertEquals('test', $_SERVER['APP_SECRET']);
26+
}
27+
28+
public function testDontChangeAppSecret() : void
29+
{
30+
putenv('PLATFORM_APPLICATION=test');
31+
putenv('PLATFORM_PROJECT_ENTROPY=test');
32+
$_SERVER['APP_SECRET'] = 'original';
33+
34+
mapPlatformShEnvironment();
35+
36+
$this->assertEquals('original', $_SERVER['APP_SECRET']);
37+
}
38+
39+
public function testAppEnvAlreadySetInServer() : void
40+
{
41+
putenv('PLATFORM_APPLICATION=test');
42+
$_SERVER['APP_ENV'] = 'dev';
43+
44+
mapPlatformShEnvironment();
45+
46+
$this->assertEquals('dev', $_SERVER['APP_ENV']);
47+
}
48+
49+
public function testAppEnvAlreadySetInEnv() : void
50+
{
51+
putenv('PLATFORM_APPLICATION=test');
52+
putenv('APP_ENV=dev');
53+
54+
mapPlatformShEnvironment();
55+
56+
$this->assertEquals('dev', $_SERVER['APP_ENV']);
57+
}
58+
59+
public function testAppEnvNeedsDefault() : void
60+
{
61+
putenv('PLATFORM_APPLICATION=test');
62+
63+
mapPlatformShEnvironment();
64+
65+
$this->assertEquals('prod', $_SERVER['APP_ENV']);
66+
}
67+
68+
}

0 commit comments

Comments
 (0)