public function testCanCreateRequestsFromCommand() { $client = $this->getMockBuilder('GuzzleHttp\\Command\\AbstractClient')->setConstructorArgs([new Client()])->getMockForAbstractClass(); $request = new Request('GET', 'http://httpbin.org/get'); $command = new Command('foo'); $command->getEmitter()->on('prepare', function (PrepareEvent $event) use($request) { $event->setRequest($request); }); $this->assertSame($request, Command::createRequest($client, $command)); }
public function __construct($module, $method, array $args = [], array $options = []) { $this->module = $module; $this->method = $method; if (isset($args['id'])) { $this->id = $args['id']; unset($args['id']); } else { $this->id = 1; } $this->params = $this->buildParams($args); parent::__construct($this->module . '.' . $this->method, $args, $options); }
public function testProvidesDebugCommandEvents() { $http = new Client(); $http->getEmitter()->attach(new Mock([new Response(200, ['foo' => 'bar'])])); $command = new Command('CmdName', ['arg' => 'value', 'foo' => Stream::factory('foo')]); $client = $this->getMockBuilder('GuzzleHttp\\Command\\AbstractClient')->setConstructorArgs([$http])->getMockForAbstractClass(); $command->getEmitter()->on('prepare', function (PrepareEvent $e) use($http) { $e->setRequest($http->createRequest('GET', 'http://foo.com')); }); $command->getEmitter()->on('process', function (ProcessEvent $e) use($http) { $e->setResult(['result' => 'value']); }); $res = fopen('php://temp', 'r+'); $debug = new Debug(['output' => $res]); $command->getEmitter()->attach($debug); $client->execute($command); rewind($res); $out = stream_get_contents($res); $checks = ['Starting the command:prepare event for ', 'Done with the command:prepare event for ', '(took ', 'Starting the request:before event for ', 'Done with the request:before event for ', 'Starting the request:complete event for ', 'Done with the request:complete event for ', 'Starting the command:process event for ', 'Done with the command:process event for ']; foreach ($checks as $check) { $this->assertContains($check, $out); } }
public function testCorrectlyHandlesRequestsThatFailBeforeSending() { $client = new Client(); $request = $client->createRequest('GET', 'http://httpbin.org'); $request->getEmitter()->attach(new Mock([new RequestException('foo', $request, null)])); $mock = $this->getMockBuilder('GuzzleHttp\\Command\\AbstractClient')->setConstructorArgs([$client, []])->getMockForAbstractClass(); $command = new Command('foo'); $emitter = $command->getEmitter(); $emitter->on('prepare', function (PrepareEvent $event) use($request) { $event->setRequest($request); }); try { $mock->execute($command); $this->fail('Did not throw'); } catch (CommandException $e) { } }
public function testExecutesCommandsInBatch() { $client = new Client(); $mock = $this->getMockBuilder('GuzzleHttp\\Command\\AbstractClient')->setConstructorArgs([$client, []])->getMockForAbstractClass(); $request = $client->createRequest('GET', 'http://httbin.org'); $command1 = new Command('foo'); $command1->getEmitter()->on('prepare', function (PrepareEvent $e) use($request) { $e->setResult('foo'); }); $command2 = new Command('foo'); $command2->getEmitter()->on('prepare', function (PrepareEvent $e) use($request) { $e->setResult('bar'); }); $command3 = new Command('foo'); $command3->getEmitter()->on('prepare', function () { throw new \Exception('foo'); }); $hash = $mock->batch([$command1, $command2, $command3]); $this->assertCount(3, $hash); $this->assertEquals('foo', $hash[$command1]); $this->assertEquals('bar', $hash[$command2]); $this->assertEquals('foo', $hash[$command3]->getPrevious()->getMessage()); }
public function testSkipsInterceptedCommands() { $client = $this->getMockForAbstractClass('GuzzleHttp\\Command\\ServiceClientInterface'); $request = new Request('GET', 'http://httbin.org'); $command1 = new Command('foo'); $command1->getEmitter()->on('prepare', function (PrepareEvent $e) use($request) { $e->setRequest($request); }); $command2 = new Command('bar'); $command2->getEmitter()->on('prepare', function (PrepareEvent $e) { $e->setResult('baz'); }); $commands = [$command1, $command2]; $i = new CommandToRequestIterator($commands, $client); $this->assertTrue($i->valid()); $this->assertSame($request, $i->current()); $i->next(); $this->assertFalse($i->valid()); $this->assertNull($i->current()); }
private function wrapBefore($source, $dest, Command $command) { if ($this->before) { $command->getEmitter()->on('init', function () use($source, $dest, $command) { call_user_func($this->before, $source, $dest, $command); }); } return $command; }
/** * @param Operation $operation Operation associated with the command * @param array $args Arguments to pass to the command * @param EmitterInterface $emitter Emitter used by the command */ public function __construct(Operation $operation, array $args, EmitterInterface $emitter = null) { $this->operation = $operation; parent::__construct('', $args, $emitter); }