Open Journal Systems  3.3.0
OperationTest.php
1 <?php
2 
4 
8 
13 {
14  public static function strtoupper($string)
15  {
16  return strtoupper($string);
17  }
18 
19  public function testOperationIsDataObject()
20  {
21  $c = new Operation(array(
22  'name' => 'test',
23  'summary' => 'doc',
24  'notes' => 'notes',
25  'documentationUrl' => 'http://www.example.com',
26  'httpMethod' => 'POST',
27  'uri' => '/api/v1',
28  'responseClass' => 'array',
29  'responseNotes' => 'returns the json_decoded response',
30  'deprecated' => true,
31  'parameters' => array(
32  'key' => array(
33  'required' => true,
34  'type' => 'string',
35  'maxLength' => 10
36  ),
37  'key_2' => array(
38  'required' => true,
39  'type' => 'integer',
40  'default' => 10
41  )
42  )
43  ));
44 
45  $this->assertEquals('test', $c->getName());
46  $this->assertEquals('doc', $c->getSummary());
47  $this->assertEquals('http://www.example.com', $c->getDocumentationUrl());
48  $this->assertEquals('POST', $c->getHttpMethod());
49  $this->assertEquals('/api/v1', $c->getUri());
50  $this->assertEquals('array', $c->getResponseClass());
51  $this->assertEquals('returns the json_decoded response', $c->getResponseNotes());
52  $this->assertTrue($c->getDeprecated());
53  $this->assertEquals('Guzzle\\Service\\Command\\OperationCommand', $c->getClass());
54  $this->assertEquals(array(
55  'key' => new Parameter(array(
56  'name' => 'key',
57  'required' => true,
58  'type' => 'string',
59  'maxLength' => 10,
60  'parent' => $c
61  )),
62  'key_2' => new Parameter(array(
63  'name' => 'key_2',
64  'required' => true,
65  'type' => 'integer',
66  'default' => 10,
67  'parent' => $c
68  ))
69  ), $c->getParams());
70 
71  $this->assertEquals(new Parameter(array(
72  'name' => 'key_2',
73  'required' => true,
74  'type' => 'integer',
75  'default' => 10,
76  'parent' => $c
77  )), $c->getParam('key_2'));
78 
79  $this->assertNull($c->getParam('afefwef'));
80  $this->assertArrayNotHasKey('parent', $c->getParam('key_2')->toArray());
81  }
82 
83  public function testAllowsConcreteCommands()
84  {
85  $c = new Operation(array(
86  'name' => 'test',
87  'class' => 'Guzzle\\Service\\Command\ClosureCommand',
88  'parameters' => array(
89  'p' => new Parameter(array(
90  'name' => 'foo'
91  ))
92  )
93  ));
94  $this->assertEquals('Guzzle\\Service\\Command\ClosureCommand', $c->getClass());
95  }
96 
97  public function testConvertsToArray()
98  {
99  $data = array(
100  'name' => 'test',
101  'class' => 'Guzzle\\Service\\Command\ClosureCommand',
102  'summary' => 'test',
103  'documentationUrl' => 'http://www.example.com',
104  'httpMethod' => 'PUT',
105  'uri' => '/',
106  'parameters' => array('p' => array('name' => 'foo'))
107  );
108  $c = new Operation($data);
109  $toArray = $c->toArray();
110  unset($data['name']);
111  $this->assertArrayHasKey('parameters', $toArray);
112  $this->assertInternalType('array', $toArray['parameters']);
113 
114  // Normalize the array
115  unset($data['parameters']);
116  unset($toArray['parameters']);
117 
118  $data['responseType'] = 'primitive';
119  $data['responseClass'] = 'array';
120  $this->assertEquals($data, $toArray);
121  }
122 
123  public function testDeterminesIfHasParam()
124  {
125  $command = $this->getTestCommand();
126  $this->assertTrue($command->hasParam('data'));
127  $this->assertFalse($command->hasParam('baz'));
128  }
129 
130  public function testReturnsParamNames()
131  {
132  $command = $this->getTestCommand();
133  $this->assertEquals(array('data'), $command->getParamNames());
134  }
135 
136  protected function getTestCommand()
137  {
138  return new Operation(array(
139  'parameters' => array(
140  'data' => new Parameter(array(
141  'type' => 'string'
142  ))
143  )
144  ));
145  }
146 
147  public function testCanBuildUpCommands()
148  {
149  $c = new Operation(array());
150  $c->setName('foo')
151  ->setClass('Baz')
152  ->setDeprecated(false)
153  ->setSummary('summary')
154  ->setDocumentationUrl('http://www.foo.com')
155  ->setHttpMethod('PUT')
156  ->setResponseNotes('oh')
157  ->setResponseClass('string')
158  ->setUri('/foo/bar')
159  ->addParam(new Parameter(array(
160  'name' => 'test'
161  )));
162 
163  $this->assertEquals('foo', $c->getName());
164  $this->assertEquals('Baz', $c->getClass());
165  $this->assertEquals(false, $c->getDeprecated());
166  $this->assertEquals('summary', $c->getSummary());
167  $this->assertEquals('http://www.foo.com', $c->getDocumentationUrl());
168  $this->assertEquals('PUT', $c->getHttpMethod());
169  $this->assertEquals('oh', $c->getResponseNotes());
170  $this->assertEquals('string', $c->getResponseClass());
171  $this->assertEquals('/foo/bar', $c->getUri());
172  $this->assertEquals(array('test'), $c->getParamNames());
173  }
174 
175  public function testCanRemoveParams()
176  {
177  $c = new Operation(array());
178  $c->addParam(new Parameter(array('name' => 'foo')));
179  $this->assertTrue($c->hasParam('foo'));
180  $c->removeParam('foo');
181  $this->assertFalse($c->hasParam('foo'));
182  }
183 
185  {
186  $command = new Operation(array('parameters' => array('foo' => new Parameter(array()))));
187  $this->assertEquals('foo', $command->getParam('foo')->getName());
188  }
189 
191  {
192  $command = $this->getOperation();
193  $this->assertEquals(1, count($command->getErrorResponses()));
194  $arr = $command->toArray();
195  $this->assertEquals(1, count($arr['errorResponses']));
196  $command->addErrorResponse(400, 'Foo', 'Baz\\Bar');
197  $this->assertEquals(2, count($command->getErrorResponses()));
198  $command->setErrorResponses(array());
199  $this->assertEquals(0, count($command->getErrorResponses()));
200  }
201 
202  public function testHasNotes()
203  {
204  $o = new Operation(array('notes' => 'foo'));
205  $this->assertEquals('foo', $o->getNotes());
206  $o->setNotes('bar');
207  $this->assertEquals('bar', $o->getNotes());
208  }
209 
210  public function testHasData()
211  {
212  $o = new Operation(array('data' => array('foo' => 'baz', 'bar' => 123)));
213  $o->setData('test', false);
214  $this->assertEquals('baz', $o->getData('foo'));
215  $this->assertEquals(123, $o->getData('bar'));
216  $this->assertNull($o->getData('wfefwe'));
217  $this->assertEquals(array(
218  'parameters' => array(),
219  'class' => 'Guzzle\Service\Command\OperationCommand',
220  'data' => array('foo' => 'baz', 'bar' => 123, 'test' => false),
221  'responseClass' => 'array',
222  'responseType' => 'primitive'
223  ), $o->toArray());
224  }
225 
226  public function testHasServiceDescription()
227  {
228  $s = new ServiceDescription();
229  $o = new Operation(array(), $s);
230  $this->assertSame($s, $o->getServiceDescription());
231  }
232 
236  public function testValidatesResponseType()
237  {
238  $o = new Operation(array('responseClass' => 'array', 'responseType' => 'foo'));
239  }
240 
241  public function testInfersResponseType()
242  {
243  $o = $this->getOperation();
244  $o->setServiceDescription(new ServiceDescription(array('models' => array('Foo' => array()))));
245  $this->assertEquals('primitive', $o->getResponseType());
246  $this->assertEquals('primitive', $o->setResponseClass('boolean')->getResponseType());
247  $this->assertEquals('primitive', $o->setResponseClass('array')->getResponseType());
248  $this->assertEquals('primitive', $o->setResponseClass('integer')->getResponseType());
249  $this->assertEquals('primitive', $o->setResponseClass('string')->getResponseType());
250  $this->assertEquals('class', $o->setResponseClass('foo')->getResponseType());
251  $this->assertEquals('class', $o->setResponseClass(__CLASS__)->getResponseType());
252  $this->assertEquals('model', $o->setResponseClass('Foo')->getResponseType());
253  }
254 
255  public function testHasResponseType()
256  {
257  // infers in the constructor
258  $o = new Operation(array('responseClass' => 'array'));
259  $this->assertEquals('primitive', $o->getResponseType());
260  // Infers when set
261  $o = new Operation();
262  $this->assertEquals('primitive', $o->getResponseType());
263  $this->assertEquals('model', $o->setResponseType('model')->getResponseType());
264  }
265 
266  public function testHasAdditionalParameters()
267  {
268  $o = new Operation(array(
269  'additionalParameters' => array(
270  'type' => 'string', 'name' => 'binks'
271  ),
272  'parameters' => array(
273  'foo' => array('type' => 'integer')
274  )
275  ));
276  $this->assertEquals('string', $o->getAdditionalParameters()->getType());
277  $arr = $o->toArray();
278  $this->assertEquals(array(
279  'type' => 'string'
280  ), $arr['additionalParameters']);
281  }
282 
286  protected function getOperation()
287  {
288  return new Operation(array(
289  'name' => 'OperationTest',
290  'class' => get_class($this),
291  'parameters' => array(
292  'test' => array('type' => 'object'),
293  'bool_1' => array('default' => true, 'type' => 'boolean'),
294  'bool_2' => array('default' => false),
295  'float' => array('type' => 'numeric'),
296  'int' => array('type' => 'integer'),
297  'date' => array('type' => 'string'),
298  'timestamp' => array('type' => 'string'),
299  'string' => array('type' => 'string'),
300  'username' => array('type' => 'string', 'required' => true, 'filters' => 'strtolower'),
301  'test_function' => array('type' => 'string', 'filters' => __CLASS__ . '::strtoupper')
302  ),
303  'errorResponses' => array(
304  array('code' => 503, 'reason' => 'InsufficientCapacity', 'class' => 'Guzzle\\Exception\\RuntimeException')
305  )
306  ));
307  }
308 }
Guzzle\Tests\Service\Description\OperationTest\testDeterminesIfHasParam
testDeterminesIfHasParam()
Definition: OperationTest.php:123
Guzzle\Tests\Service\Description\OperationTest\strtoupper
static strtoupper($string)
Definition: OperationTest.php:14
Guzzle\Tests\Service\Description\OperationTest\testInfersResponseType
testInfersResponseType()
Definition: OperationTest.php:241
Guzzle\Service\Description\Operation
Definition: Operation.php:10
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Tests\Service\Description\OperationTest\testOperationIsDataObject
testOperationIsDataObject()
Definition: OperationTest.php:19
Guzzle\Tests\Service\Description\OperationTest\testAddsNameToParametersIfNeeded
testAddsNameToParametersIfNeeded()
Definition: OperationTest.php:184
Guzzle\Tests\Service\Description\OperationTest
Definition: OperationTest.php:12
Guzzle\Tests\Service\Description\OperationTest\testHasNotes
testHasNotes()
Definition: OperationTest.php:202
Guzzle\Tests\Service\Description\OperationTest\testContainsApiErrorInformation
testContainsApiErrorInformation()
Definition: OperationTest.php:190
Guzzle\Tests\Service\Description\OperationTest\testValidatesResponseType
testValidatesResponseType()
Definition: OperationTest.php:236
Guzzle\Tests\Service\Description\OperationTest\testReturnsParamNames
testReturnsParamNames()
Definition: OperationTest.php:130
Guzzle\Service\Description\ServiceDescription
Definition: ServiceDescription.php:11
Guzzle\Tests\Service\Description
Definition: OperationTest.php:3
Guzzle\Tests\Service\Description\OperationTest\testHasResponseType
testHasResponseType()
Definition: OperationTest.php:255
Guzzle\Tests\Service\Description\OperationTest\getOperation
getOperation()
Definition: OperationTest.php:286
Guzzle\Tests\Service\Description\OperationTest\testCanRemoveParams
testCanRemoveParams()
Definition: OperationTest.php:175
Guzzle\Tests\Service\Description\OperationTest\testHasAdditionalParameters
testHasAdditionalParameters()
Definition: OperationTest.php:266
Guzzle\Tests\Service\Description\OperationTest\testCanBuildUpCommands
testCanBuildUpCommands()
Definition: OperationTest.php:147
Guzzle\Tests\Service\Description\OperationTest\testAllowsConcreteCommands
testAllowsConcreteCommands()
Definition: OperationTest.php:83
Guzzle\Tests\Service\Description\OperationTest\testConvertsToArray
testConvertsToArray()
Definition: OperationTest.php:97
Guzzle\Tests\Service\Description\OperationTest\testHasServiceDescription
testHasServiceDescription()
Definition: OperationTest.php:226
Guzzle\Tests\Service\Description\OperationTest\testHasData
testHasData()
Definition: OperationTest.php:210
Guzzle\Tests\Service\Description\OperationTest\getTestCommand
getTestCommand()
Definition: OperationTest.php:136