/** * Execute a command and return the response * * @param CommandInterface|CommandSet|array $command Command or set to execute * * @return mixed Returns the result of the executed command's * {@see CommandInterface::getResult} method if a CommandInterface is * passed, or the CommandSet itself if a CommandSet is passed * @throws InvalidArgumentException if an invalid command is passed * @throws CommandSetException if a set contains commands associated with other clients */ public function execute($command) { if ($command instanceof CommandInterface) { $request = $command->setClient($this)->prepare(); $this->dispatch('command.before_send', array('command' => $command)); // Set the state to new if the command was previously executed if ($request->getState() !== RequestInterface::STATE_NEW) { $request->setState(RequestInterface::STATE_NEW); } $request->send(); $this->dispatch('command.after_send', array('command' => $command)); return $command->getResult(); } elseif ($command instanceof CommandSet) { foreach ($command as $c) { if ($c->getClient() && $c->getClient() !== $this) { throw new CommandSetException('Attempting to run a mixed-Client CommandSet from a ' . 'Client context. Run the set using CommandSet::execute() '); } $c->setClient($this); } return $command->execute(); } elseif (is_array($command)) { return $this->execute(new CommandSet($command)); } throw new InvalidArgumentException('Invalid command sent to ' . __METHOD__); }
/** * @covers Guzzle\Service\Command\CommandSet::execute * @covers Guzzle\Service\Command\CommandSet::update */ public function testExecutesCommands() { $client = $this->getClient(); $observer = $this->getWildcardObserver($client); // Create a Mock response $response = new Response(200, array('Content-Type' => 'application/xml'), '<xml><data>123</data></xml>'); $client->getEventDispatcher()->addSubscriber(new MockPlugin(array($response, $response, $response))); $command1 = new MockCommand(); $command1->setClient($client); $command2 = new MockCommand(); $command2->setClient($client); $command3 = new MockCommand(); $command3->setClient($client); $commandSet = new CommandSet(array($command1, $command2, $command3)); $commandSet->execute(); $this->assertTrue($command1->isExecuted()); $this->assertTrue($command1->isPrepared()); $this->assertTrue($command2->isExecuted()); $this->assertTrue($command2->isPrepared()); $this->assertTrue($command3->isExecuted()); $this->assertTrue($command3->isPrepared()); $this->assertEquals($response, $command1->getResponse()); $this->assertEquals($response, $command2->getResponse()); $grouped = $observer->getGrouped(); $this->assertEquals(3, count($grouped['command.before_send'])); $this->assertEquals(3, count($grouped['command.after_send'])); // make sure the command set was detached as a listener on the request $listeners = $command1->getRequest()->getEventDispatcher()->getListeners('request.complete'); $this->assertFalse(in_array($commandSet, $listeners)); // make sure that the command reference was removed $this->assertFalse($command1->getRequest()->getParams()->hasKey('command')); // Make sure that the command.after_send events are staggered, meaning they happened as requests completed $lastEvent = ''; foreach ($observer->events as $e) { if ($lastEvent == 'command.after_send' && $e == 'command.after_send') { $this->fail('Not completing commands as they complete: ' . var_export($observer->events, true)); } $lastEvent = $e; } }