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