Added superficial tests for addConditionsToQuery

This commit is contained in:
naomi 2022-05-15 19:22:30 +01:00
parent b000e9414b
commit f1965ff3c1
3 changed files with 45 additions and 9 deletions

View File

@ -6,20 +6,22 @@ use \Drupal;
use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use RuntimeException;
class Utils { class Utils {
// public function __construct( public function __construct(
// EntityTypeManager $entityTypeManager = null EntityTypeManagerInterface $entityTypeManager = null
// ) { ) {
// if ($entityTypeManager == null) { if ($entityTypeManager == null) {
// $entityTypeManager = Drupal::entityTypeManager(); $entityTypeManager = Drupal::entityTypeManager();
// } }
// $this->entityTypeManager = $entityTypeManager; $this->entityTypeManager = $entityTypeManager;
// } }
public function addConditionsToQuery(QueryInterface $query, array $conditions): void { public function addConditionsToQuery(QueryInterface $query, array $conditions): void {
foreach($conditions as $condition) { foreach($conditions as $condition) {
if (sizeof($condition) != 3) throw new RuntimeException('Utils::addConditionsToQuery needs each condition to consist of 3 strings');
$field = $condition[0]; $field = $condition[0];
$value = $condition[1]; $value = $condition[1];
$operator = $condition[2]; $operator = $condition[2];

View File

@ -9,7 +9,7 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
function setUp():void { function setUp():void {
/** @var \Drupal\opencase\Utils&\PHPUnit\Framework\MockObject\MockObject $utils */ /** @var \Drupal\opencase\Utils&\PHPUnit\Framework\MockObject\MockObject $utils */
$this->utils = $this->getMockBuilder('\\Drupal\\opencase\\Utils')->getMock(); $this->utils = $this->getMockBuilder('\\Drupal\\opencase\\Utils')->disableOriginalConstructor()->getMock();
/** @var \Drupal\core\Entity\EntityTypeManagerInterface&\PHPUnit\Framework\MockObject\MockObject $entityTypeManager */ /** @var \Drupal\core\Entity\EntityTypeManagerInterface&\PHPUnit\Framework\MockObject\MockObject $entityTypeManager */
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock(); $this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock(); $this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();

View File

@ -0,0 +1,34 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\opencase\Utils;
class UtilsTest extends UnitTestCase{
public function setUp(): void {
/** @var \Drupal\core\Entity\EntityTypeManager&\PHPUnit\Framework\MockObject\MockObject $entityTypeManager */
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$this->utils = new Utils($this->entityTypeManager);
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();
$this->query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
}
public function testGetTidByNameGetsTid():void {
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
$term_entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$term_entity->expects($this->once())->method('id')->willReturn('3');
$this->storage->expects($this->once())->method('loadByProperties')->with(['name' => 'foo', 'vid' => 'bar'])
->willReturn([$term_entity]);
$this->assertEquals($this->utils->getTidByName('foo', 'bar'), 3);
}
public function testAddConditionToQueryExceptsIfNotGivenArraysOf3Things():void {
$this->expectException(\RuntimeException::class);
$this->utils->addConditionsToQuery($this->query, [["1","2"], ["1"]]);
}
public function testAddConditionToQueryCallsAddsTheRightAmountOfConditions():void {
$this->query->expects($this->exactly(4))->method('condition');
$this->utils->addConditionsToQuery($this->query, [["1","2","3"], ["lk", "n", "kk"], ['sfd', 'ds', 'fds'], ["1","2","3"]]);
}
}