class EntityReferenceFieldItemNormalizerTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest
@coversDefaultClass \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer @group serialization
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest
Expanded class hierarchy of EntityReferenceFieldItemNormalizerTest
File
- core/
modules/ serialization/ tests/ src/ Unit/ Normalizer/ EntityReferenceFieldItemNormalizerTest.php, line 22 - Contains \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest.
Namespace
Drupal\Tests\serialization\Unit\NormalizerView source
class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
/**
* The mock serializer.
*
* @var \Symfony\Component\Serializer\SerializerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $serializer;
/**
* The normalizer under test.
*
* @var \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
*/
protected $normalizer;
/**
* The mock field item.
*
* @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem|\Prophecy\Prophecy\ObjectProphecy
*/
protected $fieldItem;
/**
* {@inheritdoc}
*/
public function setUp() {
$this->normalizer = new EntityReferenceFieldItemNormalizer();
$this->serializer = $this
->prophesize(Serializer::class);
// Set up the serializer to return an entity property.
$this->serializer
->normalize(Argument::cetera())
->willReturn([
'value' => 'test',
]);
$this->normalizer
->setSerializer($this->serializer
->reveal());
$this->fieldItem = $this
->prophesize(EntityReferenceItem::class);
$this->fieldItem
->getIterator()
->willReturn(new \ArrayIterator([
'target_id' => [],
]));
}
/**
* @covers ::supportsNormalization
*/
public function testSupportsNormalization() {
$this
->assertTrue($this->normalizer
->supportsNormalization($this->fieldItem
->reveal()));
$this
->assertFalse($this->normalizer
->supportsNormalization(new \stdClass()));
}
/**
* @covers ::normalize
*/
public function testNormalize() {
$test_url = '/test/100';
$entity = $this
->prophesize(EntityInterface::class);
$entity
->url('canonical')
->willReturn($test_url)
->shouldBeCalled();
$entity_reference = $this
->prophesize(TypedDataInterface::class);
$entity_reference
->getValue()
->willReturn($entity
->reveal())
->shouldBeCalled();
$this->fieldItem
->get('entity')
->willReturn($entity_reference)
->shouldBeCalled();
$normalized = $this->normalizer
->normalize($this->fieldItem
->reveal());
$expected = [
'target_id' => [
'value' => 'test',
],
'url' => $test_url,
];
$this
->assertSame($expected, $normalized);
}
/**
* @covers ::normalize
*/
public function testNormalizeWithNoEntity() {
$entity_reference = $this
->prophesize(TypedDataInterface::class);
$entity_reference
->getValue()
->willReturn(NULL)
->shouldBeCalled();
$this->fieldItem
->get('entity')
->willReturn($entity_reference
->reveal())
->shouldBeCalled();
$normalized = $this->normalizer
->normalize($this->fieldItem
->reveal());
$expected = [
'target_id' => [
'value' => 'test',
],
];
$this
->assertSame($expected, $normalized);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityReferenceFieldItemNormalizerTest:: |
protected | property | The mock field item. | |
EntityReferenceFieldItemNormalizerTest:: |
protected | property | The normalizer under test. | |
EntityReferenceFieldItemNormalizerTest:: |
protected | property | The mock serializer. | |
EntityReferenceFieldItemNormalizerTest:: |
public | function |
Overrides UnitTestCase:: |
|
EntityReferenceFieldItemNormalizerTest:: |
public | function | @covers ::normalize | |
EntityReferenceFieldItemNormalizerTest:: |
public | function | @covers ::normalize | |
EntityReferenceFieldItemNormalizerTest:: |
public | function | @covers ::supportsNormalization | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |