class ComplexDataNormalizerTest in Drupal 8
Same name and namespace in other branches
- 9 core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest
- 10 core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest
@coversDefaultClass \Drupal\serialization\Normalizer\ComplexDataNormalizer @group serialization
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest uses InternalTypedDataTestTrait
Expanded class hierarchy of ComplexDataNormalizerTest
File
- core/
modules/ serialization/ tests/ src/ Unit/ Normalizer/ ComplexDataNormalizerTest.php, line 19 - Contains \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest.
Namespace
Drupal\Tests\serialization\Unit\NormalizerView source
class ComplexDataNormalizerTest extends UnitTestCase {
use InternalTypedDataTestTrait;
/**
* Test format string.
*
* @var string
*/
const TEST_FORMAT = 'test_format';
/**
* The Complex data normalizer under test.
*
* @var \Drupal\serialization\Normalizer\ComplexDataNormalizer
*/
protected $normalizer;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->normalizer = new ComplexDataNormalizer();
}
/**
* @covers ::supportsNormalization
*/
public function testSupportsNormalization() {
$complex_data = $this
->prophesize(ComplexDataInterface::class)
->reveal();
$this
->assertTrue($this->normalizer
->supportsNormalization($complex_data));
// Also test that an object not implementing ComplexDataInterface fails.
$this
->assertFalse($this->normalizer
->supportsNormalization(new \stdClass()));
}
/**
* Test normalizing complex data.
*
* @covers ::normalize
*/
public function testNormalizeComplexData() {
$serializer_prophecy = $this
->prophesize(Serializer::class);
$non_internal_property = $this
->getTypedDataProperty(FALSE);
$serializer_prophecy
->normalize($non_internal_property, static::TEST_FORMAT, [])
->willReturn('A-normalized')
->shouldBeCalled();
$this->normalizer
->setSerializer($serializer_prophecy
->reveal());
$complex_data = $this
->prophesize(ComplexDataInterface::class);
$complex_data
->getProperties(TRUE)
->willReturn([
'prop:a' => $non_internal_property,
'prop:internal' => $this
->getTypedDataProperty(TRUE),
])
->shouldBeCalled();
$normalized = $this->normalizer
->normalize($complex_data
->reveal(), static::TEST_FORMAT);
$this
->assertEquals([
'prop:a' => 'A-normalized',
], $normalized);
}
/**
* Test normalize() where $object does not implement ComplexDataInterface.
*
* Normalizers extending ComplexDataNormalizer may have a different supported
* class.
*
* @covers ::normalize
*/
public function testNormalizeNonComplex() {
$normalizer = new TestExtendedNormalizer();
$serialization_context = [
'test' => 'test',
];
$serializer_prophecy = $this
->prophesize(Serializer::class);
$serializer_prophecy
->normalize('A', static::TEST_FORMAT, $serialization_context)
->willReturn('A-normalized')
->shouldBeCalled();
$serializer_prophecy
->normalize('B', static::TEST_FORMAT, $serialization_context)
->willReturn('B-normalized')
->shouldBeCalled();
$normalizer
->setSerializer($serializer_prophecy
->reveal());
$stdClass = new \stdClass();
$stdClass->a = 'A';
$stdClass->b = 'B';
$normalized = $normalizer
->normalize($stdClass, static::TEST_FORMAT, $serialization_context);
$this
->assertEquals([
'a' => 'A-normalized',
'b' => 'B-normalized',
], $normalized);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ComplexDataNormalizerTest:: |
protected | property | The Complex data normalizer under test. | |
ComplexDataNormalizerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ComplexDataNormalizerTest:: |
public | function | Test normalizing complex data. | |
ComplexDataNormalizerTest:: |
public | function | Test normalize() where $object does not implement ComplexDataInterface. | |
ComplexDataNormalizerTest:: |
public | function | @covers ::supportsNormalization | |
ComplexDataNormalizerTest:: |
constant | Test format string. | ||
InternalTypedDataTestTrait:: |
protected | function | Gets a typed data property. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed 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. |