You are here

protected function RecursiveContextualValidatorTest::setupTypedData in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php \Drupal\Tests\Core\TypedData\RecursiveContextualValidatorTest::setupTypedData()

Setups a typed data object used for test purposes.

Parameters

array $tree: An array of value, constraints and properties.

Return value

\Drupal\Core\TypedData\TypedDataInterface|\PHPUnit\Framework\MockObject\MockObject

2 calls to RecursiveContextualValidatorTest::setupTypedData()
RecursiveContextualValidatorTest::buildExampleTypedDataWithProperties in core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php
Builds some example type data object.
RecursiveContextualValidatorTest::testValidatePropertyWithCustomGroup in core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php
@covers ::validateProperty

File

core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php, line 194
Contains \Drupal\Tests\Core\TypedData\RecursiveContextualValidatorTest.

Class

RecursiveContextualValidatorTest
@coversDefaultClass \Drupal\Core\TypedData\Validation\RecursiveContextualValidator @group typedData

Namespace

Drupal\Tests\Core\TypedData

Code

protected function setupTypedData(array $tree, $name = '') {
  $callback = function ($value, ExecutionContextInterface $context) {
    $context
      ->addViolation('violation: ' . (is_array($value) ? count($value) : $value));
  };
  $tree += [
    'constraints' => [],
  ];
  if (isset($tree['properties'])) {
    $map_data_definition = MapDataDefinition::create();
    $map_data_definition
      ->addConstraint('Callback', [
      'callback' => $callback,
    ]);
    foreach ($tree['properties'] as $property_name => $property) {
      $sub_typed_data = $this
        ->setupTypedData($property, $property_name);
      $map_data_definition
        ->setPropertyDefinition($property_name, $sub_typed_data
        ->getDataDefinition());
    }
    $typed_data = $this->typedDataManager
      ->create($map_data_definition, $tree['value'], $name);
  }
  else {

    /** @var \Drupal\Core\TypedData\TypedDataInterface $typed_data */
    $typed_data = $this->typedDataManager
      ->create(DataDefinition::create('string')
      ->addConstraint('Callback', [
      'callback' => $callback,
    ]), $tree['value'], $name);
  }
  return $typed_data;
}