diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e5e6b55..4928306b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,8 @@ -on: [push] +on: + pull_request: + push: + branches: + - main name: CI env: SIMPLETEST_DB: "mysql://drupal:drupal@db:3306/drupal" @@ -17,7 +21,7 @@ jobs: services: db: - image: mariadb:10.5 + image: mariadb env: MYSQL_USER: drupal MYSQL_PASSWORD: drupal @@ -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 @@ -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" @@ -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: | diff --git a/src/Environment/EnvironmentResolver.php b/src/Environment/EnvironmentResolver.php index a2e4650b..c888b5b2 100644 --- a/src/Environment/EnvironmentResolver.php +++ b/src/Environment/EnvironmentResolver.php @@ -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'; @@ -118,7 +120,7 @@ public function getActiveEnvironmentName() : string { $this->configurationMissingExceptionMessage('No active environment found', self::ENVIRONMENT_NAME_KEY) ); } - return $env; + return $this->normalizeEnvironmentName($env); } /** diff --git a/src/Environment/EnvironmentTrait.php b/src/Environment/EnvironmentTrait.php new file mode 100644 index 00000000..1cf8626b --- /dev/null +++ b/src/Environment/EnvironmentTrait.php @@ -0,0 +1,37 @@ + 'test', + 'staging' => 'stage', + 'production' => 'prod', + ]; + + if (array_key_exists($environment, $environments)) { + return $environments[$environment]; + } + return $environment; + } + +} diff --git a/src/Environment/Project.php b/src/Environment/Project.php index dcc81736..14860d5e 100644 --- a/src/Environment/Project.php +++ b/src/Environment/Project.php @@ -11,6 +11,8 @@ */ final class Project { + use EnvironmentTrait; + public const ASUMINEN = 'asuminen'; public const ETUSIVU = 'etusivu'; public const KASVATUS_KOULUTUS = 'kasvatus-koulutus'; @@ -75,7 +77,7 @@ 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)); @@ -83,30 +85,4 @@ public function getEnvironment(string $environment) : 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; - } - } diff --git a/tests/src/Unit/EnvironmentResolverTest.php b/tests/src/Unit/EnvironmentResolverTest.php index d4058f8f..01f0a12a 100644 --- a/tests/src/Unit/EnvironmentResolverTest.php +++ b/tests/src/Unit/EnvironmentResolverTest.php @@ -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()); @@ -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); @@ -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( @@ -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 { @@ -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( @@ -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 @@ -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');