diff --git a/documentation/environment-resolver.md b/documentation/environment-resolver.md index 7c7fc242..3caf4b68 100644 --- a/documentation/environment-resolver.md +++ b/documentation/environment-resolver.md @@ -6,6 +6,7 @@ ## Usage ### Environment resolver + ```php // See \Drupal\helfi_api_base\Environment\Project for all available project constants. $projectName = \Drupal\helfi_api_base\Environment\Project::ASUMINEN; @@ -15,8 +16,6 @@ $resolver = \Drupal::service('helfi_api_base.environment_resolver'); $project = $resolver->getProject($projectName); /** @var \Drupal\helfi_api_base\Environment\ProjectMetadata $projectMetadata */ $projectMetadata = $project->getMetadata(); -// A link to the Azure DevOps service. -$projectMetadata->getAzureDevopsLink(); // 'https://dev.azure.com/City-of-Helsinki/asuminen'. // A link to the Git repository. $projectMetadata->getRepositoryUrl(); // 'https://github.com/City-of-Helsinki/drupal-helfi-asuminen'. @@ -29,10 +28,8 @@ $internalUrl = $environment->getInternalAddress('fi'); // 'https://helfi-asumine $path = $environment->getPath('fi'); // '/fi/dev-asuminen'. $domain = $environment->getDomain(); // 'nginx-asuminen-dev.agw.arodevtest.hel.fi'. $baseUrl = $environment->getBaseUrl(); // 'https://nginx-asuminen-dev.agw.arodevtest.hel.fi' -/** @var \Drupal\helfi_api_base\Environment\EnvironmentMetadata $environmentMetadata */ -$environmentMetadata = $environment->getMetadata(); -// A link to the OpenShift service. -$environmentMetadata->getOpenshiftConsoleLink(); // 'https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kanslia-asuminen-test' +/** @var \Drupal\helfi_api_base\Environment\Service $services */ +$service = $environment->getService(\Drupal\helfi_api_base\Environment\ServiceEnum::ElasticProxy); // Gets the elastic-proxy service. ``` ### Active environment @@ -55,7 +52,3 @@ $environment = $service->getActiveEnvironment(); // Failure will throw an InvalidArgumentException. $project = $service->getActiveProject(); ``` - -## Usage in other projects - -The [environments.json](/fixtures/environments.json) file should contain up-to-date information of all our available projects in JSON format. diff --git a/fixtures/environments.json b/fixtures/environments.json deleted file mode 100644 index bd1161eb..00000000 --- a/fixtures/environments.json +++ /dev/null @@ -1,713 +0,0 @@ -{ - "asuminen": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-asuminen", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/asuminen" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/asuminen", - "sv": "/sv/boende", - "en": "/en/housing" - }, - "address": { - "protocol": "https", - "domain": "helfi-asuminen.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-asuminen", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi/asuminen", - "sv": "/sv/boende", - "en": "/en/housing" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-asuminen-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kanslia-asuminen-test" - } - }, - "stage": { - "path": { - "fi": "/fi/asuminen", - "sv": "/sv/boende", - "en": "/en/housing" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-asuminen-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-asuminen-staging" - } - }, - "prod": { - "path": { - "fi": "/fi/asuminen", - "sv": "/sv/boende", - "en": "/en/housing" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-asuminen-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-asuminen-prod" - } - } - } - }, - "etusivu": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-etusivu", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/helfi-etusivu" - }, - "environments": { - "local": { - "path": { - "fi": "/fi", - "sv": "/sv", - "en": "/en" - }, - "address": { - "protocol": "https", - "domain": "helfi-etusivu.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-etusivu", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi", - "sv": "/sv", - "en": "/en" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-etusivu-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kanslia-helfi-etusivu-test" - } - }, - "stage": { - "path": { - "fi": "/fi", - "sv": "/sv", - "en": "/en" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-etusivu-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-helfi-etusivu-staging" - } - }, - "prod": { - "path": { - "fi": "/fi", - "sv": "/sv", - "en": "/en" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-etusivu-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-helfi-etusivu-prod" - } - } - } - }, - "kasvatus-koulutus": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-kasvatus-koulutus", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/kasvatus-koulutus" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/kasvatus-ja-koulutus", - "sv": "/sv/fostran-och-utbildning", - "en": "/en/childhood-and-education" - }, - "address": { - "protocol": "https", - "domain": "helfi-kasko.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-kasko", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi/kasvatus-ja-koulutus", - "sv": "/sv/fostran-och-utbildning", - "en": "/en/childhood-and-education" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-kasvatus-koulutus-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kasko-kasvatus-koulutus-test" - } - }, - "stage": { - "path": { - "fi": "/fi/kasvatus-ja-koulutus", - "sv": "/sv/fostran-och-utbildning", - "en": "/en/childhood-and-education" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-kasvatus-koulutus-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kasko-kasvatus-koulutus-staging" - } - }, - "prod": { - "path": { - "fi": "/fi/kasvatus-ja-koulutus", - "sv": "/sv/fostran-och-utbildning", - "en": "/en/childhood-and-education" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-kasvatus-koulutus-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kasko-kasvatus-koulutus-prod" - } - } - } - }, - "kuva": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-kuva", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/helfi-kuva" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/kulttuuri-ja-vapaa-aika", - "sv": "/sv/kultur-och-fritid", - "en": "/en/culture-and-leisure" - }, - "address": { - "protocol": "https", - "domain": "helfi-kuva.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-kuva", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi/kulttuuri-ja-vapaa-aika", - "sv": "/sv/kultur-och-fritid", - "en": "/en/culture-and-leisure" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-kuva-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kuva-helfi-kuva-test" - } - }, - "stage": { - "path": { - "fi": "/fi/kulttuuri-ja-vapaa-aika", - "sv": "/sv/kultur-och-fritid", - "en": "/en/culture-and-leisure" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-kuva-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kuva-helfi-kuva-staging" - } - }, - "prod": { - "path": { - "fi": "/fi/kulttuuri-ja-vapaa-aika", - "sv": "/sv/kultur-och-fritid", - "en": "/en/culture-and-leisure" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-kuva-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kuva-helfi-kuva-prod" - } - } - } - }, - "liikenne": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-kymp", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/liikenne" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/kaupunkiymparisto-ja-liikenne", - "sv": "/sv/stadsmiljo-och-trafik", - "en": "/en/urban-environment-and-traffic" - }, - "address": { - "protocol": "https", - "domain": "helfi-kymp.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-kymp", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi/kaupunkiymparisto-ja-liikenne", - "sv": "/sv/stadsmiljo-och-trafik", - "en": "/en/urban-environment-and-traffic" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-liikenne-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kanslia-liikenne-test" - } - }, - "stage": { - "path": { - "fi": "/fi/kaupunkiymparisto-ja-liikenne", - "sv": "/sv/stadsmiljo-och-trafik", - "en": "/en/urban-environment-and-traffic" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-liikenne-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-liikenne-staging" - } - }, - "prod": { - "path": { - "fi": "/fi/kaupunkiymparisto-ja-liikenne", - "sv": "/sv/stadsmiljo-och-trafik", - "en": "/en/urban-environment-and-traffic" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-liikenne-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-liikenne-prod" - } - } - } - }, - "rekry": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-rekry", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/helfi-rekry" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/avoimet-tyopaikat", - "sv": "/sv/lediga-jobb", - "en": "/en/open-jobs" - }, - "address": { - "protocol": "https", - "domain": "helfi-rekry.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-rekry", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi/avoimet-tyopaikat", - "sv": "/sv/lediga-jobb", - "en": "/en/open-jobs" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-rekry-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kanslia-helfi-rekry-test" - } - }, - "stage": { - "path": { - "fi": "/fi/avoimet-tyopaikat", - "sv": "/sv/lediga-jobb", - "en": "/en/open-jobs" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-rekry-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-helfi-rekry-staging" - } - }, - "prod": { - "path": { - "fi": "/fi/avoimet-tyopaikat", - "sv": "/sv/lediga-jobb", - "en": "/en/open-jobs" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-rekry-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-helfi-rekry-prod" - } - } - } - }, - "strategia": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-strategia", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/strategia-talous" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/paatoksenteko-ja-hallinto", - "sv": "/sv/beslutsfattande-och-forvaltning", - "en": "/en/decision-making" - }, - "address": { - "protocol": "https", - "domain": "strategia.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "strategia", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi/paatoksenteko-ja-hallinto", - "sv": "/sv/beslutsfattande-och-forvaltning", - "en": "/en/decision-making" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-strategia-talous-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kanslia-strategia-talous-test" - } - }, - "stage": { - "path": { - "fi": "/fi/paatoksenteko-ja-hallinto", - "sv": "/sv/beslutsfattande-och-forvaltning", - "en": "/en/decision-making" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-strategia-talous-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-strategia-talous-staging" - } - }, - "prod": { - "path": { - "fi": "/fi/paatoksenteko-ja-hallinto", - "sv": "/sv/beslutsfattande-och-forvaltning", - "en": "/en/decision-making" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-strategia-talous-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-strategia-talous-prod" - } - } - } - }, - "terveys": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-sote", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/terveys" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/sosiaali-ja-terveyspalvelut", - "sv": "/sv/social-och-halsovardstjanster", - "en": "/en/health-and-social-services" - }, - "address": { - "protocol": "https", - "domain": "helfi-sote.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-sote", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi/sosiaali-ja-terveyspalvelut", - "sv": "/sv/social-och-halsovardstjanster", - "en": "/en/health-and-social-services" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-terveys-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kanslia-terveys-test" - } - }, - "stage": { - "path": { - "fi": "/fi/sosiaali-ja-terveyspalvelut", - "sv": "/sv/social-och-halsovardstjanster", - "en": "/en/health-and-social-services" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-terveys-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-terveys-staging" - } - }, - "prod": { - "path": { - "fi": "/fi/sosiaali-ja-terveyspalvelut", - "sv": "/sv/social-och-halsovardstjanster", - "en": "/en/health-and-social-services" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-terveys-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-terveys-prod" - } - } - } - }, - "tyo-yrittaminen": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-tyo-yrittaminen", - "azure_devops_link": "https://dev.azure.com/City-of-Helsinki/tyo-yrittaminen" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/yritykset-ja-tyo", - "sv": "/sv/foretag-och-arbete", - "en": "/en/business-and-work" - }, - "address": { - "protocol": "https", - "domain": "helfi-elo.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-elo", - "port": 8080 - }, - "meta": {} - }, - "test": { - "path": { - "fi": "/fi/yritykset-ja-tyo", - "sv": "/sv/foretag-och-arbete", - "en": "/en/business-and-work" - }, - "address": { - "protocol": "https", - "domain": "www.test.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-tyo-yrittaminen-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.arodevtest.hel.fi/k8s/cluster/projects/hki-kanslia-tyo-yrittaminen-test" - } - }, - "stage": { - "path": { - "fi": "/fi/yritykset-ja-tyo", - "sv": "/sv/foretag-och-arbete", - "en": "/en/business-and-work" - }, - "address": { - "protocol": "https", - "domain": "www.stage.hel.ninja" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-tyo-yrittaminen-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-tyo-yrittaminen-staging" - } - }, - "prod": { - "path": { - "fi": "/fi/yritykset-ja-tyo", - "sv": "/sv/foretag-och-arbete", - "en": "/en/business-and-work" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-tyo-yrittaminen-prod.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://console-openshift-console.apps.platta.hel.fi/k8s/cluster/projects/hki-kanslia-tyo-yrittaminen-prod" - } - } - } - } -} diff --git a/helfi_api_base.services.yml b/helfi_api_base.services.yml index dc428794..1c4e2ffa 100644 --- a/helfi_api_base.services.yml +++ b/helfi_api_base.services.yml @@ -1,5 +1,4 @@ parameters: - helfi_api_base.environment_file: '' helfi_api_base.default_composer_lock: '%app.root%/../composer.lock' helfi_api_base.internal_domains: - 'hel.fi' @@ -33,7 +32,7 @@ services: Drupal\helfi_api_base\Environment\EnvironmentResolver: '@helfi_api_base.environment_resolver' helfi_api_base.environment_resolver: class: Drupal\helfi_api_base\Environment\EnvironmentResolver - arguments: ['%helfi_api_base.environment_file%', '@config.factory'] + arguments: ['@config.factory'] Drupal\helfi_api_base\Link\InternalDomainResolver: '@helfi_api_base.internal_domain_resolver' helfi_api_base.internal_domain_resolver: diff --git a/src/Environment/Environment.php b/src/Environment/Environment.php index 5e0bf9b9..29a45c96 100644 --- a/src/Environment/Environment.php +++ b/src/Environment/Environment.php @@ -4,11 +4,20 @@ namespace Drupal\helfi_api_base\Environment; +use Webmozart\Assert\Assert; + /** * A value object to store environment data. */ final class Environment { + /** + * The services. + * + * @var \Drupal\helfi_api_base\Environment\Service[] + */ + private array $services; + /** * Constructs a new instance. * @@ -18,31 +27,23 @@ final class Environment { * The internal address. * @param array $paths * The paths. - * @param string $id - * Environment resolver identifier for the project. * @param \Drupal\helfi_api_base\Environment\EnvironmentEnum $environment * The environment name. - * @param \Drupal\helfi_api_base\Environment\EnvironmentMetadata|null $metadata - * The environment specific metadata. + * @param \Drupal\helfi_api_base\Environment\Service[] $services + * The environment services. */ public function __construct( - private readonly Address $address, - private readonly Address $internalAddress, - private readonly array $paths, - private readonly string $id, - private readonly EnvironmentEnum $environment, - private readonly ?EnvironmentMetadata $metadata, + public readonly Address $address, + public readonly Address $internalAddress, + public readonly array $paths, + public readonly EnvironmentEnum $environment, + array $services = [], ) { - } + Assert::allIsInstanceOf($services, Service::class); - /** - * Gets the project identifier. - * - * @return string - * Site identifier. - */ - public function getId() : string { - return $this->id; + foreach ($services as $service) { + $this->services[$service->name->value] = $service; + } } /** @@ -130,13 +131,16 @@ public function getEnvironment() : EnvironmentEnum { } /** - * Gets the environment metadata. + * Gets the given service. + * + * @param \Drupal\helfi_api_base\Environment\ServiceEnum $name + * The service to get. * - * @return \Drupal\helfi_api_base\Environment\EnvironmentMetadata|null - * The metadata. + * @return \Drupal\helfi_api_base\Environment\Service|null + * The service or null. */ - public function getMetadata(): ?EnvironmentMetadata { - return $this->metadata; + public function getService(ServiceEnum $name): ?Service { + return $this->services[$name->value] ?? NULL; } } diff --git a/src/Environment/EnvironmentMetadata.php b/src/Environment/EnvironmentMetadata.php deleted file mode 100644 index 19f63fd1..00000000 --- a/src/Environment/EnvironmentMetadata.php +++ /dev/null @@ -1,64 +0,0 @@ - $openShiftConsoleLink, - ] = $data; - - return new self($openShiftConsoleLink); - } - - /** - * Gets the link to OpenShift console. - * - * @return string - * The OpenShift console link. - */ - public function getOpenshiftConsoleLink(): string { - return $this->openshiftConsoleLink; - } - -} diff --git a/src/Environment/EnvironmentResolver.php b/src/Environment/EnvironmentResolver.php index c2d95949..f906c23b 100644 --- a/src/Environment/EnvironmentResolver.php +++ b/src/Environment/EnvironmentResolver.php @@ -5,7 +5,6 @@ namespace Drupal\helfi_api_base\Environment; use Drupal\Core\Config\ConfigFactoryInterface; -use Symfony\Component\DependencyInjection\Attribute\Autowire; /** * Environment resolver. @@ -41,76 +40,361 @@ final class EnvironmentResolver implements EnvironmentResolverInterface { /** * Constructs a new instance. * - * @param string $pathOrJson - * The path to environments.json file. * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory * The configuration factory. */ public function __construct( - #[Autowire('%helfi_api_base.environment_file%')] string $pathOrJson, private readonly ConfigFactoryInterface $configFactory, ) { - $this->populateEnvironments($pathOrJson); } /** - * Populates the environments for given json config file. - * - * @param string $pathOrJson - * The path to config.json file. + * Populates the environments. */ - private function populateEnvironments(string $pathOrJson) : void { - // Fallback to default environments.json file. - if ($pathOrJson === '') { - $pathOrJson = __DIR__ . '/../../fixtures/environments.json'; - } - - if (!is_file($pathOrJson)) { - try { - $projects = json_decode($pathOrJson, TRUE, flags: JSON_THROW_ON_ERROR); - } - catch (\JsonException) { - throw new \InvalidArgumentException('Environment file not found or is not a valid JSON.'); - } - } - else { - $projects = json_decode(file_get_contents($pathOrJson), TRUE); - } - - if (empty($projects)) { - throw new \InvalidArgumentException('Failed to parse projects.'); - } - - foreach ($projects as $id => $item) { - if (!isset($item['meta'], $item['environments'])) { - throw new \InvalidArgumentException('Project missing meta or environments.'); - } - ['meta' => $meta, 'environments' => $environments] = $item; - - $project = new Project($id, ProjectMetadata::createFromArray($meta)); - - foreach ($environments as $environment => $settings) { - if (!isset($settings['address'], $settings['internal_address'], $settings['path'])) { - throw new \InvalidArgumentException('Project missing "address", "internal_address" or "paths" setting.'); - } - - $project->addEnvironment($environment, new Environment( - new Address(...$settings['address']), - new Address(...$settings['internal_address']), - $settings['path'], - $id, - EnvironmentEnum::tryFrom($environment), - EnvironmentMetadata::createFromArray($settings['meta'] ?? []) - )); - } - $this->projects[$id] = $project; + private function populateEnvironments() : void { + if (!empty($this->projects)) { + return; } + $paths = [ + Project::ASUMINEN => [ + 'fi' => '/fi/asuminen', + 'sv' => '/sv/boende', + 'en' => '/en/housing', + ], + Project::ETUSIVU => [ + 'fi' => '/fi', + 'sv' => '/sv', + 'en' => '/en', + ], + Project::KASVATUS_KOULUTUS => [ + 'fi' => '/fi/kasvatus-ja-koulutus', + 'sv' => '/sv/fostran-och-utbildning', + 'en' => '/en/childhood-and-education', + ], + Project::KUVA => [ + 'fi' => '/fi/kulttuuri-ja-vapaa-aika', + 'sv' => '/sv/kultur-och-fritid', + 'en' => '/en/culture-and-leisure', + ], + Project::LIIKENNE => [ + 'fi' => '/fi/kaupunkiymparisto-ja-liikenne', + 'sv' => '/sv/stadsmiljo-och-trafik', + 'en' => '/en/urban-environment-and-traffic', + ], + Project::REKRY => [ + 'fi' => '/fi/avoimet-tyopaikat', + 'sv' => '/sv/lediga-jobb', + 'en' => '/en/open-jobs', + ], + Project::STRATEGIA => [ + 'fi' => '/fi/paatoksenteko-ja-hallinto', + 'sv' => '/sv/beslutsfattande-och-forvaltning', + 'en' => '/en/decision-making', + ], + Project::TERVEYS => [ + 'fi' => '/fi/sosiaali-ja-terveyspalvelut', + 'sv' => '/sv/social-och-halsovardstjanster', + 'en' => '/en/health-and-social-services', + ], + Project::TYO_YRITTAMINEN => [ + 'fi' => '/fi/yritykset-ja-tyo', + 'sv' => '/sv/foretag-och-arbete', + 'en' => '/en/business-and-work', + ], + ]; + $this->projects = [ + Project::ASUMINEN => new Project( + Project::ASUMINEN, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-asuminen'), + [ + new Environment( + address: new Address('helfi-asuminen.docker.so'), + internalAddress: new Address('helfi-asuminen', 'http', 8080), + paths: $paths[Project::ASUMINEN], + environment: EnvironmentEnum::Local, + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-asuminen-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::ASUMINEN], + environment: EnvironmentEnum::Test, + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-asuminen-staging.apps.platta.hel.fi'), + paths: $paths[Project::ASUMINEN], + environment: EnvironmentEnum::Stage, + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-asuminen-prod.apps.platta.hel.fi'), + paths: $paths[Project::ASUMINEN], + environment: EnvironmentEnum::Prod, + ), + ], + ), + Project::ETUSIVU => new Project( + Project::ETUSIVU, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-etusivu'), + [ + new Environment( + address: new Address('helfi-etusivu.docker.so'), + internalAddress: new Address('helfi-etusivu', 'http', 8080), + paths: $paths[Project::ETUSIVU], + environment: EnvironmentEnum::Local, + services: [ + new Service(ServiceEnum::ElasticProxy, new Address('helfi-etusivu-elastic', 'http', 9200)), + ], + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-etusivu-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::ETUSIVU], + environment: EnvironmentEnum::Test, + services: [ + new Service(ServiceEnum::ElasticProxy, new Address('etusivu-elastic-proxy-test.agw.arodevtest.hel.fi')), + ], + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-etusivu-staging.apps.platta.hel.fi'), + paths: $paths[Project::ETUSIVU], + environment: EnvironmentEnum::Stage, + services: [ + // @todo This shouldn't be using prod address. + new Service(ServiceEnum::ElasticProxy, new Address('uutisapi.hel.fi')), + ], + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-etusivu-prod.apps.platta.hel.fi'), + paths: $paths[Project::ETUSIVU], + environment: EnvironmentEnum::Prod, + services: [ + new Service(ServiceEnum::ElasticProxy, new Address('uutisapi.hel.fi')), + ], + ), + ], + ), + Project::KASVATUS_KOULUTUS => new Project( + Project::KASVATUS_KOULUTUS, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-kasvatus-koulutus'), + [ + new Environment( + address: new Address('helfi-kasko.docker.so'), + internalAddress: new Address('helfi-kasko', 'http', 8080), + paths: $paths[Project::KASVATUS_KOULUTUS], + environment: EnvironmentEnum::Local, + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-kasvatus-koulutus-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::KASVATUS_KOULUTUS], + environment: EnvironmentEnum::Test, + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-kasvatus-koulutus-staging.apps.platta.hel.fi'), + paths: $paths[Project::KASVATUS_KOULUTUS], + environment: EnvironmentEnum::Stage, + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-kasvatus-koulutus-prod.apps.platta.hel.fi'), + paths: $paths[Project::KASVATUS_KOULUTUS], + environment: EnvironmentEnum::Prod, + ), + ], + ), + Project::KUVA => new Project( + Project::KUVA, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-kuva'), + [ + new Environment( + address: new Address('helfi-kuva.docker.so'), + internalAddress: new Address('helfi-kuva', 'http', 8080), + paths: $paths[Project::KUVA], + environment: EnvironmentEnum::Local, + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-kuva-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::KUVA], + environment: EnvironmentEnum::Test, + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-kuva-staging.apps.platta.hel.fi'), + paths: $paths[Project::KUVA], + environment: EnvironmentEnum::Stage, + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-kuva-prod.apps.platta.hel.fi'), + paths: $paths[Project::KUVA], + environment: EnvironmentEnum::Prod, + ), + ], + ), + Project::LIIKENNE => new Project( + Project::LIIKENNE, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-kymp'), + [ + new Environment( + address: new Address('helfi-kymp.docker.so'), + internalAddress: new Address('helfi-kymp', 'http', 8080), + paths: $paths[Project::LIIKENNE], + environment: EnvironmentEnum::Local, + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-liikenne-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::LIIKENNE], + environment: EnvironmentEnum::Test, + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-liikenne-staging.apps.platta.hel.fi'), + paths: $paths[Project::LIIKENNE], + environment: EnvironmentEnum::Stage, + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-liikenne-prod.apps.platta.hel.fi'), + paths: $paths[Project::LIIKENNE], + environment: EnvironmentEnum::Prod, + ), + ], + ), + Project::REKRY => new Project( + Project::REKRY, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-rekry'), + [ + new Environment( + address: new Address('helfi-rekry.docker.so'), + internalAddress: new Address('helfi-rekry', 'http', 8080), + paths: $paths[Project::REKRY], + environment: EnvironmentEnum::Local, + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-rekry-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::REKRY], + environment: EnvironmentEnum::Test, + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-rekry-staging.apps.platta.hel.fi'), + paths: $paths[Project::REKRY], + environment: EnvironmentEnum::Stage, + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-rekry-prod.apps.platta.hel.fi'), + paths: $paths[Project::REKRY], + environment: EnvironmentEnum::Prod, + ), + ], + ), + Project::STRATEGIA => new Project( + Project::STRATEGIA, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-strategia'), + [ + new Environment( + address: new Address('helfi-strategia.docker.so'), + internalAddress: new Address('helfi-strategia', 'http', 8080), + paths: $paths[Project::STRATEGIA], + environment: EnvironmentEnum::Local, + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-strategia-talous-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::STRATEGIA], + environment: EnvironmentEnum::Test, + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-strategia-talous-staging.apps.platta.hel.fi'), + paths: $paths[Project::STRATEGIA], + environment: EnvironmentEnum::Stage, + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-strategia-talous-prod.apps.platta.hel.fi'), + paths: $paths[Project::STRATEGIA], + environment: EnvironmentEnum::Prod, + ), + ], + ), + Project::TERVEYS => new Project( + Project::TERVEYS, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-sote'), + [ + new Environment( + address: new Address('helfi-sote.docker.so'), + internalAddress: new Address('helfi-sote', 'http', 8080), + paths: $paths[Project::TERVEYS], + environment: EnvironmentEnum::Local, + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-terveys-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::TERVEYS], + environment: EnvironmentEnum::Test, + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-terveys-staging.apps.platta.hel.fi'), + paths: $paths[Project::TERVEYS], + environment: EnvironmentEnum::Stage, + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-terveys-prod.apps.platta.hel.fi'), + paths: $paths[Project::TERVEYS], + environment: EnvironmentEnum::Prod, + ), + ], + ), + Project::TYO_YRITTAMINEN => new Project( + Project::TYO_YRITTAMINEN, + new ProjectMetadata('https://github.com/city-of-helsinki/drupal-helfi-tyo-yrittaminen'), + [ + new Environment( + address: new Address('helfi-elo.docker.so'), + internalAddress: new Address('helfi-elo', 'http', 8080), + paths: $paths[Project::TYO_YRITTAMINEN], + environment: EnvironmentEnum::Local, + ), + new Environment( + address: new Address('www.test.hel.ninja'), + internalAddress: new Address('nginx-tyo-yrittaminen-test.apps.arodevtest.hel.fi'), + paths: $paths[Project::TYO_YRITTAMINEN], + environment: EnvironmentEnum::Test, + ), + new Environment( + address: new Address('www.stage.hel.ninja'), + internalAddress: new Address('nginx-tyo-yrittaminen-staging.apps.platta.hel.fi'), + paths: $paths[Project::TYO_YRITTAMINEN], + environment: EnvironmentEnum::Stage, + ), + new Environment( + address: new Address('www.hel.fi'), + internalAddress: new Address('nginx-tyo-yrittaminen-prod.apps.platta.hel.fi'), + paths: $paths[Project::TYO_YRITTAMINEN], + environment: EnvironmentEnum::Prod, + ), + ], + ), + ]; } /** * {@inheritdoc} */ public function getProjects() : array { + $this->populateEnvironments(); + return $this->projects; } @@ -219,6 +503,8 @@ private function getProjectForRepository(string $repository) : Project { * {@inheritdoc} */ public function getProject(string $project) : Project { + $this->populateEnvironments(); + if (!isset($this->projects[$project])) { return $this->getProjectForRepository($project); } diff --git a/src/Environment/EnvironmentTrait.php b/src/Environment/EnvironmentTrait.php index da12b679..cc6fd42e 100644 --- a/src/Environment/EnvironmentTrait.php +++ b/src/Environment/EnvironmentTrait.php @@ -19,6 +19,7 @@ trait EnvironmentTrait { * The environment name. */ protected function normalizeEnvironmentName(string $environment) : ? string { + $environment = strtolower($environment); // Some environments have an incorrect APP_ENV value, like 'production', // 'staging' and 'testing' instead of 'local', 'test,' 'stage' and 'prod'. // Map all known environment name variations to match environment resolver. diff --git a/src/Environment/Project.php b/src/Environment/Project.php index 66328dcf..e7927910 100644 --- a/src/Environment/Project.php +++ b/src/Environment/Project.php @@ -42,12 +42,15 @@ final class Project { * The environments. */ public function __construct( - private readonly string $name, - private readonly ProjectMetadata $metadata, + public readonly string $name, + public readonly ProjectMetadata $metadata, array $environments = [], ) { Assert::allIsInstanceOf($environments, Environment::class); - $this->environments = $environments; + + foreach ($environments as $environment) { + $this->environments[$environment->environment->value] = $environment; + } } /** @@ -100,22 +103,6 @@ public function getEnvironments() : array { return $this->environments; } - /** - * Adds an environment. - * - * @param string $key - * The environment key. - * @param \Drupal\helfi_api_base\Environment\Environment $environment - * The environment. - * - * @return $this - * The self. - */ - public function addEnvironment(string $key, Environment $environment) : self { - $this->environments[$key] = $environment; - return $this; - } - /** * Checks if environment exists. * diff --git a/src/Environment/ProjectMetadata.php b/src/Environment/ProjectMetadata.php index 3928d347..f8d37631 100644 --- a/src/Environment/ProjectMetadata.php +++ b/src/Environment/ProjectMetadata.php @@ -10,56 +10,25 @@ final class ProjectMetadata { /** - * Constructs a new instance. + * The repository. * - * @param string $repository - * The repository. - * @param string $azureDevopsLink - * The azure devops link. + * @var string */ - public function __construct( - private readonly string $repository, - private readonly string $azureDevopsLink, - ) { - } + public readonly string $repository; /** - * Construct a new instance from array. - * - * @param array $data - * The data. + * Constructs a new instance. * - * @return self - * The + * @param string $repositoryUrl + * The repository url. */ - public static function createFromArray(array $data) : self { - $required = [ - 'repository', - 'azure_devops_link', - ]; - - foreach ($required as $key) { - if (!isset($data[$key])) { - throw new \InvalidArgumentException(sprintf('Missing required "%s".', $key)); - } + public function __construct( + private readonly string $repositoryUrl, + ) { + if (!$path = parse_url($this->repositoryUrl, PHP_URL_PATH)) { + throw new \InvalidArgumentException('The repositoryUrl must be a valid URL.'); } - - [ - 'repository' => $repository, - 'azure_devops_link' => $devopsLink, - ] = $data; - - return new self($repository, $devopsLink); - } - - /** - * Gets the Azure DevOps link. - * - * @return string - * The azure_devops_link link. - */ - public function getAzureDevopsLink() : string { - return $this->azureDevopsLink; + $this->repository = ltrim($path, '/'); } /** @@ -89,7 +58,7 @@ public function getNormalizedRepository() : string { * The repository URL. */ public function getRepositoryUrl() : string { - return sprintf('https://github.com/%s', $this->repository); + return $this->repositoryUrl; } } diff --git a/src/Environment/Service.php b/src/Environment/Service.php new file mode 100644 index 00000000..43cd578c --- /dev/null +++ b/src/Environment/Service.php @@ -0,0 +1,21 @@ +getResponse(); try { - $environment = $this->environmentResolver - ->getActiveEnvironment(); - $response->headers->add([self::INSTANCE_HEADER_NAME => $environment->getId()]); - $response->headers->add([self::ENVIRONMENT_HEADER_NAME => $environment->getEnvironmentName()]); + $response->headers->add([ + self::INSTANCE_HEADER_NAME => $this->environmentResolver->getActiveProject()->getName(), + ]); + $response->headers->add([ + self::ENVIRONMENT_HEADER_NAME => $this->environmentResolver->getActiveEnvironmentName(), + ]); } catch (\InvalidArgumentException) { } diff --git a/tests/fixtures/environments.json b/tests/fixtures/environments.json deleted file mode 100644 index ca58687d..00000000 --- a/tests/fixtures/environments.json +++ /dev/null @@ -1,78 +0,0 @@ -{ - "asuminen": { - "meta": { - "repository": "City-of-Helsinki/drupal-helfi-asuminen", - "azure_devops_link": "https://example.com" - }, - "environments": { - "local": { - "path": { - "fi": "/fi/asuminen", - "sv": "/sv/boende", - "en": "/en/housing" - }, - "address": { - "protocol": "https", - "domain": "helfi-asuminen.docker.so" - }, - "internal_address": { - "protocol": "http", - "domain": "helfi-asuminen.docker.so", - "port": 8080 - } - }, - "prod": { - "path": { - "fi": "/fi/asuminen", - "sv": "/sv/boende", - "en": "/en/housing" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-asuminen-prod.apps.platta.hel.fi" - }, - "meta": {} - }, - "stage": { - "path": { - "fi": "/fi/staging-asuminen", - "sv": "/sv/staging-boende", - "en": "/en/staging-housing" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-asuminen-staging.apps.platta.hel.fi" - }, - "meta": { - "openshift_console_link": "https://example.com" - } - }, - "test": { - "path": { - "fi": "/fi/test-asuminen", - "sv": "/sv/test-boende", - "en": "/en/test-housing" - }, - "address": { - "protocol": "https", - "domain": "www.hel.fi" - }, - "internal_address": { - "protocol": "https", - "domain": "nginx-asuminen-test.apps.arodevtest.hel.fi" - }, - "meta": { - "openshift_console_link": "https://example.com" - } - } - } - } -} diff --git a/tests/src/Traits/EnvironmentResolverTrait.php b/tests/src/Traits/EnvironmentResolverTrait.php index 5fd00864..abaf7e34 100644 --- a/tests/src/Traits/EnvironmentResolverTrait.php +++ b/tests/src/Traits/EnvironmentResolverTrait.php @@ -81,7 +81,7 @@ protected function getConfigStub(mixed $projectName = NULL, mixed $envName = NUL */ protected function getEnvironmentResolver(mixed $projectName = NULL, mixed $envName = NULL) : EnvironmentResolver { $configStub = $this->getConfigStub($projectName, $envName); - return new EnvironmentResolver(__DIR__ . '/../../../fixtures/environments.json', $configStub); + return new EnvironmentResolver($configStub); } } diff --git a/tests/src/Unit/ApiClient/ApiClientTest.php b/tests/src/Unit/ApiClient/ApiClientTest.php index 82f14f97..6df26736 100644 --- a/tests/src/Unit/ApiClient/ApiClientTest.php +++ b/tests/src/Unit/ApiClient/ApiClientTest.php @@ -11,10 +11,10 @@ use Drupal\helfi_api_base\ApiClient\ApiClient; use Drupal\helfi_api_base\ApiClient\ApiResponse; use Drupal\helfi_api_base\ApiClient\CacheValue; -use Drupal\helfi_api_base\Environment\EnvironmentResolver; -use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface; +use Drupal\helfi_api_base\Environment\EnvironmentEnum; use Drupal\helfi_api_base\Environment\Project; use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait; +use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait; use Drupal\Tests\UnitTestCase; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\ClientException; @@ -37,6 +37,7 @@ class ApiClientTest extends UnitTestCase { use ApiTestTrait; use ProphecyTrait; + use EnvironmentResolverTrait; /** * The cache. @@ -46,11 +47,11 @@ class ApiClientTest extends UnitTestCase { private ?CacheBackendInterface $cache; /** - * The default environment resolver config. + * The default environment. * - * @var array + * @var \Drupal\helfi_api_base\Environment\EnvironmentEnum */ - private array $environmentResolverConfiguration = []; + private EnvironmentEnum $environment = EnvironmentEnum::Local; /** * Response fixture JSON file. @@ -67,10 +68,6 @@ protected function setUp() : void { $this->fixture = sprintf('%s/../../../fixtures/response.json', __DIR__); $this->cache = new MemoryBackend(); - $this->environmentResolverConfiguration = [ - EnvironmentResolver::PROJECT_NAME_KEY => Project::ASUMINEN, - EnvironmentResolver::ENVIRONMENT_NAME_KEY => 'local', - ]; } /** @@ -97,8 +94,6 @@ private function getTimeMock(int $expectedTime) : ObjectProphecy { * The time prophecy. * @param \Psr\Log\LoggerInterface|null $logger * The logger. - * @param \Drupal\helfi_api_base\Environment\EnvironmentResolverInterface|null $environmentResolver - * The environment resolver. * @param array $requestOptions * The default request options. * @@ -109,7 +104,6 @@ private function getSut( ClientInterface $client = NULL, TimeInterface $time = NULL, LoggerInterface $logger = NULL, - EnvironmentResolverInterface $environmentResolver = NULL, array $requestOptions = [], ) : ApiClient { if (!$client) { @@ -123,17 +117,11 @@ private function getSut( if (!$logger) { $logger = $this->prophesize(LoggerInterface::class)->reveal(); } - if (!$environmentResolver) { - $environmentResolver = new EnvironmentResolver('', $this->getConfigFactoryStub([ - 'helfi_api_base.environment_resolver.settings' => $this->environmentResolverConfiguration, - ])); - } - return new ApiClient( $client, $this->cache, $time, - $environmentResolver, + $this->getEnvironmentResolver(Project::ASUMINEN, $this->environment), $logger, $requestOptions, ); @@ -342,8 +330,8 @@ public function testMockFallback() : void { * @covers ::getRequestOptions */ public function testFastRequestFailure() : void { - // Override environment name so we don't fallback to mock responses. - $this->environmentResolverConfiguration[EnvironmentResolver::ENVIRONMENT_NAME_KEY] = 'test'; + // Override environment so we don't fallback to mock responses. + $this->environment = EnvironmentEnum::Test; $client = $this->createMockHttpClient([ new ConnectException( diff --git a/tests/src/Unit/ApiClient/ApiFixtureTest.php b/tests/src/Unit/ApiClient/ApiFixtureTest.php index 490237a7..833400d9 100644 --- a/tests/src/Unit/ApiClient/ApiFixtureTest.php +++ b/tests/src/Unit/ApiClient/ApiFixtureTest.php @@ -20,7 +20,7 @@ class ApiFixtureTest extends UnitTestCase { * @covers \Drupal\helfi_api_base\ApiClient\ApiResponse */ public function testFixtures() { - $response = ApiFixture::requestFromFile(vsprintf('%s/../../../fixtures/environments.json', [ + $response = ApiFixture::requestFromFile(vsprintf('%s/../../../fixtures/response.json', [ __DIR__, ])); diff --git a/tests/src/Unit/Environment/AddressTest.php b/tests/src/Unit/Environment/AddressTest.php index 665e5ec5..28337426 100644 --- a/tests/src/Unit/Environment/AddressTest.php +++ b/tests/src/Unit/Environment/AddressTest.php @@ -10,14 +10,13 @@ /** * Tests Project value object. * - * @coversDefaultClass \Drupal\helfi_api_base\Environment\Address * @group helfi_api_base */ class AddressTest extends UnitTestCase { /** - * @covers ::__construct - * @covers ::getAddress + * Tests getAddress() method. + * * @dataProvider addressData */ public function testGetAddress(string $expected, array $values) : void { diff --git a/tests/src/Unit/Environment/EnvironmentEnumTest.php b/tests/src/Unit/Environment/EnvironmentEnumTest.php index 431fa868..bcfef1dd 100644 --- a/tests/src/Unit/Environment/EnvironmentEnumTest.php +++ b/tests/src/Unit/Environment/EnvironmentEnumTest.php @@ -11,13 +11,12 @@ /** * Tests environment enum. * - * @coversDefaultClass \Drupal\helfi_api_base\Environment\EnvironmentEnum * @group helfi_api_base */ class EnvironmentEnumTest extends UnitTestCase { /** - * @covers ::label + * Tests label() method. */ public function testLabel() : void { $found = 0; diff --git a/tests/src/Unit/Environment/EnvironmentMetadataTest.php b/tests/src/Unit/Environment/EnvironmentMetadataTest.php deleted file mode 100644 index daa99636..00000000 --- a/tests/src/Unit/Environment/EnvironmentMetadataTest.php +++ /dev/null @@ -1,53 +0,0 @@ - 'dsa']); - } - catch (\InvalidArgumentException $e) { - $this->assertMatchesRegularExpression('/Missing required/', $e->getMessage()); - $caught = TRUE; - } - $this->assertTrue($caught); - } - - /** - * @covers ::createFromArray - */ - public function testNull() : void { - $sut = EnvironmentMetadata::createFromArray([]); - $this->assertNull($sut); - } - - /** - * @covers ::__construct - * @covers ::createFromArray - * @covers ::getOpenshiftConsoleLink - */ - public function testGetters() : void { - $sut = EnvironmentMetadata::createFromArray([ - 'openshift_console_link' => 'https://example.com', - ]); - $this->assertEquals('https://example.com', $sut->getOpenshiftConsoleLink()); - } - -} diff --git a/tests/src/Unit/Environment/EnvironmentResolverTest.php b/tests/src/Unit/Environment/EnvironmentResolverTest.php index c105a287..ccfdf608 100644 --- a/tests/src/Unit/Environment/EnvironmentResolverTest.php +++ b/tests/src/Unit/Environment/EnvironmentResolverTest.php @@ -7,6 +7,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; use Drupal\helfi_api_base\Environment\Environment; +use Drupal\helfi_api_base\Environment\EnvironmentEnum; use Drupal\helfi_api_base\Environment\EnvironmentResolver; use Drupal\helfi_api_base\Environment\Project; use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait; @@ -17,7 +18,6 @@ /** * Tests environment resolver value objects. * - * @coversDefaultClass \Drupal\helfi_api_base\Environment\EnvironmentResolver * @group helfi_api_base */ class EnvironmentResolverTest extends UnitTestCase { @@ -26,26 +26,11 @@ class EnvironmentResolverTest extends UnitTestCase { use EnvironmentResolverTrait; /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers ::getEnvironment - * @covers ::getProject - * @covers ::getProjects - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\Environment::getPath - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::hasEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment - * @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct + * Ensures all defined projects have a matching constant. */ public function testProjectConstant() : void { $constants = new \ReflectionClass(Project::class); - $resolver = new EnvironmentResolver('', $this->getConfigStub()); + $resolver = new EnvironmentResolver($this->getConfigStub()); foreach ($constants->getConstants() as $value) { $this->assertNotEmpty($resolver->getProject($value)); @@ -55,72 +40,8 @@ public function testProjectConstant() : void { } /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @dataProvider populateEnvironmentsExceptionsData - */ - public function testPopulateEnvironmentsExceptions(string $data, string $message) : void { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage($message); - new EnvironmentResolver($data, $this->getConfigStub()); - } - - /** - * Data provider. + * Tests getEnvironment() validation. * - * @return array - * The data. - */ - public function populateEnvironmentsExceptionsData() : array { - return [ - ['nonexistent.json', 'Environment file not found or is not a valid JSON.'], - [__FILE__, 'Failed to parse projects.'], - [ - json_encode([ - 'asuminen' => [ - 'meta' => [ - 'repository' => '123', - 'azure_devops_link' => 'https://example.com', - ], - 'environments' => [ - 'local' => [], - ], - ], - ]), - 'Project missing "address", "internal_address" or "paths" setting.', - ], - [ - json_encode([ - 'asuminen' => [], - ]), - 'Project missing meta or environments.', - ], - ]; - } - - /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers ::getEnvironment - * @covers ::getProject - * @covers ::getProjectForRepository - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\Environment::getPath - * @covers \Drupal\helfi_api_base\Environment\Environment::getUrl - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::hasEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::getMetadata - * @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::getNormalizedRepository - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct * @dataProvider resolveEnvironmentExceptionData */ public function testGetEnvironmentException( @@ -149,23 +70,8 @@ public function resolveEnvironmentExceptionData() : array { } /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers ::getEnvironment - * @covers ::getProject - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\Environment::getPath - * @covers \Drupal\helfi_api_base\Environment\Environment::getUrl - * @covers \Drupal\helfi_api_base\Environment\Environment::getEnvironmentName - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::hasEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment - * @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct + * Test environment mapping. + * * @dataProvider environmentMapData */ public function testEnvironmentMap(string $envName, string $expected) : void { @@ -189,22 +95,8 @@ public function environmentMapData() : array { } /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers ::getEnvironment - * @covers ::getProject - * @covers ::getActiveProject - * @covers ::getConfig - * @covers ::configurationMissingExceptionMessage - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::populateEnvironments - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct + * Test ::getActiveProject() validation. + * * @dataProvider activeProjectExceptionData */ public function testGetActiveProjectException(mixed $value) : void { @@ -218,7 +110,7 @@ public function testGetActiveProjectException(mixed $value) : void { $configFactory = $this->prophesize(ConfigFactoryInterface::class); $configFactory->get(Argument::any()) ->willReturn($config->reveal()); - $sut = new EnvironmentResolver('', $configFactory->reveal()); + $sut = new EnvironmentResolver($configFactory->reveal()); $sut->getActiveProject(); } @@ -237,23 +129,7 @@ public function activeProjectExceptionData() : array { } /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers ::getEnvironment - * @covers ::getProject - * @covers ::getActiveProject - * @covers ::getConfig - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::populateEnvironments - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::getEnvironments - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::getProject - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct + * Test getActiveProject(). */ public function testGetActiveProject() : void { $sut = $this->getEnvironmentResolver(Project::ASUMINEN, 'dev'); @@ -262,24 +138,7 @@ public function testGetActiveProject() : void { } /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers ::getEnvironment - * @covers ::getProject - * @covers ::configurationMissingExceptionMessage - * @covers ::getActiveEnvironment - * @covers ::getActiveEnvironmentName - * @covers ::getConfig - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::populateEnvironments - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::getProject - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct + * Tests getActiveEnvironment() validation. */ public function testGetActiveEnvironmentException() : void { putenv('APP_ENV='); @@ -289,18 +148,7 @@ public function testGetActiveEnvironmentException() : void { } /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers ::getActiveEnvironmentName - * @covers ::getConfig - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment - * @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct + * Make sure getActiveEnvironment() fallbacks to env variable. */ public function testGetActiveEnvironmentFallback() : void { // Make sure environment resolver fallbacks to APP_ENV env variable when @@ -310,30 +158,10 @@ public function testGetActiveEnvironmentFallback() : void { } /** - * @covers ::populateEnvironments - * @covers ::__construct - * @covers ::getEnvironment - * @covers ::getProject - * @covers ::getConfig - * @covers ::getActiveEnvironment - * @covers ::getActiveEnvironmentName - * @covers ::getActiveProject - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::getProject - * @covers \Drupal\helfi_api_base\Environment\EnvironmentResolver::populateEnvironments - * @covers \Drupal\helfi_api_base\Environment\Project::__construct - * @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment - * @covers \Drupal\helfi_api_base\Environment\Project::hasEnvironment - * @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::createFromArray - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct + * Tests getActiveEnvironment(). */ public function testGetActiveEnvironment() : void { - $sut = $this->getEnvironmentResolver(Project::ASUMINEN, 'test'); + $sut = $this->getEnvironmentResolver(Project::ASUMINEN, EnvironmentEnum::Test); $this->assertInstanceOf(Environment::class, $sut->getActiveEnvironment()); } diff --git a/tests/src/Unit/Environment/EnvironmentTest.php b/tests/src/Unit/Environment/EnvironmentTest.php index ca7530af..39a0df89 100644 --- a/tests/src/Unit/Environment/EnvironmentTest.php +++ b/tests/src/Unit/Environment/EnvironmentTest.php @@ -7,79 +7,99 @@ use Drupal\helfi_api_base\Environment\Address; use Drupal\helfi_api_base\Environment\Environment; use Drupal\helfi_api_base\Environment\EnvironmentEnum; -use Drupal\helfi_api_base\Environment\EnvironmentMetadata; +use Drupal\helfi_api_base\Environment\Service; +use Drupal\helfi_api_base\Environment\ServiceEnum; use Drupal\Tests\UnitTestCase; /** * Tests environment value object. * - * @coversDefaultClass \Drupal\helfi_api_base\Environment\Environment * @group helfi_api_base */ class EnvironmentTest extends UnitTestCase { /** - * @covers ::__construct - * @covers ::getId - * @covers ::getEnvironment - * @covers ::getEnvironmentName - * @covers ::getInternalBaseUrl - * @covers ::getBaseUrl - * @cove - * @covers ::getMetadata - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentMetadata::getOpenshiftConsoleLink + * Gets the SUT. + * + * @param array $paths + * The paths. + * @param array $services + * The services. + * + * @return \Drupal\helfi_api_base\Environment\Environment + * The SUT. */ - public function testSimpleGetters() : void { - $sut = new Environment( + private function getSut(array $paths = [], array $services = []) : Environment { + return new Environment( new Address('www.hel.fi'), new Address('internal-address.local', 'http', 8080), - [], - 'test', + $paths, EnvironmentEnum::Test, - new EnvironmentMetadata('https://localhost')); - $this->assertEquals('test', $sut->getId()); + $services, + ); + } + + /** + * Tests getters. + */ + public function testSimpleGetters() : void { + $sut = $this->getSut(); $this->assertEquals(EnvironmentEnum::Test, $sut->getEnvironment()); $this->assertEquals(EnvironmentEnum::Test->value, $sut->getEnvironmentName()); $this->assertEquals('https://www.hel.fi', $sut->getBaseUrl()); $this->assertEquals('http://internal-address.local:8080', $sut->getInternalBaseUrl()); - $this->assertEquals('https://localhost', $sut->getMetadata()->getOpenshiftConsoleLink()); + $this->assertNull($sut->getService(ServiceEnum::ElasticProxy)); + } + + /** + * Make sure only Service instances can be passed. + */ + public function testServiceException() : void { + $this->expectException(\InvalidArgumentException::class); + $this->getSut(services: [1]); + } + + /** + * Tests service getter. + */ + public function testGetService() : void { + $sut = $this->getSut( + services: [ + new Service(ServiceEnum::ElasticProxy, new Address('helfi-etusivu-elastic')), + ], + ); + $this->assertInstanceOf(Service::class, $sut->getService(ServiceEnum::ElasticProxy)); } /** - * @covers ::__construct - * @covers ::getUrl - * @covers ::getPath - * @covers ::getInternalAddress + * Test path validation. + */ + public function testGetUrlException() : void { + $sut = $this->getSut(['fi' => 'test-path']); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Path not found for "en" language.'); + $sut->getUrl('en'); + } + + /** + * Tests getUrl() method. */ public function testGetUrl() : void { $sut = new Environment( new Address('www.hel.fi'), new Address('www.hel.fi', 'http'), ['fi' => 'test-path'], - 'test', EnvironmentEnum::Local, - NULL); + ); $this->assertEquals('https://www.hel.fi/test-path', $sut->getUrl('fi')); $this->assertEquals('http://www.hel.fi/test-path', $sut->getInternalAddress('fi')); - $caught = FALSE; - try { - $sut->getUrl('en'); - } - catch (\InvalidArgumentException $e) { - $this->assertEquals('Path not found for "en" language.', $e->getMessage()); - $caught = TRUE; - } - $this->assertTrue($caught); - $sut = new Environment( new Address('www.hel.fi'), new Address('www.hel.fi', 'https', 8080), ['fi' => 'test-path'], - 'test', EnvironmentEnum::Local, - NULL); + ); $this->assertEquals('https://www.hel.fi/test-path', $sut->getUrl('fi')); $this->assertEquals('https://www.hel.fi:8080/test-path', $sut->getInternalAddress('fi')); } diff --git a/tests/src/Unit/Environment/ProjectMetadataTest.php b/tests/src/Unit/Environment/ProjectMetadataTest.php index 3e29a9b8..2d031546 100644 --- a/tests/src/Unit/Environment/ProjectMetadataTest.php +++ b/tests/src/Unit/Environment/ProjectMetadataTest.php @@ -10,43 +10,27 @@ /** * Tests Project metadata value object. * - * @coversDefaultClass \Drupal\helfi_api_base\Environment\ProjectMetadata * @group helfi_api_base */ class ProjectMetadataTest extends UnitTestCase { /** - * @covers ::createFromArray + * Tests constructor validation. */ public function testRequiredValueException() : void { - $caught = FALSE; - try { - ProjectMetadata::createFromArray(['repository' => 'test']); - } - catch (\InvalidArgumentException $e) { - $this->assertMatchesRegularExpression('/Missing required/', $e->getMessage()); - $caught = TRUE; - } - $this->assertTrue($caught); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The repositoryUrl must be a valid URL.'); + new ProjectMetadata(''); } /** - * @covers ::__construct - * @covers ::createFromArray - * @covers ::getAzureDevopsLink - * @covers ::getRepository - * @covers ::getRepositoryUrl - * @covers ::getNormalizedRepository + * Test getter methods. */ public function testGetters() : void { - $sut = ProjectMetadata::createFromArray([ - 'repository' => 'City-of-Helsinki/test', - 'azure_devops_link' => 'https://example.com/2', - ]); + $sut = new ProjectMetadata('https://github.com/City-of-Helsinki/test'); $this->assertEquals('City-of-Helsinki/test', $sut->getRepository()); $this->assertEquals('city-of-helsinki/test', $sut->getNormalizedRepository()); $this->assertEquals('https://github.com/City-of-Helsinki/test', $sut->getRepositoryUrl()); - $this->assertEquals('https://example.com/2', $sut->getAzureDevopsLink()); } } diff --git a/tests/src/Unit/Environment/ProjectTest.php b/tests/src/Unit/Environment/ProjectTest.php index b0e78a53..42a1d6f7 100644 --- a/tests/src/Unit/Environment/ProjectTest.php +++ b/tests/src/Unit/Environment/ProjectTest.php @@ -15,82 +15,55 @@ /** * Tests Project value object. * - * @coversDefaultClass \Drupal\helfi_api_base\Environment\Project * @group helfi_api_base */ class ProjectTest extends UnitTestCase { /** - * @covers ::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct + * Validate constructor arguments. */ public function testConstructorException() : void { - $caught = FALSE; - try { - // Make sure $environments parameter must be an Environment object. - new Project(Project::ASUMINEN, new ProjectMetadata('', ''), ['invalid env']); - } - catch (\InvalidArgumentException $e) { - $caught = TRUE; - } - $this->assertTrue($caught); + $this->expectException(\InvalidArgumentException::class); + // Make sure $environments parameter must be an Environment object. + new Project(Project::ASUMINEN, new ProjectMetadata(''), ['invalid env']); } /** - * @covers ::__construct - * @covers ::getName - * @covers ::getMetadata - * @covers ::getEnvironments - * @covers \Drupal\helfi_api_base\Environment\Environment::__construct - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct + * Test getters. */ public function testGetters() : void { - $sut = new Project(Project::ASUMINEN, new ProjectMetadata('', ''), [ + $sut = new Project(Project::ASUMINEN, new ProjectMetadata('https://github.com/city-of-helsinki/something'), [ new Environment( new Address('www.hel.fi'), new Address('www.hel.fi'), [], - Project::ASUMINEN, EnvironmentEnum::Local, - NULL ), ]); $this->assertEquals(Project::ASUMINEN, $sut->getName()); $this->assertInstanceOf(ProjectMetadata::class, $sut->getMetadata()); - $this->assertInstanceOf(Environment::class, $sut->getEnvironments()[0]); + $this->assertInstanceOf(Environment::class, $sut->getEnvironments()[EnvironmentEnum::Local->value]); } /** - * @covers ::getEnvironment - * @covers ::hasEnvironment - * @covers ::__construct - * @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct + * Validate environment. */ public function testGetEnvironmentException() : void { - $caught = FALSE; - $sut = new Project(Project::ASUMINEN, new ProjectMetadata('', '')); - try { - $sut->getEnvironment('local'); - } - catch (\InvalidArgumentException $e) { - $this->assertEquals('Environment "local" not found.', $e->getMessage()); - $caught = TRUE; - } - $this->assertTrue($caught); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Environment "local" not found.'); + $sut = new Project(Project::ASUMINEN, new ProjectMetadata('https://github.com/city-of-helsinki/something')); + $sut->getEnvironment('local'); } /** - * @covers ::__construct - * @covers ::label - * @covers \Drupal\helfi_api_base\Environment\ProjectMetadata::__construct + * Test label. */ public function testLabel() : void { $constants = new \ReflectionClass(Project::class); $found = 0; foreach ($constants->getConstants() as $value) { $found++; - $sut = new Project($value, new ProjectMetadata('', '')); + $sut = new Project($value, new ProjectMetadata('https://github.com/city-of-helsinki/something')); $this->assertInstanceOf(TranslatableMarkup::class, $sut->label()); } $this->assertTrue($found > 0);