RadioactivityFieldTypeTest.php in Radioactivity 8.3
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;
public 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
->assertEquals($defaultEnergy, $this->entity
->get('field_radioactivity')->energy);
$this
->assertEquals(1000, $this->entity
->get('field_radioactivity')->timestamp);
$this
->setRequestTime(1010);
$this->entity
->save();
$this
->assertEquals(1000, $this->entity
->get('field_radioactivity')->timestamp);
$this
->setRequestTime(1020);
$this->entity
->get('field_radioactivity')->energy = 88;
$this->entity
->save();
$this
->assertEquals(88, $this->entity
->get('field_radioactivity')->energy);
$this
->assertEquals(1020, $this->entity
->get('field_radioactivity')->timestamp);
}
private function setRequestTime(int $time) {
$this->requestTime = $time;
}
}