Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit configuration file and auto_prepend_file

Tags:

php

phpunit

I'm trying to use the phpunit configuration file to set a specific php-ini setting. When I try to set the auto_prepend_file to a file that exist, having tested both relative path and absolute path, it doesn't work.

I does however work if I set it in the php-ini file. php.ini

[...]
auto_prepend_file=/path/to/project/config.php
[...]

What I'm trying to do is to load this config.php file before loading my testcase, because I have some functions and config variables I need access to. Maybe I can do it some other way than using auto_prepend_file!? Or do somebody know how to make it work this way!?

phpunit config file:

<phpunit>
  [...]
  <php>
    <ini name="auto_prepend_file" value="/path/to/project/config.php" />
  </php>
  [...]
</phpunit>

Thanks in advance!

like image 909
englund Avatar asked Mar 29 '26 22:03

englund


1 Answers

You can only use the <ini> tags to set values that can be set from within PHP using ini_set().

So only ini values that are PHP_INI_ALL. The auto_prepent_file directive is PHP_INI_PERDIR (see the docs) meaning it can't be set when php process is already running.


For including a file before the tests are run you have two options:

Ether use the --bootstrap file_to_run_before_tests_run.php option

or use php -d auto_prepend_file=yourfile.php /path/to/phpunit to run phpunit.

Both ways will ensure the file contents are executed before any tests are executed with --bootstrap being the more common way to do that.

like image 186
edorian Avatar answered Apr 03 '26 18:04

edorian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!