refactored tests, added EntityTrait

This commit is contained in:
naomi 2022-06-05 13:15:33 +01:00
parent d81e1b3711
commit c318c451dc
2 changed files with 46 additions and 16 deletions

View 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);
}
}

View File

@ -1,31 +1,31 @@
<?php declare(strict_types = 1); <?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit; namespace Drupal\Tests\opencase\Unit;
include "EntityTrait.php";
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\opencase_entities\Entity\OCActor;
use Drupal\Core\DependencyInjection\ContainerBuilder;
class OCActorTest extends UnitTestCase{ class OCActorTest extends UnitTestCase{
use EntityTrait;
public function setUp(): void { public function setUp(): void {
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock(); $this->etm = $this->getEntityTypeManager();
$container = new ContainerBuilder(); $this->setupContainer([
$container->set('entity_type.manager', $this->entityTypeManager); 'entity_type.manager' => $this->etm
\Drupal::setContainer($container); ]);
$this->actor = $this->getMockBuilder(OCActor::class)->disableOriginalConstructor()
->onlyMethods(['id'])
->getMock();
} }
public function testGetCountOfCaseProvisions(){ public function testGetCountOfCaseProvisions(): void{
$caseProvisionStorage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->disableOriginalConstructor()->getMock(); $storage = $this->getStorage($this->etm, 'oc_case_provision');
$this->entityTypeManager->expects($this->once())->method('getStorage')->with('oc_case_provision')->willReturn($caseProvisionStorage); $query = $this->getQuery($storage);
$query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock(); $actor = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\\OCActor')->disableOriginalConstructor()
$caseProvisionStorage->method('getQuery')->willReturn($query); ->onlyMethods(['id'])
$this->actor->expects($this->once())->method('id')->willReturn(5); ->getMock();
$actor->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->once())->method('condition')->with('oc_provider', 5); $query->expects($this->once())->method('condition')->with('oc_provider', 5);
$query->expects($this->once())->method('execute')->willReturn([1,2,3,4]); $query->expects($this->once())->method('execute')->willReturn([1,2,3,4]);
$count = $this->actor->getCountOfCaseProvisions(); $count = $actor->getCountOfCaseProvisions();
$this->assertTrue($count == 4); $this->assertTrue($count == 4);
} }
} }