This repository has been archived on 2022-07-12. You can view files and clone it, but cannot push or open issues or pull requests.
opencase/tests/src/Unit/EntityTrait.php

38 lines
1.5 KiB
PHP
Raw Normal View History

2022-06-05 12:15:33 +00:00
<?php
namespace Drupal\Tests\opencase\Unit;
2022-06-05 12:15:33 +00:00
use PHPUnit\Framework\MockObject\MockObject;
use Drupal\Core\DependencyInjection\ContainerBuilder;
trait EntityTrait {
public function getEntityTypeManager() {
return $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
}
public function getStorage(MockObject $entityTypeManager, string $entityTypeToExpect = ''): MockObject {
2022-06-05 12:15:33 +00:00
$storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->disableOriginalConstructor()->getMock();
$entityTypeManager->method('getStorage')->willReturn($storage);
if ($entityTypeToExpect) {
$entityTypeManager->expects($this->any())->method('getStorage')->with($entityTypeToExpect)->willReturn($storage);
}
2022-06-05 12:15:33 +00:00
return $storage;
}
public function getQuery(MockObject $storage): MockObject {
$query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
$storage->method('getQuery')->willReturn($query);
return $query;
}
public function getEntity(): MockObject {
return $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
}
public function getContainer(array $services): ContainerBuilder {
2022-06-05 12:15:33 +00:00
$container = new ContainerBuilder();
foreach ($services as $key => $mock) {
$container->set($key, $mock);
}
\Drupal::setContainer($container);
return $container;
2022-06-05 12:15:33 +00:00
}
}