添加网站文件

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,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());
}
}