1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the xAPI package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xabbuh\XApi\Client\Tests\Api; |
13
|
|
|
|
14
|
|
|
use Xabbuh\XApi\Client\Api\StatementsApiClient; |
15
|
|
|
use Xabbuh\XApi\DataFixtures\StatementFixtures; |
16
|
|
|
use Xabbuh\XApi\Model\Agent; |
17
|
|
|
use Xabbuh\XApi\Model\Statement; |
18
|
|
|
use Xabbuh\XApi\Model\StatementReference; |
19
|
|
|
use Xabbuh\XApi\Model\StatementResult; |
20
|
|
|
use Xabbuh\XApi\Model\StatementsFilter; |
21
|
|
|
use Xabbuh\XApi\Model\Verb; |
22
|
|
|
use Xabbuh\XApi\Serializer\ActorSerializer; |
23
|
|
|
use Xabbuh\XApi\Serializer\StatementResultSerializer; |
24
|
|
|
use Xabbuh\XApi\Serializer\StatementSerializer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Christian Flothmann <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class StatementsApiClientTest extends ApiClientTest |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var StatementsApiClient |
33
|
|
|
*/ |
34
|
|
|
private $client; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
$this->client = new StatementsApiClient( |
40
|
|
|
$this->requestHandler, |
41
|
|
|
'1.0.1', |
42
|
|
|
new StatementSerializer($this->serializer), |
43
|
|
|
new StatementResultSerializer($this->serializer), |
44
|
|
|
new ActorSerializer($this->serializer) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testStoreStatement() |
49
|
|
|
{ |
50
|
|
|
$statementId = '12345678-1234-5678-1234-567812345678'; |
51
|
|
|
$statement = $this->createStatement(); |
52
|
|
|
$this->validateStoreApiCall( |
53
|
|
|
'post', |
54
|
|
|
'statements', |
55
|
|
|
array(), |
56
|
|
|
200, |
57
|
|
|
'["'.$statementId.'"]', |
58
|
|
|
$this->createStatement() |
59
|
|
|
); |
60
|
|
|
$returnedStatement = $this->client->storeStatement($statement); |
61
|
|
|
$expectedStatement = $this->createStatement($statementId); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($expectedStatement, $returnedStatement); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testStoreStatementWithId() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$statementId = '12345678-1234-5678-1234-567812345678'; |
69
|
|
|
$statement = $this->createStatement($statementId); |
70
|
|
|
$this->validateStoreApiCall( |
71
|
|
|
'put', |
72
|
|
|
'statements', |
73
|
|
|
array('statementId' => $statementId), |
74
|
|
|
204, |
75
|
|
|
'["'.$statementId.'"]', |
76
|
|
|
$statement |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->assertEquals($statement, $this->client->storeStatement($statement)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function testStoreStatementWithIdEnsureThatTheIdIsNotOverwritten() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$statementId = '12345678-1234-5678-1234-567812345678'; |
85
|
|
|
$statement = $this->createStatement($statementId); |
86
|
|
|
$this->validateStoreApiCall( |
87
|
|
|
'put', |
88
|
|
|
'statements', |
89
|
|
|
array('statementId' => $statementId), |
90
|
|
|
204, |
91
|
|
|
'', |
92
|
|
|
$statement |
93
|
|
|
); |
94
|
|
|
$storedStatement = $this->client->storeStatement($statement); |
95
|
|
|
|
96
|
|
|
$this->assertEquals($statementId, $storedStatement->getId()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testStoreStatements() |
100
|
|
|
{ |
101
|
|
|
$statementId1 = '12345678-1234-5678-1234-567812345678'; |
102
|
|
|
$statementId2 = '12345678-1234-5678-1234-567812345679'; |
103
|
|
|
$statement1 = $this->createStatement(); |
104
|
|
|
$statement2 = $this->createStatement(); |
105
|
|
|
$this->validateStoreApiCall( |
106
|
|
|
'post', |
107
|
|
|
'statements', |
108
|
|
|
array(), |
109
|
|
|
'200', |
110
|
|
|
'["'.$statementId1.'","'.$statementId2.'"]', |
111
|
|
|
array($this->createStatement(), $this->createStatement()) |
112
|
|
|
); |
113
|
|
|
$statements = $this->client->storeStatements(array($statement1, $statement2)); |
114
|
|
|
$expectedStatement1 = $this->createStatement($statementId1); |
115
|
|
|
$expectedStatement2 = $this->createStatement($statementId2); |
116
|
|
|
$expectedStatements = array($expectedStatement1, $expectedStatement2); |
117
|
|
|
|
118
|
|
|
$this->assertNotContains($statements[0], array($statement1, $statement2)); |
119
|
|
|
$this->assertNotContains($statements[1], array($statement1, $statement2)); |
120
|
|
|
$this->assertEquals($expectedStatements, $statements); |
121
|
|
|
$this->assertEquals($statementId1, $statements[0]->getId()); |
122
|
|
|
$this->assertEquals($statementId2, $statements[1]->getId()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @expectedException \InvalidArgumentException |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function testStoreStatementsWithNonStatementObject() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$statement1 = $this->createStatement(); |
131
|
|
|
$statement2 = $this->createStatement(); |
132
|
|
|
|
133
|
|
|
$this->client->storeStatements(array($statement1, new \stdClass(), $statement2)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @expectedException \InvalidArgumentException |
138
|
|
|
*/ |
139
|
|
View Code Duplication |
public function testStoreStatementsWithNonObject() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$statement1 = $this->createStatement(); |
142
|
|
|
$statement2 = $this->createStatement(); |
143
|
|
|
|
144
|
|
|
$this->client->storeStatements(array($statement1, 'foo', $statement2)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @expectedException \InvalidArgumentException |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function testStoreStatementsWithId() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$statement1 = $this->createStatement(); |
153
|
|
|
$statement2 = $this->createStatement('12345678-1234-5678-1234-567812345679'); |
154
|
|
|
|
155
|
|
|
$this->client->storeStatements(array($statement1, $statement2)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testVoidStatement() |
159
|
|
|
{ |
160
|
|
|
$voidedStatementId = '12345678-1234-5678-1234-567812345679'; |
161
|
|
|
$voidingStatementId = '12345678-1234-5678-1234-567812345678'; |
162
|
|
|
$agent = new Agent('mailto:[email protected]'); |
163
|
|
|
$statementReference = new StatementReference($voidedStatementId); |
164
|
|
|
$voidingStatement = new Statement(null, $agent, Verb::createVoidVerb(), $statementReference); |
165
|
|
|
$voidedStatement = $this->createStatement($voidedStatementId); |
166
|
|
|
$this->validateStoreApiCall( |
167
|
|
|
'post', |
168
|
|
|
'statements', |
169
|
|
|
array(), |
170
|
|
|
200, |
171
|
|
|
'["'.$voidingStatementId.'"]', |
172
|
|
|
$voidingStatement |
173
|
|
|
); |
174
|
|
|
$returnedVoidingStatement = $this->client->voidStatement($voidedStatement, $agent); |
175
|
|
|
$expectedVoidingStatement = new Statement( |
176
|
|
|
$voidingStatementId, |
177
|
|
|
$agent, |
178
|
|
|
Verb::createVoidVerb(), |
179
|
|
|
$statementReference |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
$this->assertEquals($expectedVoidingStatement, $returnedVoidingStatement); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testGetStatement() |
186
|
|
|
{ |
187
|
|
|
$statementId = '12345678-1234-5678-1234-567812345678'; |
188
|
|
|
$statement = $this->createStatement(); |
189
|
|
|
$this->validateRetrieveApiCall( |
190
|
|
|
'get', |
191
|
|
|
'statements', |
192
|
|
|
array('statementId' => $statementId), |
193
|
|
|
200, |
194
|
|
|
'Statement', |
195
|
|
|
$statement |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$this->client->getStatement($statementId); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException |
203
|
|
|
*/ |
204
|
|
|
public function testGetStatementWithNotExistingStatement() |
205
|
|
|
{ |
206
|
|
|
$statementId = '12345678-1234-5678-1234-567812345678'; |
207
|
|
|
$this->validateRetrieveApiCall( |
208
|
|
|
'get', |
209
|
|
|
'statements', |
210
|
|
|
array('statementId' => $statementId), |
211
|
|
|
404, |
212
|
|
|
'Statement', |
213
|
|
|
'There is no statement associated with this id' |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
$this->client->getStatement($statementId); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
View Code Duplication |
public function testGetVoidedStatement() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$statementId = '12345678-1234-5678-1234-567812345678'; |
222
|
|
|
$statement = $this->createStatement(); |
223
|
|
|
$this->validateRetrieveApiCall( |
224
|
|
|
'get', |
225
|
|
|
'statements', |
226
|
|
|
array('voidedStatementId' => $statementId), |
227
|
|
|
200, |
228
|
|
|
'Statement', |
229
|
|
|
$statement |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
$this->client->getVoidedStatement($statementId); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException |
237
|
|
|
*/ |
238
|
|
View Code Duplication |
public function testGetVoidedStatementWithNotExistingStatement() |
|
|
|
|
239
|
|
|
{ |
240
|
|
|
$statementId = '12345678-1234-5678-1234-567812345678'; |
241
|
|
|
$this->validateRetrieveApiCall( |
242
|
|
|
'get', |
243
|
|
|
'statements', |
244
|
|
|
array('voidedStatementId' => $statementId), |
245
|
|
|
404, |
246
|
|
|
'Statement', |
247
|
|
|
'There is no statement associated with this id' |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
$this->client->getVoidedStatement($statementId); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testGetStatements() |
254
|
|
|
{ |
255
|
|
|
$statementResult = $this->createStatementResult(); |
256
|
|
|
$this->validateRetrieveApiCall( |
257
|
|
|
'get', |
258
|
|
|
'statements', |
259
|
|
|
array(), |
260
|
|
|
200, |
261
|
|
|
'StatementResult', |
262
|
|
|
$statementResult |
263
|
|
|
); |
264
|
|
|
|
265
|
|
|
$this->assertEquals($statementResult, $this->client->getStatements()); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testGetStatementsWithStatementsFilter() |
269
|
|
|
{ |
270
|
|
|
$filter = new StatementsFilter(); |
271
|
|
|
$filter->limit(10)->ascending(); |
272
|
|
|
$statementResult = $this->createStatementResult(); |
273
|
|
|
$this->validateRetrieveApiCall( |
274
|
|
|
'get', |
275
|
|
|
'statements', |
276
|
|
|
array('limit' => 10, 'ascending' => 'True'), |
277
|
|
|
200, |
278
|
|
|
'StatementResult', |
279
|
|
|
$statementResult |
280
|
|
|
); |
281
|
|
|
|
282
|
|
|
$this->assertEquals($statementResult, $this->client->getStatements($filter)); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function testGetStatementsWithAgentInStatementsFilter() |
286
|
|
|
{ |
287
|
|
|
// {"mbox":"mailto:[email protected]","objectType":"Agent"} |
|
|
|
|
288
|
|
|
$filter = new StatementsFilter(); |
289
|
|
|
$agent = new Agent('mailto:[email protected]'); |
290
|
|
|
$filter->byActor($agent); |
291
|
|
|
$statementResult = $this->createStatementResult(); |
292
|
|
|
$agentJson = '{"mbox":"mailto:[email protected]","objectType":"Agent"}'; |
293
|
|
|
$this->serializer |
|
|
|
|
294
|
|
|
->expects($this->once()) |
295
|
|
|
->method('serialize') |
296
|
|
|
->with($agent, 'json') |
297
|
|
|
->will($this->returnValue($agentJson)); |
298
|
|
|
$this->validateRetrieveApiCall( |
299
|
|
|
'get', |
300
|
|
|
'statements', |
301
|
|
|
array('agent' => $agentJson), |
302
|
|
|
200, |
303
|
|
|
'StatementResult', |
304
|
|
|
$statementResult |
305
|
|
|
); |
306
|
|
|
|
307
|
|
|
$this->assertEquals($statementResult, $this->client->getStatements($filter)); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function testGetStatementsWithVerbInStatementsFilter() |
311
|
|
|
{ |
312
|
|
|
$filter = new StatementsFilter(); |
313
|
|
|
$verb = new Verb('http://adlnet.gov/expapi/verbs/attended'); |
314
|
|
|
$filter->byVerb($verb); |
315
|
|
|
$statementResult = $this->createStatementResult(); |
316
|
|
|
$this->validateRetrieveApiCall( |
317
|
|
|
'get', |
318
|
|
|
'statements', |
319
|
|
|
array('verb' => 'http://adlnet.gov/expapi/verbs/attended'), |
320
|
|
|
200, |
321
|
|
|
'StatementResult', |
322
|
|
|
$statementResult |
323
|
|
|
); |
324
|
|
|
|
325
|
|
|
$this->assertEquals($statementResult, $this->client->getStatements($filter)); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function testGetNextStatements() |
329
|
|
|
{ |
330
|
|
|
$moreUrl = '/xapi/statements/more/b381d8eca64a61a42c7b9b4ecc2fabb6'; |
331
|
|
|
$previousStatementResult = new StatementResult(array(), $moreUrl); |
332
|
|
|
$this->validateRetrieveApiCall( |
333
|
|
|
'get', |
334
|
|
|
$moreUrl, |
335
|
|
|
array(), |
336
|
|
|
200, |
337
|
|
|
'StatementResult', |
338
|
|
|
$previousStatementResult |
339
|
|
|
); |
340
|
|
|
|
341
|
|
|
$statementResult = $this->client->getNextStatements($previousStatementResult); |
342
|
|
|
|
343
|
|
|
$this->assertInstanceOf('\Xabbuh\XApi\Model\StatementResult', $statementResult); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @param int $id |
348
|
|
|
* |
349
|
|
|
* @return Statement |
350
|
|
|
*/ |
351
|
|
|
private function createStatement($id = null) |
352
|
|
|
{ |
353
|
|
|
return StatementFixtures::getMinimalStatement($id); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return StatementResult |
358
|
|
|
*/ |
359
|
|
|
private function createStatementResult() |
360
|
|
|
{ |
361
|
|
|
return new StatementResult(array()); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.