简体   繁体   English

将PHPUnit与PHP-DI一起使用

[英]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. 我正在使用PHP-DI库,并且按照PHP-DI文档的建议,通过使用引导程序和配置文件 ,它对我的​​代码非常有用。

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. 所以现在我正在尝试使用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 . 我想在每个Test类中使用$ container,以便可以按照PHP-DI文档中的建议使用$ container-> injectOn($ this)注入每个测试的所有依赖项。

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 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? 我唯一的想法是我可以像我的主应用程序一样在setUp()中调用引导程序?

$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). 如果您的引导程序做了太多事情,可能会变得更加复杂,但是理想情况下,您的引导程序应该不做太多事情(只需配置容器并创建应用程序类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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