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);
 }
Esempio n. 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);
 }
Esempio n. 3
0
 /**
  * @covers Guzzle\Http\Plugin\MockPlugin::onRequestCreate
  * @covers Guzzle\Http\Plugin\MockPlugin::dequeue
  * @depends testAddsMockResponseToRequestFromClient
  */
 public function testDetachesTemporaryWhenEmpty()
 {
     $p = new MockPlugin(null, true);
     $p->addResponse(MockPlugin::getMockFile(__DIR__ . '/../../TestData/mock_response'));
     $client = new Client('http://localhost:123/');
     $client->getEventDispatcher()->addSubscriber($p, 9999);
     $request = $client->get();
     $request->send();
     $this->assertFalse($this->hasSubscriber($client, $p));
 }
Esempio n. 4
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);
 }