Skip to content

Instantly share code, notes, and snippets.

@ukautz
Created June 18, 2014 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukautz/04ea325ef40f6b6c97a6 to your computer and use it in GitHub Desktop.
Save ukautz/04ea325ef40f6b6c97a6 to your computer and use it in GitHub Desktop.
------- Foo ---------
Expected: {
"under_me": [
{
"foo": "BAR"
},
{
"baz": "BAM"
}
]
}
Got: {
"nested": [
{
"foo": "BAR"
},
{
"baz": "BAM"
}
]
}
------- Bar ---------
Expected: {
"not_me": "not the array",
"under_me": [
{
"foo": "BAR"
},
{
"baz": "BAM"
}
]
}
Got: {
"not_me": "not the array",
"under_me": [
"not the array",
[]
]
}
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
include __DIR__ . '/../vendor/autoload.php';
$description = new Description([
'baseUrl' => 'http://localhost/',
'operations' => [
'getFoo' => [
'httpMethod' => 'GET',
'uri' => '/foo',
'responseModel' => 'a'
],
'getBar' => [
'httpMethod' => 'GET',
'uri' => '/bar',
'responseModel' => 'b'
],
],
'models' => [
'a' => [
'type' => 'array',
'location' => 'json',
'name' => 'nested',
'items' => [
'type' => 'object',
'additionalProperties' => [
'type' => 'string',
'filters' => ['strtoupper']
]
]
],
'b' => [
'type' => 'object',
'properties' => [
'not_me' => [
'type' => 'string',
'location' => 'json'
],
'under_me' => [
'location' => 'json',
'type' => 'array',
'items' => [
'type' => 'object',
'additionalProperties' => [
'type' => 'string',
'filters' => ['strtoupper']
]
]
]
]
],
]
]);
$expectations = [
[
'under_me' => [
[
'foo' => 'BAR'
],
[
'baz' => 'BAM'
]
]
],
[
'not_me' => 'not the array',
'under_me' => [
[
'foo' => 'BAR'
],
[
'baz' => 'BAM'
]
]
]
];
$responses = [
[
[
'foo' => 'bar'
],
[
'baz' => 'bam'
]
],
[
'not_me' => 'not the array',
'under_me' => [
[
'foo' => 'bar'
],
[
'baz' => 'bam'
]
]
]
];
$client = new Client();
$client->getEmitter()->on('before', function (BeforeEvent $event) use (&$responses) {
$data = array_shift($responses);
$response = new Response(200, [], Stream::factory(json_encode($data, JSON_PRETTY_PRINT)));
$event->intercept($response);
});
$guzzleClient = new GuzzleClient($client, $description);
$foo = $guzzleClient->getFoo();
$bar = $guzzleClient->getBar();
echo "------- Foo ---------\n";
echo "Expected: " . json_encode($expectations[0], JSON_PRETTY_PRINT) . "\n";
echo "Got: " . json_encode($foo->toArray(), JSON_PRETTY_PRINT) . "\n";
echo "\n\n------- Bar ---------\n";
echo "Expected: " . json_encode($expectations[1], JSON_PRETTY_PRINT) . "\n";
echo "Got: " . json_encode($bar->toArray(), JSON_PRETTY_PRINT) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment