Completed
Push — master ( 3a3042...0e85eb )
by Christian
02:04
created

testCreatedVoidStatementCanBeRetrievedByGeneratedId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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