You are here

protected static function EntityResourceTestBase::castToString in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::castToString()
  2. 10 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.

2 calls to EntityResourceTestBase::castToString()
EntityResourceTestBase::assertStoredEntityMatchesSentNormalization in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
Asserts that the stored entity matches the sent normalization.
EntityResourceTestBase::testGet in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
Test a GET request for an entity, plus edge cases to ensure good DX.

File

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

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;
}