/**
  * Adds an entity.
  *
  * It overwrites the entity, if it has the same UUID.
  *
  * @param \Acquia\ContentHubClient\Entity    $new_entity
  * @return \Acquia\ContentHubClient\Entities $this
  */
 public function addEntity(Entity $new_entity)
 {
     foreach ($this['entities'] as $key => $entity) {
         if ($entity->getUuid() == $new_entity->getUuid()) {
             unset($this['entities'][$key]);
         }
     }
     $this['entities'][] = $new_entity;
 }
 public function testCreateEntities()
 {
     $data = $this->getData()['entities'][0];
     $entities = new Entities();
     $entity = new Entity();
     $entity->setUuid($data['uuid']);
     $entity->setType($data['type']);
     $entity->setCreated($data['created']);
     $entity->setOrigin($data['origin']);
     $entity->setModified($data['modified']);
     // Adding Assets
     $assets = [new Asset($data['assets'][0]), new Asset($data['assets'][1])];
     $entity->setAssets($assets);
     // Adding Attributes
     $attribute = new Attribute($data['attributes']['title']['type']);
     $attribute->setValues($data['attributes']['title']['value']);
     $attributes = ['title' => $attribute];
     $entity->setAttributes($attributes);
     $entities->addEntity($entity);
     $this->assertEquals($entity, $entities->getEntity($entity->getUuid()));
     // Adding second Entity.
     $data = $this->getData()['entities'][1];
     $entity = new Entity();
     $entity->setUuid($data['uuid']);
     $entity->setType($data['type']);
     $entity->setCreated($data['created']);
     $entity->setOrigin($data['origin']);
     $entity->setModified($data['modified']);
     // Adding Assets
     $assets = [new Asset($data['assets'][0]), new Asset($data['assets'][1])];
     $entity->setAssets($assets);
     // Adding Attributes
     $attribute = new Attribute($data['attributes']['title']['type']);
     $attribute->setValues($data['attributes']['title']['value']);
     $attributes = ['title' => $attribute];
     $entity->setAttributes($attributes);
     $entities->addEntity($entity);
     $this->assertEquals($entity, $entities->getEntity($entity->getUuid()));
     $uuid = '66666666-0000-0000-0000-000000000000';
     $this->assertFalse($entities->getEntity($uuid));
     $entity->setUuid($uuid);
     $entities->addEntity($entity);
     $this->assertEquals($entity, $entities->getEntity($uuid));
     $entities->removeEntity($uuid);
     $this->assertFalse($entities->getEntity($uuid));
     foreach ($entities->getEntities() as $entity) {
         $this->assertInstanceOf('Acquia\\ContentHubClient\\Entity', $entity);
     }
     $data = $this->getData();
     $json = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
     $this->assertJson($json, $entities->json());
 }
 public function testCreateEntity()
 {
     $data = $this->getData();
     $entity = new Entity();
     $entity->setUuid($data['uuid']);
     $entity->setType($data['type']);
     $entity->setCreated($data['created']);
     $entity->setOrigin($data['origin']);
     $entity->setModified($data['modified']);
     $this->assertEquals($data['uuid'], $entity->getUuid());
     $this->assertEquals($data['type'], $entity->getType());
     $this->assertEquals($data['created'], $entity->getCreated());
     $this->assertEquals($data['origin'], $entity->getOrigin());
     $this->assertEquals($data['modified'], $entity->getModified());
     // Adding Assets
     $assets = [new Asset($data['assets'][0]), new Asset($data['assets'][1])];
     $entity->setAssets($assets);
     // Adding Attributes
     $attribute = new Attribute($data['attributes']['title']['type']);
     $attribute->setValues($data['attributes']['title']['value']);
     $attributes = ['title' => $attribute];
     $entity->setAttributes($attributes);
     // Checks
     $this->assertEquals($assets, $entity->getAssets());
     $this->assertEquals("http://acquia.com/sites/default/files/foo.png", $entity->getAssets()[0]['url']);
     // Adding the same asset does not mess up with the asset's list.
     $entity->addAsset(new Asset($data['assets'][0]));
     $this->assertEquals($assets, $entity->getAssets());
     $this->assertEquals($attributes, $entity->getAttributes());
     // Adding / Removing Assets
     $asset_array = ['url' => 'http://acquia.com/sites/default/files/foo-bar.png', 'replace-token' => '[acquia-foobar]'];
     $asset = new Asset($asset_array);
     $entity->addAsset($asset);
     $myasset = $entity->getAsset($asset_array['replace-token']);
     $this->assertEquals($asset_array['url'], $myasset->getUrl());
     $this->assertEquals($asset_array['replace-token'], $myasset->getReplaceToken());
     $entity->removeAsset($asset_array['replace-token']);
     $this->assertFalse($entity->getAsset($asset_array['replace-token']));
     // Adding / Removing Attributes
     $attribute_value = ['my_attribute' => ['type' => 'integer', 'value' => ['en' => '4', 'es' => '3', 'und' => 0]]];
     $attribute = new Attribute(Attribute::TYPE_INTEGER);
     $attribute->setValues($attribute_value['my_attribute']['value']);
     $name = array_keys($attribute_value);
     $name = reset($name);
     $entity->setAttribute($name, $attribute);
     $this->assertEquals((array) $attribute, (array) $entity->getAttribute($name));
     $attribute_value['my_attribute']['value']['it'] = 400;
     $entity->setAttributeValue($name, $attribute_value['my_attribute']['value']['it'], 'it');
     $this->assertEquals($attribute_value['my_attribute']['value']['it'], $entity->getAttribute($name)->getValue('it'));
     $entity->removeAttribute($name);
     $this->assertEquals($attributes, $entity->getAttributes());
     // Handling NULL Attributes.
     $attribute = new Attribute(Attribute::TYPE_BOOLEAN);
     $attribute->setValue(NULL);
     $entity->setAttribute('empty1', $attribute);
     $this->assertEquals($attributes, $entity->getAttributes());
     $attribute = new Attribute(Attribute::TYPE_ARRAY_NUMBER);
     $entity->setAttribute('empty2', $attribute);
     $this->assertEquals($attributes, $entity->getAttributes());
     $json = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
     $this->assertJson($json, $entity->json());
 }