Skip to content

Commit

Permalink
Merge pull request #113 from City-of-Helsinki/UHF-X-make-sure-config-…
Browse files Browse the repository at this point in the history
…is-null-or-string

Make sure EnvironmentResolver::getConfig() always returns string or null
  • Loading branch information
tuutti authored Jun 6, 2023
2 parents 6c622a9 + 1a34921 commit a241d09
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Environment/EnvironmentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function getProjects() : array {
private function getConfig(string $key) : ?string {
return $this->configFactory
->get('helfi_api_base.environment_resolver.settings')
->get($key);
->get($key) ?: NULL;
}

/**
Expand Down
41 changes: 35 additions & 6 deletions tests/src/Unit/Environment/EnvironmentResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace Drupal\Tests\helfi_api_base\Unit\Environment;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\helfi_api_base\Environment\Environment;
use Drupal\helfi_api_base\Environment\EnvironmentResolver;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

/**
* Tests environment resolver value objects.
Expand All @@ -18,18 +21,20 @@
*/
class EnvironmentResolverTest extends UnitTestCase {

use ProphecyTrait;

/**
* Constructs a new config factory instance.
*
* @param string|null $projectName
* @param mixed $projectName
* The project name.
* @param string|null $envName
* @param mixed $envName
* The environment name.
*
* @return \Drupal\Core\Config\ConfigFactoryInterface
* The config factory stub.
*/
private function getConfigStub(string $projectName = NULL, string $envName = NULL) : ConfigFactoryInterface {
private function getConfigStub(mixed $projectName = NULL, mixed $envName = NULL) : ConfigFactoryInterface {
$config = [];

if ($projectName) {
Expand All @@ -49,7 +54,7 @@ private function getConfigStub(string $projectName = NULL, string $envName = NUL
* @return \Drupal\helfi_api_base\Environment\EnvironmentResolver
* The sut.
*/
private function getEnvironmentResolver(string $projectName = NULL, string $envName = NULL) : EnvironmentResolver {
private function getEnvironmentResolver(mixed $projectName = NULL, mixed $envName = NULL) : EnvironmentResolver {
$configStub = $this->getConfigStub($projectName, $envName);
return new EnvironmentResolver(__DIR__ . '/../../../fixtures/environments.json', $configStub);
}
Expand Down Expand Up @@ -243,11 +248,35 @@ public function environmentMapData() : array {
* @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray
* @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray
* @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct
* @dataProvider activeProjectExceptionData
*/
public function testGetActiveProjectException() : void {
public function testGetActiveProjectException(mixed $value) : void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/^No active project found./');
$this->getEnvironmentResolver()->getActiveProject();

// Construct config mock manually because ::getConfigStub() will never
// return boolean.
$config = $this->prophesize(ImmutableConfig::class);
$config->get(Argument::any())->willReturn($value);
$configFactory = $this->prophesize(ConfigFactoryInterface::class);
$configFactory->get(Argument::any())
->willReturn($config->reveal());
$sut = new EnvironmentResolver('', $configFactory->reveal());
$sut->getActiveProject();
}

/**
* Data provider for active project exception test.
*
* @return array
* The data.
*/
public function activeProjectExceptionData() : array {
return [
[NULL],
[FALSE],
[''],
];
}

/**
Expand Down

0 comments on commit a241d09

Please sign in to comment.