lundi 25 juillet 2016

Phake framework: how to clone objects on captureAll?

\Phake::captureAll returns an array of parameters used in consecutive calls. It works fine with scalars, or when different objects are being passed, but is not very useful when the same object is being used. It happens quite often for data mapper mocks, when CUT modifies and persists an object several times.

In the following example I am trying to assert that the first $mock->fooWithArgument was called with expected arguments, but cannot find a way to do so:

public function testArgumentCapturingAllValls()
{
    $mock = \Phake::mock('PhakeTest_MockedClass');
    $obj1 = new \stdClass;
    $obj1->bar = 1;
    $mock->fooWithArgument($obj1);
    $obj1->bar = 2;
    $mock->fooWithArgument($obj1);
    \Phake::verify($mock, \Phake::atLeast(1))->fooWithArgument(\Phake::captureAll($toArgument));

    $this->assertEquals(1, $toArgument[0]->bar);  //fails, as both elements point to the same instance
}

Aucun commentaire:

Enregistrer un commentaire