StatementRepositoryTest   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 184
Duplicated Lines 29.35 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 54
loc 184
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
createStatementRepository() 0 1 ?
cleanDatabase() 0 1 ?
A setUp() 0 5 1
A tearDown() 0 4 1
A testFetchingNonExistingStatementThrowsException() 0 4 1
A testFetchingStatementAsVoidedStatementThrowsException() 0 7 1
A testUuidIsGeneratedForNewStatementIfNotPresent() 8 8 1
A testUuidIsNotGeneratedForNewStatementIfPresent() 0 6 1
A testCreatedStatementCanBeRetrievedByOriginalId() 5 12 2
A testCreatedStatementCanBeRetrievedByGeneratedId() 5 14 2
A getStatementsWithId() 14 14 3
A getStatementsWithoutId() 14 14 3
A testFetchingNonExistingVoidStatementThrowsException() 0 4 1
A testFetchingVoidStatementAsStatementThrowsException() 0 7 1
A testUuidIsGeneratedForNewVoidStatementIfNotPresent() 8 8 1
A testUuidIsNotGeneratedForNewVoidStatementIfPresent() 0 7 1
A testCreatedVoidStatementCanBeRetrievedByOriginalId() 0 8 1
A testCreatedVoidStatementCanBeRetrievedByGeneratedId() 0 9 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 XApi\Repository\Api\Test\Functional;
13
14
use PHPUnit\Framework\TestCase;
15
use Xabbuh\XApi\DataFixtures\StatementFixtures;
16
use Xabbuh\XApi\Model\Statement;
17
use Xabbuh\XApi\Model\StatementId;
18
use XApi\Repository\Api\StatementRepository;
19
20
/**
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
abstract class StatementRepositoryTest extends TestCase
24
{
25
    const UUID_REGEXP = '/^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i';
26
27
    /**
28
     * @var StatementRepository
29
     */
30
    private $statementRepository;
31
32
    protected function setUp()
33
    {
34
        $this->statementRepository = $this->createStatementRepository();
35
        $this->cleanDatabase();
36
    }
37
38
    protected function tearDown()
39
    {
40
        $this->cleanDatabase();
41
    }
42
43
    /**
44
     * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
45
     */
46
    public function testFetchingNonExistingStatementThrowsException()
47
    {
48
        $this->statementRepository->findStatementById(StatementId::fromString('12345678-1234-5678-8234-567812345678'));
49
    }
50
51
    /**
52
     * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
53
     */
54
    public function testFetchingStatementAsVoidedStatementThrowsException()
55
    {
56
        $statement = StatementFixtures::getTypicalStatement()->withId(null);
57
        $statementId = $this->statementRepository->storeStatement($statement);
58
59
        $this->statementRepository->findVoidedStatementById($statementId);
60
    }
61
62
    /**
63
     * @dataProvider getStatementsWithoutId
64
     */
65 View Code Duplication
    public function testUuidIsGeneratedForNewStatementIfNotPresent(Statement $statement)
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...
66
    {
67
        $statement = $statement->withId(null);
68
        $statementId = $this->statementRepository->storeStatement($statement);
69
70
        $this->assertNull($statement->getId());
71
        $this->assertRegExp(self::UUID_REGEXP, $statementId->getValue());
72
    }
73
74
    /**
75
     * @dataProvider getStatementsWithId
76
     */
77
    public function testUuidIsNotGeneratedForNewStatementIfPresent(Statement $statement)
78
    {
79
        $statementId = $this->statementRepository->storeStatement($statement);
80
81
        $this->assertEquals($statement->getId(), $statementId);
82
    }
83
84
    /**
85
     * @dataProvider getStatementsWithId
86
     */
87
    public function testCreatedStatementCanBeRetrievedByOriginalId(Statement $statement)
88
    {
89
        $this->statementRepository->storeStatement($statement);
90
91 View Code Duplication
        if ($statement->getVerb()->isVoidVerb()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92
            $fetchedStatement = $this->statementRepository->findVoidedStatementById($statement->getId());
93
        } else {
94
            $fetchedStatement = $this->statementRepository->findStatementById($statement->getId());
95
        }
96
97
        $this->assertTrue($statement->equals($fetchedStatement));
98
    }
