class PrimitiveDataNormalizerTest

Same name and namespace in other branches
  1. 9 core/modules/serialization/tests/src/Unit/Normalizer/PrimitiveDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\PrimitiveDataNormalizerTest
  2. 8.9.x core/modules/serialization/tests/src/Unit/Normalizer/PrimitiveDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\PrimitiveDataNormalizerTest
  3. 10 core/modules/serialization/tests/src/Unit/Normalizer/PrimitiveDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\PrimitiveDataNormalizerTest

@coversDefaultClass \Drupal\serialization\Normalizer\PrimitiveDataNormalizer
@group serialization

Hierarchy

Expanded class hierarchy of PrimitiveDataNormalizerTest

File

core/modules/serialization/tests/src/Unit/Normalizer/PrimitiveDataNormalizerTest.php, line 24

Namespace

Drupal\Tests\serialization\Unit\Normalizer
View source
class PrimitiveDataNormalizerTest extends UnitTestCase {
  use JsonSchemaTestTrait;
  
  /**
   * The TypedDataNormalizer instance.
   *
   * @var \Drupal\serialization\Normalizer\TypedDataNormalizer
   */
  protected $normalizer;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->normalizer = new PrimitiveDataNormalizer();
  }
  
  /**
   * @covers ::supportsNormalization
   * @dataProvider dataProviderPrimitiveData
   */
  public function testSupportsNormalization($primitive_data, $expected) : void {
    $this->assertTrue($this->normalizer
      ->supportsNormalization($primitive_data));
  }
  
  /**
   * @covers ::supportsNormalization
   */
  public function testSupportsNormalizationFail() : void {
    // Test that an object not implementing PrimitiveInterface fails.
    $this->assertFalse($this->normalizer
      ->supportsNormalization(new \stdClass()));
  }
  
  /**
   * @covers ::normalize
   * @dataProvider dataProviderPrimitiveData
   */
  public function testNormalize($primitive_data, $expected) : void {
    $this->assertSame($expected, $this->normalizer
      ->normalize($primitive_data));
  }
  
  /**
   * Data provider for testNormalize().
   */
  public static function dataProviderPrimitiveData() {
    $data = [];
    $definition = DataDefinition::createFromDataType('string');
    $string = new StringData($definition, 'string');
    $string->setValue('test');
    $data['string'] = [
      $string,
      'test',
    ];
    $definition = DataDefinition::createFromDataType('string');
    $string = new StringData($definition, 'string');
    $string->setValue(NULL);
    $data['string-null'] = [
      $string,
      NULL,
    ];
    $definition = DataDefinition::createFromDataType('integer');
    $integer = new IntegerData($definition, 'integer');
    $integer->setValue(5);
    $data['integer'] = [
      $integer,
      5,
    ];
    $definition = DataDefinition::createFromDataType('integer');
    $integer = new IntegerData($definition, 'integer');
    $integer->setValue(NULL);
    $data['integer-null'] = [
      $integer,
      NULL,
    ];
    $definition = DataDefinition::createFromDataType('boolean');
    $boolean = new BooleanData($definition, 'boolean');
    $boolean->setValue(TRUE);
    $data['boolean'] = [
      $boolean,
      TRUE,
    ];
    $definition = DataDefinition::createFromDataType('boolean');
    $boolean = new BooleanData($definition, 'boolean');
    $boolean->setValue(NULL);
    $data['boolean-null'] = [
      $boolean,
      NULL,
    ];
    return $data;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function jsonSchemaDataProvider() : array {
    $email = new Email(DataDefinition::createFromDataType('email'));
    $email->setValue('[email protected]');
    $float = new FloatData(DataDefinition::createFromDataType('float'));
    $float->setValue(9.99);
    $uri = new Uri(DataDefinition::createFromDataType('uri'));
    $uri->setValue('https://example.com');
    $decimal = new DecimalData(DataDefinition::createFromDataType('decimal'));
    $decimal->setValue('9.99');
    // TimeSpan normalizes to an integer, however Iso8601 matches a format.
    $duration = new DurationIso8601(DataDefinition::createFromDataType('duration_iso8601'));
    $duration->setValue('P1D');
    return [
      'email' => [
        $email,
      ],
      'float' => [
        $float,
      ],
      'uri' => [
        $uri,
      ],
      'decimal' => [
        $decimal,
      ],
      'duration' => [
        $duration,
      ],
      array_map(fn($value) => [
        $value[0],
      ], static::dataProviderPrimitiveData()),
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
JsonSchemaTestTrait::doCheckSchemaAgainstMetaSchema protected function Check a schema is valid against the meta-schema.
JsonSchemaTestTrait::doProphesize public function Method to make prophecy public for use in data provider closures.
JsonSchemaTestTrait::doTestJsonSchemaIsValid public function Validate the normalizer's JSON schema.
JsonSchemaTestTrait::getJsonSchemaTestNormalizationFormat protected function Format that should be used when performing test normalizations. 1
JsonSchemaTestTrait::getNormalizationForValue protected function Get the normalization for a value.
JsonSchemaTestTrait::getNormalizer protected function Helper method to retrieve the normalizer. 1
JsonSchemaTestTrait::getValidator protected function Get the JSON Schema Validator. 1
JsonSchemaTestTrait::supportedTypesDataProvider public static function
JsonSchemaTestTrait::testNormalizedValuesAgainstJsonSchema public function Test normalized values against the JSON schema.
JsonSchemaTestTrait::testSupportedTypesSchemaIsValid public function Test that a valid schema is returned for the explicitly supported types.
PrimitiveDataNormalizerTest::$normalizer protected property The TypedDataNormalizer instance.
PrimitiveDataNormalizerTest::dataProviderPrimitiveData public static function Data provider for testNormalize().
PrimitiveDataNormalizerTest::jsonSchemaDataProvider public static function Data provider for ::testNormalizedValuesAgainstJsonSchema. Overrides JsonSchemaTestTrait::jsonSchemaDataProvider
PrimitiveDataNormalizerTest::setUp protected function Overrides UnitTestCase::setUp
PrimitiveDataNormalizerTest::testNormalize public function @covers ::normalize[[api-linebreak]]
@dataProvider dataProviderPrimitiveData
PrimitiveDataNormalizerTest::testSupportsNormalization public function @covers ::supportsNormalization[[api-linebreak]]
@dataProvider dataProviderPrimitiveData
PrimitiveDataNormalizerTest::testSupportsNormalizationFail public function @covers ::supportsNormalization[[api-linebreak]]
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.