/** * Returns a mock entity for testing. * * @param string $class * The class name to mock. Should be \Drupal\Core\Entity\Entity or a * subclass. * @param array $values * An array of entity values to construct the mock entity with. * @param array $methods * (optional) An array of additional methods to mock on the entity object. * The getEntityType() and entityManager() methods are always mocked. * * @return \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject */ protected function getEntity($class, array $values, array $methods = []) { $methods = array_merge($methods, ['getEntityType', 'entityManager']); // Prophecy does not allow prophesizing abstract classes while actually // calling their code. We use Prophecy below because that allows us to // add method prophecies later while still revealing the prophecy now. $entity = $this->getMockBuilder($class)->setConstructorArgs([$values, $this->entityTypeId])->setMethods($methods)->getMockForAbstractClass(); $this->entityType = $this->prophesize(EntityTypeInterface::class); $this->entityType->getLinkTemplates()->willReturn([]); $this->entityType->getKey('langcode')->willReturn(FALSE); $entity->method('getEntityType')->willReturn($this->entityType->reveal()); $this->entityManager = $this->prophesize(EntityManagerInterface::class); $entity->method('entityManager')->willReturn($this->entityManager->reveal()); return $entity; }