refactored tests, added EntityTrait
This commit is contained in:
parent
d81e1b3711
commit
c318c451dc
30
tests/src/Unit/EntityTrait.php
Normal file
30
tests/src/Unit/EntityTrait.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Drupal\Tests\opencase\Unit;
|
||||
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 $entityTypeId): MockObject {
|
||||
$storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->disableOriginalConstructor()->getMock();
|
||||
$entityTypeManager->method('getStorage')->with($entityTypeId)->willReturn($storage);
|
||||
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 setupContainer(array $services): void {
|
||||
$container = new ContainerBuilder();
|
||||
foreach ($services as $key => $mock) {
|
||||
$container->set($key, $mock);
|
||||
}
|
||||
\Drupal::setContainer($container);
|
||||
}
|
||||
}
|
@ -1,31 +1,31 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace Drupal\Tests\opencase\Unit;
|
||||
include "EntityTrait.php";
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\opencase_entities\Entity\OCActor;
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
|
||||
class OCActorTest extends UnitTestCase{
|
||||
|
||||
use EntityTrait;
|
||||
|
||||
public function setUp(): void {
|
||||
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('entity_type.manager', $this->entityTypeManager);
|
||||
\Drupal::setContainer($container);
|
||||
$this->actor = $this->getMockBuilder(OCActor::class)->disableOriginalConstructor()
|
||||
->onlyMethods(['id'])
|
||||
->getMock();
|
||||
$this->etm = $this->getEntityTypeManager();
|
||||
$this->setupContainer([
|
||||
'entity_type.manager' => $this->etm
|
||||
]);
|
||||
}
|
||||
|
||||
public function testGetCountOfCaseProvisions(){
|
||||
$caseProvisionStorage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->disableOriginalConstructor()->getMock();
|
||||
$this->entityTypeManager->expects($this->once())->method('getStorage')->with('oc_case_provision')->willReturn($caseProvisionStorage);
|
||||
$query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
|
||||
$caseProvisionStorage->method('getQuery')->willReturn($query);
|
||||
$this->actor->expects($this->once())->method('id')->willReturn(5);
|
||||
public function testGetCountOfCaseProvisions(): void{
|
||||
$storage = $this->getStorage($this->etm, 'oc_case_provision');
|
||||
$query = $this->getQuery($storage);
|
||||
$actor = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\\OCActor')->disableOriginalConstructor()
|
||||
->onlyMethods(['id'])
|
||||
->getMock();
|
||||
$actor->expects($this->once())->method('id')->willReturn(5);
|
||||
$query->expects($this->once())->method('condition')->with('oc_provider', 5);
|
||||
$query->expects($this->once())->method('execute')->willReturn([1,2,3,4]);
|
||||
$count = $this->actor->getCountOfCaseProvisions();
|
||||
$count = $actor->getCountOfCaseProvisions();
|
||||
$this->assertTrue($count == 4);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user