public function testHasData() { $command = $this->getMock('GuzzleHttp\\Command\\CommandInterface'); $client = $this->getMock('GuzzleHttp\\Command\\ServiceClientInterface'); $trans = new CommandTransaction($client, $command); $request = new Request('GET', 'http://httbin.org'); $response = new Response(200); $trans->setRequest($request); $trans->setResponse($response); $event = new ProcessEvent($trans); $this->assertSame($command, $event->getCommand()); $this->assertSame($client, $event->getClient()); $this->assertSame($request, $event->getRequest()); $this->assertSame($response, $event->getResponse()); $this->assertSame($trans, $event->getTransaction()); $this->assertNull($event->getResult()); }
public function testCanMutateData() { $client = $this->getMockForAbstractClass('GuzzleHttp\\Command\\ServiceClientInterface'); $command = new Command('foo', []); $trans = new CommandTransaction($client, $command, ['foo' => 'bar']); $request = new Request('GET', 'http://foo.com'); $trans->setRequest($request); $this->assertSame($request, $trans->getRequest()); $response = new Response(200); $trans->setResponse($response); $this->assertSame($response, $trans->getResponse()); $trans->setResult('foo'); $this->assertSame('foo', $trans->getResult()); $e = new \Exception('foo'); $trans->setException($e); $this->assertSame($e, $trans->getException()); }
public function testHasData() { $command = $this->getMock('GuzzleHttp\\Command\\CommandInterface'); $client = $this->getMock('GuzzleHttp\\Command\\ServiceClientInterface'); $ctrans = new CommandTransaction($client, $command); $response = new Response(200); $ex = new \Exception('foo'); $ctrans->setException($ex); $ctrans->setResponse($response); $event = new CommandErrorEvent($ctrans); $this->assertSame($ctrans, $event->getTransaction()); $this->assertSame($command, $event->getCommand()); $this->assertSame($client, $event->getClient()); $this->assertSame($ex, $event->getException()); $this->assertSame($response, $event->getResponse()); $this->assertNull($event->getResult()); $event->setResult('foo'); $this->assertSame('foo', $event->getResult()); $this->assertTrue($event->isPropagationStopped()); $event->getContext()->set('abc', '123'); $this->assertEquals('123', $ctrans->getContext()->get('abc')); }
public function testEmitsProcessEvent() { $req = new Request('GET', 'http://httbin.org'); $res = new Response(200); $client = $this->getMockForAbstractClass('GuzzleHttp\\Command\\ServiceClientInterface'); $cmd = new Command('foo', []); $trans = new CommandTransaction($client, $cmd); $trans->setRequest($req); $trans->setResponse($res); $c = false; $cmd->getEmitter()->on('process', function (ProcessEvent $e) use(&$c, $client, $cmd, $req, $res) { $e->setResult('foo'); $this->assertSame($client, $e->getClient()); $this->assertSame($cmd, $e->getCommand()); $this->assertSame($req, $e->getRequest()); $this->assertSame($res, $e->getResponse()); $c = true; }); CommandEvents::process($trans); $this->assertEquals('foo', $trans->getResult()); $this->assertTrue($c); }
/** * Create a CommandException from a request error event. * @param CommandTransaction $trans * @param ErrorEvent $re * @return \Exception */ private static function exceptionFromError(CommandTransaction $trans, ErrorEvent $re) { if ($response = $re->getResponse()) { $trans->setResponse($response); } else { self::stopRequestError($re); } return $trans->getClient()->createCommandException($trans, $re->getException()); }
/** * Set the current request of the iterator and hook the request's event * system up to the command's event system. */ private function processCurrentRequest(CommandTransaction $trans) { $this->currentRequest = $trans->getRequest(); if ($this->currentRequest) { // Emit the command's process event when the request completes $this->currentRequest->getEmitter()->on('complete', function (CompleteEvent $event) use($trans) { $trans->setResponse($event->getResponse()); CommandEvents::process($trans); }); } }