Commit | Line | Data |
---|---|---|
70be69e2 TO |
1 | <?php |
2 | namespace Civi\API; | |
3 | ||
4 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
5 | ||
6 | /** | |
7 | */ | |
d3159a21 | 8 | class RequestTest extends \CiviUnitTestCase { |
70be69e2 | 9 | |
e9479dcf EM |
10 | /** |
11 | * @return array | |
12 | */ | |
00be9182 | 13 | public function v4options() { |
70be69e2 TO |
14 | $cases = array(); // array(0 => $requestParams, 1 => $expectedOptions, 2 => $expectedData, 3 => $expectedChains) |
15 | $cases[] = array( | |
16 | array('version' => 4), // requestParams | |
17 | array(), // expectedOptions | |
18 | array(), // expectedData | |
19 | array(), // expectedChains | |
20 | ); | |
21 | $cases[] = array( | |
22 | array('version' => 4, 'debug' => TRUE), // requestParams | |
23 | array('debug' => TRUE), // expectedOptions | |
24 | array(), // expectedData | |
25 | array(), // expectedChains | |
26 | ); | |
27 | $cases[] = array( | |
28 | array('version' => 4, 'format.is_success' => TRUE), // requestParams | |
29 | array('format' => 'is_success'), // expectedOptions | |
30 | array(), // expectedData | |
31 | array(), // expectedChains | |
32 | ); | |
33 | $cases[] = array( | |
92915c55 TO |
34 | array( |
35 | 'version' => 4, | |
36 | 'option.limit' => 15, | |
37 | 'option.foo' => array('bar'), | |
38 | 'options' => array('whiz' => 'bang'), | |
389bcebf | 39 | 'optionnotreally' => 'data', |
92915c55 | 40 | ), // requestParams |
70be69e2 TO |
41 | array('limit' => 15, 'foo' => array('bar'), 'whiz' => 'bang'), // expectedOptions |
42 | array('optionnotreally' => 'data'), // expectedData | |
43 | array(), // expectedChains | |
44 | ); | |
45 | $cases[] = array( | |
92915c55 TO |
46 | array( |
47 | 'version' => 4, | |
48 | 'return' => array('field1', 'field2'), | |
49 | 'return.field3' => 1, | |
50 | 'return.field4' => 0, | |
389bcebf | 51 | 'returnontreally' => 'data', |
92915c55 | 52 | ), // requestParams |
70be69e2 TO |
53 | array('return' => array('field1', 'field2', 'field3')), // expectedOptions |
54 | array('returnontreally' => 'data'), // expectedData | |
55 | array(), // expectedChains | |
56 | ); | |
57 | $cases[] = array( | |
58 | array('version' => 4, 'foo' => array('bar'), 'whiz' => 'bang'), // requestParams | |
59 | array(), // expectedOptions | |
60 | array('foo' => array('bar'), 'whiz' => 'bang'), // expectedData | |
61 | array(), // expectedChains | |
62 | ); | |
63 | $cases[] = array( | |
64 | array('version' => 4, 'api.foo.bar' => array('whiz' => 'bang')), // requestParams | |
65 | array(), // expectedOptions | |
66 | array(), // expectedData | |
67 | array('api.foo.bar' => array('whiz' => 'bang')), // expectedChains | |
68 | ); | |
69 | $cases[] = array( | |
70 | array( | |
71 | 'version' => 4, | |
72 | 'option.limit' => 15, | |
73 | 'options' => array('whiz' => 'bang'), | |
74 | 'somedata' => 'data', | |
75 | 'moredata' => array('woosh'), | |
76 | 'return.field1' => 1, | |
77 | 'return' => array('field2'), | |
78 | 'api.first' => array('the first call'), | |
79 | 'api.second' => array('the second call'), | |
80 | ), // requestParams | |
81 | array('limit' => 15, 'whiz' => 'bang', 'return' => array('field1', 'field2')), // expectedOptions | |
82 | array('somedata' => 'data', 'moredata' => array('woosh')), // expectedData | |
83 | array('api.first' => array('the first call'), 'api.second' => array('the second call')), // expectedChains | |
84 | ); | |
85 | return $cases; | |
86 | } | |
87 | ||
88 | /** | |
100fef9d | 89 | * @param array $inputParams |
70be69e2 TO |
90 | * @param $expectedOptions |
91 | * @param $expectedData | |
92 | * @param $expectedChains | |
93 | * @dataProvider v4options | |
94 | */ | |
00be9182 | 95 | public function testCreateRequest_v4Options($inputParams, $expectedOptions, $expectedData, $expectedChains) { |
d3159a21 | 96 | $apiRequest = Request::create('MyEntity', 'MyAction', $inputParams, NULL); |
70be69e2 TO |
97 | $this->assertEquals($expectedOptions, $apiRequest['options']->getArray()); |
98 | $this->assertEquals($expectedData, $apiRequest['data']->getArray()); | |
99 | $this->assertEquals($expectedChains, $apiRequest['chains']); | |
100 | } | |
101 | ||
102 | /** | |
103 | * @expectedException \API_Exception | |
104 | */ | |
00be9182 | 105 | public function testCreateRequest_v4BadEntity() { |
d3159a21 | 106 | Request::create('Not!Valid', 'create', array('version' => 4), NULL); |
70be69e2 TO |
107 | } |
108 | ||
109 | /** | |
110 | * @expectedException \API_Exception | |
111 | */ | |
00be9182 | 112 | public function testCreateRequest_v4BadAction() { |
d3159a21 | 113 | Request::create('MyEntity', 'bad!action', array('version' => 4), NULL); |
70be69e2 | 114 | } |
d3159a21 | 115 | |
4cbe18b8 EM |
116 | /** |
117 | * @return array | |
118 | */ | |
00be9182 | 119 | public function validEntityActionPairs() { |
d3159a21 TO |
120 | $cases = array(); |
121 | $cases[] = array( | |
122 | array('MyEntity', 'MyAction', 3), | |
6730d7a1 | 123 | array('MyEntity', 'myaction', 3), |
d3159a21 TO |
124 | ); |
125 | $cases[] = array( | |
126 | array('my+entity', 'MyAction', 3), | |
6730d7a1 CW |
127 | array('MyEntity', 'myaction', 3), |
128 | ); | |
129 | $cases[] = array( | |
130 | array('my entity with under_scores', 'My_Action', 3), | |
131 | array('MyEntityWithUnderScores', 'my_action', 3), | |
132 | ); | |
133 | $cases[] = array( | |
134 | array('u_f_match', 'get Something', 3), | |
135 | array('UFMatch', 'get_something', 3), | |
d3159a21 TO |
136 | ); |
137 | $cases[] = array( | |
138 | array('MyEntity', 'MyAction', 4), | |
139 | array('MyEntity', 'myAction', 4), | |
140 | ); | |
141 | return $cases; | |
142 | } | |
143 | ||
144 | /** | |
145 | * @dataProvider validEntityActionPairs | |
1e1fdcf6 EM |
146 | * @param $input |
147 | * @param $expected | |
148 | * @throws \API_Exception | |
d3159a21 | 149 | */ |
00be9182 | 150 | public function testCreateRequest_EntityActionMunging($input, $expected) { |
d3159a21 TO |
151 | list ($inEntity, $inAction, $inVersion) = $input; |
152 | $apiRequest = Request::create($inEntity, $inAction, array('version' => $inVersion), NULL); | |
153 | $this->assertEquals($expected, array($apiRequest['entity'], $apiRequest['action'], $apiRequest['version'])); | |
154 | } | |
155 | ||
4cbe18b8 EM |
156 | /** |
157 | * @return array | |
158 | */ | |
00be9182 | 159 | public function invalidEntityActionPairs() { |
d3159a21 TO |
160 | $cases = array(); |
161 | $cases[] = array('My+Entity', 'MyAction', 4); | |
162 | $cases[] = array('My Entity', 'MyAction', 4); | |
163 | $cases[] = array('2MyEntity', 'MyAction', 4); | |
164 | $cases[] = array('MyEntity', 'My+Action', 4); | |
165 | $cases[] = array('MyEntity', 'My Action', 4); | |
166 | $cases[] = array('MyEntity', '2Action', 4); | |
167 | return $cases; | |
168 | } | |
169 | ||
170 | /** | |
171 | * @dataProvider invalidEntityActionPairs | |
172 | * @expectedException \API_Exception | |
1e1fdcf6 EM |
173 | * @param $inEntity |
174 | * @param $inAction | |
175 | * @param $inVersion | |
176 | * @throws \API_Exception | |
d3159a21 | 177 | */ |
00be9182 | 178 | public function testCreateRequest_InvalidEntityAction($inEntity, $inAction, $inVersion) { |
d3159a21 TO |
179 | Request::create($inEntity, $inAction, array('version' => $inVersion), NULL); |
180 | } | |
181 | ||
4cbe18b8 | 182 | } |