Exemplo n.º 1
0
 public function test_To_String()
 {
     $document = new Document();
     $operation = new Operation();
     $operation->setPath('/foo');
     $operation->setType(OperationType::REPLACE);
     $operation->setValue('bar');
     $document->addOperation($operation);
     $this->assertEquals('[{"op": "replace", "path": "/foo", "value": "bar"}]', $document->toString());
 }
Exemplo n.º 2
0
 public function test_Escaping_Tilde()
 {
     $property = new Property();
     $property->setName('~foo');
     $operation = new Operation();
     $operation->setPath($property->getPath());
     $operation->setType(OperationType::REPLACE);
     $operation->setValue('bar~baz~');
     $encoded = Encoder::encode(array($operation));
     $this->assertEquals('[{"op": "replace", "path": "/~0foo", "value": "bar~0baz~0"}]', $encoded);
 }
Exemplo n.º 3
0
 /**
  * Update this resource
  *
  * @param array  $params
  * @param Schema $schema
  * @return \Guzzle\Http\Message\Response
  * @throws \RuntimeException
  */
 public function update(array $params, Schema $schema = null)
 {
     $schema = $schema ?: $this->getService()->getImageSchema();
     $document = new JsonDocument();
     foreach ($params as $propertyName => $value) {
         // find property object
         if (!($property = $schema->getProperty($propertyName))) {
             // check whether additional properties are found
             if (false === ($property = $schema->validateAdditionalProperty($value))) {
                 throw new \RuntimeException('If a property does not exist in the schema, the `additionalProperties` property must be set');
             }
         }
         // do validation checks
         $property->setName($propertyName);
         $property->setValue($value);
         $property->validate();
         // decide operation type
         if (!$value) {
             $operationType = OperationType::REMOVE;
         } elseif ($this->offsetExists($propertyName)) {
             $operationType = OperationType::REPLACE;
         } else {
             $operationType = $schema->decideOperationType($property);
         }
         // create JSON-patch operation
         $operation = JsonOperation::factory($schema, $property, $operationType);
         // add to JSON document
         $document->addOperation($operation);
     }
     // create request
     $body = $document->toString();
     return $this->getClient()->patch($this->getUrl(), $this->getService()->getPatchHeaders(), $body)->send();
 }
Exemplo n.º 4
0
 /**
  * @expectedException \RuntimeException
  */
 public function test_Validate_Fails()
 {
     $operation = new Operation();
     $operation->setType('foo');
     $operation->validate();
 }