PHPUnit bootstrap in PhpStorm

I am working with Zend Framework 2 and I want to run tests for all of my modules in PhpStorm 5.0.4. I have installed PhpStorm to test the tests in myproject/module and it successfully finds my tests. The problem is that it does not read my configuration file in every module that is needed (it points to a bootstrap file).

Here is the directory structure for the module ( source ):

 /module /User /tests /UserTest /Model /UserTest.php Bootstrap.php phpunit.xml.dist TestConfig.php.dist 

When I run the test, it gives me an error because Bootstrap.php does not start until UserTest.php . All files are correct, because if I cd to /myproject/module/User/tests/ and run phpunit in the terminal, it works fine.

I would like it to use the configuration (and thus the load) in each module. I tried using the --configuration option with a relative path, but I could not get it to work.

Here is my current configuration:

PhpStorm PHPUnit configuration

Any pointers to how I can run the configuration file (and load) when the module is being tested? That is, the module has its own configuration file and boot file.

Thanks in advance.

+3
source share
3 answers

If I missed something, you will need to set up a test configuration for each module. In your case, you have myproject. Instead, you will need one for each module, and then configure the configuration for each (use an alternative configuration file).

+1
source

PHP Storm 7 assumes that you only need one boot file by default and thus does not allow separate bootsrap files WRONG for each PHPUnit test configuration.

However, zf2 conducts tests based on each module. Thus, after you set the default values ​​for the first module, other modules do not work. The way around this is

  • Delete default options in File | Settings | Php | PHPUnit You do not need to delete the default configuration file, but you must EMPTY OUT and uncheck the bootstrap file. Just unchecking will not be enough.

  • Go run | Edit Configurations (there are other shortcuts for this window) For each module you need to create a test configuration. For example, you will have a standard application module and, therefore, an “Application module test” for it, possibly an administrator module, and then a “Administrator module test” for this

  • For each test (assuming a standard zf2 directory structure)

a. Testing area: catalog b. Directory: C: \ wamp \ www \ PROJECT-NAME \ module \ MODULE-NAME \ test c. Check "Use an alternative configuration file:" e. Enter C: \ wamp \ www \ PROJECT-NAME \ module \ MODULE-NAME \ test \ MODULE-NAMETest \ phpunit.xml.dist e. In the "Test run parameters" enter "- -bootstrap C: \ wamp \ www \ PROJECT-NAME \ module \ MODULE-NAME \ test \ MODULE-NAMETest \ Bootstrap.php "

  • Repeat for next module

The problem is that as long as the bootstrap field has a record by default, phpstorm will add that default as the -bootstrap option AFTER what you specified in the specific Test Runner options. This way, no matter what you do, you end up running the wrong bootstrap file, with the exception of the first / standard test case

Hope this helps

+1
source

I use the environment variable parameter in the startup configuration to determine the value that I can use in the global bootstrap.php to pull out requirements specific to a given module or application section.

 class GlobalBootstrap { private static $applicationSections = [ 'section_a', 'section_b', 'section_c' ]; public static function init() { $localMethod = self::fetchLocalMethod(); if (!is_null($localMethod)) { self::$localMethod(); } else { throw new Exception( __CLASS__ . '::' . __FUNCTION__ . 'Says: No local method has been defined for this test section' ); } } private static function fetchLocalMethod() { $section = getenv('APPLICATION_SECTION'); if (is_null($section) || !in_array($section, self::$applicationSections)) { return null; } $section = preg_replace("/[^a-zA-Z]+/", "", $section); $method = 'bootstrap' . ucfirst(strtolower($section)); return $method; } /** * Section specific methods */ protected static function bootstrapSectiona() { require __DIR__ . '/../../{section_a}/module/Test/Bootstrap.php'; } } GlobalBootstrap::init(); 

Any arbitrary variable and value can be created and then specified in your bootstrap.php using: getevn (VARIABLE_NAME); This saves a lot of legacy configurations in PHPStorm, but can potentially be just as complicated if you rely on many different bootstrap features.

0
source

All Articles