mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-13 03:45:22 +01:00
[Http] [Service] Adding more coverage to Url. Fixing CookiePlugintest. Allowing XML based commands to extend previously defined commands.
This commit is contained in:
parent
da781d6202
commit
620e13057c
@ -93,9 +93,11 @@ class CurlHandle
|
||||
*/
|
||||
public function isAvailable()
|
||||
{
|
||||
//@codeCoverageIgnoreStart
|
||||
if (!$this->handle) {
|
||||
return false;
|
||||
}
|
||||
//@codeCoverageIgnoreEnd
|
||||
|
||||
return false != @curl_getinfo($this->handle, CURLINFO_EFFECTIVE_URL);
|
||||
}
|
||||
@ -346,6 +348,7 @@ class CurlHandle
|
||||
*/
|
||||
public function hasProblematicOption()
|
||||
{
|
||||
//@codeCoverageIgnoreStart
|
||||
if (!self::$pollute) {
|
||||
self::$pollute = array(
|
||||
CURLOPT_RANGE,
|
||||
@ -365,6 +368,7 @@ class CurlHandle
|
||||
self::$pollute[] = \CURLOPT_TIMEOUT_MS;
|
||||
}
|
||||
}
|
||||
//@codeCoverageIgnoreEnd
|
||||
|
||||
return count(array_intersect(self::$pollute, $this->options->getKeys())) > 0;
|
||||
}
|
||||
|
@ -412,7 +412,13 @@ class Url
|
||||
*/
|
||||
public function setQuery($query)
|
||||
{
|
||||
$this->query = $query;
|
||||
if (is_string($query)) {
|
||||
$output = null;
|
||||
parse_str($query, $output);
|
||||
$this->query = new QueryString($output);
|
||||
} else {
|
||||
$this->query = $query;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -100,7 +100,11 @@ class ApiCommand
|
||||
if (isset($config['args']) && is_array($config['args'])) {
|
||||
$this->args = array();
|
||||
foreach ($config['args'] as $argName => $arg) {
|
||||
$this->args[$argName] = new Collection($arg);
|
||||
if ($arg instanceof Collection) {
|
||||
$this->args[$argName] = $arg;
|
||||
} else {
|
||||
$this->args[$argName] = new Collection($arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -67,6 +67,56 @@ class XmlDescriptionBuilder implements DescriptionBuilderInterface
|
||||
// Parse the commands in the XML doc
|
||||
foreach ($xml->commands->command as $command) {
|
||||
$args = array();
|
||||
$parentData = array();
|
||||
$parentArgs = array();
|
||||
$attr = $command->attributes();
|
||||
$data = array(
|
||||
'name' => (string) $attr->name
|
||||
);
|
||||
if ($v = (string) $command->doc) {
|
||||
$data['doc'] = $v;
|
||||
}
|
||||
if ($v = (string) $attr->method) {
|
||||
$data['method'] = $v;
|
||||
}
|
||||
if ($v = (string) $attr->path) {
|
||||
$data['path'] = $v;
|
||||
}
|
||||
if ($v = (int) (string) $attr->min_args) {
|
||||
$data['min_args'] = $v;
|
||||
}
|
||||
if ($v = (string) $attr->can_batch) {
|
||||
$data['can_batch'] = $v == 'false' ? false : true;
|
||||
}
|
||||
if ($v = (string) $attr->class) {
|
||||
$data['class'] = $v;
|
||||
}
|
||||
|
||||
$extends = (string) $attr->extends;
|
||||
if ($extends) {
|
||||
$match = false;
|
||||
foreach ($commands as $cmd) {
|
||||
if ($cmd->getName() == $extends) {
|
||||
$match = $cmd;
|
||||
}
|
||||
}
|
||||
if (!$match) {
|
||||
throw new \RuntimeException((string) $attr->name
|
||||
. 'is trying to extend non-existent command ' . $extends);
|
||||
} else {
|
||||
$parentArgs = $match->getArgs();
|
||||
$parentData = array(
|
||||
'name' => $match->getName(),
|
||||
'doc' => $match->getDoc(),
|
||||
'method' => $match->getMethod(),
|
||||
'path' => $match->getPath(),
|
||||
'min_args' => $match->getMinArgs(),
|
||||
'can_batch' => $match->canBatch(),
|
||||
'class' => $match->getConcreteClass()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Add the arguments to the command
|
||||
foreach ($command->param as $arg) {
|
||||
$row = array();
|
||||
@ -83,19 +133,16 @@ class XmlDescriptionBuilder implements DescriptionBuilderInterface
|
||||
$args[(string) $arg->attributes()->name] = $row;
|
||||
}
|
||||
|
||||
$attr = $command->attributes();
|
||||
$data = array_merge($parentData, $data);
|
||||
$data['args'] = array_merge($parentArgs, $args);
|
||||
if (!isset($data['class'])) {
|
||||
$data['class'] = ServiceDescription::DEFAULT_COMMAND_CLASS;
|
||||
} else {
|
||||
$data['class'] = str_replace('.', '\\', $data['class']);
|
||||
}
|
||||
|
||||
// Create a new command using the parsed XML
|
||||
$commands[] = new ApiCommand(array(
|
||||
'name' => (string) $attr->name,
|
||||
'doc' => (string) $command->doc,
|
||||
'method' => (string) $attr->method,
|
||||
'path' => (string) $attr->path,
|
||||
'min_args' => (int) (string) $attr->min_args,
|
||||
'can_batch' => (string) $attr->can_batch == 'false' ? false : true,
|
||||
'class' => (string) $attr->class ?: ServiceDescription::DEFAULT_COMMAND_CLASS,
|
||||
'args' => $args
|
||||
));
|
||||
$commands[] = new ApiCommand($data);
|
||||
}
|
||||
|
||||
return new ServiceDescription($commands);
|
||||
|
@ -540,7 +540,7 @@ class CookiePluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
{
|
||||
$this->getServer()->enqueue(array(
|
||||
"HTTP/1.1 302 Moved Temporarily\r\n" .
|
||||
"Set-Cookie: test=583551; expires=Wednesday, 23-Mar-2011 19:49:45 GMT; path=/\r\n" .
|
||||
"Set-Cookie: test=583551; expires=Wednesday, 23-Mar-2050 19:49:45 GMT; path=/\r\n" .
|
||||
"Location: /redirect\r\n\r\n",
|
||||
|
||||
"HTTP/1.1 200 OK\r\n" .
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
namespace Guzzle\Tests\Http;
|
||||
|
||||
use Guzzle\Http\QueryString;
|
||||
use Guzzle\Http\Url;
|
||||
use Guzzle\Http\HttpException;
|
||||
|
||||
@ -181,4 +182,25 @@ class UrlTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
{
|
||||
$this->assertEquals($c, (string) Url::factory($a)->combine($b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Url
|
||||
*/
|
||||
public function testHasGettersAndSetters()
|
||||
{
|
||||
$url = Url::factory('http://www.test.com/');
|
||||
$this->assertEquals('example.com', $url->setHost('example.com')->getHost());
|
||||
$this->assertEquals('8080', $url->setPort(8080)->getPort());
|
||||
$this->assertEquals('/foo/bar', $url->setPath(array('foo', 'bar'))->getPath());
|
||||
$this->assertEquals('a', $url->setPassword('a')->getPassword());
|
||||
$this->assertEquals('b', $url->setUsername('b')->getUsername());
|
||||
$this->assertEquals('abc', $url->setFragment('abc')->getFragment());
|
||||
$this->assertEquals('https', $url->setScheme('https')->getScheme());
|
||||
$this->assertEquals('?a=123', (string) $url->setQuery('a=123')->getQuery());
|
||||
$this->assertEquals('https://b:a@example.com:8080/foo/bar?a=123#abc', (string)$url);
|
||||
$this->assertEquals('?b=boo', (string) $url->setQuery(new QueryString(array(
|
||||
'b' => 'boo'
|
||||
)))->getQuery());
|
||||
$this->assertEquals('https://b:a@example.com:8080/foo/bar?b=boo#abc', (string)$url);
|
||||
}
|
||||
}
|
@ -84,7 +84,11 @@ class ApiCommandTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$c = new ApiCommand(array(
|
||||
'name' => 'test',
|
||||
'class' => 'Guzzle\\Service\\Command\ClosureCommand',
|
||||
'args' => array()
|
||||
'args' => array(
|
||||
'p' => new Collection(array(
|
||||
'name' => 'foo'
|
||||
))
|
||||
)
|
||||
));
|
||||
$this->assertEquals('Guzzle\\Service\\Command\ClosureCommand', $c->getConcreteClass());
|
||||
}
|
||||
|
@ -75,8 +75,16 @@ class XmlDescriptionBuilderTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
<type name="slug" class="Guzzle.Common.InspectorFilter.Regex" default_args="/[0-9a-zA-z_\-]+/" />
|
||||
</types>
|
||||
<commands>
|
||||
<command name="geo.id" method="GET" auth_required="true" path="/geo/id/:place_id">
|
||||
<param name="place_id" type="string" required="true"/>
|
||||
<command name="abstract" method="GET" path="/path/{{def}}" min_args="2">
|
||||
<param name="st" static="static" />
|
||||
<param name="def" default="123" location="path" />
|
||||
</command>
|
||||
<command name="test1" extends="abstract">
|
||||
<param name="hd" type="string" required="true" location="header:X-Hd" />
|
||||
</command>
|
||||
<command name="test2" extends="abstract" method="DELETE" />
|
||||
<command name="test3" class="Guzzle.Service.Command.ClosureCommand">
|
||||
<param name="a" type="string" required="true" />
|
||||
</command>
|
||||
</commands>
|
||||
</client>
|
||||
@ -84,7 +92,45 @@ EOT;
|
||||
|
||||
$builder = new XmlDescriptionBuilder($xml);
|
||||
$service = $builder->build();
|
||||
$this->assertTrue($service->hasCommand('geo.id'));
|
||||
$this->arrayHasKey('slug', Inspector::getInstance()->getRegisteredFilters());
|
||||
$this->assertTrue($service->hasCommand('abstract'));
|
||||
$this->assertTrue($service->hasCommand('test1'));
|
||||
$this->assertTrue($service->hasCommand('test1'));
|
||||
$this->assertTrue($service->hasCommand('test2'));
|
||||
$this->assertTrue($service->hasCommand('test3'));
|
||||
|
||||
$test1 = $service->getCommand('test1');
|
||||
$test2 = $service->getCommand('test2');
|
||||
$test3 = $service->getCommand('test3');
|
||||
|
||||
$this->assertEquals('GET', $test1->getMethod());
|
||||
$this->assertEquals('/path/{{def}}', $test1->getPath());
|
||||
$this->assertEquals('2', $test1->getMinArgs());
|
||||
$this->assertEquals('static', $test1->getArg('st')->get('static'));
|
||||
$this->assertEquals('123', $test1->getArg('def')->get('default'));
|
||||
|
||||
$this->assertEquals('DELETE', $test2->getMethod());
|
||||
$this->assertEquals('/path/{{def}}', $test1->getPath());
|
||||
$this->assertEquals('2', $test1->getMinArgs());
|
||||
$this->assertEquals('static', $test1->getArg('st')->get('static'));
|
||||
$this->assertEquals('123', $test1->getArg('def')->get('default'));
|
||||
$this->assertEquals('header:X-Hd', $test1->getArg('hd')->get('location'));
|
||||
|
||||
$this->assertEquals('', $test3->getMethod());
|
||||
$this->assertEquals('Guzzle\Service\Command\ClosureCommand', $test3->getConcreteClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Service\Description\XmlDescriptionBuilder
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
public function testvalidatesXmlExtensions()
|
||||
{
|
||||
$xml = <<<EOT
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<client><commands><command name="invalid" extends="abstract" method="DELETE" /></commands></client>
|
||||
EOT;
|
||||
$builder = new XmlDescriptionBuilder($xml);
|
||||
$service = $builder->build();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user