添加网站文件

This commit is contained in:
2025-12-22 13:59:40 +08:00
commit 117aaf83d1
19468 changed files with 2111999 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\Asset\Exception;
use GuzzleHttp\Command\Exception\CommandException;
/**
* Class CustomCommandException
*
* @package GuzzleHttp\Tests\Command\Guzzle\Asset\Exception
*/
class CustomCommandException extends CommandException
{
}

View File

@@ -0,0 +1,13 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\Asset\Exception;
use GuzzleHttp\Command\Exception\CommandException;
/**
* Class OtherCustomCommandException
*
* @package GuzzleHttp\Tests\Command\Guzzle\Asset\Exception
*/
class OtherCustomCommandException extends CommandException
{
}

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@@ -0,0 +1,184 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\SchemaFormatter;
/**
* @covers \GuzzleHttp\Command\Guzzle\Description
*/
class DescriptionTest extends \PHPUnit_Framework_TestCase
{
protected $operations;
public function setup()
{
$this->operations = [
'test_command' => [
'name' => 'test_command',
'description' => 'documentationForCommand',
'httpMethod' => 'DELETE',
'class' => 'FooModel',
'parameters' => [
'bucket' => ['required' => true],
'key' => ['required' => true]
]
]
];
}
public function testConstructor()
{
$service = new Description(['operations' => $this->operations]);
$this->assertEquals(1, count($service->getOperations()));
$this->assertFalse($service->hasOperation('foobar'));
$this->assertTrue($service->hasOperation('test_command'));
}
public function testContainsModels()
{
$d = new Description([
'operations' => ['foo' => []],
'models' => [
'Tag' => ['type' => 'object'],
'Person' => ['type' => 'object']
]
]);
$this->assertTrue($d->hasModel('Tag'));
$this->assertTrue($d->hasModel('Person'));
$this->assertFalse($d->hasModel('Foo'));
$this->assertInstanceOf(Parameter::class, $d->getModel('Tag'));
$this->assertEquals(['Tag', 'Person'], array_keys($d->getModels()));
}
public function testCanUseResponseClass()
{
$d = new Description([
'operations' => [
'foo' => ['responseClass' => 'Tag']
],
'models' => ['Tag' => ['type' => 'object']]
]);
$op = $d->getOperation('foo');
$this->assertNotNull($op->getResponseModel());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testRetrievingMissingModelThrowsException()
{
$d = new Description([]);
$d->getModel('foo');
}
public function testHasAttributes()
{
$d = new Description([
'operations' => [],
'name' => 'Name',
'description' => 'Description',
'apiVersion' => '1.24'
]);
$this->assertEquals('Name', $d->getName());
$this->assertEquals('Description', $d->getDescription());
$this->assertEquals('1.24', $d->getApiVersion());
}
public function testPersistsCustomAttributes()
{
$data = [
'operations' => ['foo' => ['class' => 'foo', 'parameters' => []]],
'name' => 'Name',
'description' => 'Test',
'apiVersion' => '1.24',
'auth' => 'foo',
'keyParam' => 'bar'
];
$d = new Description($data);
$this->assertEquals('foo', $d->getData('auth'));
$this->assertEquals('bar', $d->getData('keyParam'));
$this->assertEquals(['auth' => 'foo', 'keyParam' => 'bar'], $d->getData());
$this->assertNull($d->getData('missing'));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsExceptionForMissingOperation()
{
$s = new Description([]);
$this->assertNull($s->getOperation('foo'));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesOperationTypes()
{
new Description([
'operations' => ['foo' => new \stdClass()]
]);
}
public function testHasbaseUrl()
{
$description = new Description(['baseUrl' => 'http://foo.com']);
$this->assertEquals('http://foo.com', $description->getBaseUri());
}
public function testHasbaseUri()
{
$description = new Description(['baseUri' => 'http://foo.com']);
$this->assertEquals('http://foo.com', $description->getBaseUri());
}
public function testModelsHaveNames()
{
$desc = [
'models' => [
'date' => ['type' => 'string'],
'user'=> [
'type' => 'object',
'properties' => [
'dob' => ['$ref' => 'date']
]
]
]
];
$s = new Description($desc);
$this->assertEquals('string', $s->getModel('date')->getType());
$this->assertEquals('dob', $s->getModel('user')->getProperty('dob')->getName());
}
public function testHasOperations()
{
$desc = ['operations' => ['foo' => ['parameters' => ['foo' => [
'name' => 'foo'
]]]]];
$s = new Description($desc);
$this->assertInstanceOf(Operation::class, $s->getOperation('foo'));
$this->assertSame($s->getOperation('foo'), $s->getOperation('foo'));
}
public function testHasFormatter()
{
$s = new Description([]);
$this->assertNotEmpty($s->format('date', 'now'));
}
public function testCanUseCustomFormatter()
{
$formatter = $this->getMockBuilder(SchemaFormatter::class)
->setMethods(['format'])
->getMock();
$formatter->expects($this->once())
->method('format');
$s = new Description([], ['formatter' => $formatter]);
$s->format('time', 'now');
}
}

View File

@@ -0,0 +1,386 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\DescriptionInterface;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\ServiceClientInterface;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException;
use GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\OtherCustomCommandException;
use Predis\Response\ResponseInterface;
/**
* @covers \GuzzleHttp\Command\Guzzle\Deserializer
*/
class DeserializerTest extends \PHPUnit_Framework_TestCase
{
/** @var ServiceClientInterface|\PHPUnit_Framework_MockObject_MockObject */
private $serviceClient;
/** @var CommandInterface|\PHPUnit_Framework_MockObject_MockObject */
private $command;
public function setUp()
{
$this->serviceClient = $this->getMockBuilder(GuzzleClient::class)
->disableOriginalConstructor()
->getMock();
$this->command = $this->getMockBuilder(CommandInterface::class)->getMock();
}
protected function prepareErrorResponses($commandName, array $errors = [])
{
$this->command->expects($this->once())->method('getName')->will($this->returnValue($commandName));
$description = $this->getMockBuilder(DescriptionInterface::class)->getMock();
$operation = new Operation(['errorResponses' => $errors], $description);
$description->expects($this->once())
->method('getOperation')
->with($commandName)
->will($this->returnValue($operation));
$this->serviceClient->expects($this->once())
->method('getDescription')
->will($this->returnValue($description));
}
public function testDoNothingIfNoException()
{
$mock = new MockHandler([new Response(200)]);
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org/{foo}',
'httpMethod' => 'GET',
'responseModel' => 'j',
'parameters' => [
'bar' => [
'type' => 'string',
'required' => true,
'location' => 'uri'
]
]
]
],
'models' => [
'j' => [
'type' => 'object'
]
]
]);
$httpClient = new HttpClient(['handler' => $mock]);
$client = new GuzzleClient($httpClient, $description);
$client->foo(['bar' => 'baz']);
}
/**
* @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException
*/
public function testCreateExceptionWithCode()
{
$response = new Response(404);
$mock = new MockHandler([$response]);
$description = new Description([
'name' => 'Test API',
'baseUri' => 'http://httpbin.org',
'operations' => [
'foo' => [
'uri' => '/{foo}',
'httpMethod' => 'GET',
'responseClass' => 'Foo',
'parameters' => [
'bar' => [
'type' => 'string',
'required' => true,
'description' => 'Unique user name (alphanumeric)',
'location' => 'json'
],
],
'errorResponses' => [
['code' => 404, 'class' => CustomCommandException::class]
]
]
],
'models' => [
'Foo' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json'
]
]
]
]);
$httpClient = new HttpClient(['handler' => $mock]);
$client = new GuzzleClient($httpClient, $description);
$client->foo(['bar' => 'baz']);
}
public function testNotCreateExceptionIfDoesNotMatchCode()
{
$response = new Response(401);
$mock = new MockHandler([$response]);
$description = new Description([
'name' => 'Test API',
'baseUri' => 'http://httpbin.org',
'operations' => [
'foo' => [
'uri' => '/{foo}',
'httpMethod' => 'GET',
'responseClass' => 'Foo',
'parameters' => [
'bar' => [
'type' => 'string',
'required' => true,
'description' => 'Unique user name (alphanumeric)',
'location' => 'json'
],
],
'errorResponses' => [
['code' => 404, 'class' => CustomCommandException::class]
]
]
],
'models' => [
'Foo' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json'
]
]
]
]);
$httpClient = new HttpClient(['handler' => $mock]);
$client = new GuzzleClient($httpClient, $description);
$client->foo(['bar' => 'baz']);
}
/**
* @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException
*/
public function testCreateExceptionWithExactMatchOfReasonPhrase()
{
$response = new Response(404, [], null, '1.1', 'Bar');
$mock = new MockHandler([$response]);
$description = new Description([
'name' => 'Test API',
'baseUri' => 'http://httpbin.org',
'operations' => [
'foo' => [
'uri' => '/{foo}',
'httpMethod' => 'GET',
'responseClass' => 'Foo',
'parameters' => [
'bar' => [
'type' => 'string',
'required' => true,
'description' => 'Unique user name (alphanumeric)',
'location' => 'json'
],
],
'errorResponses' => [
['code' => 404, 'phrase' => 'Bar', 'class' => CustomCommandException::class]
]
]
],
'models' => [
'Foo' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json'
]
]
]
]);
$httpClient = new HttpClient(['handler' => $mock]);
$client = new GuzzleClient($httpClient, $description);
$client->foo(['bar' => 'baz']);
}
/**
* @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\OtherCustomCommandException
*/
public function testFavourMostPreciseMatch()
{
$response = new Response(404, [], null, '1.1', 'Bar');
$mock = new MockHandler([$response]);
$description = new Description([
'name' => 'Test API',
'baseUri' => 'http://httpbin.org',
'operations' => [
'foo' => [
'uri' => '/{foo}',
'httpMethod' => 'GET',
'responseClass' => 'Foo',
'parameters' => [
'bar' => [
'type' => 'string',
'required' => true,
'description' => 'Unique user name (alphanumeric)',
'location' => 'json'
],
],
'errorResponses' => [
['code' => 404, 'class' => CustomCommandException::class],
['code' => 404, 'phrase' => 'Bar', 'class' => OtherCustomCommandException::class],
]
]
],
'models' => [
'Foo' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json'
]
]
]
]);
$httpClient = new HttpClient(['handler' => $mock]);
$client = new GuzzleClient($httpClient, $description);
$client->foo(['bar' => 'baz']);
}
/**
* @expectedException \GuzzleHttp\Command\Exception\CommandException
* @expectedExceptionMessage 404
*/
public function testDoesNotAddResultWhenExceptionIsPresent()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org/{foo}',
'httpMethod' => 'GET',
'responseModel' => 'j',
'parameters' => [
'bar' => [
'type' => 'string',
'required' => true,
'location' => 'uri'
]
]
]
],
'models' => [
'j' => [
'type' => 'object'
]
]
]);
$mock = new MockHandler([new Response(404)]);
$stack = HandlerStack::create($mock);
$httpClient = new HttpClient(['handler' => $stack]);
$client = new GuzzleClient($httpClient, $description);
$client->foo(['bar' => 'baz']);
}
public function testReturnsExpectedResult()
{
$loginResponse = new Response(
200,
[],
'{
"LoginResponse":{
"result":{
"type":4,
"username":{
"uid":38664492,
"content":"skyfillers-api-test"
},
"token":"3FB1F21014D630481D35CBC30CBF4043"
},
"status":{
"code":200,
"content":"OK"
}
}
}'
);
$mock = new MockHandler([$loginResponse]);
$description = new Description([
'name' => 'Test API',
'baseUri' => 'http://httpbin.org',
'operations' => [
'Login' => [
'uri' => '/{foo}',
'httpMethod' => 'POST',
'responseClass' => 'LoginResponse',
'parameters' => [
'username' => [
'type' => 'string',
'required' => true,
'description' => 'Unique user name (alphanumeric)',
'location' => 'json'
],
'password' => [
'type' => 'string',
'required' => true,
'description' => 'User\'s password',
'location' => 'json'
],
'response' => [
'type' => 'string',
'required' => false,
'description' => 'Determines the response type: xml = result content will be xml formatted (default); plain = result content will be simple text, without structure; json = result content will be json formatted',
'location' => 'json'
],
'token' => [
'type' => 'string',
'required' => false,
'description' => 'Provides the authentication token',
'location' => 'json'
]
]
]
],
'models' => [
'LoginResponse' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json'
]
]
]
]);
$httpClient = new HttpClient(['handler' => $mock]);
$client = new GuzzleClient($httpClient, $description);
$result = $client->Login([
'username' => 'test',
'password' => 'test',
'response' => 'json',
]);
$expected = [
'result' => [
'type' => 4,
'username' => [
'uid' => 38664492,
'content' => 'skyfillers-api-test'
],
'token' => '3FB1F21014D630481D35CBC30CBF4043'
],
'status' => [
'code' => 200,
'content' => 'OK'
]
];
$this->assertArraySubset($expected, $result['LoginResponse']);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,112 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\Handler;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
/**
* @covers \GuzzleHttp\Command\Guzzle\Handler\ValidatedDescriptionHandler
*/
class ValidatedDescriptionHandlerTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \GuzzleHttp\Command\Exception\CommandException
* @expectedExceptionMessage Validation errors: [bar] is a required string
*/
public function testValidates()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j',
'parameters' => [
'bar' => [
'type' => 'string',
'required' => true
]
]
]
]
]);
$client = new GuzzleClient(new HttpClient(), $description);
$client->foo([]);
}
public function testSuccessfulValidationDoesNotThrow()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j',
'parameters' => []
]
],
'models' => [
'j' => [
'type' => 'object'
]
]
]);
$client = new GuzzleClient(new HttpClient(), $description);
$client->foo([]);
}
/**
* @expectedException \GuzzleHttp\Command\Exception\CommandException
* @expectedExceptionMessage Validation errors: [bar] must be of type string
*/
public function testValidatesAdditionalParameters()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j',
'additionalParameters' => [
'type' => 'string'
]
]
],
'models' => [
'j' => [
'type' => 'object'
]
]
]);
$client = new GuzzleClient(new HttpClient(), $description);
$client->foo(['bar' => new \stdClass()]);
}
public function testFilterBeforeValidate()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'parameters' => [
'bar' => [
'location' => 'uri',
'type' => 'string',
'format' => 'date-time',
'required' => true
]
]
]
]
]);
$client = new GuzzleClient(new HttpClient(), $description);
$client->foo(['bar' => new \DateTimeImmutable()]); // Should not throw any exception
}
}

