我发现了关于phpunit Mock的奇怪结果
我问自己这个错误是由serialize()
使用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);
}
}
答案 0 :(得分:1)
如PHP docs中所述,私有和受保护成员在序列化期间前面加上*
或类名。这些前置值在两边都有空字节。
这意味着,虽然眼睛不可见,但字符串的实际字节表示会发生变化。例如,使用bin2hex
:
class Foo
{
public $value;
protected $one;
private $two;
}
$serialized = serialize(new Foo());
$expected = 'O:3:"Foo":3:{s:5:"value";N;s:6:"*one";N;s:8:"Footwo";N;}';
echo $serialized; // O:3:"Foo":3:{s:5:"value";N;s:6:"*one";N;s:8:"Footwo";N;}
echo $expected; // O:3:"Foo":3:{s:5:"value";N;s:6:"*one";N;s:8:"Footwo";N;}
echo bin2hex($serialized); // 4f3a333a22466f6f223a333a7b733a353a2276616c7565223b4e3b733a363a22002a006f6e65223b4e3b733a383a2200466f6f0074776f223b4e3b7d
echo bin2hex($expected); // 4f3a333a22466f6f223a333a7b733a353a2276616c7565223b4e3b733a363a222a6f6e65223b4e3b733a383a22466f6f74776f223b4e3b7d
您可以清楚地看到一个字符串比另一个字符串长。如果您查看描述受保护$one
属性的片段,您可以发现空字节:
s:6:"*one";N
733a363a22002a006f6e65223b4e
733a363a22 2a 6f6e65223b4e
现在你知道了差异的来源,让我们来解决你的问题。
通过实现Serializable接口,您可以使用serialize()
和unserialize()
返回表示对象的序列化数组。由于数组的所有值都是公共的,因此不会在字符串中插入空字节,因此您可以安全地比较它。您的问题已解决:
class Foo implements Serializable
{
public $value;
protected $one;
private $two;
public function serialize()
{
return serialize([$this->value, $this->one, $this->two]);
}
public function unserialize($str)
{
list($this->value, $this->one, $this->two) = unserialize($str);
}
}
// true
var_dump(serialize(new Foo()) === 'C:3:"Foo":24:{a:3:{i:0;N;i:1;N;i:2;N;}}');
希望这有帮助。