You are here

protected static function EntityResourceTestBase::castToString in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::castToString()

Transforms a normalization: casts all non-string types to strings.

Parameters

array $normalization: A normalization to transform.

Return value

array The transformed normalization.

1 call to EntityResourceTestBase::castToString()
EntityResourceTestBase::assertStoredEntityMatchesSentNormalization in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
Asserts that the stored entity matches the sent normalization.

File

core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php, line 673

Class

EntityResourceTestBase
Even though there is the generic EntityResource, it's necessary for every entity type to have its own test, because they each have different fields, validation constraints, et cetera. It's not because the generic case works, that every case…

Namespace

Drupal\Tests\rest\Functional\EntityResource

Code

protected static function castToString(array $normalization) {
  foreach ($normalization as $key => $value) {
    if (is_bool($value)) {
      $normalization[$key] = (string) (int) $value;
    }
    elseif (is_int($value) || is_float($value)) {
      $normalization[$key] = (string) $value;
    }
    elseif (is_array($value)) {
      $normalization[$key] = static::castToString($value);
    }
  }
  return $normalization;
}