Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Service] Clients will now create a command set for you if you pass a…

…n array of commands to the execute method
  • Loading branch information...
commit 45e5f83e58ab6a55c52c930e307dc6be2edd679e 1 parent db3cd76
@mtdowling mtdowling authored
View
14 src/Guzzle/Service/Client.php
@@ -287,7 +287,7 @@ public function getCommand($name, array $args = array())
/**
* Execute a command and return the response
*
- * @param CommandInterface|CommandSet $command The command or set to execute
+ * @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
@@ -299,16 +299,12 @@ public function getCommand($name, array $args = array())
public function execute($command)
{
if ($command instanceof CommandInterface) {
-
$command->setClient($this)->prepare();
$this->getEventManager()->notify('command.before_send', $command);
$command->getRequest()->send();
$this->getEventManager()->notify('command.after_send', $command);
-
return $command->getResult();
-
} else if ($command instanceof CommandSet) {
-
foreach ($command as $c) {
if ($c->getClient() && $c->getClient() !== $this) {
throw new Command\CommandSetException(
@@ -318,12 +314,12 @@ public function execute($command)
}
$c->setClient($this);
}
-
return $command->execute();
-
- } else {
- throw new \InvalidArgumentException('Invalid command sent to ' . __METHOD__);
+ } else if (is_array($command)) {
+ return $this->execute(new CommandSet($command));
}
+
+ throw new \InvalidArgumentException('Invalid command sent to ' . __METHOD__);
}
/**
View
20 tests/Guzzle/Tests/Service/ClientTest.php
@@ -211,6 +211,26 @@ public function testExecutesCommands()
/**
* @covers Guzzle\Service\Client::execute
+ */
+ public function testExecutesCommandsWithArray()
+ {
+ $client = new Client('http://www.test.com/');
+ $client->getEventManager()->attach(new MockPlugin(array(
+ new \Guzzle\Http\Message\Response(200),
+ new \Guzzle\Http\Message\Response(200)
+ )));
+
+ // Create a command set and a command
+ $set = array(new MockCommand(), new MockCommand());
+ $client->execute($set);
+
+ // Make sure it sent
+ $this->assertTrue($set[0]->isExecuted());
+ $this->assertTrue($set[1]->isExecuted());
+ }
+
+ /**
+ * @covers Guzzle\Service\Client::execute
* @expectedException Guzzle\Service\Command\CommandSetException
*/
public function testThrowsExceptionWhenExecutingMixedClientCommandSets()
Please sign in to comment.
Something went wrong with that request. Please try again.