Travis.ci Environment Variables not reading in phpunit

environment-variables, php, phpunit, travis-ci

Solution

Try using `getenv` function call instead. In travis enviroment the $_ENV variables are not available

Problem

Can someone help me understand why my environment variables aren't being read inside of my phpunit test from travis.ci? So I'm attempting to write some automatic testing using travis for a php/javascript app I'm working on. However when I wrote a test to check for environment variables reading into phpunit from travis, they fail. That means (as far as I can tell) that either the environment variables aren't able to be read by phpunit, or they aren't being passed through to the travis test properly. .travis.yml ``` language: php php: - '7.0' - '7.1' before_install: - echo "extension=ldap.so" >>php --ini | grep "Loaded Configuration" | sed -e "s|.:\s||"`` install: - cd test - npm install - cd .. script: - echo $API_BASE_URL - phpunit test/build_tests.php notifications: on_success: never on_failure: never ``` phpunit Test File ``` <?php use PHPUnit\Framework\TestCase; class build_tests extends TestCase { public function testForEnv() { $this->assertEquals(isset($_ENV['API_BASE_URL']), true); $this->assertEquals(isset($_ENV['DRINK_SERVER_URL']), true); $this->assertEquals(isset($_ENV['LOCAL_DRINK_SERVER_URL']), true); $this->assertEquals(isset($_ENV['RATE_LIMIT_DROPS_DROP']), true); $this->assertEquals(isset($_ENV['DEBUG']), true); $this->assertEquals(isset($_ENV['DEBUG_USER_UID']), true); $this->assertEquals(isset($_ENV['DEBUG_USER_CN']), true); $this->assertEquals(isset($_ENV['USE_LOCAL_DRINK_SERVER']), true); } } ?> ``` travis Exporting of Environment Variables ``` $ Setting environment variables from repository settings $ export DRINK_SERVER_URL=https://drink.csh.rit.edu:8080 $ export LOCAL_DRINK_SERVER_URL=http://localhost:3000 $ export RATE_LIMIT_DROPS_DROP=3 $ export DEBUG=true $ export DEBUG_USER_UID=[secure] $ export DEBUG_USER_CN=[secure] $ export USE_LOCAL_DRINK_SERVER=true $ export API_BASE_URL='api/index.php?request=' ``` phpunit Result ``` PHPUnit 6.1.1 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 260 ms, Memory: 6.00MB There was 1 failure: 1) build_tests::testForEnv Failed asserting that true matches expected false. /home/travis/build/devinmatte/WebDrink-2.0/test/build_tests.php:9 FAILURES! Tests: 1, Assertions: 1, Failures: 1. ``` Can someone help me understand why my environment variables aren't being read inside of my phpunit test? I would really appreciate it.

Original source

Related problems