diff --git a/src/Environment/EnvironmentResolver.php b/src/Environment/EnvironmentResolver.php index bdd18ab9..ed2336a6 100644 --- a/src/Environment/EnvironmentResolver.php +++ b/src/Environment/EnvironmentResolver.php @@ -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; } /** diff --git a/tests/src/Unit/Environment/EnvironmentResolverTest.php b/tests/src/Unit/Environment/EnvironmentResolverTest.php index 7f485b69..57309a50 100644 --- a/tests/src/Unit/Environment/EnvironmentResolverTest.php +++ b/tests/src/Unit/Environment/EnvironmentResolverTest.php @@ -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. @@ -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) { @@ -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); } @@ -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], + [''], + ]; } /**