Ejemplo n.º 1
0
 public function send($uri, array $payload = array())
 {
     // add key to payload
     $payload['key'] = $this->apiKey;
     $request = new Request(new URL(strtr(static::API_URL, array(':action' => $uri))));
     $request->setMethod('POST');
     $request->getHeaders()->add(new ContentType('application/json charset=utf-8'));
     $request->setBody(json_encode($payload));
     $response = $this->client->send($request, new Response());
     if ($response->getCode() != 200) {
         $data = json_decode($response->getContent());
         switch ($data->name) {
             case "ValidationError":
                 throw new ValidationErrorException($data->message, $data->code);
                 break;
             case "Unknown_Webhook":
                 throw new UnknownWebhookException($data->message);
                 break;
             case "Unknown_Url":
                 throw new UnknownUrlException($data->message);
                 break;
             default:
                 throw new \RuntimeException(sprintf('%s: %s', $data->name, $data->message), $data->code);
         }
     }
     return $response->getContent();
 }
Ejemplo n.º 2
0
 /**
  * @depends testSetMethod
  * @covers Versionable\Prospect\Request\Request::setMethod
  * @covers Versionable\Prospect\Request\Request::getFiles
  * @covers Versionable\Prospect\Request\Request::hasContent
  * @covers Versionable\Prospect\Request\Request::isMultipart
  * @covers Versionable\Prospect\Request\Request::getMethod
  * @covers Versionable\Prospect\Request\Request::isBodySupported
  */
 public function testBodyNotSupport()
 {
     $files = $this->getMock('Versionable\\Prospect\\File\\CollectionInterface', array('isEmpty', 'toString', 'setBoundary'));
     $files->expects($this->any())->method('isEmpty')->will($this->returnValue(false));
     $this->object->setFiles($files);
     $this->object->setMethod('test');
     $this->object->setBody('some body');
     $this->assertFalse($this->object->isMultipart());
 }
Ejemplo n.º 3
0
 public function testToStringWithParameterAndBody()
 {
     $request = new Request();
     $request->setMethod('POST');
     $request->setUrl(new Url('http://www.versionable.co.uk'));
     $request->getParameters()->add(new Parameter('name', 'prospect'));
     $request->getFiles()->add(new File('file1', __DIR__ . '/../../../../data/file/file1.txt', 'text/plain'));
     $request->getFiles()->add(new File('file2', __DIR__ . '/../../../../data/file/file2.txt', 'text/plain'));
     $this->object->setRequest($request);
     $actual = $this->object->toString();
     $expected = str_replace('!boundary!', str_replace('-', '', $this->object->getBoundary()), file_get_contents(__DIR__ . '/../../../../data/request/request-with-parameters-files.txt'));
     $this->assertEquals($expected, $actual);
 }