/
callHelperTest.php
307 lines (256 loc) · 9.94 KB
/
callHelperTest.php
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
<?php
/*
* Mashape PHP library.
*
* Copyright (C) 2011 Mashape, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* The author of this software is Mashape, Inc.
* For any question or feedback please contact us at: support@mashape.com
*
*/
require_once(MASHAPE_LIBRAY_PATH . "/configuration/restMethod.php");
require_once(MASHAPE_LIBRAY_PATH . "/mashape.php");
require_once(MASHAPE_LIBRAY_PATH . "/methods/call/helpers/callHelper.php");
define("SERVER_KEY", "serverkey");
class CallTest extends PHPUnit_Framework_TestCase
{
function testNull() {
// Test NULL Simple result
$method = new RESTMethod();
$method->setName("touchNull");
$method->setResult("message");
$this->assertEquals('{"errors":[],"result":{"message":null}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
// Test NULL Object
$method = new RESTMethod();
$method->setName("touchNull");
$method->setObject("Ret");
$this->assertEquals('{"errors":[],"result":null}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
}
function testSimple() {
$method = new RESTMethod();
$method->setName("touchSimple");
$method->setResult("message");
$this->assertEquals('{"errors":[],"result":{"message":"simpleValue"}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
$method = new RESTMethod();
$method->setName("touchSimpleArray");
$method->setResult("message");
$method->setArray(true);
$this->assertEquals('{"errors":[],"result":{"message":["value1",3,"value3"]}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
}
function testComplex() {
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test.xml");
$method = new RESTMethod();
$method->setName("touchComplex");
$method->setObject("ClassOne");
$this->assertEquals('{"errors":[],"result":{"field1":"value1","field2":"value2"}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test2.xml");
$method = new RESTMethod();
$method->setName("touchComplex2");
$method->setObject("ClassOne");
$this->assertEquals('{"errors":[],"result":{"field2":"value2"}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test3.xml");
$method = new RESTMethod();
$method->setName("touchComplex3");
$method->setObject("ClassOne");
$this->assertEquals('{"errors":[],"result":{"field1":"value1","field2":{"childField1":"child value 1","childField2":"child value 2"}}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test3.xml");
$method = new RESTMethod();
$method->setName("touchComplex4");
$method->setObject("ClassOne");
$this->assertEquals('{"errors":[],"result":{"field1":"value1","field2":null}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test4.xml");
$method = new RESTMethod();
$method->setName("touchComplex5");
$method->setObject("ClassOne");
$this->assertEquals('{"errors":[],"result":{"field1":["value1",3,"value3"]}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test5.xml");
$method = new RESTMethod();
$method->setName("touchComplex6");
$method->setObject("ClassOne");
$this->assertEquals('{"errors":[],"result":{"field1":[{"childField1":"child value 1","childField2":"child value 2"},{"childField1":"second child value 1","childField2":"second child value 2"}]}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test6.xml");
$method = new RESTMethod();
$method->setName("touchComplex7");
$method->setObject("ClassThree");
$this->assertEquals('{"errors":[],"result":{"field1":"this is field1","field2":["this","is","field",2,true],"field4":[{"field1":"child value 1","field2":"child value 2"},{"field1":"second child value 1","field2":"second child value 2"}]}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test.xml");
$method = new RESTMethod();
$method->setName("touchComplex8");
$method->setObject("ClassOne");
$method->setArray(true);
$this->assertEquals('{"errors":[],"result":[{"field1":"value1","field2":"value2"},{"field1":"second value1","field2":"second value2"},{"field1":"third value1","field2":"third value2"}]}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
$method = new RESTMethod();
$method->setName("touchComplex9");
$method->setArray(true);
$method->setResult("val");
$this->assertEquals('{"errors":[],"result":{"val":["ciao","marco",false]}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
$method = new RESTMethod();
$method->setName("touchComplex10");
$method->setArray(true);
$method->setResult("val");
$this->assertEquals('{"errors":[],"result":{"val":{"key1":"value1","key2":"value2","key3":{"nested1":"nv1","nested2":"nv2"}}}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
$method = new RESTMethod();
$method->setName("touchComplex11");
$method->setArray(true);
$method->setResult("val");
$this->assertEquals('{"errors":[],"result":{"val":{"key1":"value1","key2":"value2","key3":{"nested1":"nv1","nested2":{"yo1":"vyo1","yo2":[1,2,3]}}}}}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
}
function testError() {
RESTConfigurationLoader::reloadConfiguration(SERVER_KEY, dirname(__FILE__) . "/test7.xml");
$method = new RESTMethod();
$method->setName("touchError");
$method->setObject("ClassOne");
$method->setArray(true);
$this->assertEquals('{"errors":[{"code":1,"message":"custom message"}],"result":null}', doCall($method, null, new NewSampleAPI(), SERVER_KEY));
}
}
class ClassOne {
public $field1;
public $field2;
}
class ClassTwo {
public $childField1;
public $childField2;
}
class ClassThree {
public $f1;
public $f2;
public $f3;
public $f4;
public function getField1() {
return $this->f1;
}
public function getField2() {
return $this->f2;
}
public function getField3() {
return $this->f3;
}
public function getField4() {
return $this->f4;
}
}
class NewSampleAPI extends MashapeRestAPI {
// Don't edit the constructor code
public function __construct() {
parent::__construct(dirname(__FILE__));
}
public function touchNull() {
return null;
}
public function touchSimple() {
return "simpleValue";
}
public function touchSimpleArray() {
$result = array("value1", 3, "value3");
return $result;
}
public function touchComplex() {
$result = new ClassOne();
$result->field1 = "value1";
$result->field2 = "value2";
return $result;
}
public function touchComplex2() {
$result = new ClassOne();
$result->field1 = null;
$result->field2 = "value2";
return $result;
}
public function touchComplex3() {
$result = new ClassOne();
$result->field1 = "value1";
$child = new ClassTwo();
$child->childField1 = "child value 1";
$child->childField2 = "child value 2";
$result->field2 = $child;
return $result;
}
public function touchComplex4() {
$result = new ClassOne();
$result->field1 = "value1";
$result->field2 = null;
return $result;
}
public function touchComplex5() {
$result = new ClassOne();
$result->field1 = array("value1", 3, "value3");
$result->field2 = null;
return $result;
}
public function touchComplex6() {
$result = new ClassOne();
$childs = array();
$child = new ClassTwo();
$child->childField1 = "child value 1";
$child->childField2 = "child value 2";
array_push($childs, $child);
$child = new ClassTwo();
$child->childField1 = "second child value 1";
$child->childField2 = "second child value 2";
array_push($childs, $child);
$result->field1 = $childs;
$result->field2 = null;
return $result;
}
public function touchComplex7() {
$result = new ClassThree();
$result->f1 = "this is field1";
$result->f2 = array("this", "is", "field", 2, true);
$result->f3 = null;
$childs = array();
$child = new ClassOne();
$child->field1 = "child value 1";
$child->field2 = "child value 2";
array_push($childs, $child);
$child = new ClassOne();
$child->field1 = "second child value 1";
$child->field2 = "second child value 2";
array_push($childs, $child);
$result->f4 = $childs;
return $result;
}
public function touchComplex8() {
$result = array();
$obj = new ClassOne();
$obj->field1 = "value1";
$obj->field2 = "value2";
array_push($result, $obj);
$obj = new ClassOne();
$obj->field1 = "second value1";
$obj->field2 = "second value2";
array_push($result, $obj);
$obj = new ClassOne();
$obj->field1 = "third value1";
$obj->field2 = "third value2";
array_push($result, $obj);
return $result;
}
public function touchComplex9() {
return array("ciao", "marco", false);
}
public function touchComplex10() {
return array("key1"=>"value1", "key2"=>"value2", "key3"=>array("nested1"=>"nv1", "nested2"=>"nv2"));
}
public function touchComplex11() {
return array("key1"=>"value1", "key2"=>"value2", "key3"=>array("nested1"=>"nv1", "nested2"=>array("yo1"=>"vyo1", "yo2"=>array(1,2,3))));
}
public function touchError() {
parent::addError(1, "custom message");
return null;
}
}