99
100
    /**
101
     * @dataProvider getStatementsWithoutId
102
     */
103
    public function testCreatedStatementCanBeRetrievedByGeneratedId(Statement $statement)
104
    {
105
        $statement  =$statement->withId(null);
106
        $statementId = $this->statementRepository->storeStatement($statement);
107
108 View Code Duplication
        if ($statement->getVerb()->isVoidVerb()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
109
            $fetchedStatement = $this->statementRepository->findVoidedStatementById($statementId);
110
        } else {
111
            $fetchedStatement = $this->statementRepository->findStatementById($statementId);
112
        }
113
114
        $this->assertNull($statement->getId());
115
        $this->assertTrue($statement->equals($fetchedStatement->withId(null)));
116
    }
117
118 View Code Duplication
    public function getStatementsWithId()
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...
119
    {
120
        $fixtures = array();
121
122
        foreach (get_class_methods('Xabbuh\XApi\DataFixtures\StatementFixtures') as $method) {
123
            $statement = call_user_func(array('Xabbuh\XApi\DataFixtures\StatementFixtures', $method));
124
125
            if ($statement instanceof Statement) {
126
                $fixtures[$method] = array($statement->withId(StatementId::fromString(StatementFixtures::DEFAULT_STATEMENT_ID)));
127
            }
128
        }
129
130
        return $fixtures;
131
    }
132
133 View Code Duplication
    public function getStatementsWithoutId()
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...
134
    {
135
        $fixtures = array();
136
137
        foreach (get_class_methods('Xabbuh\XApi\DataFixtures\StatementFixtures') as $method) {
138
            $statement = call_user_func(array('Xabbuh\XApi\DataFixtures\StatementFixtures', $method));
139
140
            if ($statement instanceof Statement) {
141
                $fixtures[$method] = array($statement->withId(null));
142
            }
143
        }
144
145
        return $fixtures;
146
    }
147
148
    /**
149
     * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
150
     */
151
    public function testFetchingNonExistingVoidStatementThrowsException()
152
    {
153
        $this->statementRepository->findVoidedStatementById(StatementId::fromString('12345678-1234-5678-8234-567812345678'));
154
    }
155
156
    /**
157
     * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
158
     */
159
    public function testFetchingVoidStatementAsStatementThrowsException()
160
    {
161
        $statement = StatementFixtures::getVoidingStatement()->withId(null);
162
        $statementId = $this->statementRepository->storeStatement($statement);
163
164
        $this->statementRepository->findStatementById($statementId);
165
    }
166
167 View Code Duplication
    public function testUuidIsGeneratedForNewVoidStatementIfNotPresent()
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...
168
    {
169
        $statement = StatementFixtures::getVoidingStatement()->withId(null);
170
        $statementId = $this->statementRepository->storeStatement($statement);
171
172
        $this->assertNull($statement->getId());
173
        $this->assertRegExp(self::UUID_REGEXP, $statementId->getValue());
174
    }
175
176
    public function testUuidIsNotGeneratedForNewVoidStatementIfPresent()
177
    {
178
        $statement = StatementFixtures::getVoidingStatement();
179
        $statementId = $this->statementRepository->storeStatement($statement);
180
181
        $this->assertEquals($statement->getId(), $statementId);
182
    }
183
184
    public function testCreatedVoidStatementCanBeRetrievedByOriginalId()
185
    {
186
        $statement = StatementFixtures::getVoidingStatement();
187
        $this->statementRepository->storeStatement($statement);
188
        $fetchedStatement = $this->statementRepository->findVoidedStatementById($statement->getId());
189
190
        $this->assertTrue($statement->equals($fetchedStatement));
191
    }
192
193
    public function testCreatedVoidStatementCanBeRetrievedByGeneratedId()
194
    {
195
        $statement = StatementFixtures::getVoidingStatement()->withId(null);
196
        $statementId = $this->statementRepository->storeStatement($statement);
197
        $fetchedStatement = $this->statementRepository->findVoidedStatementById($statementId);
198
199
        $this->assertNull($statement->getId());
200
        $this->assertTrue($statement->equals($fetchedStatement->withId(null)));
201
    }
202
203
    abstract protected function createStatementRepository();
204
205
    abstract protected function cleanDatabase();
206
}
207