Completed
Push — master ( 2eeb7e...97e31e )
by Christian
02:59
created

StatementsApiClientTest::testGetStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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\InverseFunctionalIdentifier;
18
use Xabbuh\XApi\Model\IRI;
19
use Xabbuh\XApi\Model\IRL;
20
use Xabbuh\XApi\Model\Statement;
21
use Xabbuh\XApi\Model\StatementId;
22
use Xabbuh\XApi\Model\StatementReference;
23
use Xabbuh\XApi\Model\StatementResult;
24
use Xabbuh\XApi\Model\StatementsFilter;
25
use Xabbuh\XApi\Model\Verb;
26
use Xabbuh\XApi\Serializer\ActorSerializer;
27
use Xabbuh\XApi\Serializer\StatementResultSerializer;
28
use Xabbuh\XApi\Serializer\StatementSerializer;
29
30
/**
31
 * @author Christian Flothmann <[email protected]>
32
 */
33
class StatementsApiClientTest extends ApiClientTest
34
{
35
    /**
36
     * @var StatementsApiClient
37
     */
38
    private $client;
39
40
    protected function setUp()
41
    {
42
        parent::setUp();
43
        $this->client = new StatementsApiClient(
44
            $this->requestHandler,
45
            '1.0.1',
46
            new StatementSerializer($this->serializer),
47
            new StatementResultSerializer($this->serializer),
48
            new ActorSerializer($this->serializer)
49
        );
50
    }
51
52
    public function testStoreStatement()
53
    {
54
        $statementId = '12345678-1234-5678-1234-567812345678';
55
        $statement = $this->createStatement();
56
        $this->validateStoreApiCall(
57
            'post',
58
            'statements',
59
            array(),
60
            200,
61
            '["'.$statementId.'"]',
62
            $this->createStatement()
63
        );
64
        $returnedStatement = $this->client->storeStatement($statement);
65
        $expectedStatement = $this->createStatement($statementId);
66
67
        $this->assertEquals($expectedStatement, $returnedStatement);
68
    }
69
70
    public function testStoreStatementWithId()
71
    {
72
        $statementId = '12345678-1234-5678-1234-567812345678';
73
        $statement = $this->createStatement($statementId);
74
        $this->validateStoreApiCall(
75
            'put',
76
            'statements',
77
            array('statementId' => $statementId),
78
            204,
79
            '["'.$statementId.'"]',
80
            $statement
81
        );
82
83
        $this->assertEquals($statement, $this->client->storeStatement($statement));
84
    }
85
86
    public function testStoreStatementWithIdEnsureThatTheIdIsNotOverwritten()
87
    {
88
        $statementId = '12345678-1234-5678-1234-567812345678';
89
        $statement = $this->createStatement($statementId);
90
        $this->validateStoreApiCall(
91
            'put',
92
            'statements',
93
            array('statementId' => $statementId),
94
            204,
95
            '',
96
            $statement
97
        );
98
        $storedStatement = $this->client->storeStatement($statement);
99
100
        $this->assertEquals($statementId, $storedStatement->getId()->getValue());
101
    }
102
103
    public function testStoreStatements()
104
    {
105
        $statementId1 = '12345678-1234-5678-1234-567812345678';
106
        $statementId2 = '12345678-1234-5678-1234-567812345679';
107
        $statement1 = $this->createStatement();
108
        $statement2 = $this->createStatement();
109
        $this->validateStoreApiCall(
110
            'post',
111
            'statements',
112
            array(),
113
            '200',
114
            '["'.$statementId1.'","'.$statementId2.'"]',
115
            array($this->createStatement(), $this->createStatement())
116
        );
117
        $statements = $this->client->storeStatements(array($statement1, $statement2));
118
        $expectedStatement1 = $this->createStatement($statementId1);
119
        $expectedStatement2 = $this->createStatement($statementId2);
120
        $expectedStatements = array($expectedStatement1, $expectedStatement2);
121
122
        $this->assertNotContains($statements[0], array($statement1, $statement2));
123
        $this->assertNotContains($statements[1], array($statement1, $statement2));
124
        $this->assertEquals($expectedStatements, $statements);
125
        $this->assertEquals($statementId1, $statements[0]->getId()->getValue());
126
        $this->assertEquals($statementId2, $statements[1]->getId()->getValue());
127
    }
128
129
    /**
130
     * @expectedException \InvalidArgumentException
131
     */
132
    public function testStoreStatementsWithNonStatementObject()
133
    {
134
        $statement1 = $this->createStatement();
135
        $statement2 = $this->createStatement();
136
137
        $this->client->storeStatements(array($statement1, new \stdClass(), $statement2));
138
    }
139
140
    /**
141
     * @expectedException \InvalidArgumentException
142
     */
143
    public function testStoreStatementsWithNonObject()
144
    {
145
        $statement1 = $this->createStatement();
146
        $statement2 = $this->createStatement();
147
148
        $this->client->storeStatements(array($statement1, 'foo', $statement2));
149
    }
150
151
    /**
152
     * @expectedException \InvalidArgumentException
153
     */
154
    public function testStoreStatementsWithId()
155
    {
156
        $statement1 = $this->createStatement();
157
        $statement2 = $this->createStatement('12345678-1234-5678-1234-567812345679');
158
159
        $this->client->storeStatements(array($statement1, $statement2));
160
    }
161
162
    public function testVoidStatement()
163
    {
164
        $voidedStatementId = '12345678-1234-5678-1234-567812345679';
165
        $voidingStatementId = '12345678-1234-5678-1234-567812345678';
166
        $agent = new Agent(InverseFunctionalIdentifier::withMbox(IRI::fromString('mailto:[email protected]')));
167
        $statementReference = new StatementReference(StatementId::fromString($voidedStatementId));
168
        $voidingStatement = new Statement(null, $agent, Verb::createVoidVerb(), $statementReference);
169
        $voidedStatement = $this->createStatement($voidedStatementId);
170
        $this->validateStoreApiCall(
171
            'post',
172
            'statements',
173
            array(),
174
            200,
175
            '["'.$voidingStatementId.'"]',
176
            $voidingStatement
177
        );
178
        $returnedVoidingStatement = $this->client->voidStatement($voidedStatement, $agent);
179
        $expectedVoidingStatement = new Statement(
180
            StatementId::fromString($voidingStatementId),
181
            $agent,
182
            Verb::createVoidVerb(),
183
            $statementReference
184
        );
185
186
        $this->assertEquals($expectedVoidingStatement, $returnedVoidingStatement);
187
    }
188
189
    public function testGetStatement()
190
    {
191
        $statementId = '12345678-1234-5678-1234-567812345678';
192
        $statement = $this->createStatement();
193
        $this->validateRetrieveApiCall(
194
            'get',
195
            'statements',
196
            array('statementId' => $statementId),
197
            200,
198
            'Statement',
199
            $statement
200
        );
201
202
        $this->client->getStatement(StatementId::fromString($statementId));
203
    }
204
205
    /**
206
     * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
207
     */
208
    public function testGetStatementWithNotExistingStatement()
209
    {
210
        $statementId = '12345678-1234-5678-1234-567812345678';
211
        $this->validateRetrieveApiCall(
212
            'get',
213
            'statements',
214
            array('statementId' => $statementId),
215
            404,
216
            'Statement',
217
            'There is no statement associated with this id'
218
        );
219
220
        $this->client->getStatement(StatementId::fromString($statementId));
221
    }
222
223
    public function testGetVoidedStatement()
224
    {
225
        $statementId = '12345678-1234-5678-1234-567812345678';
226
        $statement = $this->createStatement();
227
        $this->validateRetrieveApiCall(
228
            'get',
229
            'statements',
230
            array('voidedStatementId' => $statementId),
231
            200,
232
            'Statement',
233
            $statement
234
        );
235
236
        $this->client->getVoidedStatement(StatementId::fromString($statementId));
237
    }
238
239
    /**
240
     * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
241
     */
242
    public function testGetVoidedStatementWithNotExistingStatement()
243
    {
244
        $statementId = '12345678-1234-5678-1234-567812345678';
245
        $this->validateRetrieveApiCall(
246
            'get',
247
            'statements',
248
            array('voidedStatementId' => $statementId),
249
            404,
250
            'Statement',
251
            'There is no statement associated with this id'
252
        );
253
254
        $this->client->getVoidedStatement(StatementId::fromString($statementId));
255
    }
256
257
    public function testGetStatements()
258
    {
259
        $statementResult = $this->createStatementResult();
260
        $this->validateRetrieveApiCall(
261
            'get',
262
            'statements',
263
            array(),
264
            200,
265
            'StatementResult',
266
            $statementResult
267
        );
268
269
        $this->assertEquals($statementResult, $this->client->getStatements());
270
    }
271
272
    public function testGetStatementsWithStatementsFilter()
273
    {
274
        $filter = new StatementsFilter();
275
        $filter->limit(10)->ascending();
276
        $statementResult = $this->createStatementResult();
277
        $this->validateRetrieveApiCall(
278
            'get',
279
            'statements',
280
            array('limit' => 10, 'ascending' => 'true'),
281
            200,
282
            'StatementResult',
283
            $statementResult
284
        );
285
286
        $this->assertEquals($statementResult, $this->client->getStatements($filter));
287
    }
288
289
    public function testGetStatementsWithAgentInStatementsFilter()
290
    {
291
        // {"mbox":"mailto:[email protected]","objectType":"Agent"}
0 ignored issues
show
Unused Code Comprehensibility introduced by
90% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
292
        $filter = new StatementsFilter();
293
        $agent = new Agent(InverseFunctionalIdentifier::withMbox(IRI::fromString('mailto:[email protected]')));
294
        $filter->byActor($agent);
295
        $statementResult = $this->createStatementResult();
296
        $agentJson = '{"mbox":"mailto:[email protected]","objectType":"Agent"}';
297
        $this->serializer
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...er\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
298
            ->expects($this->once())
299
            ->method('serialize')
300
            ->with($agent, 'json')
301
            ->will($this->returnValue($agentJson));
302
        $this->validateRetrieveApiCall(
303
            'get',
304
            'statements',
305
            array('agent' => $agentJson),
306
            200,
307
            'StatementResult',
308
            $statementResult
309
        );
310
311
        $this->assertEquals($statementResult, $this->client->getStatements($filter));
312
    }
313
314
    public function testGetStatementsWithVerbInStatementsFilter()
315
    {
316
        $filter = new StatementsFilter();
317
        $verb = new Verb(IRI::fromString('http://adlnet.gov/expapi/verbs/attended'));
318
        $filter->byVerb($verb);
319
        $statementResult = $this->createStatementResult();
320
        $this->validateRetrieveApiCall(
321
            'get',
322
            'statements',
323
            array('verb' => 'http://adlnet.gov/expapi/verbs/attended'),
324
            200,
325
            'StatementResult',
326
            $statementResult
327
        );
328
329
        $this->assertEquals($statementResult, $this->client->getStatements($filter));
330
    }
331
332
    public function testGetNextStatements()
333
    {
334
        $moreUrl = '/xapi/statements/more/b381d8eca64a61a42c7b9b4ecc2fabb6';
335
        $previousStatementResult = new StatementResult(array(), IRL::fromString($moreUrl));
336
        $this->validateRetrieveApiCall(
337
            'get',
338
            $moreUrl,
339
            array(),
340
            200,
341
            'StatementResult',
342
            $previousStatementResult
343
        );
344
345
        $statementResult = $this->client->getNextStatements($previousStatementResult);
346
347
        $this->assertInstanceOf('\Xabbuh\XApi\Model\StatementResult', $statementResult);
348
    }
349
350
    /**
351
     * @param int $id
352
     *
353
     * @return Statement
354
     */
355
    private function createStatement($id = null)
356
    {
357
        $statement = StatementFixtures::getMinimalStatement($id);
358
359
        if (null === $id) {
360
            $statement = $statement->withId(null);
361
        }
362
363
        return $statement;
364
    }
365
366
    /**
367
     * @return StatementResult
368
     */
369
    private function createStatementResult()
370
    {
371
        return new StatementResult(array());
372
    }
373
}
374