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: 310: 311: 312: 313: 314: 315: 316: 317: 318:
<?php
namespace Guzzle\Tests\Service;
use Guzzle\Plugin\History\HistoryPlugin;
use Guzzle\Service\Builder\ServiceBuilder;
use Guzzle\Service\Client;
class ServiceBuilderTest extends \Guzzle\Tests\GuzzleTestCase
{
protected $arrayData = array(
'michael.mock' => array(
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'username' => 'michael',
'password' => 'testing123',
'subdomain' => 'michael',
),
),
'billy.mock' => array(
'alias' => 'Hello!',
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'username' => 'billy',
'password' => 'passw0rd',
'subdomain' => 'billy',
),
),
'billy.testing' => array(
'extends' => 'billy.mock',
'params' => array(
'subdomain' => 'test.billy',
),
),
'missing_params' => array(
'extends' => 'billy.mock'
)
);
public function testAllowsSerialization()
{
$builder = ServiceBuilder::factory($this->arrayData);
$cached = unserialize(serialize($builder));
$this->assertEquals($cached, $builder);
}
public function testDelegatesFactoryMethodToAbstractFactory()
{
$builder = ServiceBuilder::factory($this->arrayData);
$c = $builder->get('michael.mock');
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $c);
}
public function testThrowsExceptionWhenGettingInvalidClient()
{
ServiceBuilder::factory($this->arrayData)->get('foobar');
}
public function testStoresClientCopy()
{
$builder = ServiceBuilder::factory($this->arrayData);
$client = $builder->get('michael.mock');
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $client);
$this->assertEquals('http://127.0.0.1:8124/v1/michael', $client->getBaseUrl());
$this->assertEquals($client, $builder->get('michael.mock'));
$client2 = $builder->get('billy.mock', true);
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $client2);
$this->assertEquals('http://127.0.0.1:8124/v1/billy', $client2->getBaseUrl());
$this->assertTrue($client === $builder->get('michael.mock'));
$client3 = $builder->get('billy.mock');
$this->assertTrue($client3 === $builder->get('billy.mock'));
$this->assertFalse($client2 === $builder->get('billy.mock'));
}
public function testBuildersPassOptionsThroughToClients()
{
$s = new ServiceBuilder(array(
'michael.mock' => array(
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'base_url' => 'http://www.test.com/',
'subdomain' => 'michael',
'password' => 'test',
'username' => 'michael',
'curl.curlopt_proxyport' => 8080
)
)
));
$c = $s->get('michael.mock');
$this->assertEquals(8080, $c->getConfig('curl.curlopt_proxyport'));
}
public function testUsesTheDefaultBuilderWhenNoBuilderIsSpecified()
{
$s = new ServiceBuilder(array(
'michael.mock' => array(
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'base_url' => 'http://www.test.com/',
'subdomain' => 'michael',
'password' => 'test',
'username' => 'michael',
'curl.curlopt_proxyport' => 8080
)
)
));
$c = $s->get('michael.mock');
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $c);
}
public function testUsedAsArray()
{
$b = ServiceBuilder::factory($this->arrayData);
$this->assertTrue($b->offsetExists('michael.mock'));
$this->assertFalse($b->offsetExists('not_there'));
$this->assertInstanceOf('Guzzle\Service\Client', $b['michael.mock']);
unset($b['michael.mock']);
$this->assertFalse($b->offsetExists('michael.mock'));
$b['michael.mock'] = new Client('http://www.test.com/');
$this->assertInstanceOf('Guzzle\Service\Client', $b['michael.mock']);
}
public function testFactoryCanCreateFromJson()
{
$tmp = sys_get_temp_dir() . '/test.js';
file_put_contents($tmp, json_encode($this->arrayData));
$b = ServiceBuilder::factory($tmp);
unlink($tmp);
$s = $b->get('billy.testing');
$this->assertEquals('test.billy', $s->getConfig('subdomain'));
$this->assertEquals('billy', $s->getConfig('username'));
}
public function testFactoryCanCreateFromArray()
{
$b = ServiceBuilder::factory($this->arrayData);
$s = $b->get('billy.testing');
$this->assertEquals('test.billy', $s->getConfig('subdomain'));
$this->assertEquals('billy', $s->getConfig('username'));
}
public function testFactoryDoesNotRequireParams()
{
$b = ServiceBuilder::factory($this->arrayData);
$s = $b->get('missing_params');
$this->assertEquals('billy', $s->getConfig('username'));
}
public function testBuilderAllowsReferencesBetweenClients()
{
$builder = ServiceBuilder::factory(array(
'a' => array(
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'other_client' => '{b}',
'username' => 'x',
'password' => 'y',
'subdomain' => 'z'
)
),
'b' => array(
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'username' => '1',
'password' => '2',
'subdomain' => '3'
)
)
));
$client = $builder['a'];
$this->assertEquals('x', $client->getConfig('username'));
$this->assertSame($builder['b'], $client->getConfig('other_client'));
$this->assertEquals('1', $builder['b']->getConfig('username'));
}
public function testEmitsEventsWhenClientsAreCreated()
{
$this->assertEquals(array('service_builder.create_client'), ServiceBuilder::getAllEvents());
$builder = ServiceBuilder::factory(array(
'a' => array(
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'username' => 'test',
'password' => '123',
'subdomain' => 'z'
)
)
));
$emits = 0;
$builder->getEventDispatcher()->addListener('service_builder.create_client', function($event) use (&$emits) {
$emits++;
});
$client = $builder->get('a');
$this->assertEquals(1, $emits);
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $client);
}
public function testCanAddGlobalParametersToServicesOnLoad()
{
$builder = ServiceBuilder::factory($this->arrayData, array(
'username' => 'fred',
'new_value' => 'test'
));
$data = json_decode($builder->serialize(), true);
foreach ($data as $service) {
$this->assertEquals('fred', $service['params']['username']);
$this->assertEquals('test', $service['params']['new_value']);
}
}
public function testAddsGlobalPlugins()
{
$b = new ServiceBuilder($this->arrayData);
$b->addGlobalPlugin(new HistoryPlugin());
$s = $b->get('michael.mock');
$this->assertTrue($s->getEventDispatcher()->hasListeners('request.sent'));
}
public function testCanGetData()
{
$b = new ServiceBuilder($this->arrayData);
$this->assertEquals($this->arrayData['michael.mock'], $b->getData('michael.mock'));
$this->assertNull($b->getData('ewofweoweofe'));
}
public function testCanGetByAlias()
{
$b = new ServiceBuilder($this->arrayData);
$this->assertSame($b->get('billy.mock'), $b->get('Hello!'));
}
public function testCanOverwriteParametersForThrowawayClients()
{
$b = new ServiceBuilder($this->arrayData);
$c1 = $b->get('michael.mock');
$this->assertEquals('michael', $c1->getConfig('username'));
$c2 = $b->get('michael.mock', array('username' => 'jeremy'));
$this->assertEquals('jeremy', $c2->getConfig('username'));
}
public function testGettingAThrowawayClientWithParametersDoesNotAffectGettingOtherClients()
{
$b = new ServiceBuilder($this->arrayData);
$c1 = $b->get('michael.mock', array('username' => 'jeremy'));
$this->assertEquals('jeremy', $c1->getConfig('username'));
$c2 = $b->get('michael.mock');
$this->assertEquals('michael', $c2->getConfig('username'));
}
public function testCanUseArbitraryData()
{
$b = new ServiceBuilder();
$b['a'] = 'foo';
$this->assertTrue(isset($b['a']));
$this->assertEquals('foo', $b['a']);
unset($b['a']);
$this->assertFalse(isset($b['a']));
}
public function testCanRegisterServiceData()
{
$b = new ServiceBuilder();
$b['a'] = array(
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'username' => 'billy',
'password' => 'passw0rd',
'subdomain' => 'billy',
)
);
$this->assertTrue(isset($b['a']));
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $b['a']);
$client = $b['a'];
unset($b['a']);
$this->assertFalse(isset($b['a']));
$b['mock'] = $client;
$this->assertSame($client, $b['mock']);
}
}