简体   繁体   中英

Using PHPUnit with PHP-DI

I'm using the PHP-DI library and it works great for my code by using a bootstrap and config file , as recommended by the PHP-DI documentation.

Then, in my app, I'm getting the container and kicking things off by calling the bootstrap:

$container = require __DIR__ . '/Container/bootstrap.php';
$main = $container->get( Main::class );

So now I'm trying to set up unit testing with PHPUnit.

I'd like to get ahold of the $container in each Test class so that I can inject all of the dependencies of each test by using $container->injectOn($this) as suggested in the PHP-DI documentation .

I stumbled on this open issue [which is mostly over my head], but it is still open and doesn't seem to have any recommendations.

https://github.com/PHP-DI/PHP-DI/issues/125

My only thoughts are that I could just call bootstrap in setUp() the same way as my main app?

$container = require __DIR__ . '/Container/bootstrap.php';

Is this what I should be doing? Is there a more elegant or recommended way to do this?

Thanks!

Yes bootstrapping your container in your test is perfectly OK. If your bootstrap is doing too many things that might get more complicated, but ideally your bootstrap shouldn't do much (just configure the container and create the application class).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM