This repository has been archived on 2022-07-12. You can view files and clone it, but cannot push or open issues or pull requests.
opencase/tests/src/Unit/TimeBasedFieldUpdaterTest.php

35 lines
2.2 KiB
PHP

<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\opencase\TimeBasedFieldUpdater;
class TimeBasedFieldUpdaterTest extends UnitTestCase{
function setUp():void {
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$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);
$this->storage->method('getQuery')->willReturn($this->query);
}
function testFieldIsUpdatedOnEntityReturnedByQuery():void {
$this->query->method('execute')->willReturn([1]);
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity);
$updater = new TimeBasedFieldUpdater($this->entityTypeManager, 'dummy_entity', 'dummy_date_field');
$updater->update('3 months', ['dummy_status_field' => 3], ['dummy_status_field' => 4]);
$this->assertEquals($this->entity->dummy_status_field, 4);
}
function testFieldIsUpdatedOnAllEntitiesReturnedByQuery():void {
$this->query->method('execute')->willReturn([1, 2]);
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$this->entity2 = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$this->storage->method('load')->willReturnMap([[1, $this->entity], [2, $this-> entity2]]);
$updater = new TimeBasedFieldUpdater($this->entityTypeManager, 'dummy_entity', 'dummy_date_field');
$updater->update('3 months', ['dummy_status_field' => 3], ['dummy_status_field' => 4]);
$this->assertEquals($this->entity->dummy_status_field, 4);
$this->assertEquals($this->entity2->dummy_status_field, 4);
}
}