如果 Doctrine 包含项目但 $arrayCollection->toArray() 返回 null,我想创建 ArrayCollection 的 MockObject。我的代码如下:
$subEntity = $this->createMock(SubEntity::class);
$arrayCollection = $this->getMockBuilder(ArrayCollection::class)
->setConstructorArgs([
[$subEntity]
])
->getMock();
$entity = $this->createMock(Entity::class);
$entity
->expects($this->once())
->method('getSubEntities')
->willReturn($arrayCollection);
$testingClass = new TestingClass();
$result = $testingClass->converts($entity);
$this->assertInstanceOf(OtherEntity::class, $result);
这是converts()
函数:
class TestingClass {
...
public function converts(iterable $object){
return array_map(
function ($item) { ... },
$object->toArray()
);
}
....
}
运行测试时出现错误:TypeError: array_map(): 参数 #2 ($array) 必须是数组类型,给定为 null
有人有想法吗?