"
70 | }
71 |
72 | Executing the `GetPeople` command will now return an array of `Vendor\MyBundle\Entity\Person` instances:
73 |
74 | $command = $client->getCommand('GetPeople');
75 | $people = $client->execute($command);
76 |
77 | Requests
78 | --------
79 |
80 | To send a (serialized) object in your request, put your object in a `body` parameter. You should also set the `instanceOf` value in the parameter as the fully-qualified class name.
81 |
82 | By default it will serialize the object into XML. The change this, set the `sentAs` value as a format that the JMSSerializerBundle can use (ie `json`, `yml` or `xml`).
83 |
84 | For example:
85 |
86 | "CreatePerson":{
87 | "httpMethod":"POST",
88 | "uri":"person",
89 | "summary":"Create a person",
90 | "parameters":{
91 | "person":{
92 | "location":"body",
93 | "type":"object",
94 | "instanceOf":"Vendor\\MyBundle\\Entity\\Person",
95 | "sentAs":"json",
96 | "required":"true"
97 | }
98 | }
99 | }
100 |
101 | Executing the `CreatePerson` command will now send an instance of `Vendor\MyBundle\Entity\Person` as JSON:
102 |
103 | $command = $client->getCommand('CreatePerson', array('person' => $person));
104 | $client->execute($command);
105 |
106 | If you wish to customize the serialization context of the serializer, you can do so by usage of the `data` property of the Parameter. Groups, version, serializing nulls and max depth checks are configurable for serialization:
107 |
108 | For example:
109 |
110 | "CreatePerson":{
111 | "httpMethod":"POST",
112 | "uri":"person",
113 | "summary":"Create a person",
114 | "parameters":{
115 | "person":{
116 | "location":"body",
117 | "type":"object",
118 | "instanceOf":"Vendor\\MyBundle\\Entity\\Person",
119 | "sentAs":"json",
120 | "required":"true",
121 | "data": {
122 | "jms_serializer.groups": "person-details",
123 | "jms_serializer.version": 1,
124 | "jms_serializer.serialize_nulls": true,
125 | "jms_serializer.max_depth_checks": true,
126 | }
127 | }
128 | }
129 | }
130 |
131 | Concrete commands
132 | -----------------
133 |
134 | When using a concrete command, you can still make use of the JMSSerializer by implementating `Misd\GuzzleBundle\Service\Command\JMSSerializerAwareCommandInterface`.
135 |
--------------------------------------------------------------------------------
/Resources/views/Collector/guzzle.html.twig:
--------------------------------------------------------------------------------
1 | {% extends 'WebProfilerBundle:Profiler:layout.html.twig' %}
2 |
3 | {% block toolbar %}
4 | {% set icon %}
5 |
7 | {{ collector.requests|length }}
8 | {% endset %}
9 | {% set text %}
10 |
11 | HTTP Requests
12 | {{ collector.requests|length }}
13 |
14 | {% endset %}
15 | {% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
16 | {% endblock %}
17 |
18 | {% block menu %}
19 |
20 |
21 | Guzzle
22 |
23 | {{ collector.requests|length }}
24 |
25 |
26 | {% endblock %}
27 |
28 | {% block panel %}
29 | HTTP requests
30 |
31 | {% if collector.requests|length == 0 %}
32 |
33 | No requests sent.
34 |
35 | {% else %}
36 |
37 | {% for i, request in collector.requests %}
38 | -
39 |
40 |
42 |
44 |
46 |
47 |
{{ request.message }} ({{ request.time|number_format }} ms)
48 |
49 |
50 |
51 |
52 | Request:
53 |
54 |
{{ request.request }}
55 |
56 | Response:
57 |
58 |
{{ request.response }}
59 |
60 |
61 | {% endfor %}
62 |
63 | {% endif %}
64 |
65 |
77 | {% endblock %}
78 |
--------------------------------------------------------------------------------
/Request/ParamConverter/AbstractGuzzleParamConverter.php:
--------------------------------------------------------------------------------
1 |
26 | */
27 | abstract class AbstractGuzzleParamConverter implements ParamConverterInterface
28 | {
29 | /**
30 | * @var ClientInterface[]
31 | */
32 | private $clients = array();
33 |
34 | /**
35 | * Make the param converter aware of a client.
36 | *
37 | * @param string $id
38 | * @param ClientInterface $client
39 | */
40 | public function registerClient($id, ClientInterface $client)
41 | {
42 | $this->clients[$id] = $client;
43 | }
44 |
45 | /**
46 | * Stores the object in the request.
47 | *
48 | * @param Request $request The request
49 | * @param ParamConverter $configuration Contains the name, class and options of the object
50 | *
51 | * @return boolean True if the object has been successfully set, else false
52 | *
53 | * @throws NotFoundHttpException
54 | * @throws BadResponseException
55 | */
56 | protected function execute(Request $request, ParamConverter $configuration)
57 | {
58 | $found = $this->find($configuration);
59 |
60 | $name = $configuration->getName();
61 | $class = $configuration->getClass();
62 | $options = $configuration->getOptions();
63 |
64 | $client = $found['client'];
65 | $command = $found['command'];
66 | $operation = $client->getDescription()->getOperation($command);
67 |
68 | $routeParameters = $request->attributes->get('_route_params');
69 |
70 | if (true === isset($options['exclude'])) {
71 | foreach ($options['exclude'] as $exclude) {
72 | unset($routeParameters[$exclude]);
73 | }
74 | }
75 |
76 | if (false === isset($options['mapping'])) {
77 | $options['parameters'] = $routeParameters;
78 | } else {
79 | $options['parameters'] = array();
80 | foreach ($options['mapping'] as $key => $parameter) {
81 | $options['parameters'][$parameter] = $routeParameters[$key];
82 | }
83 | }
84 |
85 | $parameters = array();
86 |
87 | foreach ($options['parameters'] as $key => $value) {
88 | if ($operation->hasParam($key)) {
89 | switch ($operation->getParam($key)->getType()) {
90 | case 'integer':
91 | $value = (int) $value;
92 | }
93 | $parameters[$key] = $value;
94 | }
95 | }
96 |
97 | $command = $client->getCommand($command, $parameters);
98 |
99 | try {
100 | $result = $client->execute($command);
101 | } catch (BadResponseException $e) {
102 | if (true === $configuration->isOptional()) {
103 | $result = null;
104 | } elseif (404 === $e->getResponse()->getStatusCode()) {
105 | throw new NotFoundHttpException(sprintf('%s object not found.', $class), $e);
106 | } else {
107 | throw $e;
108 | }
109 | }
110 |
111 | $request->attributes->set($name, $result);
112 |
113 | return true;
114 | }
115 |
116 | /**
117 | * Try and find a command for the requested class.
118 | *
119 | * @param ParamConverter $configuration
120 | *
121 | * @return array|null Array containing the client and command if found, null if not.
122 | *
123 | * @throws LogicException
124 | */
125 | protected function find(ParamConverter $configuration)
126 | {
127 | $options = $configuration->getOptions();
128 |
129 | // determine the client, if possible
130 | if (true === isset($options['client'])) {
131 | if (false === isset($this->clients[$options['client']])) {
132 | throw new LogicException(sprintf('Unknown client \'%s\'', $options['client']));
133 | }
134 | $client = $this->clients[$options['client']];
135 | } elseif (1 === count($this->clients)) {
136 | $ids = array_keys($this->clients);
137 | $options['client'] = reset($ids);
138 | $client = reset($this->clients);
139 | } else {
140 | $client = null;
141 | }
142 |
143 | // determine the command, if possible
144 | if (true === isset($options['command'])) {
145 | if (null === $client) {
146 | throw new LogicException('Command defined without a client');
147 | }
148 | $operations = $client->getDescription()->getOperations();
149 | if (false === isset($operations[$options['command']])) {
150 | throw new LogicException(sprintf(
151 | 'Unknown command \'%s\' for client \'%s\'',
152 | $options['command'],
153 | $options['client']
154 | ));
155 | }
156 |
157 | $command = $options['command'];
158 |
159 | if (false === ($configuration->getClass() === $operations[$options['command']]->getResponseClass())) {
160 | throw new LogicException(sprintf(
161 | 'Command \'%s\' return \'%s\' rather than \'%s\'',
162 | $options['command'],
163 | $operations[$options['command']]->getResponseClass(),
164 | $configuration->getClass()
165 | ));
166 | }
167 | } else {
168 | $command = null;
169 | }
170 |
171 | // if we don't know the command yet, try and find it
172 | if (null === $command) {
173 | if (null !== $client) {
174 | $searchClients = array($options['client'] => $client);
175 | } else {
176 | $searchClients = $this->clients;
177 | }
178 | foreach ($searchClients as $thisClient) {
179 | if (null === $thisClient->getDescription()) {
180 | continue;
181 | }
182 |
183 | foreach ($thisClient->getDescription()->getOperations() as $operation) {
184 | if (
185 | 'GET' === $operation->getHttpMethod() &&
186 | $configuration->getClass() === $operation->getResponseClass()
187 | ) {
188 | $client = $thisClient;
189 | $command = $operation->getName();
190 |
191 | break 2;
192 | }
193 | }
194 | }
195 | }
196 |
197 | if (null !== $client && null !== $command) {
198 | return array('client' => $client, 'command' => $command);
199 | } else {
200 | return null;
201 | }
202 | }
203 | }
204 |
--------------------------------------------------------------------------------
/Tests/Functional/JMSSerializerResponseTest.php:
--------------------------------------------------------------------------------
1 | getCommand('GetPerson', array('id' => 1));
24 | self::$mock->addResponse(Response::fromMessage(self::xmlResponse()));
25 |
26 | $this->assertInstanceOf('SimpleXMLElement', $client->execute($command));
27 | }
28 |
29 | public function testGetPersonXmlResponseWithoutSerializer()
30 | {
31 | $client = self::getClient('Basic');
32 | $command = $client->getCommand('GetPerson', array('id' => 1));
33 | self::$mock->addResponse(Response::fromMessage(self::xmlResponse()));
34 |
35 | $this->assertInstanceOf('SimpleXMLElement', $client->execute($command));
36 | }
37 |
38 | public function testGetPersonJsonResponseWithSerializer()
39 | {
40 | $client = self::getClient('JMSSerializerBundle');
41 | $command = $client->getCommand('GetPerson', array('id' => 1));
42 | self::$mock->addResponse(Response::fromMessage(self::jsonResponse()));
43 |
44 | $this->assertTrue(is_array($client->execute($command)));
45 | }
46 |
47 | public function testGetPersonJsonResponseWithoutSerializer()
48 | {
49 | $client = self::getClient('Basic');
50 | $command = $client->getCommand('GetPerson', array('id' => 1));
51 | self::$mock->addResponse(Response::fromMessage(self::jsonResponse()));
52 |
53 | $this->assertTrue(is_array($client->execute($command)));
54 | }
55 |
56 | public function testGetPersonUnknownResponseWithSerializer()
57 | {
58 | $client = self::getClient('JMSSerializerBundle');
59 | $command = $client->getCommand('GetPerson', array('id' => 1));
60 | self::$mock->addResponse(Response::fromMessage(self::unknownResponse()));
61 |
62 | $this->assertInstanceOf('Guzzle\Http\Message\Response', $client->execute($command));
63 | }
64 |
65 | public function testGetPersonUnknownResponseWithoutSerializer()
66 | {
67 | $client = self::getClient('Basic');
68 | $command = $client->getCommand('GetPerson', array('id' => 1));
69 | self::$mock->addResponse(Response::fromMessage(self::unknownResponse()));
70 |
71 | $this->assertInstanceOf('Guzzle\Http\Message\Response', $client->execute($command));
72 | }
73 |
74 | public function testGetPersonClassXmlResponseWithSerializer()
75 | {
76 | $client = self::getClient('JMSSerializerBundle');
77 | $command = $client->getCommand('GetPersonClass', array('id' => 1));
78 | self::$mock->addResponse(Response::fromMessage(self::xmlResponse()));
79 | $person = $client->execute($command);
80 |
81 | $this->assertInstanceOf('Misd\GuzzleBundle\Tests\Fixtures\Person', $person);
82 | $this->assertEquals(1, $person->id);
83 | $this->assertEquals('Foo', $person->firstName);
84 | $this->assertEquals('Bar', $person->familyName);
85 | }
86 |
87 | public function testGetPersonClassXmlResponseWithoutSerializer()
88 | {
89 | $client = self::getClient('Basic');
90 | $command = $client->getCommand('GetPersonClass', array('id' => 1));
91 | self::$mock->addResponse(Response::fromMessage(self::xmlResponse()));
92 |
93 | if (version_compare(Version::VERSION, '3.3.0', '>=')) {
94 | $this->setExpectedException('Guzzle\Service\Exception\ResponseClassException');
95 | }
96 |
97 | $this->assertInstanceOf('SimpleXMLElement', $client->execute($command));
98 | }
99 |
100 | public function testGetPersonClassJsonResponseWithSerializer()
101 | {
102 | $client = self::getClient('JMSSerializerBundle');
103 | $command = $client->getCommand('GetPersonClass', array('id' => 1));
104 | self::$mock->addResponse(Response::fromMessage(self::jsonResponse()));
105 | $person = $client->execute($command);
106 |
107 | $this->assertInstanceOf('Misd\GuzzleBundle\Tests\Fixtures\Person', $person);
108 | $this->assertEquals(1, $person->id);
109 | $this->assertEquals('Foo', $person->firstName);
110 | $this->assertEquals('Bar', $person->familyName);
111 | }
112 |
113 | public function testGetPersonClassJsonResponseWithoutSerializer()
114 | {
115 | $client = self::getClient('Basic');
116 | $command = $client->getCommand('GetPersonClass', array('id' => 1));
117 | self::$mock->addResponse(Response::fromMessage(self::jsonResponse()));
118 |
119 | if (version_compare(Version::VERSION, '3.3.0', '>=')) {
120 | $this->setExpectedException('Guzzle\Service\Exception\ResponseClassException');
121 | }
122 |
123 | $this->assertTrue(is_array($client->execute($command)));
124 | }
125 |
126 | public function testGetPersonClassUnknownResponseWithSerializer()
127 | {
128 | $client = self::getClient('JMSSerializerBundle');
129 | $command = $client->getCommand('GetPersonClass', array('id' => 1));
130 | self::$mock->addResponse(Response::fromMessage(self::unknownResponse()));
131 |
132 | if (version_compare(Version::VERSION, '3.3.0', '>=')) {
133 | $this->setExpectedException('Guzzle\Service\Exception\ResponseClassException');
134 | }
135 |
136 | $this->assertInstanceOf('Guzzle\Http\Message\Response', $client->execute($command));
137 | }
138 |
139 | public function testGetPersonClassUnknownResponseWithoutSerializer()
140 | {
141 | $client = self::getClient('Basic');
142 | $command = $client->getCommand('GetPersonClass', array('id' => 1));
143 | self::$mock->addResponse(Response::fromMessage(self::unknownResponse()));
144 |
145 | if (version_compare(Version::VERSION, '3.3.0', '>=')) {
146 | $this->setExpectedException('Guzzle\Service\Exception\ResponseClassException');
147 | }
148 |
149 | $this->assertInstanceOf('Guzzle\Http\Message\Response', $client->execute($command));
150 | }
151 |
152 | /**
153 | * @expectedException \Exception
154 | */
155 | public function testGetPersonInvalidClassXmlResponseWithSerializer()
156 | {
157 | $client = self::getClient('JMSSerializerBundle');
158 | $command = $client->getCommand('GetPersonInvalidClass', array('id' => 1));
159 | self::$mock->addResponse(Response::fromMessage(self::xmlResponse()));
160 | $client->execute($command);
161 | }
162 |
163 | public function testGetPersonInvalidClassXmlResponseWithoutSerializer()
164 | {
165 | $client = self::getClient('Basic');
166 | $command = $client->getCommand('GetPersonInvalidClass', array('id' => 1));
167 | self::$mock->addResponse(Response::fromMessage(self::xmlResponse()));
168 |
169 | if (version_compare(Version::VERSION, '3.3.0', '>=')) {
170 | $this->setExpectedException('Guzzle\Service\Exception\ResponseClassException');
171 | }
172 |
173 | $this->assertInstanceOf('SimpleXMLElement', $client->execute($command));
174 | }
175 |
176 | public function testGetPersonClassArrayJsonResponseWithSerializer()
177 | {
178 | $client = self::getClient('JMSSerializerBundle');
179 | $command = $client->getCommand('GetPersonClassArray');
180 | self::$mock->addResponse(Response::fromMessage(self::jsonArrayResponse()));
181 | $people = $client->execute($command);
182 |
183 | $this->assertTrue(is_array($people));
184 | $this->assertCount(2, $people);
185 |
186 | $this->assertInstanceOf('Misd\GuzzleBundle\Tests\Fixtures\Person', $people[0]);
187 | $this->assertEquals(1, $people[0]->id);
188 | $this->assertEquals('Foo', $people[0]->firstName);
189 | $this->assertEquals('Bar', $people[0]->familyName);
190 |
191 | $this->assertInstanceOf('Misd\GuzzleBundle\Tests\Fixtures\Person', $people[1]);
192 | $this->assertEquals(2, $people[1]->id);
193 | $this->assertEquals('Baz', $people[1]->firstName);
194 | $this->assertEquals('Qux', $people[1]->familyName);
195 | }
196 |
197 | protected function xmlResponse()
198 | {
199 | return <<
207 |
208 | Foo
209 | Bar
210 |
211 | EOT;
212 | }
213 |
214 | protected function jsonResponse()
215 | {
216 | return <<converter->registerClient('without.description', new Client());
29 | $this->converter->registerClient('with.description', self::getClient('JMSSerializerBundle'));
30 | }
31 |
32 | public function testSupports()
33 | {
34 | $config = $this->createConfiguration(__CLASS__);
35 | $this->assertFalse($this->converter->supports($config));
36 |
37 | $config = $this->createConfiguration('Misd\GuzzleBundle\Tests\Fixtures\Person');
38 | $this->assertTrue($this->converter->supports($config));
39 |
40 | $config = $this->createConfiguration(
41 | 'Misd\GuzzleBundle\Tests\Fixtures\Person',
42 | array('client' => 'with.description')
43 | );
44 | $this->assertTrue($this->converter->supports($config));
45 |
46 | $config = $this->createConfiguration(
47 | 'Misd\GuzzleBundle\Tests\Fixtures\Person',
48 | array('client' => 'with.description', 'command' => 'GetPersonClass')
49 | );
50 | $this->assertTrue($this->converter->supports($config));
51 | }
52 |
53 | /**
54 | * @expectedException \LogicException
55 | */
56 | public function testUnknownClient()
57 | {
58 | $config = $this->createConfiguration(
59 | 'Misd\GuzzleBundle\Tests\Fixtures\Person',
60 | array('client' => 'unknown.client')
61 | );
62 | $this->converter->supports($config);
63 | }
64 |
65 | /**
66 | * @expectedException \LogicException
67 | */
68 | public function testCommandWithIndeterminableClient()
69 | {
70 | $config = $this->createConfiguration(
71 | 'Misd\GuzzleBundle\Tests\Fixtures\Person',
72 | array('command' => 'UnknownCommand')
73 | );
74 | $this->converter->supports($config);
75 | }
76 |
77 | public function testCommandWithSingleClient()
78 | {
79 | $class = get_class($this->converter);
80 | $converter = new $class;
81 | $converter->registerClient('with.description', self::getClient('JMSSerializerBundle'));
82 |
83 | $config = $this->createConfiguration(
84 | 'Misd\GuzzleBundle\Tests\Fixtures\Person',
85 | array('command' => 'GetPersonClass')
86 | );
87 |
88 | $this->assertTrue($converter->supports($config));
89 | }
90 |
91 | /**
92 | * @expectedException \LogicException
93 | * @expectedExceptionMessage Unknown command 'UnknownCommand' for client 'with.description'
94 | */
95 | public function testCommandWithSingleClientWithUnknownCommand()
96 | {
97 | $class = get_class($this->converter);
98 | $converter = new $class;
99 | $converter->registerClient('with.description', self::getClient('JMSSerializerBundle'));
100 |
101 | $config = $this->createConfiguration(
102 | 'Misd\GuzzleBundle\Tests\Fixtures\Person',
103 | array('command' => 'UnknownCommand')
104 | );
105 |
106 | $converter->supports($config);
107 | }
108 |
109 | /**
110 | * @expectedException \LogicException
111 | */
112 | public function testUnknownCommand()
113 | {
114 | $config = $this->createConfiguration(
115 | 'Misd\GuzzleBundle\Tests\Fixtures\Person',
116 | array('client' => 'with.description', 'command' => 'UnknownCommand')
117 | );
118 | $this->converter->supports($config);
119 | }
120 |
121 | /**
122 | * @expectedException \LogicException
123 | */
124 | public function testCommandReturnsWrongClass()
125 | {
126 | $config = $this->createConfiguration(
127 | __CLASS__,
128 | array('client' => 'with.description', 'command' => 'GetPersonClass')
129 | );
130 | $this->converter->supports($config);
131 | }
132 |
133 | public function testApply()
134 | {
135 | $request = new Request();
136 | $request->attributes->set('_route_params', array('id' => 1));
137 | $config = $this->createConfiguration('Misd\GuzzleBundle\Tests\Fixtures\Person', array(), 'arg');
138 |
139 | self::$mock->addResponse(self::response200());
140 |
141 | $this->converter->apply($request, $config);
142 |
143 | $this->assertInstanceOf('Misd\GuzzleBundle\Tests\Fixtures\Person', $request->attributes->get('arg'));
144 | }
145 |
146 | public function testApplyWithMappingAndExclude()
147 | {
148 | $request = new Request();
149 |
150 | $request->attributes->set('_route_params', array('real-id' => 2, 'id' => 1));
151 | $config = $this->createConfiguration(
152 | 'Misd\GuzzleBundle\Tests\Fixtures\Person',
153 | array('mapping' => array('real-id' => 'id'), 'exclude' => array('id')),
154 | 'arg'
155 | );
156 |
157 | self::$mock->addResponse(self::response200(2));
158 |
159 | $this->converter->apply($request, $config);
160 |
161 | $this->assertInstanceOf('Misd\GuzzleBundle\Tests\Fixtures\Person', $request->attributes->get('arg'));
162 | $this->assertEquals(2, $request->attributes->get('arg')->id);
163 | }
164 |
165 | /**
166 | * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
167 | */
168 | public function testApplyWith404()
169 | {
170 | $request = new Request();
171 | $request->attributes->set('_route_params', array('id' => 1));
172 | $config = $this->createConfiguration('Misd\GuzzleBundle\Tests\Fixtures\Person', array(), 'arg');
173 |
174 | self::$mock->addResponse(self::response404());
175 |
176 | $this->converter->apply($request, $config);
177 | }
178 |
179 | public function testApplyWith404WhenOptional()
180 | {
181 | $request = new Request();
182 | $request->attributes->set('_route_params', array('id' => 1));
183 | $config = $this->createConfiguration('Misd\GuzzleBundle\Tests\Fixtures\Person', array(), 'arg', true);
184 |
185 | self::$mock->addResponse(self::response404());
186 |
187 | $this->converter->apply($request, $config);
188 |
189 | $this->assertNull($request->attributes->get('arg'));
190 | }
191 |
192 | /**
193 | * @expectedException \Guzzle\Http\Exception\ServerErrorResponseException
194 | */
195 | public function testApplyWith500()
196 | {
197 | $request = new Request();
198 | $request->attributes->set('_route_params', array('id' => 1));
199 | $config = $this->createConfiguration('Misd\GuzzleBundle\Tests\Fixtures\Person', array(), 'arg');
200 |
201 | self::$mock->addResponse(self::response500());
202 |
203 | $this->converter->apply($request, $config);
204 | }
205 |
206 | public function testIgnoresNonServiceClients() {
207 | $paramConverter = self::getContainer('SensioFrameworkExtraBundle')->get('misd_guzzle.param_converter');
208 |
209 | $this->assertAttributeEmpty('clients', $paramConverter);
210 | }
211 |
212 | /**
213 | * @param null $class
214 | * @param array $options
215 | * @param string $name
216 | * @param bool $isOptional
217 | *
218 | * @return ConfigurationInterface
219 | */
220 | protected function createConfiguration($class = null, array $options = null, $name = 'arg', $isOptional = false)
221 | {
222 | $config = $this->getMockBuilder('Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter')
223 | ->disableOriginalConstructor()
224 | ->setMethods(
225 | array(
226 | 'getClass',
227 | 'getAliasName',
228 | 'getOptions',
229 | 'getName',
230 | 'isOptional',
231 | 'allowArray',
232 | )
233 | )
234 | ->getMock()
235 | ;
236 | if ($class !== null) {
237 | $config->expects($this->any())
238 | ->method('getClass')
239 | ->will($this->returnValue($class));
240 | }
241 | if ($options !== null) {
242 | $config->expects($this->any())
243 | ->method('getOptions')
244 | ->will($this->returnValue($options));
245 | }
246 | $config->expects($this->any())
247 | ->method('getName')
248 | ->will($this->returnValue($name));
249 | $config->expects($this->any())
250 | ->method('isOptional')
251 | ->will($this->returnValue($isOptional));
252 |
253 | return $config;
254 | }
255 |
256 | protected function response200($id = 1)
257 | {
258 | return Response::fromMessage(
259 | 'HTTP/1.1 200 OK
260 | Date: Wed, 25 Nov 2009 12:00:00 GMT
261 | Connection: close
262 | Server: Test
263 | Content-Type: application/xml
264 |
265 |
266 |
267 | Foo
268 | Bar
269 |
270 | '
271 | );
272 | }
273 |
274 | protected function response404()
275 | {
276 | return Response::fromMessage('HTTP/1.1 404 Not Found');
277 | }
278 |
279 | protected function response500()
280 | {
281 | return Response::fromMessage('HTTP/1.1 500 Internal Server Error');
282 | }
283 | }
284 |
--------------------------------------------------------------------------------
/Tests/DependencyInjection/ContainerTest.php:
--------------------------------------------------------------------------------
1 | getContainer(array($config));
25 |
26 | // core
27 |
28 | $this->assertTrue($container->has('guzzle.client'));
29 | $this->assertInstanceOf('Guzzle\Service\Client', $container->get('guzzle.client'));
30 | $this->assertTrue($container->getDefinition('guzzle.client')->hasTag('guzzle.client'));
31 |
32 | $this->assertTrue($container->hasParameter('guzzle.service_builder.class'));
33 | $this->assertTrue($container->has('guzzle.service_builder'));
34 | $this->assertInstanceOf(
35 | 'Guzzle\Service\Builder\ServiceBuilderInterface',
36 | $container->get('guzzle.service_builder')
37 | );
38 |
39 | $this->assertTrue($container->has('misd_guzzle.response.parser'));
40 | $this->assertInstanceOf(
41 | 'Misd\GuzzleBundle\Service\Command\LocationVisitor\Request\JMSSerializerBodyVisitor',
42 | $container->get('misd_guzzle.request.visitor.body')
43 | );
44 |
45 | $this->assertTrue($container->has('misd_guzzle.response.parser'));
46 | $this->assertInstanceOf(
47 | 'Misd\GuzzleBundle\Service\Command\JMSSerializerResponseParser',
48 | $container->get('misd_guzzle.response.parser')
49 | );
50 |
51 | $this->assertTrue($container->has('misd_guzzle.log.array'));
52 | $this->assertInstanceOf('Guzzle\Plugin\Log\LogPlugin', $container->get('misd_guzzle.log.array'));
53 | $this->assertTrue($container->getDefinition('misd_guzzle.log.array')->hasTag('misd_guzzle.plugin'));
54 |
55 | $this->assertTrue($container->has('misd_guzzle.log.adapter.array'));
56 | $this->assertInstanceOf('Guzzle\Log\ArrayLogAdapter', $container->get('misd_guzzle.log.adapter.array'));
57 |
58 | $this->assertTrue($container->has('misd_guzzle.data_collector'));
59 | $this->assertInstanceOf(
60 | 'Misd\GuzzleBundle\DataCollector\GuzzleDataCollector',
61 | $container->get('misd_guzzle.data_collector')
62 | );
63 | $this->assertTrue($container->getDefinition('misd_guzzle.data_collector')->hasTag('data_collector'));
64 |
65 | $this->assertTrue($container->has('misd_guzzle.listener.request_listener'));
66 | $this->assertInstanceOf(
67 | 'Misd\GuzzleBundle\EventListener\RequestListener',
68 | $container->get('misd_guzzle.listener.request_listener')
69 | );
70 | $this->assertTrue(
71 | $container->getDefinition('misd_guzzle.listener.request_listener')->hasTag('misd_guzzle.plugin')
72 | );
73 |
74 | $this->assertTrue($container->has('misd_guzzle.listener.command_listener'));
75 | $this->assertInstanceOf(
76 | 'Misd\GuzzleBundle\EventListener\CommandListener',
77 | $container->get('misd_guzzle.listener.command_listener')
78 | );
79 | $this->assertTrue(
80 | $container->getDefinition('misd_guzzle.listener.command_listener')->hasTag('misd_guzzle.plugin')
81 | );
82 |
83 | $this->assertTrue($container->has('misd_guzzle.param_converter'));
84 |
85 | // the misd_guzzle.param_converter implementation depends on the
86 | // version of the SensioFrameworkExtraBundle
87 | $parameter = new \ReflectionParameter(
88 | array(
89 | 'Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface',
90 | 'supports',
91 | ),
92 | 'configuration'
93 | );
94 | if ('Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter' == $parameter->getClass()->getName()) {
95 | $this->assertInstanceOf(
96 | 'Misd\GuzzleBundle\Request\ParamConverter\GuzzleParamConverter3x',
97 | $container->get('misd_guzzle.param_converter')
98 | );
99 | } else {
100 | $this->assertInstanceOf(
101 | 'Misd\GuzzleBundle\Request\ParamConverter\GuzzleParamConverter2x',
102 | $container->get('misd_guzzle.param_converter')
103 | );
104 | }
105 |
106 | // monolog
107 |
108 | $this->assertTrue($container->has('misd_guzzle.log.monolog'));
109 | $this->assertInstanceOf('Guzzle\Plugin\Log\LogPlugin', $container->get('misd_guzzle.log.monolog'));
110 | $this->assertTrue($container->getDefinition('misd_guzzle.log.monolog')->hasTag('misd_guzzle.plugin'));
111 | $this->assertEquals(MessageFormatter::DEFAULT_FORMAT, $container->getDefinition('misd_guzzle.log.monolog')->getArgument(1));
112 |
113 | $this->assertTrue($container->has('misd_guzzle.log.adapter.monolog'));
114 | $this->assertInstanceOf('Guzzle\Log\MonologLogAdapter', $container->get('misd_guzzle.log.adapter.monolog'));
115 | $this->assertTrue($container->getDefinition('misd_guzzle.log.adapter.monolog')->hasTag('monolog.logger'));
116 |
117 | // plugin
118 |
119 | $this->assertTrue($container->hasParameter('guzzle.plugin.async.class'));
120 | $this->assertEquals('Guzzle\Plugin\Async\AsyncPlugin', $container->getParameter('guzzle.plugin.async.class'));
121 |
122 | $this->assertTrue($container->hasParameter('guzzle.plugin.backoff.class'));
123 | $this->assertEquals(
124 | 'Guzzle\Plugin\Backoff\BackoffPlugin',
125 | $container->getParameter('guzzle.plugin.backoff.class')
126 | );
127 |
128 | $this->assertTrue($container->hasParameter('guzzle.plugin.cache.class'));
129 | $this->assertEquals('Guzzle\Plugin\Cache\CachePlugin', $container->getParameter('guzzle.plugin.cache.class'));
130 |
131 | $this->assertTrue($container->hasParameter('guzzle.plugin.cookie.class'));
132 | $this->assertEquals(
133 | 'Guzzle\Plugin\Cookie\CookiePlugin',
134 | $container->getParameter('guzzle.plugin.cookie.class')
135 | );
136 |
137 | $this->assertTrue($container->hasParameter('guzzle.plugin.curl_auth.class'));
138 | $this->assertEquals(
139 | 'Guzzle\Plugin\CurlAuth\CurlAuthPlugin',
140 | $container->getParameter('guzzle.plugin.curl_auth.class')
141 | );
142 |
143 | $this->assertTrue($container->hasParameter('guzzle.plugin.history.class'));
144 | $this->assertEquals(
145 | 'Guzzle\Plugin\History\HistoryPlugin',
146 | $container->getParameter('guzzle.plugin.history.class')
147 | );
148 |
149 | $this->assertTrue($container->hasParameter('guzzle.plugin.log.class'));
150 | $this->assertEquals('Guzzle\Plugin\Log\LogPlugin', $container->getParameter('guzzle.plugin.log.class'));
151 |
152 | $this->assertTrue($container->hasParameter('guzzle.plugin.md5_validator.class'));
153 | $this->assertEquals(
154 | 'Guzzle\Plugin\Md5\Md5ValidatorPlugin',
155 | $container->getParameter('guzzle.plugin.md5_validator.class')
156 | );
157 |
158 | $this->assertTrue($container->hasParameter('guzzle.plugin.command_content_md5.class'));
159 | $this->assertEquals(
160 | 'Guzzle\Plugin\Md5\CommandContentMd5Plugin',
161 | $container->getParameter('guzzle.plugin.command_content_md5.class')
162 | );
163 |
164 | $this->assertTrue($container->hasParameter('guzzle.plugin.mock.class'));
165 | $this->assertEquals('Guzzle\Plugin\Mock\MockPlugin', $container->getParameter('guzzle.plugin.mock.class'));
166 |
167 | $this->assertTrue($container->hasParameter('guzzle.plugin.oauth.class'));
168 | $this->assertEquals('Guzzle\Plugin\Oauth\OauthPlugin', $container->getParameter('guzzle.plugin.oauth.class'));
169 |
170 | // log
171 |
172 | $this->assertTrue($container->hasParameter('guzzle.log.adapter.monolog.class'));
173 | $this->assertEquals(
174 | 'Guzzle\Log\MonologLogAdapter',
175 | $container->getParameter('guzzle.log.adapter.monolog.class')
176 | );
177 |
178 | $this->assertTrue($container->hasParameter('guzzle.log.adapter.array.class'));
179 | $this->assertEquals('Guzzle\Log\ArrayLogAdapter', $container->getParameter('guzzle.log.adapter.array.class'));
180 |
181 | // cache
182 |
183 | $this->assertTrue($container->hasParameter('guzzle.cache.doctrine.class'));
184 | $this->assertEquals(
185 | 'Guzzle\Cache\DoctrineCacheAdapter',
186 | $container->getParameter('guzzle.cache.doctrine.class')
187 | );
188 |
189 | $this->assertTrue($container->hasParameter('guzzle.cache.doctrine.filesystem.class'));
190 | $this->assertEquals(
191 | 'Doctrine\Common\Cache\FilesystemCache',
192 | $container->getParameter('guzzle.cache.doctrine.filesystem.class')
193 | );
194 |
195 | $this->assertTrue($container->has('misd_guzzle.cache.doctrine.filesystem.adapter'));
196 | $this->assertInstanceOf(
197 | 'Doctrine\Common\Cache\FilesystemCache',
198 | $container->get('misd_guzzle.cache.doctrine.filesystem.adapter')
199 | );
200 |
201 | $this->assertTrue($container->has('misd_guzzle.cache.doctrine.filesystem'));
202 | $this->assertInstanceOf(
203 | 'Guzzle\Cache\DoctrineCacheAdapter',
204 | $container->get('misd_guzzle.cache.doctrine.filesystem')
205 | );
206 |
207 | $this->assertTrue($container->has('misd_guzzle.cache.filesystem'));
208 | $this->assertInstanceOf('Guzzle\Plugin\Cache\CachePlugin', $container->get('misd_guzzle.cache.filesystem'));
209 | }
210 |
211 | public function configs()
212 | {
213 | $configs = array();
214 |
215 | $configs[] = array();
216 | $configs[] = array('guzzle' => array('service_builder' => array('class' => 'Misd\GuzzleBundle\Tests\Fixtures\ServiceBuilder')));
217 | $configs[] = array('guzzle' => array('service_builder' => array('configuration_file' => '%kernel.root_dir%/config/alt-webservices.json')));
218 |
219 | return $configs;
220 | }
221 |
222 | /**
223 | * @dataProvider logFormats
224 | */
225 | public function testLogFormat($parameter, $format = null)
226 | {
227 | $container = $this->getContainer(array(array('log' => array('format' => $parameter))));
228 | $this->assertEquals($format ?: $parameter, $container->getDefinition('misd_guzzle.log.monolog')->getArgument(1));
229 | }
230 |
231 | public function logFormats()
232 | {
233 | return array(
234 | array('debug', MessageFormatter::DEBUG_FORMAT),
235 | array('short', MessageFormatter::SHORT_FORMAT),
236 | array('foo bar baz')
237 | );
238 | }
239 | }
240 |
--------------------------------------------------------------------------------