Added superficial tests for addConditionsToQuery
This commit is contained in:
parent
b000e9414b
commit
f1965ff3c1
@ -6,20 +6,22 @@ use \Drupal;
|
||||
use Drupal\Core\Entity\EntityTypeManager;
|
||||
use Drupal\Core\Entity\Query\QueryInterface;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use RuntimeException;
|
||||
|
||||
class Utils {
|
||||
|
||||
// public function __construct(
|
||||
// EntityTypeManager $entityTypeManager = null
|
||||
// ) {
|
||||
// if ($entityTypeManager == null) {
|
||||
// $entityTypeManager = Drupal::entityTypeManager();
|
||||
// }
|
||||
// $this->entityTypeManager = $entityTypeManager;
|
||||
// }
|
||||
public function __construct(
|
||||
EntityTypeManagerInterface $entityTypeManager = null
|
||||
) {
|
||||
if ($entityTypeManager == null) {
|
||||
$entityTypeManager = Drupal::entityTypeManager();
|
||||
}
|
||||
$this->entityTypeManager = $entityTypeManager;
|
||||
}
|
||||
|
||||
public function addConditionsToQuery(QueryInterface $query, array $conditions): void {
|
||||
foreach($conditions as $condition) {
|
||||
if (sizeof($condition) != 3) throw new RuntimeException('Utils::addConditionsToQuery needs each condition to consist of 3 strings');
|
||||
$field = $condition[0];
|
||||
$value = $condition[1];
|
||||
$operator = $condition[2];
|
||||
|
@ -9,7 +9,7 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
||||
|
||||
function setUp():void {
|
||||
/** @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 */
|
||||
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
|
||||
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();
|
||||
|
34
tests/src/Unit/UtilsTest.php
Normal file
34
tests/src/Unit/UtilsTest.php
Normal 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"]]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user