3

I'm currently writing unit tests for an API written in PHP. This API implements a RateLimiting step before each request, and I want to avoid this step while I'm testing the application.

Now, if I want to run the tests locally I just have to check the local IP, which is "::1". But I'm having problems accessing the environment variables that my continuous integration server provides (I am using wercker).

If I run this from a PHPUnit test:

var_export(isset($_SERVER["CI"]) || isset($_SERVER["wercker"]));

I get true, but if I do do something similar before applying the rate limiting:

if (isset($_SERVER["CI"]) || $request->getIp() === "::1") {
   return;
} else {//...

the wercker tests keep failing because it never skips the rate limiting logic. Notice that the first piece of code is run from a test in PHPUnit, while the second one is part of the server application.

What am I doing wrong with the environment variables?

Please let me know if I must provide more information or documentation.

Enric Florit
  • 131
  • 4

2 Answers2

0

I was able to make it work by using PHP's getenv function

if (getenv("CI") ||  $request->getIp() === "::1") {
    return;
} else {//...
Enric Florit
  • 131
  • 4
0

To display all environment variables on Wercker server, add this step (E.g : in build section) :

build:
    steps:
        - script:
            name: show env vars
            code: env
Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52