简体   繁体   English

PHPUnit模拟对象替换Real类

[英]PHPUnit Mock Object replacing Real Class

I have a couple of tests in my test suite being run in Zend_Test 我的测试套件中有几个测试正在Zend_Test中运行

One test creates a mock of Foo_Bar_Baz via PHPUnit's MockBuilder as that class its dependency. 一个测试通过PHPUnit的MockBuilder创建了Foo_Bar_Baz的模拟,作为该类的依赖。 Now in the test for Foo_BAR_baz (the change in case is on purpose and is necessary due to autoloading), I am not getting the class but rather an instance of the mock, which obviously doesn't work. 现在在Foo_BAR_baz的测试中(大小写更改是有意的,由于自动加载而有必要进行更改),我没有得到该类,而是得到了该模拟的实例,这显然不起作用。

Doing a var_dump on the object results in class Foo_Bar_Baz#27115(0) { } so it appears to be creating an instance of the mock. 在对象上执行var_dump导致class Foo_Bar_Baz#27115(0) { }因此它似乎是在创建模拟实例。

Fixing the case of in the mock gets things to work as expected. 修复模拟中的大小写会使事情按预期工作。 I have never seen this behavior in mocking objects before. 我以前从未在模拟对象中看到过这种行为。 For some reason the mocked class is being loaded into PHP so that when the next test tries to instantiate the real object it instantiates the mock instead. 由于某种原因,已将模拟的类加载到PHP中,以便在下一个测试尝试实例化真实对象时,它将实例化模拟。 Why would this be happening? 为什么会这样呢?

Class names in PHP are not case-sensitive, but filenames on a *nix server are. PHP中的类名称不区分大小写,但* nix服务器上的文件名则区分大小写。

I suspect the change in case is causing a change in behaviour because of an autoload mechanism; 我怀疑这种情况的改变是由于自动加载机制引起的行为改变。 PHP would only autoload one of Foo_Bar_Baz.php and Foo_BAR_Baz.php. PHP仅会自动加载Foo_Bar_Baz.php和Foo_BAR_Baz.php之一。

In your case, if you have already defined a class Foo_BAR_Baz (as a mock) then PHP will use the same definition for Foo_Bar_Baz, thereby ignoring your real class definition. 在您的情况下,如果您已经定义了Foo_BAR_Baz类(作为模拟),则PHP将对Foo_Bar_Baz使用相同的定义,从而忽略了您的真实类定义。

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

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