41

I currently have a problem that I have to work around in legacy code to get our interaction with a PHP Extension to work properly (Singleton Testing Question).

As such, I do not want to execute this code when running our normal production code with the application. Therefore, I need to check in regular PHP code if the code being executed is being executed as part of a test or not.

Any suggestions on how to determine this? I thought about a defined variable tied to the presence of the test files themselves (we do not ship the tests to customers) but our developers need the Extension to work normally, while the CI server needs to run the tests.

Would a Global set in the PHPUnit.xml file be recommended? Other thoughts?

6 Answers 6

49

An alternative approach is to set a constant in the PHP section of your phpunit.xml.*:

<php>
   <const name="PHPUNIT_YOURAPPLICATION_TESTSUITE" value="true"/>
</php>

In your PHP application, you might then use the following check:

if (defined('PHPUNIT_YOURAPPLICATION_TESTSUITE') && PHPUNIT_YOURAPPLICATION_TESTSUITE)
{ 
    echo 'TestSuite running!';
}

First, we check that the constant is defined, secondly, we check the value of the constant.

Sign up to request clarification or add additional context in comments.

5 Comments

Keep in mind that PHP strangely evaluates an undefined constant as a string (if EXAMPLE isn't defined, it is evaluated as 'EXAMPLE'. Meaning, you should evaluate it properly using ===. It will still throw a notification about an undefined constant.
Won't === also match the type up, and since it's defined as true (a boolean) but he is checking if it is 1 (an integer), it will fail? I understand your comment, but in this example, == is better.
Thanks to your answer I found that (in Laravel 5) the APP_ENV is set to testing. This made it way really easy to implement your answer. I'd really recommend this solution for Laravel.
There is no version of PHP in which the string "PHPUNIT_YOURAPPLICATION_TESTSUITE" would be considered equal to 1. The only way a string would ever be considered equal to 1 is if it begins with a 1. Otherwise, it would be considered equal to 0 (or some other number), but not 1. There is no reason to use === here. Using == is perfectly "proper". (And as mentioned, === would not in fact work here)
Hm? PHPUNIT_YOURAPPLICATION_TESTSUITE is not a string, but a constant identifier. The second part of the comparision is the check of the value of the constant.
30

Define a constant in your PHPUnit bootstrap.php file. This is executed before loading or running any tests. This shouldn't impact developers running the application normally--just the unit tests.

Comments

14

If you're using Laravel than use App::runningUnitTests()

Comments

13

You could check the $argv different ways.

if(PHP_SAPI == 'cli') {

    if(strpos($_SERVER['argv'][0], 'phpunit') !== FALSE) { ... }
    // or
    if($_SERVER['argv'][0] == '/usr/bin/phpunit') { ... }

}

1 Comment

I used this in the autoloader option of phpunit, there you have no access to phpunit.xml or anything yet, so this works just great!
11

Use PHPUnit Constants

You can either define constant, but that requires your work, no typos and it's not generic. How to do it better?

PHPUnit defines 2 constants by itself:

if (! defined('PHPUNIT_COMPOSER_INSTALL') && ! defined('__PHPUNIT_PHAR__')) {
    // is not PHPUnit run
    return;
}

// is PHPUnit

3 Comments

Now that there are some PHPUnit values, this is a good solution as well. My answer is older before these were fully available. Using your own also allows you to know what test suite (if you have multiple) is running as an extra bonus.
In PHPUnit v3.7.28 the constant to check is 'PHPUnit_MAIN_METHOD'.
You should not use unsupported versions of PHPUnit nor PHP. It's dangerous - php.net/supported-versions.php
1

You could look for the constant PHPUNIT_COMPOSER_INSTALL defined by the official phpunit runner by using defined('PHPUNIT_COMPOSER_INSTALL'). There are also some global variables defined by phpunit.

But they could be undefined while using alternative runner (like IDE)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.