-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathcsrest_test.php
More file actions
273 lines (209 loc) · 10.8 KB
/
csrest_test.php
File metadata and controls
273 lines (209 loc) · 10.8 KB
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
<?php
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../vendor/simpletest/simpletest/autorun.php';
require_once(__DIR__.'/../vendor/simpletest/simpletest/mock_objects.php');
@Mock::generate('CS_REST_Log');
@Mock::generate('CS_REST_NativeJsonSerialiser');
@Mock::generate('CS_REST_CurlTransport');
class CS_REST_TestBase extends UnitTestCase {
var $mock_log;
var $mock_serialiser;
var $mock_transport;
var $wrapper;
var $serialisation_type = 'mockjson';
var $transport_type = 'mock_cURL';
var $auth = NULL;
var $protocol = 'hotpotatoes';
var $api_host = 'api.test.createsend.com';
var $log_level = CS_REST_LOG_NONE;
var $base_route;
function setUp() {
$this->mock_log = new MockCS_REST_Log();
$this->mock_serialiser = new MockCS_REST_NativeJsonSerialiser();
$this->mock_transport = new MockCS_REST_CurlTransport();
$this->mock_transport->setReturnValue('get_type', $this->transport_type);
$this->mock_serialiser->setReturnValue('get_type', $this->serialisation_type);
$this->base_route = $this->protocol.'://'.$this->api_host.'/api/v3.1/';
$this->set_up_inner();
}
function set_up_inner() {
$this->wrapper = new CS_REST_General($this->auth, $this->protocol, $this->log_level,
$this->api_host, $this->mock_log, $this->mock_serialiser, $this->mock_transport);
}
function get_call_options($route, $method = 'GET') {
return array (
'authdetails' => $this->auth,
'userAgent' => 'CS_REST_Wrapper v'.CS_REST_WRAPPER_VERSION.
' PHPv'.phpversion().' over '.$this->transport_type.' with '.$this->serialisation_type,
'contentType' => 'application/json; charset=utf-8',
'deserialise' => true,
'host' => $this->api_host,
'protocol' => $this->protocol,
'route' => $route,
'method' => $method
);
}
function setup_transport_and_serialisation($make_call_result, $call_options,
$deserialise_result, $deserialise_input, $serialise_result = NULL, $serialise_input = NULL) {
$this->mock_transport->setReturnValue('make_call', $make_call_result);
$this->mock_transport->expectOnce('make_call', array(new IdenticalExpectation($call_options)));
$this->mock_serialiser->setReturnValue('deserialise', $deserialise_result);
$this->mock_serialiser->expectOnce('deserialise', array(new IdenticalExpectation($deserialise_input)));
if(!is_null($serialise_result)) {
$this->mock_serialiser->setReturnValue('serialise', $serialise_result);
$this->mock_serialiser->expectOnce('serialise', array(new IdenticalExpectation($serialise_input)));
}
}
function general_test($wrapper_function, $call_options, $from_transport,
$from_deserialisation, $response_code = 200) {
$transport_result = array (
'code' => $response_code,
'response' => $from_transport
);
$expected_result = new CS_REST_Wrapper_Result($from_deserialisation, $response_code);
$this->setup_transport_and_serialisation($transport_result, $call_options,
$from_deserialisation, $from_transport, NULL, NULL, $response_code);
$result = $this->wrapper->$wrapper_function();
$this->assertIdentical($expected_result, $result);
}
function general_test_with_argument($wrapper_function, $function_argument, $call_options,
$from_transport, $from_deserialisation,
$from_serialisation = 'serialised', $response_code = 200) {
$transport_result = array (
'code' => $response_code,
'response' => $from_transport
);
$expected_result = new CS_REST_Wrapper_Result($from_deserialisation, $response_code);
if(!is_null($from_serialisation)) {
$call_options['data'] = $from_serialisation;
}
$this->setup_transport_and_serialisation($transport_result, $call_options,
$from_deserialisation, $from_transport, $from_serialisation,
$function_argument, $response_code);
$result = $this->wrapper->$wrapper_function($function_argument);
$this->assertIdentical($expected_result, $result);
}
}
class CS_REST_ApiKeyTestGeneral extends CS_REST_TestGeneral {
var $auth = array('api_key' => 'not a real api key');
}
class CS_REST_OAuthTestGeneral extends CS_REST_TestGeneral {
var $auth = array(
'access_token' => '7y872y3872i3eh',
'refresh_token' => 'kjw8qjd9ow8jo');
function test_static_authorize_url_without_state() {
$client_id = 8998879;
$redirect_uri = 'http://example.com/auth';
$scope = 'ViewReports,CreateCampaigns,SendCampaigns';
$expected_result = "https://api.createsend.com/oauth?client_id=8998879&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&scope=ViewReports%2CCreateCampaigns%2CSendCampaigns";
$result = CS_REST_General::authorize_url($client_id, $redirect_uri, $scope);
$this->assertIdentical($expected_result, $result);
}
function test_static_authorize_url_with_state() {
$client_id = 8998879;
$redirect_uri = 'http://example.com/auth';
$scope = 'ViewReports,CreateCampaigns,SendCampaigns';
$state = 89879287;
$expected_result = "https://api.createsend.com/oauth?client_id=8998879&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&scope=ViewReports%2CCreateCampaigns%2CSendCampaigns&state=89879287";
$result = CS_REST_General::authorize_url($client_id, $redirect_uri, $scope, $state);
$this->assertIdentical($expected_result, $result);
}
function test_refresh_token_error_when_refresh_token_null() {
$auth = array('access_token' => 'validaccesstoken', 'refresh_token' => NULL);
$this->wrapper = new CS_REST_General($auth, $this->protocol, $this->log_level,
$this->api_host, $this->mock_log, $this->mock_serialiser, $this->mock_transport);
$this->expectError('Error refreshing token. There is no refresh token set on this object.');
list($new_access_token, $new_expires_in, $new_refresh_token) =
$this->wrapper->refresh_token();
}
function test_refresh_token_error_when_refresh_token_not_set() {
$auth = array('access_token' => 'validaccesstoken');
$this->wrapper = new CS_REST_General($auth, $this->protocol, $this->log_level,
$this->api_host, $this->mock_log, $this->mock_serialiser, $this->mock_transport);
$this->expectError('Error refreshing token. There is no refresh token set on this object.');
list($new_access_token, $new_expires_in, $new_refresh_token) =
$this->wrapper->refresh_token();
}
function test_refresh_token_error_when_no_auth() {
$this->wrapper = new CS_REST_General(NULL, $this->protocol, $this->log_level,
$this->api_host, $this->mock_log, $this->mock_serialiser, $this->mock_transport);
$this->expectError('Error refreshing token. There is no refresh token set on this object.');
list($new_access_token, $new_expires_in, $new_refresh_token) =
$this->wrapper->refresh_token();
}
}
abstract class CS_REST_TestGeneral extends CS_REST_TestBase {
function testget_timezones() {
$raw_result = 'some timezones';
$deserialised = array('timezone1', 'timezone2');
$call_options = $this->get_call_options($this->base_route.'timezones.json');
$this->general_test('get_timezones', $call_options, $raw_result, $deserialised);
}
function testget_systemdate() {
$raw_result = 'system date';
$call_options = $this->get_call_options($this->base_route.'systemdate.json');
$this->general_test('get_systemdate', $call_options, $raw_result, $raw_result);
}
function testget_countries() {
$raw_result = 'some countries';
$deserialised = array('Australia', 'Suid Afrika');
$call_options = $this->get_call_options($this->base_route.'countries.json');
$this->general_test('get_countries', $call_options, $raw_result, $deserialised);
}
function testget_clients() {
$raw_result = 'some clients';
$deserialised = array('Curran & Hughes', 'Repsol');
$call_options = $this->get_call_options($this->base_route.'clients.json');
$this->general_test('get_clients', $call_options, $raw_result, $deserialised);
}
function testget_billing_details() {
$raw_result = 'billing details';
$call_options = $this->get_call_options($this->base_route.'billingdetails.json');
$this->general_test('get_billing_details', $call_options, $raw_result, $raw_result);
}
function testget_primary_contact() {
$raw_result = 'primary contact result';
$deserialized = array('EmailAddress' => 'test@foo.bar');
$call_options = $this->get_call_options($this->base_route.'primarycontact.json', 'GET');
$this->general_test('get_primary_contact', $call_options,
$raw_result, $deserialized);
}
function testset_primary_contact() {
$raw_result = '';
$response_code = 200;
$email = 'test@foo.bar';
$call_options = $this->get_call_options($this->base_route.'primarycontact.json?email=' . urlencode($email), 'PUT');
$call_options['data'] = '';
$transport_result = array (
'code' => $response_code,
'response' => $raw_result
);
$expected_result = new CS_REST_Wrapper_Result($raw_result, $response_code);
$this->setup_transport_and_serialisation($transport_result, $call_options,
$raw_result, $raw_result, '', '', $response_code);
$result = $this->wrapper->set_primary_contact($email);
$this->assertIdentical($expected_result, $result);
}
function testset_external_session_url() {
$session_options = array(
'Email' => "exammple@example.com",
'Chrome' => "None",
'Url' => "/subscribers",
'IntegratorID' => "qw989q8wud98qwyd",
'ClientID' => "9q8uw9d8u9wud" );
$raw_result = '';
$response_code = 200;
$call_options = $this->get_call_options($this->base_route.'externalsession.json', 'PUT');
$call_options['data'] = 'session options were serialised to this';
$transport_result = array (
'code' => $response_code,
'response' => $raw_result
);
$expected_result = new CS_REST_Wrapper_Result($raw_result, $response_code);
$this->setup_transport_and_serialisation($transport_result, $call_options,
$raw_result, $raw_result, 'session options were serialised to this',
$session_options, $response_code);
$result = $this->wrapper->external_session_url($session_options);
$this->assertIdentical($expected_result, $result);
}
}