From eec9eda22d51a21648c12a9ee4a77178ae5125d1 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Fri, 15 May 2020 18:53:23 +0200 Subject: [PATCH] Added Core::getEnv, allowing the developer to load environment variables using a default fallback. --- .drone.yml | 19 +++++++++ .gitlab-ci.yml | 82 ------------------------------------- .travis.yml | 12 ------ src/FuzeWorks/Core.php | 20 +++++++++ test/core/core_coreTest.php | 18 +++++++- 5 files changed, 56 insertions(+), 95 deletions(-) create mode 100644 .drone.yml delete mode 100644 .gitlab-ci.yml delete mode 100644 .travis.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..a4fe468 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,19 @@ +kind: pipeline +type: docker +name: test + +steps: + - name: composer + image: composer:latest + commands: + - composer install + + - name: phpunit + image: phpunit:7.3 + commands: + - vendor/bin/phpunit -c test/phpunit.xml + + - name: coverage + image: phpunit:7.3 + commands: + - vendor/bin/phpunit -c test/phpunit.xml --coverage-text \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index 8f13c09..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,82 +0,0 @@ -before_script: -# Install dependencies -- set -xe -- apt-get update -yqq -- apt-get install git zip unzip -yqq - -stages: - - build - - test - - deploy - -build:composer: - image: php:7.2 - stage: build - script: - - curl -sS https://getcomposer.org/installer | php - - php composer.phar install - cache: - key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME" - paths: - - vendor/ - -test:7.1: - stage: test - image: php:7.1 - script: - - vendor/bin/phpunit -c test/phpunit.xml - cache: - key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME" - paths: - - vendor - -test:7.2: - stage: test - image: php:7.2 - script: - - vendor/bin/phpunit -c test/phpunit.xml - cache: - key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME" - paths: - - vendor/ - -test:7.3: - stage: test - image: php:7.3 - script: - - vendor/bin/phpunit -c test/phpunit.xml - cache: - key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME" - paths: - - vendor/ - -test:coverage: - stage: test - image: php:7.2 - script: - - pecl install xdebug - - docker-php-ext-enable xdebug - - vendor/bin/phpunit -c test/phpunit.xml --coverage-text - cache: - key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME" - paths: - - vendor/ - -release: - stage: deploy - image: php:7.2 - only: - - master - script: - - pecl install xdebug - - docker-php-ext-enable xdebug - - vendor/bin/phpunit -c test/phpunit.xml --coverage-text - artifacts: - name: "${CI_BUILD_NAME}_${CI_BUILD_REF_NAME}" - paths: - - build/ - expire_in: 3 weeks - cache: - key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME" - paths: - - vendor/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4930ba4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,12 +0,0 @@ -language: php - -php: - - 7.1 - - 7.2 - - 7.3 - -script: - - php vendor/bin/phpunit -v -c test/phpunit.xml --coverage-text - -before_script: - - composer install diff --git a/src/FuzeWorks/Core.php b/src/FuzeWorks/Core.php index a7f7328..3c9b7bf 100644 --- a/src/FuzeWorks/Core.php +++ b/src/FuzeWorks/Core.php @@ -148,6 +148,26 @@ class Core Logger::stopLevel(); } + /** + * Retrieve a variable name from the php environment, while also providing a fallback variable + * + * @param string $varName + * @param string|null $default + * @return mixed + */ + public static function getEnv(string $varName, string $default = null) + { + // First retrieve the environment variable + $var = getenv($varName); + + // If the environment variable doesn't exist, use the default one + if ($var === FALSE) + return $default; + + // Otherwise return the variable itself + return $var; + } + /** * Checks whether the current running version of PHP is equal to the input string. * diff --git a/test/core/core_coreTest.php b/test/core/core_coreTest.php index 1749468..8beb7c2 100644 --- a/test/core/core_coreTest.php +++ b/test/core/core_coreTest.php @@ -71,5 +71,21 @@ class coreTest extends CoreTestAbstract { $this->assertTrue(Core::isPHP('1.2.0')); $this->assertFalse(Core::isphp('9999.9.9')); - } + } + + /** + * @covers ::getEnv + */ + public function testGetEnv() + { + // First push some test variables + putenv('TESTGETENV=AFFIRMED'); + + // Then try and fetch using the method + $this->assertEquals('AFFIRMED', Core::getEnv('TESTGETENV')); + + // Also test variables that don't exist + $this->assertNull(Core::getEnv('TESTNOTEXIST')); + $this->assertEquals('replacement', Core::getEnv('TESTNOTEXISTTWO', 'replacement')); + } }