DimensionsItemTest.php in Physical Fields 8
File
tests/src/Kernel/DimensionsItemTest.php
View source
<?php
namespace Drupal\Tests\physical\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\physical\Comparator\MeasurementComparator;
use Drupal\physical\Length;
use SebastianBergmann\Comparator\Factory as PhpUnitComparatorFactory;
class DimensionsItemTest extends EntityKernelTestBase {
public static $modules = [
'physical',
];
protected function setUp() {
parent::setUp();
$factory = PhpUnitComparatorFactory::getInstance();
$factory
->register(new MeasurementComparator());
$field_storage = FieldStorageConfig::create([
'field_name' => 'test_dimensions',
'entity_type' => 'entity_test',
'type' => 'physical_dimensions',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_name' => 'test_dimensions',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
]);
$field
->save();
}
public function testField() {
$entity = EntityTest::create([
'test_dimensions' => [
'length' => '5',
'width' => '7',
'height' => '2',
'unit' => 'in',
],
]);
$entity
->save();
$entity = $this
->reloadEntity($entity);
$item = $entity
->get('test_dimensions')
->first();
$length = $item
->getLength();
$this
->assertInstanceOf(Length::class, $length);
$this
->assertEquals(new Length('5', 'in'), $length);
$width = $item
->getWidth();
$this
->assertInstanceOf(Length::class, $width);
$this
->assertEquals(new Length('7', 'in'), $width);
$height = $item
->getHeight();
$this
->assertInstanceOf(Length::class, $height);
$this
->assertEquals(new Length('2', 'in'), $height);
}
}