PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/api/vendor/guzzle/guzzle/tests/Guzzle/Tests/Service/Builder/ServiceBuilderTest.php

https://gitlab.com/x33n/respond
PHP | 317 lines | 256 code | 43 blank | 18 comment | 0 complexity | c27feb27faeaad066098d16b68540492 MD5 | raw file
  1. <?php
  2. namespace Guzzle\Tests\Service;
  3. use Guzzle\Plugin\History\HistoryPlugin;
  4. use Guzzle\Service\Builder\ServiceBuilder;
  5. use Guzzle\Service\Client;
  6. /**
  7. * @covers Guzzle\Service\Builder\ServiceBuilder
  8. */
  9. class ServiceBuilderTest extends \Guzzle\Tests\GuzzleTestCase
  10. {
  11. protected $arrayData = array(
  12. 'michael.mock' => array(
  13. 'class' => 'Guzzle\Tests\Service\Mock\MockClient',
  14. 'params' => array(
  15. 'username' => 'michael',
  16. 'password' => 'testing123',
  17. 'subdomain' => 'michael',
  18. ),
  19. ),
  20. 'billy.mock' => array(
  21. 'alias' => 'Hello!',
  22. 'class' => 'Guzzle\Tests\Service\Mock\MockClient',
  23. 'params' => array(
  24. 'username' => 'billy',
  25. 'password' => 'passw0rd',
  26. 'subdomain' => 'billy',
  27. ),
  28. ),
  29. 'billy.testing' => array(
  30. 'extends' => 'billy.mock',
  31. 'params' => array(
  32. 'subdomain' => 'test.billy',
  33. ),
  34. ),
  35. 'missing_params' => array(
  36. 'extends' => 'billy.mock'
  37. )
  38. );
  39. public function testAllowsSerialization()
  40. {
  41. $builder = ServiceBuilder::factory($this->arrayData);
  42. $cached = unserialize(serialize($builder));
  43. $this->assertEquals($cached, $builder);
  44. }
  45. public function testDelegatesFactoryMethodToAbstractFactory()
  46. {
  47. $builder = ServiceBuilder::factory($this->arrayData);
  48. $c = $builder->get('michael.mock');
  49. $this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $c);
  50. }
  51. /**
  52. * @expectedException Guzzle\Service\Exception\ServiceNotFoundException
  53. * @expectedExceptionMessage No service is registered as foobar
  54. */
  55. public function testThrowsExceptionWhenGettingInvalidClient()
  56. {
  57. ServiceBuilder::factory($this->arrayData)->get('foobar');
  58. }
  59. public function testStoresClientCopy()
  60. {
  61. $builder = ServiceBuilder::factory($this->arrayData);
  62. $client = $builder->get('michael.mock');
  63. $this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $client);
  64. $this->assertEquals('http://127.0.0.1:8124/v1/michael', $client->getBaseUrl());
  65. $this->assertEquals($client, $builder->get('michael.mock'));
  66. // Get another client but throw this one away
  67. $client2 = $builder->get('billy.mock', true);
  68. $this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $client2);
  69. $this->assertEquals('http://127.0.0.1:8124/v1/billy', $client2->getBaseUrl());
  70. // Make sure the original client is still there and set
  71. $this->assertTrue($client === $builder->get('michael.mock'));
  72. // Create a new billy.mock client that is stored
  73. $client3 = $builder->get('billy.mock');
  74. // Make sure that the stored billy.mock client is equal to the other stored client
  75. $this->assertTrue($client3 === $builder->get('billy.mock'));
  76. // Make sure that this client is not equal to the previous throwaway client
  77. $this->assertFalse($client2 === $builder->get('billy.mock'));
  78. }
  79. public function testBuildersPassOptionsThroughToClients()
  80. {
  81. $s = new ServiceBuilder(array(
  82. 'michael.mock' => array(
  83. 'class' => 'Guzzle\Tests\Service\Mock\MockClient',
  84. 'params' => array(
  85. 'base_url' => 'http://www.test.com/',
  86. 'subdomain' => 'michael',
  87. 'password' => 'test',
  88. 'username' => 'michael',
  89. 'curl.curlopt_proxyport' => 8080
  90. )
  91. )
  92. ));
  93. $c = $s->get('michael.mock');
  94. $this->assertEquals(8080, $c->getConfig('curl.curlopt_proxyport'));
  95. }
  96. public function testUsesTheDefaultBuilderWhenNoBuilderIsSpecified()
  97. {
  98. $s = new ServiceBuilder(array(
  99. 'michael.mock' => array(
  100. 'class' => 'Guzzle\Tests\Service\Mock\MockClient',
  101. 'params' => array(
  102. 'base_url' => 'http://www.test.com/',
  103. 'subdomain' => 'michael',
  104. 'password' => 'test',
  105. 'username' => 'michael',
  106. 'curl.curlopt_proxyport' => 8080
  107. )
  108. )
  109. ));
  110. $c = $s->get('michael.mock');
  111. $this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $c);
  112. }
  113. public function testUsedAsArray()
  114. {
  115. $b = ServiceBuilder::factory($this->arrayData);
  116. $this->assertTrue($b->offsetExists('michael.mock'));
  117. $this->assertFalse($b->offsetExists('not_there'));
  118. $this->assertInstanceOf('Guzzle\Service\Client', $b['michael.mock']);
  119. unset($b['michael.mock']);
  120. $this->assertFalse($b->offsetExists('michael.mock'));
  121. $b['michael.mock'] = new Client('http://www.test.com/');
  122. $this->assertInstanceOf('Guzzle\Service\Client', $b['michael.mock']);
  123. }
  124. public function testFactoryCanCreateFromJson()
  125. {
  126. $tmp = sys_get_temp_dir() . '/test.js';
  127. file_put_contents($tmp, json_encode($this->arrayData));
  128. $b = ServiceBuilder::factory($tmp);
  129. unlink($tmp);
  130. $s = $b->get('billy.testing');
  131. $this->assertEquals('test.billy', $s->getConfig('subdomain'));
  132. $this->assertEquals('billy', $s->getConfig('username'));
  133. }
  134. public function testFactoryCanCreateFromArray()
  135. {
  136. $b = ServiceBuilder::factory($this->arrayData);
  137. $s = $b->get('billy.testing');
  138. $this->assertEquals('test.billy', $s->getConfig('subdomain'));
  139. $this->assertEquals('billy', $s->getConfig('username'));
  140. }
  141. public function testFactoryDoesNotRequireParams()
  142. {
  143. $b = ServiceBuilder::factory($this->arrayData);
  144. $s = $b->get('missing_params');
  145. $this->assertEquals('billy', $s->getConfig('username'));
  146. }
  147. public function testBuilderAllowsReferencesBetweenClients()
  148. {
  149. $builder = ServiceBuilder::factory(array(
  150. 'a' => array(
  151. 'class' => 'Guzzle\Tests\Service\Mock\MockClient',
  152. 'params' => array(
  153. 'other_client' => '{b}',
  154. 'username' => 'x',
  155. 'password' => 'y',
  156. 'subdomain' => 'z'
  157. )
  158. ),
  159. 'b' => array(
  160. 'class' => 'Guzzle\Tests\Service\Mock\MockClient',
  161. 'params' => array(
  162. 'username' => '1',
  163. 'password' => '2',
  164. 'subdomain' => '3'
  165. )
  166. )
  167. ));
  168. $client = $builder['a'];
  169. $this->assertEquals('x', $client->getConfig('username'));
  170. $this->assertSame($builder['b'], $client->getConfig('other_client'));
  171. $this->assertEquals('1', $builder['b']->getConfig('username'));
  172. }
  173. public function testEmitsEventsWhenClientsAreCreated()
  174. {
  175. // Ensure that the client signals that it emits an event
  176. $this->assertEquals(array('service_builder.create_client'), ServiceBuilder::getAllEvents());
  177. // Create a test service builder
  178. $builder = ServiceBuilder::factory(array(
  179. 'a' => array(
  180. 'class' => 'Guzzle\Tests\Service\Mock\MockClient',
  181. 'params' => array(
  182. 'username' => 'test',
  183. 'password' => '123',
  184. 'subdomain' => 'z'
  185. )
  186. )
  187. ));
  188. // Add an event listener to pick up client creation events
  189. $emits = 0;
  190. $builder->getEventDispatcher()->addListener('service_builder.create_client', function($event) use (&$emits) {
  191. $emits++;
  192. });
  193. // Get the 'a' client by name
  194. $client = $builder->get('a');
  195. // Ensure that the event was emitted once, and that the client was present
  196. $this->assertEquals(1, $emits);
  197. $this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $client);
  198. }
  199. public function testCanAddGlobalParametersToServicesOnLoad()
  200. {
  201. $builder = ServiceBuilder::factory($this->arrayData, array(
  202. 'username' => 'fred',
  203. 'new_value' => 'test'
  204. ));
  205. $data = json_decode($builder->serialize(), true);
  206. foreach ($data as $service) {
  207. $this->assertEquals('fred', $service['params']['username']);
  208. $this->assertEquals('test', $service['params']['new_value']);
  209. }
  210. }
  211. public function testAddsGlobalPlugins()
  212. {
  213. $b = new ServiceBuilder($this->arrayData);
  214. $b->addGlobalPlugin(new HistoryPlugin());
  215. $s = $b->get('michael.mock');
  216. $this->assertTrue($s->getEventDispatcher()->hasListeners('request.sent'));
  217. }
  218. public function testCanGetData()
  219. {
  220. $b = new ServiceBuilder($this->arrayData);
  221. $this->assertEquals($this->arrayData['michael.mock'], $b->getData('michael.mock'));
  222. $this->assertNull($b->getData('ewofweoweofe'));
  223. }
  224. public function testCanGetByAlias()
  225. {
  226. $b = new ServiceBuilder($this->arrayData);
  227. $this->assertSame($b->get('billy.mock'), $b->get('Hello!'));
  228. }
  229. public function testCanOverwriteParametersForThrowawayClients()
  230. {
  231. $b = new ServiceBuilder($this->arrayData);
  232. $c1 = $b->get('michael.mock');
  233. $this->assertEquals('michael', $c1->getConfig('username'));
  234. $c2 = $b->get('michael.mock', array('username' => 'jeremy'));
  235. $this->assertEquals('jeremy', $c2->getConfig('username'));
  236. }
  237. public function testGettingAThrowawayClientWithParametersDoesNotAffectGettingOtherClients()
  238. {
  239. $b = new ServiceBuilder($this->arrayData);
  240. $c1 = $b->get('michael.mock', array('username' => 'jeremy'));
  241. $this->assertEquals('jeremy', $c1->getConfig('username'));
  242. $c2 = $b->get('michael.mock');
  243. $this->assertEquals('michael', $c2->getConfig('username'));
  244. }
  245. public function testCanUseArbitraryData()
  246. {
  247. $b = new ServiceBuilder();
  248. $b['a'] = 'foo';
  249. $this->assertTrue(isset($b['a']));
  250. $this->assertEquals('foo', $b['a']);
  251. unset($b['a']);
  252. $this->assertFalse(isset($b['a']));
  253. }
  254. public function testCanRegisterServiceData()
  255. {
  256. $b = new ServiceBuilder();
  257. $b['a'] = array(
  258. 'class' => 'Guzzle\Tests\Service\Mock\MockClient',
  259. 'params' => array(
  260. 'username' => 'billy',
  261. 'password' => 'passw0rd',
  262. 'subdomain' => 'billy',
  263. )
  264. );
  265. $this->assertTrue(isset($b['a']));
  266. $this->assertInstanceOf('Guzzle\Tests\Service\Mock\MockClient', $b['a']);
  267. $client = $b['a'];
  268. unset($b['a']);
  269. $this->assertFalse(isset($b['a']));
  270. // Ensure that instantiated clients can be registered
  271. $b['mock'] = $client;
  272. $this->assertSame($client, $b['mock']);
  273. }
  274. }