Skip to content

Commit 22b3ea7

Browse files
committed
Improve error handling for missing property variables.
1 parent 7c521cc commit 22b3ea7

2 files changed

Lines changed: 42 additions & 32 deletions

File tree

src/Config.php

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -491,33 +491,28 @@ public function __get($property)
491491
// logic will change with it.
492492
$isUnprefixedVar = in_array($property, array_keys($this->unPrefixedVariablesRuntime));
493493

494-
if ($this->inBuild() && $isRuntimeVar) {
495-
throw new BuildTimeVariableAccessException(sprintf('The %s variable is not available during build time.', $property));
496-
}
497494

498495
if ($isBuildVar) {
499496
$value = $this->getValue($this->directVariables[$property]);
500-
if (is_null($value)) {
501-
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
502-
}
503-
return $value;
504497
}
505-
if ($isUnprefixedVar) {
498+
else if ($isUnprefixedVar) {
506499
$value = $this->environmentVariables[$this->unPrefixedVariablesRuntime[$property]] ?? null;
507-
if (is_null($value)) {
508-
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
509-
}
510-
return $value;
511500
}
512-
if ($isRuntimeVar) {
501+
else if ($isRuntimeVar) {
513502
$value = $this->getValue($this->directVariablesRuntime[$property]);
514-
if (is_null($value)) {
515-
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
503+
}
504+
else {
505+
throw new \InvalidArgumentException(sprintf('No such variable defined: %s', $property));
506+
}
507+
508+
if (is_null($value)) {
509+
if ($this->inBuild() && ($isRuntimeVar || $isUnprefixedVar)) {
510+
throw new BuildTimeVariableAccessException(sprintf('The %s variable is not available during build time.', $property));
516511
}
517-
return $value;
512+
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
518513
}
519514

520-
throw new \InvalidArgumentException(sprintf('No such variable defined: %s', $property));
515+
return $value;
521516
}
522517

523518
/**
@@ -531,21 +526,7 @@ public function __get($property)
531526
*/
532527
public function __isset($property)
533528
{
534-
$isBuildVar = in_array($property, array_keys($this->directVariables));
535-
$isRuntimeVar = in_array($property, array_keys($this->directVariablesRuntime));
536-
// For now, all unprefixed variables are also runtime variables. If that ever changes this
537-
// logic will change with it.
538-
$isUnprefixedVar = in_array($property, array_keys($this->unPrefixedVariablesRuntime));
539-
540-
if ($this->inBuild()) {
541-
return $isBuildVar && !is_null($this->$property);
542-
}
543-
544-
if ($isBuildVar || $isRuntimeVar || $isUnprefixedVar) {
545-
return !is_null($this->$property);
546-
}
547-
548-
return false;
529+
return !is_null($this->$property);
549530
}
550531

551532
/**

tests/ConfigTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,35 @@ public function test_build_and_deploy_properties_in_deploy_exists() : void
324324
$this->assertTrue(isset($config->socket));
325325
}
326326

327+
public function test_build_and_deploy_properties_mocked_in_local_exists() : void
328+
{
329+
$env = $this->mockEnvironmentDeploy;
330+
unset($env['PLATFORM_APPLICATION'], $env['PLATFORM_ENVIRONMENT'], $env['PLATFORM_BRANCH']);
331+
$config = new Config($env);
332+
333+
$this->assertEquals('/app', $config->appDir);
334+
$this->assertEquals('app', $config->applicationName);
335+
$this->assertEquals('test-project', $config->project);
336+
$this->assertEquals('abc123', $config->treeId);
337+
$this->assertEquals('def789', $config->projectEntropy);
338+
339+
$this->assertEquals('/app/web', $config->documentRoot);
340+
$this->assertEquals('1.2.3.4', $config->smtpHost);
341+
$this->assertEquals('8080', $config->port);
342+
$this->assertEquals('unix://tmp/blah.sock', $config->socket);
343+
344+
$this->assertTrue(isset($config->appDir));
345+
$this->assertTrue(isset($config->applicationName));
346+
$this->assertTrue(isset($config->project));
347+
$this->assertTrue(isset($config->treeId));
348+
$this->assertTrue(isset($config->projectEntropy));
349+
350+
$this->assertTrue(isset($config->documentRoot));
351+
$this->assertTrue(isset($config->smtpHost));
352+
$this->assertTrue(isset($config->port));
353+
$this->assertTrue(isset($config->socket));
354+
}
355+
327356
public function test_deploy_property_in_build_throws() : void
328357
{
329358
$this->expectException(BuildTimeVariableAccessException::class);

0 commit comments

Comments
 (0)