class ComplexDataNormalizerTest in Drupal 10
Same name and namespace in other branches
- 8 core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest
- 9 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 \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitWarnings
- 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() : void {
$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()));
}
/**
* Tests 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);
}
/**
* Tests 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 | Tests normalizing complex data. | |
ComplexDataNormalizerTest:: |
public | function | Tests 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. | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 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. | |
UnitTestCase:: |
public static | function |