Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment normalizer #89

Merged
merged 4 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
on: [push]
on:
pull_request:
push:
branches:
- main
name: CI
env:
SIMPLETEST_DB: "mysql://drupal:drupal@db:3306/drupal"
Expand All @@ -17,7 +21,7 @@ jobs:

services:
db:
image: mariadb:10.5
image: mariadb
env:
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
Expand All @@ -31,11 +35,14 @@ jobs:
with:
fetch-depth: 1

- name: Parse $MODULE_NAME from composer.json
run: echo "MODULE_NAME=$(cat composer.json | jq -r .name | awk -F/ '{print $NF}')" >> $GITHUB_ENV

- name: Set Drupal root
run: echo "DRUPAL_ROOT=$HOME/drupal" >> $GITHUB_ENV

- name: Set module folder
run: echo "MODULE_FOLDER=$DRUPAL_ROOT/modules/contrib/${{ secrets.MODULE_NAME }}" >> $GITHUB_ENV
run: echo "MODULE_FOLDER=$DRUPAL_ROOT/modules/contrib/$MODULE_NAME" >> $GITHUB_ENV

- name: Clone drupal
run: git clone --depth 1 --branch "$DRUPAL_CORE_VERSION" http://git.drupal.org/project/drupal.git/ $DRUPAL_ROOT
Expand All @@ -46,9 +53,9 @@ jobs:
composer config platform.php ${{ matrix.php-versions }}
composer config repositories.4 composer https://repository.drupal.hel.ninja/
composer config repositories.5 path $GITHUB_WORKSPACE
composer require drupal/${{ secrets.MODULE_NAME }} -W
composer require drupal/$MODULE_NAME -W
composer run-script drupal-phpunit-upgrade
composer require "drush/drush ^10.0"
composer require "drush/drush ^11.0"
composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require --dev donatj/mock-webserver
composer require --dev "drupal/coder"
Expand All @@ -57,7 +64,7 @@ jobs:
run: |
cd $DRUPAL_ROOT
php -d sendmail_path=$(which true); vendor/bin/drush --yes -v site-install minimal --db-url="$SIMPLETEST_DB"
vendor/bin/drush en ${{ secrets.MODULE_NAME }} -y
vendor/bin/drush en $MODULE_NAME -y

- name: Run PHPCS
run: |
Expand Down
4 changes: 3 additions & 1 deletion src/Environment/EnvironmentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
final class EnvironmentResolver implements EnvironmentResolverInterface {

use EnvironmentTrait;

public const PROJECT_NAME_KEY = 'project_name';
public const ENVIRONMENT_NAME_KEY = 'environment_name';

Expand Down Expand Up @@ -118,7 +120,7 @@ public function getActiveEnvironmentName() : string {
$this->configurationMissingExceptionMessage('No active environment found', self::ENVIRONMENT_NAME_KEY)
);
}
return $env;
return $this->normalizeEnvironmentName($env);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/Environment/EnvironmentTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_api_base\Environment;

/**
* A helper trait to deal with environments.
*/
trait EnvironmentTrait {

/**
* Temporary mapping function to match APP_ENV with environment resolver.
*
* @param string $environment
* APP_ENV or environment name.
*
* @return null|string
* The environment name.
*/
protected function normalizeEnvironmentName(string $environment) : ? string {
// 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.
$environments = [
'testing' => 'test',
'staging' => 'stage',
'production' => 'prod',
];

if (array_key_exists($environment, $environments)) {
return $environments[$environment];
}
return $environment;
}

}
30 changes: 3 additions & 27 deletions src/Environment/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
final class Project {

use EnvironmentTrait;

public const ASUMINEN = 'asuminen';
public const ETUSIVU = 'etusivu';
public const KASVATUS_KOULUTUS = 'kasvatus-koulutus';
Expand Down Expand Up @@ -75,38 +77,12 @@ public function addEnvironment(string $key, Environment $environment) : self {
* The environment.
*/
public function getEnvironment(string $environment) : Environment {
$environment = $this->mapEnvironmentName($environment);
$environment = $this->normalizeEnvironmentName($environment);

if (!isset($this->environments[$environment])) {
throw new \InvalidArgumentException(sprintf('Environment "%s" not found.', $environment));
}
return $this->environments[$environment];
}

/**
* Temporary mapping function to match APP_ENV with environment resolver.
*
* @param string $environment
* APP_ENV or environment name.
*
* @return null|string
* The environment name.
*/
private function mapEnvironmentName(string $environment) : ? string {
// APP_ENV uses 'production', 'staging' and 'testing' as
// a name, while environment resolver uses 'local', 'test,' 'stage'
// and 'prod'.
// Map all known environment name variations to match environment resolver.
$environments = [
'testing' => 'test',
'staging' => 'stage',
'production' => 'prod',
];

if (array_key_exists($environment, $environments)) {
return $environments[$environment];
}
return $environment;
}

}
13 changes: 7 additions & 6 deletions tests/src/Unit/EnvironmentResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function getEnvironmentResolver(string $projectName = NULL, string $envN
* @covers \Drupal\helfi_api_base\Environment\Project::__construct
* @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::mapEnvironmentName
* @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName
*/
public function testFallbackEnvironmentFile() : void {
$resolver = new EnvironmentResolver('', $this->getConfigStub());
Expand All @@ -85,7 +85,7 @@ public function testFallbackEnvironmentFile() : void {
* @covers \Drupal\helfi_api_base\Environment\Project::__construct
* @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::mapEnvironmentName
* @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName
*/
public function testProjectConstant() : void {
$constants = new \ReflectionClass(Project::class);
Expand Down Expand Up @@ -145,7 +145,7 @@ public function populateEnvironmentsExceptionsData() : array {
* @covers \Drupal\helfi_api_base\Environment\Project::__construct
* @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::mapEnvironmentName
* @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName
* @dataProvider resolvePathExceptionData
*/
public function testResolveUrlException(
Expand Down Expand Up @@ -191,7 +191,7 @@ public function resolvePathExceptionData() : array {
* @covers \Drupal\helfi_api_base\Environment\Project::__construct
* @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::mapEnvironmentName
* @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName
* @dataProvider environmentMapData
*/
public function testEnvironmentMap(string $envName, string $expected) : void {
Expand Down Expand Up @@ -230,7 +230,7 @@ public function environmentMapData() : array {
* @covers \Drupal\helfi_api_base\Environment\Project::__construct
* @covers \Drupal\helfi_api_base\Environment\Project::getEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::addEnvironment
* @covers \Drupal\helfi_api_base\Environment\Project::mapEnvironmentName
* @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName
* @dataProvider validUrlData
*/
public function testValidUrl(
Expand Down Expand Up @@ -356,6 +356,7 @@ public function testGetActiveEnvironmentException() : void {
* @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
*/
public function testGetActiveEnvironmentFallback() : void {
// Make sure environment resolver fallbacks to APP_ENV env variable when
Expand All @@ -379,7 +380,7 @@ public function testGetActiveEnvironmentFallback() : void {
* @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::mapEnvironmentName
* @covers \Drupal\helfi_api_base\Environment\EnvironmentTrait::normalizeEnvironmentName
*/
public function testGetActiveEnvironment() : void {
$sut = $this->getEnvironmentResolver(Project::ASUMINEN, 'test');
Expand Down