public function testHasTransactionMethods()
 {
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), new puzzle_message_Request('GET', '/'));
     $e = $this->getMockBuilder('puzzle_event_AbstractRequestEvent')->setConstructorArgs(array($t))->getMockForAbstractClass();
     $this->assertSame($t->getClient(), $e->getClient());
     $this->assertSame($t->getRequest(), $e->getRequest());
 }
Esempio n. 2
0
 public function testCanUseResponseBody()
 {
     $body = puzzle_stream_Stream::factory();
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), new puzzle_message_Request('GET', 'http://httbin.org'));
     $t->setResponse(new puzzle_message_Response(200, array(), $body));
     $m = new puzzle_adapter_curl_RequestMediator($t, new puzzle_message_MessageFactory());
     $this->assertEquals(3, $m->writeResponseBody(null, 'foo'));
     $this->assertEquals('foo', (string) $body);
 }
Esempio n. 3
0
 public function testAddsMockResponseToRequestFromClient()
 {
     $response = new puzzle_message_Response(200);
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), new puzzle_message_Request('GET', '/'));
     $m = new puzzle_subscriber_Mock(array($response));
     $ev = new puzzle_event_BeforeEvent($t);
     $m->onBefore($ev);
     $this->assertSame($response, $t->getResponse());
 }
 public function testInterceptsWithEvent()
 {
     $response = new puzzle_message_Response(200);
     $this->_closure_testInterceptsWithEvent_res = null;
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), new puzzle_message_Request('GET', '/'));
     $t->getRequest()->getEmitter()->on('complete', array($this, '__callback_testInterceptsWithEvent'));
     $e = new puzzle_event_BeforeEvent($t);
     $e->intercept($response);
     $this->assertTrue($e->isPropagationStopped());
     $this->assertSame($this->_closure_testInterceptsWithEvent_res->getClient(), $e->getClient());
 }
Esempio n. 5
0
 public function testExtractsAndStoresCookies()
 {
     $request = new puzzle_message_Request('GET', '/');
     $response = new puzzle_message_Response(200);
     $mock = $this->getMockBuilder('puzzle_cookie_CookieJar')->setMethods(array('extractCookies'))->getMock();
     $mock->expects($this->exactly(1))->method('extractCookies')->with($request, $response);
     $plugin = new puzzle_subscriber_Cookie($mock);
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), $request);
     $t->setResponse($response);
     $plugin->onComplete(new puzzle_event_CompleteEvent($t));
 }
Esempio n. 6
0
 public function testHasValues()
 {
     $c = new puzzle_Client();
     $r = new puzzle_message_Request('GET', '/');
     $t = new puzzle_adapter_Transaction($c, $r);
     $response = new puzzle_message_Response(200);
     $t->setResponse($response);
     $e = new puzzle_event_HeadersEvent($t);
     $this->assertSame($c, $e->getClient());
     $this->assertSame($r, $e->getRequest());
     $this->assertSame($response, $e->getResponse());
 }
Esempio n. 7
0
 public function testEmitsAfterSendEventAndEmitsErrorIfNeeded()
 {
     $this->_closure_testEmitsAfterSendEventAndEmitsErrorIfNeeded_ex2 = $res = null;
     $request = new puzzle_message_Request('GET', '/');
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), $request);
     $t->setResponse(new puzzle_message_Response(200));
     $this->_closure_testEmitsAfterSendEventAndEmitsErrorIfNeeded_ex = new puzzle_exception_RequestException('foo', $request);
     $t->getRequest()->getEmitter()->on('complete', array($this, '__callback_testEmitsAfterSendEventAndEmitsErrorIfNeeded_1'));
     $t->getRequest()->getEmitter()->on('error', array($this, '__callback_testEmitsAfterSendEventAndEmitsErrorIfNeeded_2'));
     puzzle_event_RequestEvents::emitComplete($t);
     $this->assertSame($this->_closure_testEmitsAfterSendEventAndEmitsErrorIfNeeded_ex, $this->_closure_testEmitsAfterSendEventAndEmitsErrorIfNeeded_ex2);
 }
Esempio n. 8
0
 /**
  * @throws OutOfBoundsException|Exception
  */
 public function onBefore(puzzle_event_BeforeEvent $event)
 {
     if (!($item = array_shift($this->queue))) {
         throw new OutOfBoundsException('Mock queue is empty');
     } elseif ($item instanceof puzzle_exception_RequestException) {
         throw $item;
     }
     // Emulate the receiving of the response headers
     $request = $event->getRequest();
     $transaction = new puzzle_adapter_Transaction($event->getClient(), $request);
     $transaction->setResponse($item);
     $request->getEmitter()->emit('headers', new puzzle_event_HeadersEvent($transaction));
     // Emulate reading a response body
     if ($this->readBodies && $request->getBody()) {
         while (!$request->getBody()->eof()) {
             $request->getBody()->read(8096);
         }
     }
     $event->intercept($item);
 }
Esempio n. 9
0
 public function testMaintainsLimitValue()
 {
     $request = new puzzle_message_Request('GET', '/');
     $response = new puzzle_message_Response(200);
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), $request);
     $t->setResponse($response);
     $ev = new puzzle_event_CompleteEvent($t);
     $h = new puzzle_subscriber_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('puzzle_message_RequestInterface', $trans['request']);
         $this->assertInstanceOf('puzzle_message_ResponseInterface', $trans['response']);
     }
     return $h;
 }
Esempio n. 10
0
 public function testHasRequestAndClient()
 {
     $c = new puzzle_Client();
     $req = new puzzle_message_Request('GET', '/');
     $response = new puzzle_message_Response(200);
     $t = new puzzle_adapter_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());
 }