RadioactivityFieldTypeTest.php in Radioactivity 4.0.x
File
tests/src/Kernel/RadioactivityFieldTypeTest.php
View source
<?php
namespace Drupal\Tests\radioactivity\Kernel;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\Tests\radioactivity\Traits\RadioactivityFunctionTestTrait;
class RadioactivityFieldTypeTest extends FieldKernelTestBase {
use RadioactivityFunctionTestTrait;
protected static $modules = [
'radioactivity',
'entity_test',
];
protected $entityViewDisplay;
protected $entity;
protected $requestTime;
protected function setUp() : void {
parent::setUp();
$dateTime = $this
->getMockBuilder(TimeInterface::class)
->disableOriginalConstructor()
->getMock();
$dateTime
->expects($this
->any())
->method('getRequestTime')
->willReturnCallback(function () {
return $this->requestTime;
});
$this->container
->set('datetime.time', $dateTime);
}
public function testSave() {
$defaultEnergy = 99;
$this
->createEnergyField('field_radioactivity', 'count', TRUE, $defaultEnergy);
$this
->setRequestTime(1000);
$this->entity = EntityTest::create();
$this->entity
->save();
$this
->assertEqual($this->entity
->get('field_radioactivity')->energy, $defaultEnergy);
$this
->assertEqual($this->entity
->get('field_radioactivity')->timestamp, 1000);
$this
->setRequestTime(1010);
$this->entity
->save();
$this
->assertEqual($this->entity
->get('field_radioactivity')->timestamp, 1000);
$this
->setRequestTime(1020);
$this->entity
->get('field_radioactivity')->energy = 88;
$this->entity
->save();
$this
->assertEqual($this->entity
->get('field_radioactivity')->energy, 88);
$this
->assertEqual($this->entity
->get('field_radioactivity')->timestamp, 1020);
}
private function setRequestTime(int $time) {
$this->requestTime = $time;
}
}