StatementsApiClientTest   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 335
Duplicated Lines 24.18 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 20
c 6
b 0
f 1
lcom 1
cbo 12
dl 81
loc 335
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testStoreStatement() 0 17 1
A testStoreStatementWithId() 15 15 1
A testStoreStatementWithIdEnsureThatTheIdIsNotOverwritten() 16 16 1
B testStoreStatements() 0 25 1
A testStoreStatementsWithNonStatementObject() 7 7 1
A testStoreStatementsWithNonObject() 7 7 1
A testStoreStatementsWithId() 7 7 1
B testVoidStatement() 0 26 1
A testGetStatement() 0 15 1
A testGetStatementWithNotExistingStatement() 0 14 1
A testGetVoidedStatement() 15 15 1
A testGetVoidedStatementWithNotExistingStatement() 14 14 1
A testGetStatements() 0 14 1
A testGetStatementsWithStatementsFilter() 0 16 1
B testGetStatementsWithAgentInStatementsFilter() 0 24 1
A testGetStatementsWithVerbInStatementsFilter() 0 17 1
A testGetNextStatements() 0 17 1
A createStatement() 0 4 1
A createStatementResult() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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"}
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...
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
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\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...
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