View File

@@ -0,0 +1,227 @@
<?php
namespace Guzzle\Tests\Service\Description;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\Operation;
/**
* @covers \GuzzleHttp\Command\Guzzle\Operation
*/
class OperationTest extends \PHPUnit_Framework_TestCase
{
public static function strtoupper($string)
{
return strtoupper($string);
}
public function testOperationIsDataObject()
{
$c = new Operation([
'name' => 'test',
'summary' => 'doc',
'notes' => 'notes',
'documentationUrl' => 'http://www.example.com',
'httpMethod' => 'POST',
'uri' => '/api/v1',
'responseModel' => 'abc',
'deprecated' => true,
'parameters' => [
'key' => [
'required' => true,
'type' => 'string',
'maxLength' => 10,
'name' => 'key'
],
'key_2' => [
'required' => true,
'type' => 'integer',
'default' => 10,
'name' => 'key_2'
]
]
]);
$this->assertEquals('test', $c->getName());
$this->assertEquals('doc', $c->getSummary());
$this->assertEquals('http://www.example.com', $c->getDocumentationUrl());
$this->assertEquals('POST', $c->getHttpMethod());
$this->assertEquals('/api/v1', $c->getUri());
$this->assertEquals('abc', $c->getResponseModel());
$this->assertTrue($c->getDeprecated());
$params = array_map(function ($c) {
return $c->toArray();
}, $c->getParams());
$this->assertEquals([
'key' => [
'required' => true,
'type' => 'string',
'maxLength' => 10,
'name' => 'key'
],
'key_2' => [
'required' => true,
'type' => 'integer',
'default' => 10,
'name' => 'key_2'
]
], $params);
$this->assertEquals([
'required' => true,
'type' => 'integer',
'default' => 10,
'name' => 'key_2'
], $c->getParam('key_2')->toArray());
$this->assertNull($c->getParam('afefwef'));
$this->assertArrayNotHasKey('parent', $c->getParam('key_2')->toArray());
}
public function testDeterminesIfHasParam()
{
$command = $this->getTestCommand();
$this->assertTrue($command->hasParam('data'));
$this->assertFalse($command->hasParam('baz'));
}
protected function getTestCommand()
{
return new Operation([
'parameters' => [
'data' => ['type' => 'string']
]
]);
}
public function testAddsNameToParametersIfNeeded()
{
$command = new Operation(['parameters' => ['foo' => []]]);
$this->assertEquals('foo', $command->getParam('foo')->getName());
}
public function testContainsApiErrorInformation()
{
$command = $this->getOperation();
$this->assertEquals(1, count($command->getErrorResponses()));
}
public function testHasNotes()
{
$o = new Operation(['notes' => 'foo']);
$this->assertEquals('foo', $o->getNotes());
}
public function testHasData()
{
$o = new Operation(['data' => ['foo' => 'baz', 'bar' => 123]]);
$this->assertEquals('baz', $o->getData('foo'));
$this->assertEquals(123, $o->getData('bar'));
$this->assertNull($o->getData('wfefwe'));
$this->assertEquals(['foo' => 'baz', 'bar' => 123], $o->getData());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMesssage Parameters must be arrays
*/
public function testEnsuresParametersAreArrays()
{
new Operation(['parameters' => ['foo' => true]]);
}
public function testHasDescription()
{
$s = new Description([]);
$o = new Operation([], $s);
$this->assertSame($s, $o->getServiceDescription());
}
public function testHasAdditionalParameters()
{
$o = new Operation([
'additionalParameters' => [
'type' => 'string', 'name' => 'binks',
],
'parameters' => [
'foo' => ['type' => 'integer'],
],
]);
$this->assertEquals('string', $o->getAdditionalParameters()->getType());
}
/**
* @return Operation
*/
protected function getOperation()
{
return new Operation([
'name' => 'OperationTest',
'class' => get_class($this),
'parameters' => [
'test' => ['type' => 'object'],
'bool_1' => ['default' => true, 'type' => 'boolean'],
'bool_2' => ['default' => false],
'float' => ['type' => 'numeric'],
'int' => ['type' => 'integer'],
'date' => ['type' => 'string'],
'timestamp' => ['type' => 'string'],
'string' => ['type' => 'string'],
'username' => ['type' => 'string', 'required' => true, 'filters' => 'strtolower'],
'test_function' => ['type' => 'string', 'filters' => __CLASS__ . '::strtoupper'],
],
'errorResponses' => [
[
'code' => 503,
'reason' => 'InsufficientCapacity',
'class' => 'Guzzle\\Exception\\RuntimeException',
],
],
]);
}
public function testCanExtendFromOtherOperations()
{
$d = new Description([
'operations' => [
'A' => [
'parameters' => [
'A' => [
'type' => 'object',
'properties' => ['foo' => ['type' => 'string']]
],
'B' => ['type' => 'string']
],
'summary' => 'foo'
],
'B' => [
'extends' => 'A',
'summary' => 'Bar'
],
'C' => [
'extends' => 'B',
'summary' => 'Bar',
'parameters' => [
'B' => ['type' => 'number']
]
]
]
]);
$a = $d->getOperation('A');
$this->assertEquals('foo', $a->getSummary());
$this->assertTrue($a->hasParam('A'));
$this->assertEquals('string', $a->getParam('B')->getType());
$b = $d->getOperation('B');
$this->assertTrue($a->hasParam('A'));
$this->assertEquals('Bar', $b->getSummary());
$this->assertEquals('string', $a->getParam('B')->getType());
$c = $d->getOperation('C');
$this->assertTrue($a->hasParam('A'));
$this->assertEquals('Bar', $c->getSummary());
$this->assertEquals('number', $c->getParam('B')->getType());
}
}

View File

@@ -0,0 +1,378 @@
<?php
namespace Guzzle\Tests\Service\Description;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\Parameter;
/**
* @covers \GuzzleHttp\Command\Guzzle\Parameter
*/
class ParameterTest extends \PHPUnit_Framework_TestCase
{
protected $data = [
'name' => 'foo',
'type' => 'bar',
'required' => true,
'default' => '123',
'description' => '456',
'minLength' => 2,
'maxLength' => 5,
'location' => 'body',
'static' => true,
'filters' => ['trim', 'json_encode']
];
public function testCreatesParamFromArray()
{
$p = new Parameter($this->data);
$this->assertEquals('foo', $p->getName());
$this->assertEquals('bar', $p->getType());
$this->assertTrue($p->isRequired());
$this->assertEquals('123', $p->getDefault());
$this->assertEquals('456', $p->getDescription());
$this->assertEquals(2, $p->getMinLength());
$this->assertEquals(5, $p->getMaxLength());
$this->assertEquals('body', $p->getLocation());
$this->assertTrue($p->isStatic());
$this->assertEquals(['trim', 'json_encode'], $p->getFilters());
$p->setName('abc');
$this->assertEquals('abc', $p->getName());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesDescription()
{
new Parameter($this->data, ['description' => 'foo']);
}
public function testCanConvertToArray()
{
$p = new Parameter($this->data);
$this->assertEquals($this->data, $p->toArray());
}
public function testUsesStatic()
{
$d = $this->data;
$d['default'] = 'booboo';
$d['static'] = true;
$p = new Parameter($d);
$this->assertEquals('booboo', $p->getValue('bar'));
}
public function testUsesDefault()
{
$d = $this->data;
$d['default'] = 'foo';
$d['static'] = null;
$p = new Parameter($d);
$this->assertEquals('foo', $p->getValue(null));
}
public function testReturnsYourValue()
{
$d = $this->data;
$d['static'] = null;
$p = new Parameter($d);
$this->assertEquals('foo', $p->getValue('foo'));
}
public function testZeroValueDoesNotCauseDefaultToBeReturned()
{
$d = $this->data;
$d['default'] = '1';
$d['static'] = null;
$p = new Parameter($d);
$this->assertEquals('0', $p->getValue('0'));
}
public function testFiltersValues()
{
$d = $this->data;
$d['static'] = null;
$d['filters'] = 'strtoupper';
$p = new Parameter($d);
$this->assertEquals('FOO', $p->filter('foo'));
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage No service description
*/
public function testRequiresServiceDescriptionForFormatting()
{
$d = $this->data;
$d['format'] = 'foo';
$p = new Parameter($d);
$p->filter('bar');
}
public function testConvertsBooleans()
{
$p = new Parameter(['type' => 'boolean']);
$this->assertEquals(true, $p->filter('true'));
$this->assertEquals(false, $p->filter('false'));
}
public function testUsesArrayByDefaultForFilters()
{
$d = $this->data;
$d['filters'] = null;
$p = new Parameter($d);
$this->assertEquals([], $p->getFilters());
}
public function testAllowsSimpleLocationValue()
{
$p = new Parameter(['name' => 'myname', 'location' => 'foo', 'sentAs' => 'Hello']);
$this->assertEquals('foo', $p->getLocation());
$this->assertEquals('Hello', $p->getSentAs());
}
public function testParsesTypeValues()
{
$p = new Parameter(['type' => 'foo']);
$this->assertEquals('foo', $p->getType());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage A [method] value must be specified for each complex filter
*/
public function testValidatesComplexFilters()
{
$p = new Parameter(['filters' => [['args' => 'foo']]]);
}
public function testAllowsComplexFilters()
{
$that = $this;
$param = new Parameter([
'filters' => [
[
'method' => function ($a, $b, $c, $d) use ($that, &$param) {
$that->assertEquals('test', $a);
$that->assertEquals('my_value!', $b);
$that->assertEquals('bar', $c);
$that->assertSame($param, $d);
return 'abc' . $b;
},
'args' => ['test', '@value', 'bar', '@api']
]
]
]);
$this->assertEquals('abcmy_value!', $param->filter('my_value!'));
}
public function testAddsAdditionalProperties()
{
$p = new Parameter([
'type' => 'object',
'additionalProperties' => ['type' => 'string']
]);
$this->assertInstanceOf('GuzzleHttp\Command\Guzzle\Parameter', $p->getAdditionalProperties());
$this->assertNull($p->getAdditionalProperties()->getAdditionalProperties());
$p = new Parameter(['type' => 'object']);
$this->assertTrue($p->getAdditionalProperties());
}
public function testAddsItems()
{
$p = new Parameter([
'type' => 'array',
'items' => ['type' => 'string']
]);
$this->assertInstanceOf('GuzzleHttp\Command\Guzzle\Parameter', $p->getItems());
$out = $p->toArray();
$this->assertEquals('array', $out['type']);
$this->assertInternalType('array', $out['items']);
}
public function testCanRetrieveKnownPropertiesUsingDataMethod()
{
$p = new Parameter(['data' => ['name' => 'test'], 'extra' => 'hi!']);
$this->assertEquals('test', $p->getData('name'));
$this->assertEquals(['name' => 'test'], $p->getData());
$this->assertNull($p->getData('fjnweefe'));
$this->assertEquals('hi!', $p->getData('extra'));
}
public function testHasPattern()
{
$p = new Parameter(['pattern' => '/[0-9]+/']);
$this->assertEquals('/[0-9]+/', $p->getPattern());
}
public function testHasEnum()
{
$p = new Parameter(['enum' => ['foo', 'bar']]);
$this->assertEquals(['foo', 'bar'], $p->getEnum());
}
public function testSerializesItems()
{
$p = new Parameter([
'type' => 'object',
'additionalProperties' => ['type' => 'string']
]);
$this->assertEquals([
'type' => 'object',
'additionalProperties' => ['type' => 'string']
], $p->toArray());
}
public function testResolvesRefKeysRecursively()
{
$description = new Description([
'models' => [
'JarJar' => ['type' => 'string', 'default' => 'Mesa address tha senate!'],
'Anakin' => ['type' => 'array', 'items' => ['$ref' => 'JarJar']]
],
]);
$p = new Parameter(['$ref' => 'Anakin', 'description' => 'added'], ['description' => $description]);
$this->assertEquals([
'description' => 'added',
'$ref' => 'Anakin'
], $p->toArray());
}
public function testResolvesExtendsRecursively()
{
$jarJar = ['type' => 'string', 'default' => 'Mesa address tha senate!', 'description' => 'a'];
$anakin = ['type' => 'array', 'items' => ['extends' => 'JarJar', 'description' => 'b']];
$description = new Description([
'models' => ['JarJar' => $jarJar, 'Anakin' => $anakin]
]);
// Description attribute will be updated, and format added
$p = new Parameter(['extends' => 'Anakin', 'format' => 'date'], ['description' => $description]);
$this->assertEquals([
'format' => 'date',
'extends' => 'Anakin'
], $p->toArray());
}
public function testHasKeyMethod()
{
$p = new Parameter(['name' => 'foo', 'sentAs' => 'bar']);
$this->assertEquals('bar', $p->getWireName());
}
public function testIncludesNameInToArrayWhenItemsAttributeHasName()
{
$p = new Parameter([
'type' => 'array',
'name' => 'Abc',
'items' => [
'name' => 'Foo',
'type' => 'object'
]
]);
$result = $p->toArray();
$this->assertEquals([
'type' => 'array',
'name' => 'Abc',
'items' => [
'name' => 'Foo',
'type' => 'object'
]
], $result);
}
public function dateTimeProvider()
{
$d = 'October 13, 2012 16:15:46 UTC';
return [
[$d, 'date-time', '2012-10-13T16:15:46Z'],
[$d, 'date', '2012-10-13'],
[$d, 'timestamp', strtotime($d)],
[new \DateTime($d), 'timestamp', strtotime($d)]
];
}
/**
* @dataProvider dateTimeProvider
*/
public function testAppliesFormat($d, $format, $result)
{
$p = new Parameter(['format' => $format], ['description' => new Description([])]);
$this->assertEquals($format, $p->getFormat());
$this->assertEquals($result, $p->filter($d));
}
public function testHasMinAndMax()
{
$p = new Parameter([
'minimum' => 2,
'maximum' => 3,
'minItems' => 4,
'maxItems' => 5,
]);
$this->assertEquals(2, $p->getMinimum());
$this->assertEquals(3, $p->getMaximum());
$this->assertEquals(4, $p->getMinItems());
$this->assertEquals(5, $p->getMaxItems());
}
public function testHasProperties()
{
$data = [
'type' => 'object',
'properties' => [
'foo' => ['type' => 'string'],
'bar' => ['type' => 'string'],
]
];
$p = new Parameter($data);
$this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Parameter', $p->getProperty('foo'));
$this->assertSame($p->getProperty('foo'), $p->getProperty('foo'));
$this->assertNull($p->getProperty('wefwe'));
$properties = $p->getProperties();
$this->assertInternalType('array', $properties);
foreach ($properties as $prop) {
$this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Parameter', $prop);
}
$this->assertEquals($data, $p->toArray());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected a string. Got: array
*/
public function testThrowsWhenNotPassString()
{
$emptyParam = new Parameter();
$this->assertFalse($emptyParam->has([]));
$this->assertFalse($emptyParam->has(new \stdClass()));
$this->assertFalse($emptyParam->has('1'));
$this->assertFalse($emptyParam->has(1));
}
public function testHasReturnsFalseForWrongOrEmptyValues()
{
$emptyParam = new Parameter();
$this->assertFalse($emptyParam->has(''));
$this->assertFalse($emptyParam->has('description'));
$this->assertFalse($emptyParam->has('noExisting'));
}
public function testHasReturnsTrueForCorrectValues()
{
$p = new Parameter([
'minimum' => 2,
'maximum' => 3,
'minItems' => 4,
'maxItems' => 5,
]);
$this->assertTrue($p->has('minimum'));
$this->assertTrue($p->has('maximum'));
$this->assertTrue($p->has('minItems'));
$this->assertTrue($p->has('maxItems'));
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\QuerySerializer;
use GuzzleHttp\Command\Guzzle\QuerySerializer\Rfc3986Serializer;
class Rfc3986SerializerTest extends \PHPUnit_Framework_TestCase
{
public function queryProvider()
{
return [
[['foo' => 'bar'], 'foo=bar'],
[['foo' => [1, 2]], 'foo[0]=1&foo[1]=2'],
[['foo' => ['bar' => 'baz', 'bim' => [4, 5]]], 'foo[bar]=baz&foo[bim][0]=4&foo[bim][1]=5']
];
}
/**
* @dataProvider queryProvider
*/
public function testSerializeQueryParams(array $params, $expectedResult)
{
$serializer = new Rfc3986Serializer();
$result = $serializer->aggregate($params);
$this->assertEquals($expectedResult, urldecode($result));
}
public function testCanRemoveNumericIndices()
{
$serializer = new Rfc3986Serializer(true);
$result = $serializer->aggregate(['foo' => ['bar', 'baz'], 'bar' => ['bim' => [4, 5]]]);
$this->assertEquals('foo[]=bar&foo[]=baz&bar[bim][]=4&bar[bim][]=5', urldecode($result));
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\RequestLocation\BodyLocation;
use GuzzleHttp\Psr7\Request;
/**
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\BodyLocation
*/
class BodyLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group RequestLocation
*/
public function testVisitsLocation()
{
$location = new BodyLocation('body');
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$request = $location->visit($command, $request, $param);
$this->assertEquals('foo=bar', $request->getBody()->getContents());
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\RequestLocation\FormParamLocation;
use GuzzleHttp\Command\Guzzle\RequestLocation\PostFieldLocation;
use GuzzleHttp\Psr7\Request;
/**
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\FormParamLocation
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\AbstractLocation
*/
class FormParamLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group RequestLocation
*/
public function testVisitsLocation()
{
$location = new FormParamLocation();
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$request = $location->visit($command, $request, $param);
$operation = new Operation();
$request = $location->after($command, $request, $operation);
$this->assertEquals('foo=bar', $request->getBody()->getContents());
$this->assertArraySubset([0 => 'application/x-www-form-urlencoded; charset=utf-8'], $request->getHeader('Content-Type'));
}
/**
* @group RequestLocation
*/
public function testAddsAdditionalProperties()
{
$location = new FormParamLocation();
$command = new Command('foo', ['foo' => 'bar']);
$command['add'] = 'props';
$request = new Request('POST', 'http://httbin.org', []);
$param = new Parameter(['name' => 'foo']);
$request = $location->visit($command, $request, $param);
$operation = new Operation([
'additionalParameters' => [
'location' => 'formParam'
]
]);
$request = $location->after($command, $request, $operation);
$this->assertEquals('foo=bar&add=props', $request->getBody()->getContents());
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\RequestLocation\HeaderLocation;
use GuzzleHttp\Psr7\Request;
/**
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\HeaderLocation
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\AbstractLocation
*/
class HeaderLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group RequestLocation
*/
public function testVisitsLocation()
{
$location = new HeaderLocation('header');
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$request = $location->visit($command, $request, $param);
$header = $request->getHeader('foo');
$this->assertTrue(is_array($header));
$this->assertArraySubset([0 => 'bar'], $request->getHeader('foo'));
}
/**
* @group RequestLocation
*/
public function testAddsAdditionalProperties()
{
$location = new HeaderLocation('header');
$command = new Command('foo', ['foo' => 'bar']);
$command['add'] = 'props';
$operation = new Operation([
'additionalParameters' => [
'location' => 'header'
]
]);
$request = new Request('POST', 'http://httbin.org');
$request = $location->after($command, $request, $operation);
$header = $request->getHeader('add');
$this->assertTrue(is_array($header));
$this->assertArraySubset([0 => 'props'], $header);
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\RequestLocation\JsonLocation;
use GuzzleHttp\Psr7\Request;
/**
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\JsonLocation
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\AbstractLocation
*/
class JsonLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group RequestLocation
*/
public function testVisitsLocation()
{
$location = new JsonLocation('json');
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$location->visit($command, $request, $param);
$operation = new Operation();
$request = $location->after($command, $request, $operation);
$this->assertEquals('{"foo":"bar"}', $request->getBody()->getContents());
$this->assertArraySubset([0 => 'application/json'], $request->getHeader('Content-Type'));
}
/**
* @group RequestLocation
*/
public function testVisitsAdditionalProperties()
{
$location = new JsonLocation('json', 'foo');
$command = new Command('foo', ['foo' => 'bar']);
$command['baz'] = ['bam' => [1]];
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$location->visit($command, $request, $param);
$operation = new Operation([
'additionalParameters' => [
'location' => 'json'
]
]);
$request = $location->after($command, $request, $operation);
$this->assertEquals('{"foo":"bar","baz":{"bam":[1]}}', $request->getBody()->getContents());
$this->assertEquals([0 => 'foo'], $request->getHeader('Content-Type'));
}
/**
* @group RequestLocation
*/
public function testVisitsNestedLocation()
{
$location = new JsonLocation('json');
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'properties' => [
'baz' => [
'type' => 'array',
'items' => [
'type' => 'string',
'filters' => ['strtoupper']
]
]
],
'additionalProperties' => [
'type' => 'array',
'items' => [
'type' => 'string',
'filters' => ['strtolower']
]
]
]);
$command['foo'] = [
'baz' => ['a', 'b'],
'bam' => ['A', 'B'],
];
$location->visit($command, $request, $param);
$operation = new Operation();
$request = $location->after($command, $request, $operation);
$this->assertEquals('{"foo":{"baz":["A","B"],"bam":["a","b"]}}', (string) $request->getBody()->getContents());
$this->assertEquals([0 => 'application/json'], $request->getHeader('Content-Type'));
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\RequestLocation\MultiPartLocation;
use GuzzleHttp\Command\Guzzle\RequestLocation\PostFileLocation;
use GuzzleHttp\Psr7\Request;
/**
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\MultiPartLocation
*/
class MultiPartLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group RequestLocation
*/
public function testVisitsLocation()
{
$location = new MultiPartLocation();
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org', []);
$param = new Parameter(['name' => 'foo']);
$request = $location->visit($command, $request, $param);
$operation = new Operation();
$request = $location->after($command, $request, $operation);
$actual = $request->getBody()->getContents();
$this->assertNotFalse(strpos($actual, 'name="foo"'));
$this->assertNotFalse(strpos($actual, 'bar'));
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\RequestLocation\QueryLocation;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
/**
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\QueryLocation
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\AbstractLocation
*/
class QueryLocationTest extends \PHPUnit_Framework_TestCase
{
public function queryProvider()
{
return [
[['foo' => 'bar'], 'foo=bar'],
[['foo' => [1, 2]], 'foo[0]=1&foo[1]=2'],
[['foo' => ['bar' => 'baz', 'bim' => [4, 5]]], 'foo[bar]=baz&foo[bim][0]=4&foo[bim][1]=5']
];
}
/**
* @group RequestLocation
*/
public function testVisitsLocation()
{
$location = new QueryLocation();
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$request = $location->visit($command, $request, $param);
$this->assertEquals('foo=bar', urldecode($request->getUri()->getQuery()));
}
public function testVisitsMultipleLocations()
{
$request = new Request('POST', 'http://httbin.org');
// First location
$location = new QueryLocation();
$command = new Command('foo', ['foo' => 'bar']);
$param = new Parameter(['name' => 'foo']);
$request = $location->visit($command, $request, $param);
// Second location
$location = new QueryLocation();
$command = new Command('baz', ['baz' => [6, 7]]);
$param = new Parameter(['name' => 'baz']);
$request = $location->visit($command, $request, $param);
$this->assertEquals('foo=bar&baz[0]=6&baz[1]=7', urldecode($request->getUri()->getQuery()));
}
/**
* @group RequestLocation
*/
public function testAddsAdditionalProperties()
{
$location = new QueryLocation();
$command = new Command('foo', ['foo' => 'bar']);
$command['add'] = 'props';
$operation = new Operation([
'additionalParameters' => [
'location' => 'query'
]
]);
$request = new Request('POST', 'http://httbin.org');
$request = $location->after($command, $request, $operation);
$this->assertEquals('props', Psr7\parse_query($request->getUri()->getQuery())['add']);
}
}

View File

@@ -0,0 +1,525 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\RequestLocation;
use GuzzleHttp\Client;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\RequestLocation\XmlLocation;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
/**
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\XmlLocation
*/
class XmlLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group RequestLocation
*/
public function testVisitsLocation()
{
$location = new XmlLocation();
$command = new Command('foo', ['foo' => 'bar']);
$command['bar'] = 'test';
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$location->visit($command, $request, $param);
$param = new Parameter(['name' => 'bar']);
$location->visit($command, $request, $param);
$operation = new Operation();
$request = $location->after($command, $request, $operation);
$xml = $request->getBody()->getContents();
$this->assertEquals('<?xml version="1.0"?>' . "\n"
. '<Request><foo>bar</foo><bar>test</bar></Request>' . "\n", $xml);
$header = $request->getHeader('Content-Type');
$this->assertArraySubset([0 => 'application/xml'], $header);
}
/**
* @group RequestLocation
*/
public function testCreatesBodyForEmptyDocument()
{
$location = new XmlLocation();
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$operation = new Operation([
'data' => ['xmlAllowEmpty' => true]
]);
$request = $location->after($command, $request, $operation);
$xml = $request->getBody()->getContents();
$this->assertEquals('<?xml version="1.0"?>' . "\n"
. '<Request/>' . "\n", $xml);
$header = $request->getHeader('Content-Type');
$this->assertArraySubset([0 => 'application/xml'], $header);
}
/**
* @group RequestLocation
*/
public function testAddsAdditionalParameters()
{
$location = new XmlLocation('xml', 'test');
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$command['foo'] = 'bar';
$location->visit($command, $request, $param);
$operation = new Operation([
'additionalParameters' => [
'location' => 'xml'
]
]);
$command['bam'] = 'boo';
$request = $location->after($command, $request, $operation);
$xml = $request->getBody()->getContents();
$this->assertEquals('<?xml version="1.0"?>' . "\n"
. '<Request><foo>bar</foo><foo>bar</foo><bam>boo</bam></Request>' . "\n", $xml);
$header = $request->getHeader('Content-Type');
$this->assertArraySubset([0 => 'test'], $header);
}
/**
* @group RequestLocation
*/
public function testAllowsXmlEncoding()
{
$location = new XmlLocation();
$operation = new Operation([
'data' => ['xmlEncoding' => 'UTF-8']
]);
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$command['foo'] = 'bar';
$location->visit($command, $request, $param);
$request = $location->after($command, $request, $operation);
$xml = $request->getBody()->getContents();
$this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>' . "\n"
. '<Request><foo>bar</foo></Request>' . "\n", $xml);
}
public function xmlProvider()
{
return [
[
[
'data' => [
'xmlRoot' => [
'name' => 'test',
'namespaces' => 'http://foo.com'
]
],
'parameters' => [
'Foo' => [
'location' => 'xml',
'type' => 'string'
],
'Baz' => [
'location' => 'xml',
'type' => 'string'
]
]
],
[
'Foo' => 'test',
'Baz' => 'bar'
],
'<test xmlns="http://foo.com"><Foo>test</Foo><Baz>bar</Baz></test>'
],
// Ensure that the content-type is not added
[
[
'parameters' => [
'Foo' => [
'location' => 'xml',
'type' => 'string'
]
]
],
[],
''
],
// Test with adding attributes and no namespace
[
[
'data' => [
'xmlRoot' => [
'name' => 'test'
]
],
'parameters' => [
'Foo' => [
'location' => 'xml',
'type' => 'string',
'data' => ['xmlAttribute' => true]
]
]
],
[
'Foo' => 'test',
'Baz' => 'bar'
],
'<test Foo="test"/>'
],
// Test adding with an array
[
[
'parameters' => [
'Foo' => [
'location' => 'xml',
'type' => 'string'
],
'Baz' => [
'type' => 'array',
'location' => 'xml',
'items' => [
'type' => 'numeric',
'sentAs' => 'Bar'
]
]
]
],
['Foo' => 'test', 'Baz' => [1, 2]],
'<Request><Foo>test</Foo><Baz><Bar>1</Bar><Bar>2</Bar></Baz></Request>'
],
// Test adding an object
[
[
'parameters' => [
'Foo' => ['location' => 'xml', 'type' => 'string'],
'Baz' => [
'type' => 'object',
'location' => 'xml',
'properties' => [
'Bar' => ['type' => 'string'],
'Bam' => []
]
]
]
],
[
'Foo' => 'test',
'Baz' => [
'Bar' => 'abc',
'Bam' => 'foo'
]
],
'<Request><Foo>test</Foo><Baz><Bar>abc</Bar><Bam>foo</Bam></Baz></Request>'
],
// Add an array that contains an object
[
[
'parameters' => [
'Baz' => [
'type' => 'array',
'location' => 'xml',
'items' => [
'type' => 'object',
'sentAs' => 'Bar',
'properties' => ['A' => [], 'B' => []]
]
]
]
],
['Baz' => [
[
'A' => '1',
'B' => '2'
],
[
'A' => '3',
'B' => '4'
]
]],
'<Request><Baz><Bar><A>1</A><B>2</B></Bar><Bar><A>3</A><B>4</B></Bar></Baz></Request>'
],
// Add an object of attributes
[
[
'parameters' => [
'Foo' => [
'location' => 'xml',
'type' => 'string'
],
'Baz' => [
'type' => 'object',
'location' => 'xml',
'properties' => [
'Bar' => [
'type' => 'string',
'data' => [
'xmlAttribute' => true
]
],
'Bam' => []
]
]
]
],
[
'Foo' => 'test',
'Baz' => [
'Bar' => 'abc',
'Bam' => 'foo'
]
],
'<Request><Foo>test</Foo><Baz Bar="abc"><Bam>foo</Bam></Baz></Request>'
],
// Check order doesn't matter
[
[
'parameters' => [
'Foo' => [
'location' => 'xml',
'type' => 'string'
],
'Baz' => [
'type' => 'object',
'location' => 'xml',
'properties' => [
'Bar' => [
'type' => 'string',
'data' => [
'xmlAttribute' => true
]
],
'Bam' => []
]
]
]
],
[
'Foo' => 'test',
'Baz' => [
'Bam' => 'foo',
'Bar' => 'abc'
]
],
'<Request><Foo>test</Foo><Baz Bar="abc"><Bam>foo</Bam></Baz></Request>'
],
// Add values with custom namespaces
[
[
'parameters' => [
'Foo' => [
'location' => 'xml',
'type' => 'string',
'data' => [
'xmlNamespace' => 'http://foo.com'
]
]
]
],
['Foo' => 'test'],
'<Request><Foo xmlns="http://foo.com">test</Foo></Request>'
],
// Add attributes with custom namespace prefix
[
[
'parameters' => [
'Wrap' => [
'type' => 'object',
'location' => 'xml',
'properties' => [
'Foo' => [
'type' => 'string',
'sentAs' => 'xsi:baz',
'data' => [
'xmlNamespace' => 'http://foo.com',
'xmlAttribute' => true
]
]
]
],
]
],
['Wrap' => [
'Foo' => 'test'
]],
'<Request><Wrap xsi:baz="test" xmlns:xsi="http://foo.com"/></Request>'
],
// Add nodes with custom namespace prefix
[
[
'parameters' => [
'Wrap' => [
'type' => 'object',
'location' => 'xml',
'properties' => [
'Foo' => [
'type' => 'string',
'sentAs' => 'xsi:Foo',
'data' => [
'xmlNamespace' => 'http://foobar.com'
]
]
]
],
]
],
['Wrap' => [
'Foo' => 'test'
]],
'<Request><Wrap><xsi:Foo xmlns:xsi="http://foobar.com">test</xsi:Foo></Wrap></Request>'
],
[
[
'parameters' => [
'Foo' => [
'location' => 'xml',
'type' => 'string',
'data' => [
'xmlNamespace' => 'http://foo.com'
]
]
]
],
['Foo' => '<h1>This is a title</h1>'],
'<Request><Foo xmlns="http://foo.com"><![CDATA[<h1>This is a title</h1>]]></Foo></Request>'
],
// Flat array at top level
[
[
'parameters' => [
'Bars' => [
'type' => 'array',
'data' => ['xmlFlattened' => true],
'location' => 'xml',
'items' => [
'type' => 'object',
'sentAs' => 'Bar',
'properties' => [
'A' => [],
'B' => []
]
]
],
'Boos' => [
'type' => 'array',
'data' => ['xmlFlattened' => true],
'location' => 'xml',
'items' => [
'sentAs' => 'Boo',
'type' => 'string'
]
]
]
],
[
'Bars' => [
['A' => '1', 'B' => '2'],
['A' => '3', 'B' => '4']
],
'Boos' => ['test', '123']
],
'<Request><Bar><A>1</A><B>2</B></Bar><Bar><A>3</A><B>4</B></Bar><Boo>test</Boo><Boo>123</Boo></Request>'
],
// Nested flat arrays
[
[
'parameters' => [
'Delete' => [
'type' => 'object',
'location' => 'xml',
'properties' => [
'Items' => [
'type' => 'array',
'data' => ['xmlFlattened' => true],
'items' => [
'type' => 'object',
'sentAs' => 'Item',
'properties' => [
'A' => [],
'B' => []
]
]
]
]
]
]
],
[
'Delete' => [
'Items' => [
['A' => '1', 'B' => '2'],
['A' => '3', 'B' => '4']
]
]
],
'<Request><Delete><Item><A>1</A><B>2</B></Item><Item><A>3</A><B>4</B></Item></Delete></Request>'
],
// Test adding root node attributes after nodes
[
[
'data' => [
'xmlRoot' => [
'name' => 'test'
]
],
'parameters' => [
'Foo' => ['location' => 'xml', 'type' => 'string'],
'Baz' => ['location' => 'xml', 'type' => 'string', 'data' => ['xmlAttribute' => true]],
]
],
['Foo' => 'test', 'Baz' => 'bar'],
'<test Baz="bar"><Foo>test</Foo></test>'
],
];
}
/**
* @param array $operation
* @param array $input
* @param string $xml
* @dataProvider xmlProvider
* @group RequestLocation
*/
public function testSerializesXml(array $operation, array $input, $xml)
{
$container = [];
$history = Middleware::history($container);
$mock = new MockHandler([new Response(200)]);
$stack = new HandlerStack($mock);
$stack->push($history);
$operation['uri'] = 'http://httpbin.org';
$client = new GuzzleClient(
new Client(['handler' => $stack]),
new Description([
'operations' => [
'foo' => $operation
]
])
);
$command = $client->getCommand('foo', $input);
$client->execute($command);
$this->assertCount(1, $container);
foreach ($container as $transaction) {
/** @var Request $request */
$request = $transaction['request'];
if (empty($input)) {
if ($request->hasHeader('Content-Type')) {
$this->assertArraySubset([0 => ''], $request->getHeader('Content-Type'));
}
} else {
$this->assertArraySubset([0 => 'application/xml'], $request->getHeader('Content-Type'));
}
$body = str_replace(["\n", "<?xml version=\"1.0\"?>"], '', (string) $request->getBody());
$this->assertEquals($xml, $body);
}
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\ResponseLocation\BodyLocation;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Psr7\Response;
/**
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\BodyLocation
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\AbstractLocation
*/
class BodyLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group ResponseLocation
*/
public function testVisitsLocation()
{
$location = new BodyLocation();
$parameter = new Parameter([
'name' => 'val',
'filters' => ['strtoupper']
]);
$response = new Response(200, [], 'foo');
$result = new Result();
$result = $location->visit($result, $response, $parameter);
$this->assertEquals('FOO', $result['val']);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\ResponseLocation\HeaderLocation;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Psr7\Response;
/**
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\HeaderLocation
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\AbstractLocation
*/
class HeaderLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group ResponseLocation
*/
public function testVisitsLocation()
{
$location = new HeaderLocation();
$parameter = new Parameter([
'name' => 'val',
'sentAs' => 'X-Foo',
'filters' => ['strtoupper']
]);
$response = new Response(200, ['X-Foo' => 'bar']);
$result = new Result();
$result = $location->visit($result, $response, $parameter);
$this->assertEquals('BAR', $result['val']);
}
}

View File

@@ -0,0 +1,581 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\ResponseLocation\JsonLocation;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Command\ResultInterface;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
/**
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\JsonLocation
* @covers \GuzzleHttp\Command\Guzzle\Deserializer
*/
class JsonLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group ResponseLocation
*/
public function testVisitsLocation()
{
$location = new JsonLocation();
$parameter = new Parameter([
'name' => 'val',
'sentAs' => 'vim',
'filters' => ['strtoupper']
]);
$response = new Response(200, [], '{"vim":"bar"}');
$result = new Result();
$result = $location->before($result, $response, $parameter);
$result = $location->visit($result, $response, $parameter);
$this->assertEquals('BAR', $result['val']);
}
/**
* @group ResponseLocation
* @param $name
* @param $expected
*/
public function testVisitsWiredArray()
{
$json = ['car_models' => ['ferrari', 'aston martin']];
$body = \GuzzleHttp\json_encode($json);
$response = new Response(200, ['Content-Type' => 'application/json'], $body);
$mock = new MockHandler([$response]);
$guzzle = new Client(['handler' => $mock]);
$description = new Description([
'operations' => [
'getCars' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'Cars'
]
],
'models' => [
'Cars' => [
'type' => 'object',
'location' => 'json',
'properties' => [
'cars' => [
'type' => 'array',
'sentAs' => 'car_models',
'items' => [
'type' => 'object',
]
]
],
]
]
]);
$guzzle = new GuzzleClient($guzzle, $description);
$result = $guzzle->getCars();
$this->assertEquals(['cars' => ['ferrari', 'aston martin']], $result->toArray());
}
/**
* @group ResponseLocation
*/
public function testVisitsAdditionalProperties()
{
$location = new JsonLocation();
$parameter = new Parameter();
$model = new Parameter(['additionalProperties' => ['location' => 'json']]);
$response = new Response(200, [], '{"vim":"bar","qux":[1,2]}');
$result = new Result();
$result = $location->before($result, $response, $parameter);
$result = $location->visit($result, $response, $parameter);
$result = $location->after($result, $response, $model);
$this->assertEquals('bar', $result['vim']);
$this->assertEquals([1, 2], $result['qux']);
}
/**
* @group ResponseLocation
*/
public function testVisitsAdditionalPropertiesWithEmptyResponse()
{
$location = new JsonLocation();
$parameter = new Parameter();
$model = new Parameter(['additionalProperties' => ['location' => 'json']]);
$response = new Response(204);
$result = new Result();
$result = $location->before($result, $response, $parameter);
$result = $location->visit($result, $response, $parameter);
$result = $location->after($result, $response, $model);
$this->assertEquals([], $result->toArray());
}
public function jsonProvider()
{
return [
[null, [['foo' => 'BAR'], ['baz' => 'BAM']]],
['under_me', ['under_me' => [['foo' => 'BAR'], ['baz' => 'BAM']]]],
];
}
/**
* @dataProvider jsonProvider
* @group ResponseLocation
* @param $name
* @param $expected
*/
public function testVisitsTopLevelArrays($name, $expected)
{
$json = [
['foo' => 'bar'],
['baz' => 'bam'],
];
$body = \GuzzleHttp\json_encode($json);
$response = new Response(200, ['Content-Type' => 'application/json'], $body);
$mock = new MockHandler([$response]);
$guzzle = new Client(['handler' => $mock]);
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j'
]
],
'models' => [
'j' => [
'type' => 'array',
'location' => 'json',
'name' => $name,
'items' => [
'type' => 'object',
'additionalProperties' => [
'type' => 'string',
'filters' => ['strtoupper']
]
]
]
]
]);
$guzzle = new GuzzleClient($guzzle, $description);
/** @var ResultInterface $result */
$result = $guzzle->foo();
$this->assertEquals($expected, $result->toArray());
}
/**
* @group ResponseLocation
*/
public function testVisitsNestedArrays()
{
$json = [
'scalar' => 'foo',
'nested' => [
'bar',
'baz'
]
];
$body = \GuzzleHttp\json_encode($json);
$response = new Response(200, ['Content-Type' => 'application/json'], $body);
$mock = new MockHandler([$response]);
$httpClient = new Client(['handler' => $mock]);
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j'
]
],
'models' => [
'j' => [
'type' => 'object',
'location' => 'json',
'properties' => [
'scalar' => ['type' => 'string'],
'nested' => [
'type' => 'array',
'items' => ['type' => 'string']
]
]
]
]
]);
$guzzle = new GuzzleClient($httpClient, $description);
/** @var ResultInterface $result */
$result = $guzzle->foo();
$expected = [
'scalar' => 'foo',
'nested' => [
'bar',
'baz'
]
];
$this->assertEquals($expected, $result->toArray());
}
public function nestedProvider()
{
return [
[
[
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j'
]
],
'models' => [
'j' => [
'type' => 'object',
'properties' => [
'nested' => [
'location' => 'json',
'type' => 'object',
'properties' => [
'foo' => ['type' => 'string'],
'bar' => ['type' => 'number'],
'bam' => [
'type' => 'object',
'properties' => [
'abc' => [
'type' => 'number'
]
]
]
]
]
],
'additionalProperties' => [
'location' => 'json',
'type' => 'string',
'filters' => ['strtoupper']
]
]
]
]
],
[
[
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j'
]
],
'models' => [
'j' => [
'type' => 'object',
'location' => 'json',
'properties' => [
'nested' => [
'type' => 'object',
'properties' => [
'foo' => ['type' => 'string'],
'bar' => ['type' => 'number'],
'bam' => [
'type' => 'object',
'properties' => [
'abc' => [
'type' => 'number'
]
]
]
]
]
],
'additionalProperties' => [
'type' => 'string',
'filters' => ['strtoupper']
]
]
]
]
]
];
}
/**
* @dataProvider nestedProvider
* @group ResponseLocation
*/
public function testVisitsNestedProperties($desc)
{
$json = [
'nested' => [
'foo' => 'abc',
'bar' => 123,
'bam' => [
'abc' => 456
]
],
'baz' => 'boo'
];
$body = \GuzzleHttp\json_encode($json);
$response = new Response(200, ['Content-Type' => 'application/json'], $body);
$mock = new MockHandler([$response]);
$httpClient = new Client(['handler' => $mock]);
$description = new Description($desc);
$guzzle = new GuzzleClient($httpClient, $description);
/** @var ResultInterface $result */
$result = $guzzle->foo();
$expected = [
'nested' => [
'foo' => 'abc',
'bar' => 123,
'bam' => [
'abc' => 456
]
],
'baz' => 'BOO'
];
$this->assertEquals($expected, $result->toArray());
}
/**
* @group ResponseLocation
*/
public function testVisitsNullResponseProperties()
{
$json = [
'data' => [
'link' => null
]
];
$body = \GuzzleHttp\json_encode($json);
$response = new Response(200, ['Content-Type' => 'application/json'], $body);
$mock = new MockHandler([$response]);
$httpClient = new Client(['handler' => $mock]);
$description = new Description(
[
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j'
]
],
'models' => [
'j' => [
'type' => 'object',
'location' => 'json',
'properties' => [
'scalar' => ['type' => 'string'],
'data' => [
'type' => 'object',
'location' => 'json',
'properties' => [
'link' => [
'name' => 'val',
'type' => 'string',
'location' => 'json'
],
],
'additionalProperties' => false
]
]
]
]
]
);
$guzzle = new GuzzleClient($httpClient, $description);
/** @var ResultInterface $result */
$result = $guzzle->foo();
$expected = [
'data' => [
'link' => null
]
];
$this->assertEquals($expected, $result->toArray());
}
/**
* @group ResponseLocation
*/
public function testVisitsNestedArrayOfArrays()
{
$json = [
'scalar' => 'foo',
'nested' => [
[
'bar' => 123,
'baz' => false,
],
[
'bar' => 345,
'baz' => true,
],
[
'bar' => 678,
'baz' => true,
],
]
];
$body = \GuzzleHttp\json_encode($json);
$response = new Response(200, ['Content-Type' => 'application/json'], $body);
$mock = new MockHandler([$response]);
$httpClient = new Client(['handler' => $mock]);
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j'
]
],
'models' => [
'j' => [
'type' => 'object',
'properties' => [
'scalar' => [
// for some reason (probably because location is also set on array of arrays)
// array of arrays sibling elements must have location set to `json`
// otherwise JsonLocation ignores them
'location' => 'json',
'type' => 'string'
],
'nested' => [
// array of arrays type must be set to `array`
// without that JsonLocation throws an exception
'type' => 'array',
// for array of arrays `location` must be set to `json`
// otherwise JsonLocation returns an empty array
'location' => 'json',
'items' => [
// although this is array of arrays, array items type
// must be set as `object`
'type' => 'object',
'properties' => [
'bar' => [
'type' => 'integer',
],
'baz' => [
'type' => 'boolean',
],
],
]
]
]
]
]
]);
$guzzle = new GuzzleClient($httpClient, $description);
/** @var ResultInterface $result */
$result = $guzzle->foo();
$expected = [
'scalar' => 'foo',
'nested' => [
[
'bar' => 123,
'baz' => false,
],
[
'bar' => 345,
'baz' => true,
],
[
'bar' => 678,
'baz' => true,
],
]
];
$this->assertEquals($expected, $result->toArray());
}
/**
* @group ResponseLocation
*/
public function testVisitsNestedArrayOfObjects()
{
$json = json_decode('{"scalar":"foo","nested":[{"bar":123,"baz":false},{"bar":345,"baz":true},{"bar":678,"baz":true}]}');
$body = \GuzzleHttp\json_encode($json);
$response = new Response(200, ['Content-Type' => 'application/json'], $body);
$mock = new MockHandler([$response]);
$httpClient = new Client(['handler' => $mock]);
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j'
]
],
'models' => [
'j' => [
'type' => 'object',
'location' => 'json',
'properties' => [
'scalar' => [
'type' => 'string'
],
'nested' => [
// array of objects type must be set to `array`
// without that JsonLocation throws an exception
'type' => 'array',
'items' => [
// array elements type must be set to `object`
'type' => 'object',
'properties' => [
'bar' => [
'type' => 'integer',
],
'baz' => [
'type' => 'boolean',
],
],
]
]
]
]
]
]);
$guzzle = new GuzzleClient($httpClient, $description);
/** @var ResultInterface $result */
$result = $guzzle->foo();
$expected = [
'scalar' => 'foo',
'nested' => [
[
'bar' => 123,
'baz' => false,
],
[
'bar' => 345,
'baz' => true,
],
[
'bar' => 678,
'baz' => true,
],
]
];
$this->assertEquals($expected, $result->toArray());
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\ResponseLocation\ReasonPhraseLocation;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Psr7\Response;
/**
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\ReasonPhraseLocation
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\AbstractLocation
*/
class ReasonPhraseLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group ResponseLocation
*/
public function testVisitsLocation()
{
$location = new ReasonPhraseLocation();
$parameter = new Parameter([
'name' => 'val',
'filters' => ['strtolower']
]);
$response = new Response(200);
$result = new Result();
$result = $location->visit($result, $response, $parameter);
$this->assertEquals('ok', $result['val']);
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\ResponseLocation\StatusCodeLocation;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Psr7\Response;
/**
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\StatusCodeLocation
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\AbstractLocation
*/
class StatusCodeLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group ResponseLocation
*/
public function testVisitsLocation()
{
$location = new StatusCodeLocation();
$parameter = new Parameter(['name' => 'val']);
$response = new Response(200);
$result = new Result();
$result = $location->visit($result, $response, $parameter);
$this->assertEquals(200, $result['val']);
}
}

