vendredi 29 avril 2016

PHPunit Bug Mock assert Binary String

i have found weird result about phpunit Mock

I ask myself if this bug is cause by UTF8 char in serialize()

When serialize object with private or protected the mock return something like this

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

Code

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);
    }
}

Aucun commentaire:

Enregistrer un commentaire