class someTest {
function setUp() {
$this->dependency = $this->getMock('DependencyInterface');
$this->testing = new classUnderTesting($dependency);
}
function test() {
$objects = someFixture::Sample();
$this->dependency->expects($this->at(0))
->method('something')
->with($objects);
$this->testing->someMethod();
}
}
class classUnderTesting {
// Constructor stores the dependency.
function someMethod() {
// Same data but not an identical object.
$objects = SomeOtherSource::returningTheSameAsSample();
$this->dependency->something($objects);
foreach ($objects as $object) {
$object->mutate();
}
return $objects;
}
}
This fails phpunit because the ObjectComparator
is called twice: once when $this->dependency->something
is called with $objects
which passes just fine but second time after the testing method finishes and __phpunit_verify
fires and now $objects
have mutated. And they are stored in phpunit as well and because the way PHP5 works, they are the same objects so they have mutated so now they don't match. How can I unit test this? I found enableArgumentCloning
and added it to $this->dependency
but it didn't change a thing.
The exact same issue was filed https://github.com/sebastianbergmann/phpunit/issues/1394 which is now closed in favor of https://github.com/sebastianbergmann/phpunit-mock-objects/issues/181 .
For now, I have simply applied the same mutate
to the $objects
in someTest::test
at the very end of the test method. It seems nothing else exists ATM.