View File

@@ -0,0 +1,795 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\ResponseLocation\XmlLocation;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Psr7\Response;
/**
* @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\XmlLocation
*/
class XmlLocationTest extends \PHPUnit_Framework_TestCase
{
/**
* @group ResponseLocation
*/
public function testVisitsLocation()
{
$location = new XmlLocation();
$parameter = new Parameter([
'name' => 'val',
'sentAs' => 'vim',
'filters' => ['strtoupper']
]);
$model = new Parameter();
$response = new Response(200, [], \GuzzleHttp\Psr7\stream_for('<w><vim>bar</vim></w>'));
$result = new Result();
$result = $location->before($result, $response, $model);
$result = $location->visit($result, $response, $parameter);
$result = $location->after($result, $response, $model);
$this->assertEquals('BAR', $result['val']);
}
/**
* @group ResponseLocation
*/
public function testVisitsAdditionalProperties()
{
$location = new XmlLocation();
$parameter = new Parameter();
$model = new Parameter(['additionalProperties' => ['location' => 'xml']]);
$response = new Response(200, [], \GuzzleHttp\Psr7\stream_for('<w><vim>bar</vim></w>'));
$result = new Result();
$result = $location->before($result, $response, $parameter);
$result = $location->visit($result, $response, $parameter);
$result = $location->after($result, $response, $model);
$this->assertEquals('bar', $result['vim']);
}
/**
* @group ResponseLocation
*/
public function testEnsuresFlatArraysAreFlat()
{
$param = new Parameter([
'location' => 'xml',
'name' => 'foo',
'type' => 'array',
'items' => ['type' => 'string'],
]);
$xml = '<xml><foo>bar</foo><foo>baz</foo></xml>';
$this->xmlTest($param, $xml, ['foo' => ['bar', 'baz']]);
$this->xmlTest($param, '<xml><foo>bar</foo></xml>', ['foo' => ['bar']]);
}
public function xmlDataProvider()
{
$param = new Parameter([
'location' => 'xml',
'name' => 'Items',
'type' => 'array',
'items' => [
'type' => 'object',
'name' => 'Item',
'properties' => [
'Bar' => ['type' => 'string'],
'Baz' => ['type' => 'string'],
],
],
]);
return [
[$param, '<Test><Items><Item><Bar>1</Bar></Item><Item><Bar>2</Bar></Item></Items></Test>', [
'Items' => [
['Bar' => 1],
['Bar' => 2],
],
]],
[$param, '<Test><Items><Item><Bar>1</Bar></Item></Items></Test>', [
'Items' => [
['Bar' => 1],
]
]],
[$param, '<Test><Items /></Test>', [
'Items' => [],
]]
];
}
/**
* @dataProvider xmlDataProvider
* @group ResponseLocation
*/
public function testEnsuresWrappedArraysAreInCorrectLocations($param, $xml, $expected)
{
$location = new XmlLocation();
$model = new Parameter();
$response = new Response(200, [], \GuzzleHttp\Psr7\stream_for($xml));
$result = new Result();
$result = $location->before($result, $response, $param);
$result = $location->visit($result, $response, $param);
$result = $location->after($result, $response, $model);
$this->assertEquals($expected, $result->toArray());
}
/**
* @group ResponseLocation
*/
public function testCanRenameValues()
{
$param = new Parameter([
'name' => 'TerminatingInstances',
'type' => 'array',
'location' => 'xml',
'sentAs' => 'instancesSet',
'items' => [
'name' => 'item',
'type' => 'object',
'sentAs' => 'item',
'properties' => [
'InstanceId' => [
'type' => 'string',
'sentAs' => 'instanceId',
],
'CurrentState' => [
'type' => 'object',
'sentAs' => 'currentState',
'properties' => [
'Code' => [
'type' => 'numeric',
'sentAs' => 'code',
],
'Name' => [
'type' => 'string',
'sentAs' => 'name',
],
],
],
'PreviousState' => [
'type' => 'object',
'sentAs' => 'previousState',
'properties' => [
'Code' => [
'type' => 'numeric',
'sentAs' => 'code',
],
'Name' => [
'type' => 'string',
'sentAs' => 'name',
],
],
],
],
]
]);
$xml = '
<xml>
<instancesSet>
<item>
<instanceId>i-3ea74257</instanceId>
<currentState>
<code>32</code>
<name>shutting-down</name>
</currentState>
<previousState>
<code>16</code>
<name>running</name>
</previousState>
</item>
</instancesSet>
</xml>
';
$this->xmlTest($param, $xml, [
'TerminatingInstances' => [
[
'InstanceId' => 'i-3ea74257',
'CurrentState' => [
'Code' => '32',
'Name' => 'shutting-down',
],
'PreviousState' => [
'Code' => '16',
'Name' => 'running',
],
],
],
]);
}
/**
* @group ResponseLocation
*/
public function testCanRenameAttributes()
{
$param = new Parameter([
'name' => 'RunningQueues',
'type' => 'array',
'location' => 'xml',
'items' => [
'type' => 'object',
'sentAs' => 'item',
'properties' => [
'QueueId' => [
'type' => 'string',
'sentAs' => 'queue_id',
'data' => [
'xmlAttribute' => true,
],
],
'CurrentState' => [
'type' => 'object',
'properties' => [
'Code' => [
'type' => 'numeric',
'sentAs' => 'code',
'data' => [
'xmlAttribute' => true,
],
],
'Name' => [
'sentAs' => 'name',
'data' => [
'xmlAttribute' => true,
],
],
],
],
'PreviousState' => [
'type' => 'object',
'properties' => [
'Code' => [
'type' => 'numeric',
'sentAs' => 'code',
'data' => [
'xmlAttribute' => true,
],
],
'Name' => [
'sentAs' => 'name',
'data' => [
'xmlAttribute' => true,
],
],
],
],
],
]
]);
$xml = '
<wrap>
<RunningQueues>
<item queue_id="q-3ea74257">
<CurrentState code="32" name="processing" />
<PreviousState code="16" name="wait" />
</item>
</RunningQueues>
</wrap>';
$this->xmlTest($param, $xml, [
'RunningQueues' => [
[
'QueueId' => 'q-3ea74257',
'CurrentState' => [
'Code' => '32',
'Name' => 'processing',
],
'PreviousState' => [
'Code' => '16',
'Name' => 'wait',
],
],
],
]);
}
/**
* @group ResponseLocation
*/
public function testAddsEmptyArraysWhenValueIsMissing()
{
$param = new Parameter([
'name' => 'Foo',
'type' => 'array',
'location' => 'xml',
'items' => [
'type' => 'object',
'properties' => [
'Baz' => ['type' => 'array'],
'Bar' => [
'type' => 'object',
'properties' => [
'Baz' => ['type' => 'array'],
],
],
],
],
]);
$xml = '<xml><Foo><Bar></Bar></Foo></xml>';
$this->xmlTest($param, $xml, [
'Foo' => [
[
'Bar' => [],
]
],
]);
}
/**
* @group issue-399, ResponseLocation
* @link https://github.com/guzzle/guzzle/issues/399
*/
public function testDiscardingUnknownProperties()
{
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'additionalProperties' => false,
'properties' => [
'bar' => [
'type' => 'string',
'name' => 'bar',
],
],
]);
$xml = '
<xml>
<foo>
<bar>15</bar>
<unknown>discard me</unknown>
</foo>
</xml>
';
$this->xmlTest($param, $xml, [
'foo' => [
'bar' => 15
]
]);
}
/**
* @group issue-399, ResponseLocation
* @link https://github.com/guzzle/guzzle/issues/399
*/
public function testDiscardingUnknownPropertiesWithAliasing()
{
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'additionalProperties' => false,
'properties' => [
'bar' => [
'name' => 'bar',
'sentAs' => 'baz',
],
],
]);
$xml = '
<xml>
<foo>
<baz>15</baz>
<unknown>discard me</unknown>
</foo>
</xml>
';
$this->xmlTest($param, $xml, [
'foo' => [
'bar' => 15,
],
]);
}
/**
* @group ResponseLocation
*/
public function testProcessingOfNestedAdditionalProperties()
{
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'additionalProperties' => true,
'properties' => [
'bar' => [
'name' => 'bar',
'sentAs' => 'baz',
],
'nestedNoAdditional' => [
'type' => 'object',
'additionalProperties' => false,
'properties' => [
'id' => [
'type' => 'integer',
],
],
],
'nestedWithAdditional' => [
'type' => 'object',
'additionalProperties' => true,
],
'nestedWithAdditionalSchema' => [
'type' => 'object',
'additionalProperties' => [
'type' => 'array',
'items' => [
'type' => 'string',
],
],
],
],
]);
$xml = '
<xml>
<foo>
<baz>15</baz>
<additional>include me</additional>
<nestedNoAdditional>
<id>15</id>
<unknown>discard me</unknown>
</nestedNoAdditional>
<nestedWithAdditional>
<id>15</id>
<additional>include me</additional>
</nestedWithAdditional>
<nestedWithAdditionalSchema>
<arrayA>
<item>1</item>
<item>2</item>
<item>3</item>
</arrayA>
<arrayB>
<item>A</item>
<item>B</item>
<item>C</item>
</arrayB>
</nestedWithAdditionalSchema>
</foo>
</xml>
';
$this->xmlTest($param, $xml, [
'foo' => [
'bar' => '15',
'additional' => 'include me',
'nestedNoAdditional' => [
'id' => '15',
],
'nestedWithAdditional' => [
'id' => '15',
'additional' => 'include me',
],
'nestedWithAdditionalSchema' => [
'arrayA' => ['1', '2', '3'],
'arrayB' => ['A', 'B', 'C'],
],
],
]);
}
/**
* @group ResponseLocation
*/
public function testConvertsMultipleAssociativeElementsToArray()
{
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'additionalProperties' => true,
]);
$xml = '
<xml>
<foo>
<baz>15</baz>
<baz>25</baz>
<bar>hi</bar>
<bam>test</bam>
<bam attr="hi" />
</foo>
</xml>
';
$this->xmlTest($param, $xml, [
'foo' => [
'baz' => ['15', '25'],
'bar' => 'hi',
'bam' => [
'test',
['@attributes' => ['attr' => 'hi']]
]
]
]);
}
/**
* @group ResponseLocation
*/
public function testUnderstandsNamespaces()
{
$param = new Parameter([
'name' => 'nstest',
'type' => 'array',
'location' => 'xml',
'items' => [
'name' => 'item',
'type' => 'object',
'sentAs' => 'item',
'properties' => [
'id' => [
'type' => 'string',
],
'isbn:number' => [
'type' => 'string',
],
'meta' => [
'type' => 'object',
'sentAs' => 'abstract:meta',
'properties' => [
'foo' => [
'type' => 'numeric',
],
'bar' => [
'type' => 'object',
'properties' =>[
'attribute' => [
'type' => 'string',
'data' => [
'xmlAttribute' => true,
'xmlNs' => 'abstract',
],
],
],
],
],
],
'gamma' => [
'type' => 'object',
'data' => [
'xmlNs' => 'abstract',
],
'additionalProperties' => true,
],
'nonExistent' => [
'type' => 'object',
'data' => [
'xmlNs' => 'abstract',
],
'additionalProperties' => true,
],
'nonExistent2' => [
'type' => 'object',
'additionalProperties' => true,
],
],
],
]);
$xml = '
<xml>
<nstest xmlns:isbn="urn:ISBN:0-395-36341-6" xmlns:abstract="urn:my.org:abstract">
<item>
<id>101</id>
<isbn:number>1568491379</isbn:number>
<abstract:meta>
<foo>10</foo>
<bar abstract:attribute="foo"></bar>
</abstract:meta>
<abstract:gamma>
<foo>bar</foo>
</abstract:gamma>
</item>
<item>
<id>102</id>
<isbn:number>1568491999</isbn:number>
<abstract:meta>
<foo>20</foo>
<bar abstract:attribute="bar"></bar>
</abstract:meta>
<abstract:gamma>
<foo>baz</foo>
</abstract:gamma>
</item>
</nstest>
</xml>
';
$this->xmlTest($param, $xml, [
'nstest' => [
[
'id' => '101',
'isbn:number' => 1568491379,
'meta' => [
'foo' => 10,
'bar' => [
'attribute' => 'foo',
],
],
'gamma' => [
'foo' => 'bar',
],
],
[
'id' => '102',
'isbn:number' => 1568491999,
'meta' => [
'foo' => 20,
'bar' => [
'attribute' => 'bar'
],
],
'gamma' => [
'foo' => 'baz',
],
],
],
]);
}
/**
* @group ResponseLocation
*/
public function testCanWalkUndefinedPropertiesWithNamespace()
{
$param = new Parameter([
'name' => 'nstest',
'type' => 'array',
'location' => 'xml',
'items' => [
'name' => 'item',
'type' => 'object',
'sentAs' => 'item',
'additionalProperties' => [
'type' => 'object',
'data' => [
'xmlNs' => 'abstract'
],
],
'properties' => [
'id' => [
'type' => 'string',
],
'isbn:number' => [
'type' => 'string',
],
],
],
]);
$xml = '
<xml>
<nstest xmlns:isbn="urn:ISBN:0-395-36341-6" xmlns:abstract="urn:my.org:abstract">
<item>
<id>101</id>
<isbn:number>1568491379</isbn:number>
<abstract:meta>
<foo>10</foo>
<bar>baz</bar>
</abstract:meta>
</item>
<item>
<id>102</id>
<isbn:number>1568491999</isbn:number>
<abstract:meta>
<foo>20</foo>
<bar>foo</bar>
</abstract:meta>
</item>
</nstest>
</xml>
';
$this->xmlTest($param, $xml, [
'nstest' => [
[
'id' => '101',
'isbn:number' => 1568491379,
'meta' => [
'foo' => 10,
'bar' => 'baz',
],
],
[
'id' => '102',
'isbn:number' => 1568491999,
'meta' => [
'foo' => 20,
'bar' => 'foo',
],
],
]
]);
}
/**
* @group ResponseLocation
*/
public function testCanWalkSimpleArrayWithNamespace()
{
$param = new Parameter([
'name' => 'nstest',
'type' => 'array',
'location' => 'xml',
'items' => [
'type' => 'string',
'sentAs' => 'number',
'data' => [
'xmlNs' => 'isbn'
],
],
]);
$xml = '
<xml>
<nstest xmlns:isbn="urn:ISBN:0-395-36341-6">
<isbn:number>1568491379</isbn:number>
<isbn:number>1568491999</isbn:number>
<isbn:number>1568492999</isbn:number>
</nstest>
</xml>
';
$this->xmlTest($param, $xml, [
'nstest' => [
1568491379,
1568491999,
1568492999,
],
]);
}
/**
* @group ResponseLocation
*/
public function testCanWalkSimpleArrayWithNamespace2()
{
$param = new Parameter([
'name' => 'nstest',
'type' => 'array',
'location' => 'xml',
'items' => [
'type' => 'string',
'sentAs' => 'isbn:number',
]
]);
$xml = '
<xml>
<nstest xmlns:isbn="urn:ISBN:0-395-36341-6">
<isbn:number>1568491379</isbn:number>
<isbn:number>1568491999</isbn:number>
<isbn:number>1568492999</isbn:number>
</nstest>
</xml>
';
$this->xmlTest($param, $xml, [
'nstest' => [
1568491379,
1568491999,
1568492999,
],
]);
}
private function xmlTest(Parameter $param, $xml, $expected)
{
$location = new XmlLocation();
$model = new Parameter();
$response = new Response(200, [], \GuzzleHttp\Psr7\stream_for($xml));
$result = new Result();
$result = $location->before($result, $response, $param);
$result = $location->visit($result, $response, $param);
$result = $location->after($result, $response, $model);
$this->assertEquals($expected, $result->toArray());
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle;
use GuzzleHttp\Command\Guzzle\SchemaFormatter;
/**
* @covers \GuzzleHttp\Command\Guzzle\SchemaFormatter
*/
class SchemaFormatterTest extends \PHPUnit_Framework_TestCase
{
public function dateTimeProvider()
{
$dateUtc = 'October 13, 2012 16:15:46 UTC';
$dateOffset = 'October 13, 2012 10:15:46 -06:00';
$expectedDateTime = '2012-10-13T16:15:46Z';
return [
['foo', 'does-not-exist', 'foo'],
[$dateUtc, 'date-time', $expectedDateTime],
[$dateUtc, 'date-time-http', 'Sat, 13 Oct 2012 16:15:46 GMT'],
[$dateUtc, 'date', '2012-10-13'],
[$dateUtc, 'timestamp', strtotime($dateUtc)],
[new \DateTime($dateUtc), 'timestamp', strtotime($dateUtc)],
[$dateUtc, 'time', '16:15:46'],
[strtotime($dateUtc), 'time', '16:15:46'],
[strtotime($dateUtc), 'timestamp', strtotime($dateUtc)],
['true', 'boolean-string', 'true'],
[true, 'boolean-string', 'true'],
['false', 'boolean-string', 'false'],
[false, 'boolean-string', 'false'],
['1350144946', 'date-time', $expectedDateTime],
[1350144946, 'date-time', $expectedDateTime],
[$dateOffset, 'date-time', $expectedDateTime],
];
}
/**
* @dataProvider dateTimeProvider
*/
public function testFilters($value, $format, $result)
{
$this->assertEquals($result, (new SchemaFormatter)->format($format, $value));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesDateTimeInput()
{
(new SchemaFormatter)->format('date-time', false);
}
public function testEnsuresTimestampsAreIntegers()
{
$t = time();
$result = (new SchemaFormatter)->format('timestamp', $t);
$this->assertSame($t, $result);
$this->assertInternalType('int', $result);
}
}

View File

@@ -0,0 +1,330 @@
<?php
namespace Guzzle\Tests\Service\Description;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\SchemaValidator;
use GuzzleHttp\Command\ToArrayInterface;
/**
* @covers \GuzzleHttp\Command\Guzzle\SchemaValidator
*/
class SchemaValidatorTest extends \PHPUnit_Framework_TestCase
{
/** @var SchemaValidator */
protected $validator;
public function setUp()
{
$this->validator = new SchemaValidator();
}
public function testValidatesArrayListsAreNumericallyIndexed()
{
$value = [[1]];
$this->assertFalse($this->validator->validate($this->getComplexParam(), $value));
$this->assertEquals(
['[Foo][0] must be an array of properties. Got a numerically indexed array.'],
$this->validator->getErrors()
);
}
public function testValidatesArrayListsContainProperItems()
{
$value = [true];
$this->assertFalse($this->validator->validate($this->getComplexParam(), $value));
$this->assertEquals(
['[Foo][0] must be of type object'],
$this->validator->getErrors()
);
}
public function testAddsDefaultValuesInLists()
{
$value = [[]];
$this->assertTrue($this->validator->validate($this->getComplexParam(), $value));
$this->assertEquals([['Bar' => true]], $value);
}
public function testMergesDefaultValuesInLists()
{
$value = [
['Baz' => 'hello!'],
['Bar' => false],
];
$this->assertTrue($this->validator->validate($this->getComplexParam(), $value));
$this->assertEquals([
[
'Baz' => 'hello!',
'Bar' => true,
],
['Bar' => false],
], $value);
}
public function testCorrectlyConvertsParametersToArrayWhenArraysArePresent()
{
$param = $this->getComplexParam();
$result = $param->toArray();
$this->assertInternalType('array', $result['items']);
$this->assertEquals('array', $result['type']);
$this->assertInstanceOf('GuzzleHttp\Command\Guzzle\Parameter', $param->getItems());
}
public function testEnforcesInstanceOfOnlyWhenObject()
{
$p = new Parameter([
'name' => 'foo',
'type' => ['object', 'string'],
'instanceOf' => get_class($this)
]);
$this->assertTrue($this->validator->validate($p, $this));
$s = 'test';
$this->assertTrue($this->validator->validate($p, $s));
}
public function testConvertsObjectsToArraysWhenToArrayInterface()
{
$o = $this->getMockBuilder(ToArrayInterface::class)
->setMethods(['toArray'])
->getMockForAbstractClass();
$o->expects($this->once())
->method('toArray')
->will($this->returnValue(['foo' => 'bar']));
$p = new Parameter([
'name' => 'test',
'type' => 'object',
'properties' => [
'foo' => ['required' => 'true'],
],
]);
$this->assertTrue($this->validator->validate($p, $o));
}
public function testMergesValidationErrorsInPropertiesWithParent()
{
$p = new Parameter([
'name' => 'foo',
'type' => 'object',
'properties' => [
'bar' => ['type' => 'string', 'required' => true, 'description' => 'This is what it does'],
'test' => ['type' => 'string', 'minLength' => 2, 'maxLength' => 5],
'test2' => ['type' => 'string', 'minLength' => 2, 'maxLength' => 2],
'test3' => ['type' => 'integer', 'minimum' => 100],
'test4' => ['type' => 'integer', 'maximum' => 10],
'test5' => ['type' => 'array', 'maxItems' => 2],
'test6' => ['type' => 'string', 'enum' => ['a', 'bc']],
'test7' => ['type' => 'string', 'pattern' => '/[0-9]+/'],
'test8' => ['type' => 'number'],
'baz' => [
'type' => 'array',
'minItems' => 2,
'required' => true,
"items" => ["type" => "string"],
],
],
]);
$value = [
'test' => 'a',
'test2' => 'abc',
'baz' => [false],
'test3' => 10,
'test4' => 100,
'test5' => [1, 3, 4],
'test6' => 'Foo',
'test7' => 'abc',
'test8' => 'abc',
];
$this->assertFalse($this->validator->validate($p, $value));
$this->assertEquals([
'[foo][bar] is a required string: This is what it does',
'[foo][baz] must contain 2 or more elements',
'[foo][baz][0] must be of type string',
'[foo][test2] length must be less than or equal to 2',
'[foo][test3] must be greater than or equal to 100',
'[foo][test4] must be less than or equal to 10',
'[foo][test5] must contain 2 or fewer elements',
'[foo][test6] must be one of "a" or "bc"',
'[foo][test7] must match the following regular expression: /[0-9]+/',
'[foo][test8] must be of type number',
'[foo][test] length must be greater than or equal to 2',
], $this->validator->getErrors());
}
public function testHandlesNullValuesInArraysWithDefaults()
{
$p = new Parameter([
'name' => 'foo',
'type' => 'object',
'required' => true,
'properties' => [
'bar' => [
'type' => 'object',
'required' => true,
'properties' => [
'foo' => ['default' => 'hi'],
],
],
],
]);
$value = [];
$this->assertTrue($this->validator->validate($p, $value));
$this->assertEquals(['bar' => ['foo' => 'hi']], $value);
}
public function testFailsWhenNullValuesInArraysWithNoDefaults()
{
$p = new Parameter([
'name' => 'foo',
'type' => 'object',
'required' => true,
'properties' => [
'bar' => [
'type' => 'object',
'required' => true,
'properties' => [
'foo' => ['type' => 'string'],
],
],
],
]);
$value = [];
$this->assertFalse($this->validator->validate($p, $value));
$this->assertEquals(['[foo][bar] is a required object'], $this->validator->getErrors());
}
public function testChecksTypes()
{
$p = new SchemaValidator();
$r = new \ReflectionMethod($p, 'determineType');
$r->setAccessible(true);
$this->assertEquals('any', $r->invoke($p, 'any', 'hello'));
$this->assertEquals(false, $r->invoke($p, 'foo', 'foo'));
$this->assertEquals('string', $r->invoke($p, 'string', 'hello'));
$this->assertEquals(false, $r->invoke($p, 'string', false));
$this->assertEquals('integer', $r->invoke($p, 'integer', 1));
$this->assertEquals(false, $r->invoke($p, 'integer', 'abc'));
$this->assertEquals('numeric', $r->invoke($p, 'numeric', 1));
$this->assertEquals('numeric', $r->invoke($p, 'numeric', '1'));
$this->assertEquals('number', $r->invoke($p, 'number', 1));
$this->assertEquals('number', $r->invoke($p, 'number', '1'));
$this->assertEquals(false, $r->invoke($p, 'numeric', 'a'));
$this->assertEquals('boolean', $r->invoke($p, 'boolean', true));
$this->assertEquals('boolean', $r->invoke($p, 'boolean', false));
$this->assertEquals(false, $r->invoke($p, 'boolean', 'false'));
$this->assertEquals('null', $r->invoke($p, 'null', null));
$this->assertEquals(false, $r->invoke($p, 'null', 'abc'));
$this->assertEquals('array', $r->invoke($p, 'array', []));
$this->assertEquals(false, $r->invoke($p, 'array', 'foo'));
}
public function testValidatesFalseAdditionalProperties()
{
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'properties' => [
'bar' => ['type' => 'string'],
],
'additionalProperties' => false,
]);
$value = ['test' => '123'];
$this->assertFalse($this->validator->validate($param, $value));
$this->assertEquals(['[foo][test] is not an allowed property'], $this->validator->getErrors());
$value = ['bar' => '123'];
$this->assertTrue($this->validator->validate($param, $value));
}
public function testAllowsUndefinedAdditionalProperties()
{
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'properties' => [
'bar' => ['type' => 'string'],
]
]);
$value = ['test' => '123'];
$this->assertTrue($this->validator->validate($param, $value));
}
public function testValidatesAdditionalProperties()
{
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'properties' => [
'bar' => ['type' => 'string'],
],
'additionalProperties' => ['type' => 'integer'],
]);
$value = ['test' => 'foo'];
$this->assertFalse($this->validator->validate($param, $value));
$this->assertEquals(['[foo][test] must be of type integer'], $this->validator->getErrors());
}
public function testValidatesAdditionalPropertiesThatArrayArrays()
{
$param = new Parameter([
'name' => 'foo',
'type' => 'object',
'additionalProperties' => [
'type' => 'array',
'items' => ['type' => 'string'],
],
]);
$value = ['test' => [true]];
$this->assertFalse($this->validator->validate($param, $value));
$this->assertEquals(['[foo][test][0] must be of type string'], $this->validator->getErrors());
}
public function testIntegersCastToStringWhenTypeMismatch()
{
$param = new Parameter([
'name' => 'test',
'type' => 'string',
]);
$value = 12;
$this->assertTrue($this->validator->validate($param, $value));
$this->assertEquals('12', $value);
}
public function testRequiredMessageIncludesType()
{
$param = new Parameter([
'name' => 'test',
'type' => [
'string',
'boolean',
],
'required' => true,
]);
$value = null;
$this->assertFalse($this->validator->validate($param, $value));
$this->assertEquals(['[test] is a required string or boolean'], $this->validator->getErrors());
}
protected function getComplexParam()
{
return new Parameter([
'name' => 'Foo',
'type' => 'array',
'required' => true,
'min' => 1,
'items' => [
'type' => 'object',
'properties' => [
'Baz' => [
'type' => 'string',
],
'Bar' => [
'required' => true,
'type' => 'boolean',
'default' => true,
],
],
],
]);
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace GuzzleHttp\Tests\Command\Guzzle;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\Serializer;
use GuzzleHttp\Psr7\Request;
/**
* @covers \GuzzleHttp\Command\Guzzle\Serializer
*/
class SerializerTest extends \PHPUnit_Framework_TestCase
{
public function testAllowsUriTemplates()
{
$description = new Description([
'baseUri' => 'http://test.com',
'operations' => [
'test' => [
'httpMethod' => 'GET',
'uri' => '/api/{key}/foo',
'parameters' => [
'key' => [
'required' => true,
'type' => 'string',
'location' => 'uri'
],
]
]
]
]);
$command = new Command('test', ['key' => 'bar']);
$serializer = new Serializer($description);
/** @var Request $request */
$request = $serializer($command);
$this->assertEquals('http://test.com/api/bar/foo', $request->getUri());
}
}