1
|
|
|
<?php |
2
|
|
|
namespace OptimizelyPHPTest; |
3
|
|
|
|
4
|
|
|
use PHPUnit_Framework_TestCase; |
5
|
|
|
use WebMarketingROI\OptimizelyPHP\OptimizelyApiClient; |
6
|
|
|
use WebMarketingROI\OptimizelyPHP\Service\v2\Experiments; |
7
|
|
|
use WebMarketingROI\OptimizelyPHP\Exception; |
8
|
|
|
|
9
|
|
|
class OptimizelyApiClientTest extends PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
public function testCreateClient() |
12
|
|
|
{ |
13
|
|
|
$authCredentials = array( |
14
|
|
|
'access_token' => '1234567890' |
15
|
|
|
); |
16
|
|
|
$client = new OptimizelyApiClient($authCredentials, 'v2'); |
17
|
|
|
|
18
|
|
|
$this->assertNotNull($client); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @expectedException WebMarketingROI\OptimizelyPHP\Exception |
23
|
|
|
*/ |
24
|
|
|
public function testCreateClientWrongCredentialFormat() |
25
|
|
|
{ |
26
|
|
|
$authCredentials = '1234567'; |
27
|
|
|
|
28
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @expectedException Exception |
33
|
|
|
*/ |
34
|
|
|
public function testCreateClientWrongApiVersion() |
35
|
|
|
{ |
36
|
|
|
$authCredentials = array( |
37
|
|
|
'access_token' => '1234567890' |
38
|
|
|
); |
39
|
|
|
$client = new OptimizelyApiClient($authCredentials, 'v1'); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testCallService() |
43
|
|
|
{ |
44
|
|
|
$authCredentials = array( |
45
|
|
|
'access_token' => '1234567890' |
46
|
|
|
); |
47
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
48
|
|
|
|
49
|
|
|
$experiments = $client->experiments(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->assertNotNull($experiments); |
52
|
|
|
$this->assertTrue($experiments instanceof Experiments); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @expectedException Exception |
57
|
|
|
*/ |
58
|
|
|
public function testSendApiRequestThrowsException() |
59
|
|
|
{ |
60
|
|
|
$authCredentials = array( |
61
|
|
|
'access_token' => '1234567890' |
62
|
|
|
); |
63
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
64
|
|
|
|
65
|
|
|
// Access token is not a real one, so it should throw an exception |
66
|
|
|
$response = $client->sendApiRequest('/experiments'); |
67
|
|
|
print_r($response); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGetApiVersion() |
71
|
|
|
{ |
72
|
|
|
$authCredentials = array( |
73
|
|
|
'access_token' => '1234567890' |
74
|
|
|
); |
75
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
76
|
|
|
|
77
|
|
|
$apiVersion = $client->getApiVersion(); |
78
|
|
|
$this->assertEquals('v2', $apiVersion); |
79
|
|
|
|
80
|
|
|
$client->setApiVersion('v2'); |
81
|
|
|
$this->assertEquals('v2', $client->getApiVersion()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @expectedException Exception |
86
|
|
|
*/ |
87
|
|
|
public function testSetApiVersion_WrongVer() |
88
|
|
|
{ |
89
|
|
|
$authCredentials = array( |
90
|
|
|
'access_token' => '1234567890' |
91
|
|
|
); |
92
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
93
|
|
|
|
94
|
|
|
$client->setApiVersion('v1'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testSetAuthCredentials() |
98
|
|
|
{ |
99
|
|
|
$authCredentials = array( |
100
|
|
|
'client_id' => '34523242342423424', |
101
|
|
|
'client_secret' => '24352342413131234636', |
102
|
|
|
'refresh_token' => '0345932524', |
103
|
|
|
'access_token' => '1234567890', |
104
|
|
|
); |
105
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
106
|
|
|
|
107
|
|
|
$this->assertEquals($authCredentials, $client->getAuthCredentials()); |
108
|
|
|
|
109
|
|
|
$authCredentials2 = array( |
110
|
|
|
'access_token' => '4252023452342134' |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$client->setAuthCredentials($authCredentials2); |
114
|
|
|
|
115
|
|
|
$this->assertEquals($authCredentials2, $client->getAuthCredentials()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @expectedException Exception |
120
|
|
|
*/ |
121
|
|
|
public function testSetAuthCredentials_InvalidArg() |
122
|
|
|
{ |
123
|
|
|
$client = new OptimizelyApiClient(1234); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @expectedException Exception |
128
|
|
|
*/ |
129
|
|
|
public function testSetAuthCredentials_EmptyArray() |
130
|
|
|
{ |
131
|
|
|
$client = new OptimizelyApiClient(array()); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testGetAccessToken() |
135
|
|
|
{ |
136
|
|
|
$authCredentials = array( |
137
|
|
|
'access_token' => '1234567890', |
138
|
|
|
'access_token_timestamp' => time(), |
139
|
|
|
'expires_in' => 7200, |
140
|
|
|
'token_type' => 'bearer' |
141
|
|
|
); |
142
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
143
|
|
|
|
144
|
|
|
$accessToken = $client->getAccessToken(); |
145
|
|
|
$this->assertEquals('1234567890', $accessToken['access_token']); |
146
|
|
|
$this->assertEquals('bearer', $accessToken['token_type']); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testGetAccessTokenByRefreshToken() |
150
|
|
|
{ |
151
|
|
|
$authCredentials = array( |
152
|
|
|
'client_id' => '23423423423424242423424', |
153
|
|
|
'client_secret' => '2323424234242424234', |
154
|
|
|
'refresh_token' => '24242424244523452gf234', |
155
|
|
|
'access_token' => '1234567890234234234234', |
156
|
|
|
'access_token_timestamp' => time() - 8000, |
157
|
|
|
'expires_in' => 7200, |
158
|
|
|
'token_type' => 'bearer' |
159
|
|
|
); |
160
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
161
|
|
|
|
162
|
|
|
// Issuing sendApiRequest() method will try to get access token with |
163
|
|
|
// refresh token, since the provided access token is invalid. |
164
|
|
|
try { |
165
|
|
|
$result = $client->sendApiRequest('/projects'); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
catch (Exception $e) { |
168
|
|
|
$this->assertEquals(Exception::CODE_API_ERROR, $e->getCode()); |
169
|
|
|
$this->assertEquals(400, $e->getHttpCode()); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testGetDiagnosticsInfo() |
174
|
|
|
{ |
175
|
|
|
$authCredentials = array( |
176
|
|
|
'access_token' => '1234567890' |
177
|
|
|
); |
178
|
|
|
$client = new OptimizelyApiClient($authCredentials); |
179
|
|
|
|
180
|
|
|
$diagInfo = $client->getDiagnosticsInfo(); |
181
|
|
|
$this->assertTrue(is_array($diagInfo)); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: