【发布时间】:2016-04-29 08:53:04
【问题描述】:
我发现了关于 phpunit Mock 的奇怪结果
我问自己这个错误是否是由 serialize() 中的 UTF8 字符引起的
当使用private 或protected 序列化对象时,模拟返回类似这样的内容
Expectation failed for method name is equal to <string:error> when invoked zero or more times
Parameter 0 for invocation Bar::error(Binary String: 0x4f3a333a22466...b4e3b7d) does not match expected value.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'O:6:"Foo":1:{s:5:"title";N;}'
+Binary String: 0x4f3a333a22466f6f223a313a7b733a32303a22002a00666f6f50726f74656374656456616c7565223b4e3b7d
代码
class Foo
{
public $fooPublicValue;
protected $fooProtectedValue; //BREAK
private $fooPrivateValue; //BREAK
}
class Bar
{
public function error($message)
{
//some process
}
}
class Baz
{
public function exec(Bar $bar)
{
$bar->error(serialize(new Foo()));
}
}
class BazTest extends \PHPUnit_Framework_TestCase
{
public function testExec()
{
$loggerMock = $this->getMockBuilder('Bar')
->getMock();
$loggerMock
->method('error')
->with($this->equalTo('O:6:"Foo":1:{s:5:"title";N;}'));
(new Baz())->exec($loggerMock);
}
}
【问题讨论】:
-
嗯...请问您有什么问题?
标签: php unit-testing serialization mocking phpunit