You are here

protected function UnserializationTest::populateCdfObject in Acquia Content Hub 8.2

Populates CDF object from array.

Parameters

array $entity: Entity.

Return value

\Acquia\ContentHubClient\CDF\CDFObject Populated CDF object.

Throws

\Exception

\ReflectionException

See also

\Acquia\ContentHubClient\ContentHubClient::getEntities()

1 call to UnserializationTest::populateCdfObject()
UnserializationTest::createCdfDocumentFromFixture in tests/src/Kernel/UnserializationTest.php
Creates CDF document from fixture.

File

tests/src/Kernel/UnserializationTest.php, line 243

Class

UnserializationTest
Tests that entities are properly unserialized.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

protected function populateCdfObject(array $entity) : CDFObject {
  $object = new CDFObject($entity['type'], $entity['uuid'], $entity['created'], $entity['modified'], $entity['origin'], $entity['metadata']);
  foreach ($entity['attributes'] as $attribute_name => $values) {

    // Refactor ClientHub.php: get rid of duplicated code blocks.
    if (!($attribute = $object
      ->getAttribute($attribute_name))) {
      $class = !empty($object
        ->getMetadata()['attributes'][$attribute_name]) ? $object
        ->getMetadata()['attributes'][$attribute_name]['class'] : FALSE;
      if ($class && class_exists($class)) {
        $object
          ->addAttribute($attribute_name, $values['type'], NULL, 'und', $class);
      }
      else {
        $object
          ->addAttribute($attribute_name, $values['type'], NULL);
      }
      $attribute = $object
        ->getAttribute($attribute_name);
    }
    $value_property = (new \ReflectionClass($attribute))
      ->getProperty('value');
    $value_property
      ->setAccessible(TRUE);
    $value_property
      ->setValue($attribute, $values['value']);
  }
  return $object;
}