TestObjectItemTest.php in Drupal 9
File
core/modules/field/tests/src/Kernel/TestObjectItemTest.php
View source
<?php
namespace Drupal\Tests\field\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class TestObjectItemTest extends FieldKernelTestBase {
protected static $modules = [
'field_test',
];
protected function setUp() : void {
parent::setUp();
FieldStorageConfig::create([
'field_name' => 'field_test',
'entity_type' => 'entity_test',
'type' => 'test_object_field',
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
])
->save();
}
public function testTestObjectItem() {
$object = new \stdClass();
$object->foo = 'bar';
$entity = EntityTest::create();
$entity->field_test->value = $object;
$entity
->save();
$id = $entity
->id();
$entity = EntityTest::load($id);
$this
->assertInstanceOf(\stdClass::class, $entity->field_test->value);
$this
->assertEquals($object, $entity->field_test->value);
}
}