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: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309:
<?php
namespace Guzzle\Tests\Service\Description;
use Guzzle\Service\Description\Operation;
use Guzzle\Service\Description\Parameter;
use Guzzle\Service\Description\ServiceDescription;
class OperationTest extends \Guzzle\Tests\GuzzleTestCase
{
public static function strtoupper($string)
{
return strtoupper($string);
}
public function testOperationIsDataObject()
{
$c = new Operation(array(
'name' => 'test',
'summary' => 'doc',
'notes' => 'notes',
'documentationUrl' => 'http://www.example.com',
'httpMethod' => 'POST',
'uri' => '/api/v1',
'responseClass' => 'array',
'responseNotes' => 'returns the json_decoded response',
'deprecated' => true,
'parameters' => array(
'key' => array(
'required' => true,
'type' => 'string',
'maxLength' => 10
),
'key_2' => array(
'required' => true,
'type' => 'integer',
'default' => 10
)
)
));
$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('array', $c->getResponseClass());
$this->assertEquals('returns the json_decoded response', $c->getResponseNotes());
$this->assertTrue($c->getDeprecated());
$this->assertEquals('Guzzle\\Service\\Command\\OperationCommand', $c->getClass());
$this->assertEquals(array(
'key' => new Parameter(array(
'name' => 'key',
'required' => true,
'type' => 'string',
'maxLength' => 10,
'parent' => $c
)),
'key_2' => new Parameter(array(
'name' => 'key_2',
'required' => true,
'type' => 'integer',
'default' => 10,
'parent' => $c
))
), $c->getParams());
$this->assertEquals(new Parameter(array(
'name' => 'key_2',
'required' => true,
'type' => 'integer',
'default' => 10,
'parent' => $c
)), $c->getParam('key_2'));
$this->assertNull($c->getParam('afefwef'));
$this->assertArrayNotHasKey('parent', $c->getParam('key_2')->toArray());
}
public function testAllowsConcreteCommands()
{
$c = new Operation(array(
'name' => 'test',
'class' => 'Guzzle\\Service\\Command\ClosureCommand',
'parameters' => array(
'p' => new Parameter(array(
'name' => 'foo'
))
)
));
$this->assertEquals('Guzzle\\Service\\Command\ClosureCommand', $c->getClass());
}
public function testConvertsToArray()
{
$data = array(
'name' => 'test',
'class' => 'Guzzle\\Service\\Command\ClosureCommand',
'summary' => 'test',
'documentationUrl' => 'http://www.example.com',
'httpMethod' => 'PUT',
'uri' => '/',
'parameters' => array('p' => array('name' => 'foo'))
);
$c = new Operation($data);
$toArray = $c->toArray();
unset($data['name']);
$this->assertArrayHasKey('parameters', $toArray);
$this->assertInternalType('array', $toArray['parameters']);
unset($data['parameters']);
unset($toArray['parameters']);
$data['responseType'] = 'primitive';
$data['responseClass'] = 'array';
$this->assertEquals($data, $toArray);
}
public function testDeterminesIfHasParam()
{
$command = $this->getTestCommand();
$this->assertTrue($command->hasParam('data'));
$this->assertFalse($command->hasParam('baz'));
}
public function testReturnsParamNames()
{
$command = $this->getTestCommand();
$this->assertEquals(array('data'), $command->getParamNames());
}
protected function getTestCommand()
{
return new Operation(array(
'parameters' => array(
'data' => new Parameter(array(
'type' => 'string'
))
)
));
}
public function testCanBuildUpCommands()
{
$c = new Operation(array());
$c->setName('foo')
->setClass('Baz')
->setDeprecated(false)
->setSummary('summary')
->setDocumentationUrl('http://www.foo.com')
->setHttpMethod('PUT')
->setResponseNotes('oh')
->setResponseClass('string')
->setUri('/foo/bar')
->addParam(new Parameter(array(
'name' => 'test'
)));
$this->assertEquals('foo', $c->getName());
$this->assertEquals('Baz', $c->getClass());
$this->assertEquals(false, $c->getDeprecated());
$this->assertEquals('summary', $c->getSummary());
$this->assertEquals('http://www.foo.com', $c->getDocumentationUrl());
$this->assertEquals('PUT', $c->getHttpMethod());
$this->assertEquals('oh', $c->getResponseNotes());
$this->assertEquals('string', $c->getResponseClass());
$this->assertEquals('/foo/bar', $c->getUri());
$this->assertEquals(array('test'), $c->getParamNames());
}
public function testCanRemoveParams()
{
$c = new Operation(array());
$c->addParam(new Parameter(array('name' => 'foo')));
$this->assertTrue($c->hasParam('foo'));
$c->removeParam('foo');
$this->assertFalse($c->hasParam('foo'));
}
public function testAddsNameToParametersIfNeeded()
{
$command = new Operation(array('parameters' => array('foo' => new Parameter(array()))));
$this->assertEquals('foo', $command->getParam('foo')->getName());
}
public function testContainsApiErrorInformation()
{
$command = $this->getOperation();
$this->assertEquals(1, count($command->getErrorResponses()));
$arr = $command->toArray();
$this->assertEquals(1, count($arr['errorResponses']));
$command->addErrorResponse(400, 'Foo', 'Baz\\Bar');
$this->assertEquals(2, count($command->getErrorResponses()));
$command->setErrorResponses(array());
$this->assertEquals(0, count($command->getErrorResponses()));
}
public function testHasNotes()
{
$o = new Operation(array('notes' => 'foo'));
$this->assertEquals('foo', $o->getNotes());
$o->setNotes('bar');
$this->assertEquals('bar', $o->getNotes());
}
public function testHasData()
{
$o = new Operation(array('data' => array('foo' => 'baz', 'bar' => 123)));
$o->setData('test', false);
$this->assertEquals('baz', $o->getData('foo'));
$this->assertEquals(123, $o->getData('bar'));
$this->assertNull($o->getData('wfefwe'));
$this->assertEquals(array(
'parameters' => array(),
'class' => 'Guzzle\Service\Command\OperationCommand',
'data' => array('foo' => 'baz', 'bar' => 123, 'test' => false),
'responseClass' => 'array',
'responseType' => 'primitive'
), $o->toArray());
}
public function testHasServiceDescription()
{
$s = new ServiceDescription();
$o = new Operation(array(), $s);
$this->assertSame($s, $o->getServiceDescription());
}
public function testValidatesResponseType()
{
$o = new Operation(array('responseClass' => 'array', 'responseType' => 'foo'));
}
public function testInfersResponseType()
{
$o = $this->getOperation();
$o->setServiceDescription(new ServiceDescription(array('models' => array('Foo' => array()))));
$this->assertEquals('primitive', $o->getResponseType());
$this->assertEquals('primitive', $o->setResponseClass('boolean')->getResponseType());
$this->assertEquals('primitive', $o->setResponseClass('array')->getResponseType());
$this->assertEquals('primitive', $o->setResponseClass('integer')->getResponseType());
$this->assertEquals('primitive', $o->setResponseClass('string')->getResponseType());
$this->assertEquals('class', $o->setResponseClass('foo')->getResponseType());
$this->assertEquals('class', $o->setResponseClass(__CLASS__)->getResponseType());
$this->assertEquals('model', $o->setResponseClass('Foo')->getResponseType());
}
public function testHasResponseType()
{
$o = new Operation(array('responseClass' => 'array'));
$this->assertEquals('primitive', $o->getResponseType());
$o = new Operation();
$this->assertEquals('primitive', $o->getResponseType());
$this->assertEquals('model', $o->setResponseType('model')->getResponseType());
}
public function testHasAdditionalParameters()
{
$o = new Operation(array(
'additionalParameters' => array(
'type' => 'string', 'name' => 'binks'
),
'parameters' => array(
'foo' => array('type' => 'integer')
)
));
$this->assertEquals('string', $o->getAdditionalParameters()->getType());
$arr = $o->toArray();
$this->assertEquals(array(
'type' => 'string'
), $arr['additionalParameters']);
}
protected function getOperation()
{
return new Operation(array(
'name' => 'OperationTest',
'class' => get_class($this),
'parameters' => array(
'test' => array('type' => 'object'),
'bool_1' => array('default' => true, 'type' => 'boolean'),
'bool_2' => array('default' => false),
'float' => array('type' => 'numeric'),
'int' => array('type' => 'integer'),
'date' => array('type' => 'string'),
'timestamp' => array('type' => 'string'),
'string' => array('type' => 'string'),
'username' => array('type' => 'string', 'required' => true, 'filters' => 'strtolower'),
'test_function' => array('type' => 'string', 'filters' => __CLASS__ . '::strtoupper')
),
'errorResponses' => array(
array('code' => 503, 'reason' => 'InsufficientCapacity', 'class' => 'Guzzle\\Exception\\RuntimeException')
)
));
}
}