예제 #1
0
 public function testAllowsForJsonBasedArrayParamsFunctionalTest()
 {
     $description = new ServiceDescription(array('operations' => array('test' => new Operation(array('httpMethod' => 'PUT', 'parameters' => array('data' => array('required' => true, 'filters' => 'json_encode', 'location' => 'body')))))));
     $client = new Client();
     $client->setDescription($description);
     $command = $client->getCommand('test', array('data' => array('foo' => 'bar')));
     $request = $command->prepare();
     $this->assertEquals('{"foo":"bar"}', (string) $request->getBody());
 }
예제 #2
0
 public function testAllowsForJsonBasedArrayParamsFunctionalTest()
 {
     $service = array('test' => new ApiCommand(array('method' => 'PUT', 'params' => array('data' => array('required' => true, 'type' => 'type:array', 'filters' => 'json_encode', 'location' => 'body')))));
     $description = new ServiceDescription($service);
     $client = new Client();
     $client->setDescription($description);
     $command = $client->getCommand('test', array('data' => array('foo' => 'bar')));
     $request = $command->prepare();
     $this->assertEquals(json_encode(array('foo' => 'bar')), (string) $request->getBody());
 }
 public function testValidatesAdditionalParameters()
 {
     $description = ServiceDescription::factory(array('operations' => array('foo' => array('httpMethod' => 'PUT', 'parameters' => array('bar' => array('location' => 'header')), 'additionalParameters' => array('location' => 'json')))));
     $client = new Client();
     $client->setDescription($description);
     $command = $client->getCommand('foo');
     $command['bar'] = 'test';
     $command['hello'] = 'abc';
     $request = $command->prepare();
     $this->assertEquals('test', (string) $request->getHeader('bar'));
     $this->assertEquals('{"hello":"abc"}', (string) $request->getBody());
 }
 public function testVisitsLocationWithMultipleFiles()
 {
     $description = ServiceDescription::factory(array('operations' => array('DoPost' => array('httpMethod' => 'POST', 'parameters' => array('foo' => array('location' => 'postFile', 'type' => array('string', 'array')))))));
     $this->getServer()->flush();
     $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length:0\r\n\r\n"));
     $client = new Client($this->getServer()->getUrl());
     $client->setDescription($description);
     $command = $client->getCommand('DoPost', array('foo' => array(__FILE__, __FILE__)));
     $command->execute();
     $received = $this->getServer()->getReceivedRequests();
     $this->assertContains('name="foo[0]";', $received[0]);
     $this->assertContains('name="foo[1]";', $received[0]);
 }
예제 #5
0
 /**
  * @expectedException \Guzzle\Service\Exception\ValidationException
  * @expectedExceptionMessage Validation errors: [abc] must be of type string
  */
 public function testValidatesAdditionalParameters()
 {
     $description = ServiceDescription::factory(array('operations' => array('foo' => array('parameters' => array('baz' => array('type' => 'integer')), 'additionalParameters' => array('type' => 'string')))));
     $client = new Client();
     $client->setDescription($description);
     $command = $client->getCommand('foo', array('abc' => false, 'command.headers' => array('foo' => 'bar')));
     $command->prepare();
 }
예제 #6
0
 /**
  * @param       $command
  * @param array $arguments
  *
  * @return array
  */
 public function executeCommand($command, $arguments = array())
 {
     $command = $this->client->getCommand($command, $arguments);
     return $this->client->execute($command);
     /*$command->getResponse()->json();*/
 }
 /**
  * @expectedException InvalidArgumentException
  */
 public function testThrowsExceptionWhenMissingCommand()
 {
     $client = new Client();
     $mock = $this->getMock('Guzzle\\Service\\Command\\Factory\\FactoryInterface');
     $mock->expects($this->any())->method('factory')->with($this->equalTo('test'))->will($this->returnValue(null));
     $client->setCommandFactory($mock);
     $client->getCommand('test');
 }
예제 #8
0
 /**
  * @covers Guzzle\Service\Command\DynamicCommand::build
  */
 public function testAllowsPostFieldsAndFiles()
 {
     $service = new ServiceDescription(array('post_command' => new ApiCommand(array('method' => 'POST', 'uri' => '/key', 'params' => array('test' => array('location' => 'post_field'), 'test_2' => array('location' => 'post_field:foo'), 'test_3' => array('location' => 'post_file'))))));
     $client = new Client('http://www.test.com/api/v2');
     $client->setDescription($service);
     $command = $client->getCommand('post_command', array('test' => 'Hi!', 'test_2' => 'There', 'test_3' => __FILE__));
     $request = $command->prepare();
     $this->assertEquals('Hi!', $request->getPostField('test'));
     $this->assertEquals('There', $request->getPostField('foo'));
     $this->assertInternalType('array', $request->getPostFile('test_3'));
     $command = $client->getCommand('post_command', array('test_3' => new PostFile('baz', __FILE__)));
     $request = $command->prepare();
     $this->assertInternalType('array', $request->getPostFile('baz'));
 }
 public function testCanAddListenerToParseDomainObjects()
 {
     $client = new Client();
     $client->setDescription(ServiceDescription::factory(array('operations' => array('test' => array('responseClass' => 'FooBazBar')))));
     $foo = new \stdClass();
     $client->getEventDispatcher()->addListener('command.parse_response', function ($e) use($foo) {
         $e['result'] = $foo;
     });
     $command = $client->getCommand('test');
     $command->prepare()->setResponse(new Response(200), true);
     $result = $command->execute();
     $this->assertSame($result, $foo);
 }
 /**
  * @covers Guzzle\Service\Command\DynamicCommand::build
  */
 public function testUsesRelativePaths()
 {
     $service = new ServiceDescription(array('test_path' => new ApiCommand(array('method' => 'GET', 'path' => 'test/abc'))));
     $client = new Client('http://www.test.com/api/v2');
     $client->setDescription($service);
     $command = $client->getCommand('test_path');
     $request = $command->prepare();
     $this->assertEquals('/api/v2/test/abc', $request->getPath());
 }
예제 #11
0
 public function getCommand($name, array $args = array())
 {
     $default_headers = array('content-type' => 'application/json', 'accept' => 'application/json');
     $args['headers'] = array_merge($default_headers, isset($args['headers']) ? $args['headers'] : array());
     return parent::getCommand($name, $args);
 }
예제 #12
0
 /**
  * @covers Guzzle\Service\Command\DynamicCommand::addVisitor
  */
 public function testAllowsCustomVisitor()
 {
     $service = new ServiceDescription(array('foo' => new ApiCommand(array('params' => array('test' => array('location' => 'query'))))));
     $client = new Client();
     $client->setDescription($service);
     $command = $client->getCommand('foo', array('test' => 'hi'));
     // Flip query and header
     $command->addVisitor('query', new HeaderVisitor());
     $request = $command->prepare();
     $this->assertEquals('hi', (string) $request->getHeader('test'));
 }
예제 #13
0
파일: Qiita.php 프로젝트: cocoiti/qiita-php
 /**
  * Call Api
  *
  * @param string $command
  * @param array $parameters
  * @return array
  */
 public function api($command, $parameters = [])
 {
     $command = $this->client->getCommand($command, $parameters);
     return $command->execute();
 }