public function testHasTransactionMethods()
 {
     $t = new Transaction(new Client(), new Request('GET', '/'));
     $e = $this->getMockBuilder('GuzzleHttp\\Event\\AbstractRequestEvent')->setConstructorArgs([$t])->getMockForAbstractClass();
     $this->assertSame($t->getClient(), $e->getClient());
     $this->assertSame($t->getRequest(), $e->getRequest());
 }
 public function testCanUseResponseBody()
 {
     $body = Stream::factory();
     $t = new Transaction(new Client(), new Request('GET', 'http://httbin.org'));
     $t->setResponse(new Response(200, [], $body));
     $m = new RequestMediator($t, new MessageFactory());
     $this->assertEquals(3, $m->writeResponseBody(null, 'foo'));
     $this->assertEquals('foo', (string) $body);
 }
示例#3
0
 public function testAddsMockResponseToRequestFromClient()
 {
     $response = new Response(200);
     $t = new Transaction(new Client(), new Request('GET', '/'));
     $m = new Mock([$response]);
     $ev = new BeforeEvent($t);
     $m->onBefore($ev);
     $this->assertSame($response, $t->getResponse());
 }
示例#4
0
 public function testExtractsAndStoresCookies()
 {
     $request = new Request('GET', '/');
     $response = new Response(200);
     $mock = $this->getMockBuilder('GuzzleHttp\\Cookie\\CookieJar')->setMethods(array('extractCookies'))->getMock();
     $mock->expects($this->exactly(1))->method('extractCookies')->with($request, $response);
     $plugin = new Cookie($mock);
     $t = new Transaction(new Client(), $request);
     $t->setResponse($response);
     $plugin->onComplete(new CompleteEvent($t));
 }
示例#5
0
 public function testHasValues()
 {
     $c = new Client();
     $r = new Request('GET', '/');
     $t = new Transaction($c, $r);
     $response = new Response(200);
     $t->setResponse($response);
     $e = new HeadersEvent($t);
     $this->assertSame($c, $e->getClient());
     $this->assertSame($r, $e->getRequest());
     $this->assertSame($response, $e->getResponse());
 }
 public function testInterceptsWithEvent()
 {
     $response = new Response(200);
     $res = null;
     $t = new Transaction(new Client(), new Request('GET', '/'));
     $t->getRequest()->getEmitter()->on('complete', function ($e) use(&$res) {
         $res = $e;
     });
     $e = new BeforeEvent($t);
     $e->intercept($response);
     $this->assertTrue($e->isPropagationStopped());
     $this->assertSame($res->getClient(), $e->getClient());
 }
 public function testEmitsAfterSendEventAndEmitsErrorIfNeeded()
 {
     $ex2 = $res = null;
     $request = new Request('GET', '/');
     $t = new Transaction(new Client(), $request);
     $t->setResponse(new Response(200));
     $ex = new RequestException('foo', $request);
     $t->getRequest()->getEmitter()->on('complete', function ($e) use($ex) {
         $ex->e = $e;
         throw $ex;
     });
     $t->getRequest()->getEmitter()->on('error', function ($e) use(&$ex2) {
         $ex2 = $e->getException();
         $e->stopPropagation();
     });
     RequestEvents::emitComplete($t);
     $this->assertSame($ex, $ex2);
 }
 public function testMaintainsLimitValue()
 {
     $request = new Request('GET', '/');
     $response = new Response(200);
     $t = new Transaction(new Client(), $request);
     $t->setResponse($response);
     $ev = new CompleteEvent($t);
     $h = new History(2);
     $h->onComplete($ev);
     $h->onComplete($ev);
     $h->onComplete($ev);
     $this->assertEquals(2, count($h));
     $this->assertSame($request, $h->getLastRequest());
     $this->assertSame($response, $h->getLastResponse());
     foreach ($h as $trans) {
         $this->assertInstanceOf('GuzzleHttp\\Message\\RequestInterface', $trans['request']);
         $this->assertInstanceOf('GuzzleHttp\\Message\\ResponseInterface', $trans['response']);
     }
     return $h;
 }
示例#9
0
 /**
  * @throws \OutOfBoundsException|\Exception
  */
 public function onBefore(BeforeEvent $event)
 {
     if (!($item = array_shift($this->queue))) {
         throw new \OutOfBoundsException('Mock queue is empty');
     } elseif ($item instanceof RequestException) {
         throw $item;
     }
     // Emulate the receiving of the response headers
     $request = $event->getRequest();
     $transaction = new Transaction($event->getClient(), $request);
     $transaction->setResponse($item);
     $request->getEmitter()->emit('headers', new HeadersEvent($transaction));
     // Emulate reading a response body
     if ($this->readBodies && $request->getBody()) {
         while (!$request->getBody()->eof()) {
             $request->getBody()->read(8096);
         }
     }
     $event->intercept($item);
 }
 public function testHasRequestAndClient()
 {
     $c = new Client();
     $req = new Request('GET', '/');
     $response = new Response(200);
     $t = new Transaction($c, $req);
     $this->assertSame($c, $t->getClient());
     $this->assertSame($req, $t->getRequest());
     $this->assertNull($t->getResponse());
     $t->setResponse($response);
     $this->assertSame($response, $t->getResponse());
 }
 public function testHooksUpEvents()
 {
     $client = $this->getMockForAbstractClass('GuzzleHttp\\Command\\ServiceClientInterface');
     $request = new Request('GET', 'http://httbin.org');
     $command = new Command('foo');
     $calledPrepare = $calledProcess = $calledError = $responseSet = false;
     $commands = [$command];
     $i = new CommandToRequestIterator($commands, $client, ['prepare' => function (PrepareEvent $event) use(&$calledPrepare, $request) {
         $calledPrepare = true;
         $event->setRequest($request);
     }, 'process' => function (ProcessEvent $event) use(&$calledProcess, &$responseSet) {
         $calledProcess = true;
         $responseSet = $event->getResponse() instanceof Response;
     }, 'error' => function (CommandErrorEvent $event) use(&$calledError) {
         $calledError = true;
         $event->setResult(null);
     }]);
     $this->assertTrue($i->valid());
     $this->assertTrue($calledPrepare);
     $this->assertFalse($calledProcess);
     $this->assertFalse($calledError);
     $this->assertFalse($responseSet);
     $transaction = new Transaction(new Client(), $request);
     $transaction->setResponse(new Response(200));
     $mockComplete = new CompleteEvent($transaction);
     $request->getEmitter()->emit('complete', $mockComplete);
     $this->assertTrue($calledProcess);
     $this->assertFalse($calledError);
     $mockError = new ErrorEvent($transaction, new RequestException('foo', $request));
     $request->getEmitter()->emit('error', $mockError);
     $this->assertTrue($calledError);
 }