Configuring PHPUnit

Configuration File

PHPUnit is configured in a phpunit.xml configuration file. If bin/phpunit is run with no arguments, it will look for a phpunit.xml file in the root of the project.

You can also specify which is selected at runtime by using bin/phpunit -c path/to/phpunit.xml

Selecting which phpunit.xml PHPQA should use

PHPQA will run the project root phpunit.xml. Rather than maintain multiple copies of the file, you can symlink either

  • PHPQA's default file using ln -s vendor/edmondscommerce/phpqa/configDefaults/generic/phpunit.xml phpunit.xml
  • Your own custom file using cp vendor/edmondscommerce/phpqa/configDefaults/generic/phpunit.xml qaConfig/phpunit.xml; ln -s qaConfig/phpunit.xml phpunit.xml

Common Customisations

Root node parameters

These attributes can all be added to the root <phpunit /> node:

  • forceCoversAnnotation=""" - Doesn't deem application code to be covered unless a test provides an @covers annotation for its class, or method. Default value false; Recommended value: true
  • beStrictAboutCoversAnnotation - Requires that the test method has @covers or @uses annotations for all application code methods that are called. Default value false; Recommended value: false
  • timeoutForSmallTests=""" - the max seconds a Small test should take. Recommended value: 1
  • timeoutForMediumTests=""" - the max seconds a Medium test should take. Recommended value: 5
  • timeoutForLargeTests=""" - the max seconds a Medium test should take. Recommended value: 300
  • executionOrder="" - If using Test Dependencies this is required to be set to depends,defects Default value: default, Recommended value: depends,defects

Test Suites

Test Suites exist to group multiple related tests into something that can be run in isolation.

Once configured, you can tell PHPUnit to run each test suite using bin/phpunit testsuitename

The default provides only tests, which includes the tests/ folder.

You can also include Test Suites for Small, Medium and Large as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    <testsuites>
        <testsuite name="tests">
            <directory suffix="Test.php">../../../../../tests/</directory>
        </testsuite>
        <testsuite name="small">
            <directory suffix="Test.php">../../../../../tests/Small</directory>
        </testsuite>
        <testsuite name="medium">
            <directory suffix="Test.php">../../../../../tests/Medium</directory>
        </testsuite>
        <testsuite name="large">
            <directory suffix="Test.php">../../../../../tests/Large</directory>
        </testsuite>
    </testsuites>

You can also add your own for any other sets of tests you'd like to run as a group.

PHPUnit Test Suites documentation

Environment Config

You can declare environment variables such as DB credentials and other config directly in the phpunit.xml

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<phpunit>
    <php>
        <ini name="error_reporting" value="-1" />
        <env name="SHELL_VERBOSITY" value="-1" />
        <env name="dbUser"       value="test" />
        <env name="dbPass"       value="password" />
        <env name="dbName"       value="thing_test" />
        <env name="dbHost"       value="127.0.0.1" />
        <env name="entitiesPath" value="vendor/edmondscommerce/thing/src/Entities" />
        <server name="dbUser"       value="test" />
        <server name="dbPass"       value="password" />
        <server name="dbName"       value="thing_test" />
        <server name="dbHost"       value="127.0.0.1" />
        <server name="entitiesPath" value="vendor/edmondscommerce/thing/src/Entities" />
    </php>
</phpunit>

Runtime parameters

Stopping on Failure, Iterating

When working towards getting the tests passing, you can use the uniterate mode which will not generate coverage and will stop on failure. This makes for a much faster iteration cycle.

1
bin/qa -t uniterate