PHPUnit function toArray() of mocked ArrayCollection returns null
The toArray()
function of the ArrayCollection
class in PHPUnit returns null
when called on a mocked instance of the class. This is because the toArray()
function is not implemented in the mock object.
To fix this issue, you can stub the toArray()
function to return an array. For example:
$arrayCollection = $this->getMockBuilder(ArrayCollection::class)
->disableOriginalConstructor()
->getMock();
$arrayCollection->method('toArray')
->willReturn(['foo', 'bar']);
$this->assertEquals(['foo', 'bar'], $arrayCollection->toArray());
Note: The disableOriginalConstructor()
method is used to prevent the mock object from calling the original constructor of the ArrayCollection
class. This is necessary because the original constructor initializes the ArrayCollection
object with an empty array, which would cause the toArray()
function to return null
.