public function getAdapter($response = null, $code = 200)
 {
     $plugin = new MockPlugin();
     $plugin->addResponse(new Response($code, null, $response));
     $clientHttp = $this->getClient();
     $clientHttp->getEventDispatcher()->addSubscriber($plugin);
     return new Adapter($clientHttp);
 }
Пример #2
0
 protected function getClient($response, $code = 200, $throwCurlException = false)
 {
     $plugin = new MockPlugin();
     $plugin->addResponse(new Response($code, null, $response));
     $clientHttp = new GuzzleClient('http://my.domain.tld/api/v1');
     $clientHttp->getEventDispatcher()->addSubscriber($plugin);
     if ($throwCurlException) {
         $clientHttp->getEventDispatcher()->addListener('request.before_send', function (\Guzzle\Common\Event $event) {
             throw new \Guzzle\Http\Exception\CurlException();
         });
     }
     $logger = new Logger('tests');
     $logger->pushHandler(new NullHandler());
     return new Client('123456', '654321', new GuzzleAdapter($clientHttp), $logger);
 }
Пример #3
0
 /**
  * @covers Guzzle\Http\Plugin\MockPlugin::readBodies
  * @covers Guzzle\Http\Plugin\MockPlugin::dequeue
  */
 public function testReadsBodiesFromMockedRequests()
 {
     $p = new MockPlugin(array(new Response(200)));
     $p->readBodies(true);
     $client = new Client('http://localhost:123/');
     $client->getEventDispatcher()->addSubscriber($p, 9999);
     $body = EntityBody::factory('foo');
     $request = $client->put();
     $request->setBody($body);
     $request->send();
     $this->assertEquals(3, $body->ftell());
 }
Пример #4
0
 /**
  * @covers Guzzle\Http\Plugin\MockPlugin::__construct
  */
 public function testLoadsResponsesFromConstructor()
 {
     $p = new MockPlugin(array(new Response(200)));
     $this->assertEquals(1, $p->count());
 }
Пример #5
0
 /**
  * Get a mock response for a client by mock file name
  *
  * @param string $path Relative path to the mock response file
  *
  * @return Response
  */
 public function getMockResponse($path)
 {
     return $path instanceof Response ? $path : MockPlugin::getMockFile(self::$mockBasePath . DIRECTORY_SEPARATOR . $path);
 }
Пример #6
0
 /**
  * @covers Guzzle\Http\Client::send
  * @expectedException Guzzle\Common\Exception\ExceptionCollection
  */
 public function testClientThrowsExceptionForMultipleRequests()
 {
     $client = new Client($this->getServer()->getUrl());
     $mock = new MockPlugin();
     $mock->addResponse(new Response(200));
     $mock->addResponse(new Response(404));
     $client->getEventDispatcher()->addSubscriber($mock);
     $client->send(array($client->get(), $client->head()));
 }
 /**
  * Set a mock response from a mock file on the next client request.
  *
  * This method assumes that mock response files are located under the
  * Command/Mock/ directory of the Service being tested
  * (e.g. Unfuddle/Command/Mock/).  A mock response is added to the next
  * request sent by the client.
  *
  * @param Client $client Client object to modify
  * @param string $paths Path to files within the Mock folder of the service
  */
 public function setMockResponse(Client $client, $paths)
 {
     $this->requests = array();
     $that = $this;
     $mock = new MockPlugin(null, true);
     $mock->getEventDispatcher()->addListener('mock.request', function (Event $event) use($that) {
         $that->addMockedRequest($event['request']);
     });
     foreach ((array) $paths as $path) {
         $mock->addResponse($this->getMockResponse($path));
     }
     $client->getEventDispatcher()->addSubscriber($mock);
 }