Skip to content

Commit

Permalink
Merge pull request #788 from City-of-Helsinki/UHF-10339
Browse files Browse the repository at this point in the history
UHF-10339: Default to testing in case matching environment does not exist
  • Loading branch information
tuutti authored Jul 24, 2024
2 parents b61591d + 729acdf commit d662479
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ function helfi_paragraphs_news_list_preprocess_paragraph__news_list(&$variables)
$resolver = \Drupal::service('helfi_api_base.environment_resolver');

try {
$environment = $resolver->getActiveEnvironmentName();
$environment = $resolver->getEnvironment(Project::ETUSIVU, $resolver->getActiveEnvironmentName());
}
catch (\InvalidArgumentException) {
$environment = EnvironmentEnum::Prod->value;
$environment = $resolver->getEnvironment(Project::ETUSIVU, EnvironmentEnum::Prod->value);
}
$instanceUrl = $resolver->getEnvironment(Project::ETUSIVU, $environment)
$instanceUrl = $environment
->getUrl($langcode);

$instanceUrl = $instanceUrl . match ($langcode) {
Expand Down
13 changes: 11 additions & 2 deletions modules/helfi_paragraphs_news_list/src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\helfi_paragraphs_news_list;

use Drupal\helfi_api_base\Environment\EnvironmentEnum;
use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\helfi_api_base\Environment\ServiceEnum;
Expand Down Expand Up @@ -34,8 +35,16 @@ public function __construct(
* The client.
*/
public function create() : Client {
$service = $this->environmentResolver
->getEnvironment(Project::ETUSIVU, $this->environmentResolver->getActiveEnvironmentName())
try {
$environment = $this->environmentResolver
->getEnvironment(Project::ETUSIVU, $this->environmentResolver->getActiveEnvironmentName());
}
catch (\InvalidArgumentException) {
// Use prod in case matching environment does not exist.
$environment = $this->environmentResolver
->getEnvironment(Project::ETUSIVU, EnvironmentEnum::Prod->value);
}
$service = $environment
->getService(ServiceEnum::ElasticProxy)
->address;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_\Unit;

use Drupal\helfi_api_base\Environment\EnvironmentResolver;
use Drupal\helfi_paragraphs_news_list\ClientBuilder;
use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait;
use Drupal\Tests\UnitTestCase;
use Elasticsearch\Client;

/**
* Tests elastic client builder.
*
* @group helfi_api_base
*/
class ClientBuilderTest extends UnitTestCase {

use EnvironmentResolverTrait;

/**
* Tests that client builder fallbacks to testing environment.
*/
public function testEnvironmentFallback() : void {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $configFactory */
$configFactory = $this->getConfigFactoryStub([
'helfi_api_base.environment_resolver.settings' => [
EnvironmentResolver::ENVIRONMENT_NAME_KEY => 'nonexistent',
],
]);
$envResolver = new EnvironmentResolver($configFactory);
$sut = new ClientBuilder($envResolver);
$this->assertInstanceOf(Client::class, $sut->create());
}

}

0 comments on commit d662479

Please sign in to comment.