Пример #1
0
 public function testCanCreateWithResult()
 {
     $command = $this->getMock('GuzzleHttp\\Command\\CommandInterface');
     $client = $this->getMock('GuzzleHttp\\Command\\ServiceClientInterface');
     $trans = new CommandTransaction($client, $command);
     $trans->setResult('foo');
     $event = new ProcessEvent($trans);
     $this->assertSame('foo', $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());
 }
Пример #3
0
 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'));
 }
Пример #4
0
 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);
 }
Пример #5
0
 public function createCommandException(CommandTransaction $transaction, RequestException $previous)
 {
     $cn = 'GuzzleHttp\\Command\\Exception\\CommandException';
     if ($response = $transaction->getResponse()) {
         $statusCode = (string) $response->getStatusCode();
         if ($statusCode[0] == '4') {
             $cn = 'GuzzleHttp\\Command\\Exception\\CommandClientException';
         } elseif ($statusCode[0] == '5') {
             $cn = 'GuzzleHttp\\Command\\Exception\\CommandServerException';
         }
     }
     return new $cn("Error executing command: " . $previous->getMessage(), $transaction, $previous);
 }
 /**
  * Get context associated with the command transfer.
  *
  * The return value is a Guzzle collection object which can be accessed and
  * mutated like a PHP associative array. You can add arbitrary data to the
  * context for application specific purposes.
  *
  * @return Collection
  */
 public function getContext()
 {
     return $this->trans->getContext();
 }
Пример #7
0
 /**
  * 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);
         });
     }
 }