1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158:
<?php
namespace Guzzle\Tests\Service\Command\LocationVisitor\Response;
use Guzzle\Service\Description\Parameter;
use Guzzle\Http\Message\Response;
use Guzzle\Service\Command\LocationVisitor\Response\JsonVisitor as Visitor;
class JsonVisitorTest extends AbstractResponseVisitorTest
{
public function testBeforeMethodParsesXml()
{
$visitor = new Visitor();
$command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')
->setMethods(array('getResponse'))
->getMockForAbstractClass();
$command->expects($this->once())
->method('getResponse')
->will($this->returnValue(new Response(200, null, '{"foo":"bar"}')));
$result = array();
$visitor->before($command, $result);
$this->assertEquals(array('foo' => 'bar'), $result);
}
public function testVisitsLocation()
{
$visitor = new Visitor();
$param = new Parameter(array(
'name' => 'foo',
'type' => 'array',
'items' => array(
'filters' => 'strtoupper',
'type' => 'string'
)
));
$this->value = array('foo' => array('a', 'b', 'c'));
$visitor->visit($this->command, $this->response, $param, $this->value);
$this->assertEquals(array('A', 'B', 'C'), $this->value['foo']);
}
public function testRenamesTopLevelValues()
{
$visitor = new Visitor();
$param = new Parameter(array(
'name' => 'foo',
'sentAs' => 'Baz',
'type' => 'string',
));
$this->value = array('Baz' => 'test');
$visitor->visit($this->command, $this->response, $param, $this->value);
$this->assertEquals(array('foo' => 'test'), $this->value);
}
public function testRenamesDoesNotFailForNonExistentKey()
{
$visitor = new Visitor();
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'properties' => array(
'bar' => array(
'name' => 'bar',
'sentAs' => 'baz',
),
),
));
$this->value = array('foo' => array('unknown' => 'Unknown'));
$visitor->visit($this->command, $this->response, $param, $this->value);
$this->assertEquals(array('foo' => array('unknown' => 'Unknown')), $this->value);
}
public function testTraversesObjectsAndAppliesFilters()
{
$visitor = new Visitor();
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'properties' => array(
'foo' => array('filters' => 'strtoupper'),
'bar' => array('filters' => 'strtolower')
)
));
$this->value = array('foo' => array('foo' => 'hello', 'bar' => 'THERE'));
$visitor->visit($this->command, $this->response, $param, $this->value);
$this->assertEquals(array('foo' => 'HELLO', 'bar' => 'there'), $this->value['foo']);
}
public function testDiscardingUnknownProperties()
{
$visitor = new Visitor();
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'additionalProperties' => false,
'properties' => array(
'bar' => array(
'type' => 'string',
'name' => 'bar',
),
),
));
$this->value = array('foo' => array('bar' => 15, 'unknown' => 'Unknown'));
$visitor->visit($this->command, $this->response, $param, $this->value);
$this->assertEquals(array('foo' => array('bar' => 15)), $this->value);
}
public function testDiscardingUnknownPropertiesWithAliasing()
{
$visitor = new Visitor();
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'additionalProperties' => false,
'properties' => array(
'bar' => array(
'name' => 'bar',
'sentAs' => 'baz',
),
),
));
$this->value = array('foo' => array('baz' => 15, 'unknown' => 'Unknown'));
$visitor->visit($this->command, $this->response, $param, $this->value);
$this->assertEquals(array('foo' => array('bar' => 15)), $this->value);
}
public function testWalksAdditionalProperties()
{
$visitor = new Visitor();
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'additionalProperties' => array(
'type' => 'object',
'properties' => array(
'bar' => array(
'type' => 'string',
'filters' => array('base64_decode')
)
),
),
));
$this->value = array('foo' => array('baz' => array('bar' => 'Zm9v')));
$visitor->visit($this->command, $this->response, $param, $this->value);
$this->assertEquals('foo', $this->value['foo']['baz']['bar']);
}
}