How To Run PHPUnit in 'test' Mode

hi,

I am trying to run unit tests for a module, and whilst this is working it appears to be running in ‘dev’ environment mode instead of ‘test’.

I am using my own docker setup, GitHub - gordonbanderson/SilverStripe-Module-Testing-Suite: Run silverstripe module unit tests in a Dockerized environment

To run tests with the web container I execute the following:

phpdbg -qrr vendor/bin/phpunit -d memory_limit=4G --coverage-html report   tests

If I add the following code to any arbitrary test, the result is MODE: DEV being returned in the output.

if (Director::isDev()) {
            error_log('MODE: DEV');
        }

        if (Director::isTest()) {
            error_log('MODE: TEST');
        }

        if (Director::isLive()) {
            error_log('MODE: LIVE');
        }

I wish to add test only config to a couple of modules, but I cannot figure out how to run the tests in the ‘test’ environment. Please note that I have set

SS_ENVIRONMENT_TYPE="test"

in the .env file prior to running dev/build flush=all and then running the tests

Cheers

Gordon

Hi,

To do this you’d have to override the Kernel class that’s used for running tests. By default it’s TestKernel, and it’s enforcing DEV environment. You might be able to do this by overriding SapphireTest. However, I would not recommend you doing this! This may have a lot of unintended side effects.

I’d rather suggest you to introduce a custom application-specific constant or an ENV var for your container environment if you need.

The environment type “test” is rather reserved for “UAT”, and not for running auto tests.

hi,

Thanks for the heads up, a misunderstanding on my part. I’ve finally hit this hurdle again and here is how I solved it, re Travis.

  1. Add test only config via an env flag,see github dot com/gordonbanderson/silverstripe-manticore-search/blob/master/_config/testing.yml (line 6) - the test config will only be invoked locally if export UNIT_TESTING_CONFIG_ENABLED=1 has been run locally (and then a command line flush invoked)
  2. For CI set the flag in Travis in the same manner, see github dot com/gordonbanderson/silverstripe-manticore-search/blob/master/.travis.yml (line 46)
  3. Invoke flush when running the tests, lines 55 and 56 of the previous link

This took me a bit of faffing around to figure out, so this reply is in part documentation for the community. Should I create a minimal example and create a PR against the core SilverStripe documentation?

Cheers, and thanks for the heads up ^^

Gordon

And just FYI the forum software blocked me from using github dot com links directly :frowning: