diff --git a/src/Utils.php b/src/Utils.php index 4c68218..6cfe828 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -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]; diff --git a/tests/src/Unit/TimeBasedFieldUpdaterTest.php b/tests/src/Unit/TimeBasedFieldUpdaterTest.php index 6615e98..cc22ad8 100644 --- a/tests/src/Unit/TimeBasedFieldUpdaterTest.php +++ b/tests/src/Unit/TimeBasedFieldUpdaterTest.php @@ -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(); diff --git a/tests/src/Unit/UtilsTest.php b/tests/src/Unit/UtilsTest.php new file mode 100644 index 0000000..7969ede --- /dev/null +++ b/tests/src/Unit/UtilsTest.php @@ -0,0 +1,34 @@ +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"]]); + } +} \ No newline at end of file