PHPunit Bug Mock断言二进制字符串


PHPunit Bug Mock assert Binary String

我发现了关于phpunit Mock 的奇怪结果

我问自己这个错误是否是由serialize() 中的UTF8字符引起的

当用privateprotected序列化对象时,mock返回类似于以下的内容

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文档中所提到的,私有成员和受保护成员在序列化过程中以*或类名作为前缀。这些前缀值的两边都有空字节。

更多细节:

这意味着,虽然肉眼看不见,但字符串的实际字节表示会发生变化。例如,使用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;}}');

希望这能有所帮助。