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:
<?php
namespace Guzzle\Tests\Service\Description;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Service\Description\Operation;
use Guzzle\Service\Description\Parameter;
use Guzzle\Service\Client;
class ServiceDescriptionTest extends \Guzzle\Tests\GuzzleTestCase
{
protected $serviceData;
public function setup()
{
$this->serviceData = array(
'test_command' => new Operation(array(
'name' => 'test_command',
'description' => 'documentationForCommand',
'httpMethod' => 'DELETE',
'class' => 'Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand',
'parameters' => array(
'bucket' => array('required' => true),
'key' => array('required' => true)
)
))
);
}
public function testFactoryDelegatesToConcreteFactories()
{
$jsonFile = __DIR__ . '/../../TestData/test_service.json';
$this->assertInstanceOf('Guzzle\Service\Description\ServiceDescription', ServiceDescription::factory($jsonFile));
}
public function testConstructor()
{
$service = new ServiceDescription(array('operations' => $this->serviceData));
$this->assertEquals(1, count($service->getOperations()));
$this->assertFalse($service->hasOperation('foobar'));
$this->assertTrue($service->hasOperation('test_command'));
}
public function testIsSerializable()
{
$service = new ServiceDescription(array('operations' => $this->serviceData));
$data = serialize($service);
$d2 = unserialize($data);
$this->assertEquals(serialize($service), serialize($d2));
}
public function testSerializesParameters()
{
$service = new ServiceDescription(array(
'operations' => array(
'foo' => new Operation(array('parameters' => array('foo' => array('type' => 'string'))))
)
));
$serialized = serialize($service);
$this->assertContains('"parameters":{"foo":', $serialized);
$service = unserialize($serialized);
$this->assertTrue($service->getOperation('foo')->hasParam('foo'));
}
public function testAllowsForJsonBasedArrayParamsFunctionalTest()
{
$description = new ServiceDescription(array(
'operations' => array(
'test' => new Operation(array(
'httpMethod' => 'PUT',
'parameters' => array(
'data' => array(
'required' => true,
'filters' => 'json_encode',
'location' => 'body'
)
)
))
)
));
$client = new Client();
$client->setDescription($description);
$command = $client->getCommand('test', array(
'data' => array(
'foo' => 'bar'
)
));
$request = $command->prepare();
$this->assertEquals('{"foo":"bar"}', (string) $request->getBody());
}
public function testContainsModels()
{
$d = new ServiceDescription(array(
'operations' => array('foo' => array()),
'models' => array(
'Tag' => array('type' => 'object'),
'Person' => array('type' => 'object')
)
));
$this->assertTrue($d->hasModel('Tag'));
$this->assertTrue($d->hasModel('Person'));
$this->assertFalse($d->hasModel('Foo'));
$this->assertInstanceOf('Guzzle\Service\Description\Parameter', $d->getModel('Tag'));
$this->assertNull($d->getModel('Foo'));
$this->assertContains('"models":{', serialize($d));
$this->assertEquals(array('Tag', 'Person'), array_keys($d->getModels()));
}
public function testCanAddModels()
{
$d = new ServiceDescription(array());
$this->assertFalse($d->hasModel('Foo'));
$d->addModel(new Parameter(array('name' => 'Foo')));
$this->assertTrue($d->hasModel('Foo'));
}
public function testHasAttributes()
{
$d = new ServiceDescription(array(
'operations' => array(),
'name' => 'Name',
'description' => 'Description',
'apiVersion' => '1.24'
));
$this->assertEquals('Name', $d->getName());
$this->assertEquals('Description', $d->getDescription());
$this->assertEquals('1.24', $d->getApiVersion());
$s = serialize($d);
$this->assertContains('"name":"Name"', $s);
$this->assertContains('"description":"Description"', $s);
$this->assertContains('"apiVersion":"1.24"', $s);
$d = unserialize($s);
$this->assertEquals('Name', $d->getName());
$this->assertEquals('Description', $d->getDescription());
$this->assertEquals('1.24', $d->getApiVersion());
}
public function testPersistsCustomAttributes()
{
$data = array(
'operations' => array('foo' => array('class' => 'foo', 'parameters' => array())),
'name' => 'Name',
'description' => 'Test',
'apiVersion' => '1.24',
'auth' => 'foo',
'keyParam' => 'bar'
);
$d = new ServiceDescription($data);
$d->setData('hello', 'baz');
$this->assertEquals('foo', $d->getData('auth'));
$this->assertEquals('baz', $d->getData('hello'));
$this->assertEquals('bar', $d->getData('keyParam'));
$data['operations']['foo']['responseClass'] = 'array';
$data['operations']['foo']['responseType'] = 'primitive';
$this->assertEquals($data + array('hello' => 'baz'), json_decode($d->serialize(), true));
}
public function testHasToArray()
{
$data = array(
'operations' => array(),
'name' => 'Name',
'description' => 'Test'
);
$d = new ServiceDescription($data);
$arr = $d->toArray();
$this->assertEquals('Name', $arr['name']);
$this->assertEquals('Test', $arr['description']);
}
public function testReturnsNullWhenRetrievingMissingOperation()
{
$s = new ServiceDescription(array());
$this->assertNull($s->getOperation('foo'));
}
public function testCanAddOperations()
{
$s = new ServiceDescription(array());
$this->assertFalse($s->hasOperation('Foo'));
$s->addOperation(new Operation(array('name' => 'Foo')));
$this->assertTrue($s->hasOperation('Foo'));
}
public function testValidatesOperationTypes()
{
$s = new ServiceDescription(array(
'operations' => array('foo' => new \stdClass())
));
}
public function testHasBaseUrl()
{
$description = new ServiceDescription(array('baseUrl' => 'http://foo.com'));
$this->assertEquals('http://foo.com', $description->getBaseUrl());
$description->setBaseUrl('http://foobar.com');
$this->assertEquals('http://foobar.com', $description->getBaseUrl());
}
public function testCanUseBasePath()
{
$description = new ServiceDescription(array('basePath' => 'http://foo.com'));
$this->assertEquals('http://foo.com', $description->getBaseUrl());
}
public function testModelsHaveNames()
{
$desc = array(
'models' => array(
'date' => array('type' => 'string'),
'user'=> array(
'type' => 'object',
'properties' => array(
'dob' => array('$ref' => 'date')
)
)
)
);
$s = ServiceDescription::factory($desc);
$this->assertEquals('date', $s->getModel('date')->getName());
$this->assertEquals('dob', $s->getModel('user')->getProperty('dob')->getName());
}
}