Completed
Push — master ( c233b7...aa3611 )
by Christian
02:29
created

StatementRepositoryTest::getStatementsWithoutId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
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 XApi\Repository\Api\StatementRepository;
17
18
/**
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
abstract class StatementRepositoryTest extends \PHPUnit_Framework_TestCase
22
{
23
    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';
24
25
    /**
26
     * @var StatementRepository
27
     */
28
    private $statementRepository;
29
30
    protected function setUp()
31
    {
32
        $this->statementRepository = $this->createStatementRepository();
33
        $this->cleanDatabase();
34
    }
35
36
    protected function tearDown()
37
    {
38
        $this->cleanDatabase();
39
    }
40
41
    /**
42
     * @expectedException \XApi\Repository\Api\Exception\NotFoundException
43
     */
44
    public function testFetchingNonExistingStatementThrowsException()
45
    {
46
        $this->statementRepository->findStatementById('12345678-1234-5678-8234-567812345678');
47
    }
48
49
    /**
50
     * @expectedException \XApi\Repository\Api\Exception\NotFoundException
51
     * @dataProvider getStatementsWithoutId
52
     */
53
    public function testFetchingStatementAsVoidedStatementThrowsException(Statement $statement)
54
    {
55
        $statementId = $this->statementRepository->storeStatement($statement);
56
57
        $this->statementRepository->findVoidedStatementById($statementId);
58
    }
59
60
    /**
61
     * @dataProvider getStatementsWithoutId
62
     */
63
    public function testUuidIsGeneratedForNewStatementIfNotPresent(Statement $statement)
64
    {
65
        $statementId = $this->statementRepository->storeStatement($statement);
66
67
        $this->assertNull($statement->getId());
68
        $this->assertRegExp(self::UUID_REGEXP, $statementId);
69
    }
70
71
    /**
72
     * @dataProvider getStatementsWithId
73
     */
74
    public function testUuidIsNotGeneratedForNewStatementIfPresent(Statement $statement)
75
    {
76
        $statementId = $this->statementRepository->storeStatement($statement);
77
78
        $this->assertEquals($statement->getId(), $statementId);
79
    }
80
81
    /**
82
     * @dataProvider getStatementsWithId
83
     */
84
    public function testCreatedStatementCanBeRetrievedByOriginalId(Statement $statement)
85
    {
86
        $this->statementRepository->storeStatement($statement);
87
        $fetchedStatement = $this->statementRepository->findStatementById($statement->getId());
88
89
        $this->assertStatementEquals($statement, $fetchedStatement);
90
    }
91
92
    /**
93
     * @dataProvider getStatementsWithoutId
94
     */
95 View Code Duplication
    public function testCreatedStatementCanBeRetrievedByGeneratedId(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...
96
    {
97
        $statementId = $this->statementRepository->storeStatement($statement);
98
        $fetchedStatement = $this->statementRepository->findStatementById($statementId);
99
100
        $this->assertNull($statement->getId());
101
        $this->assertStatementEquals($statement, $fetchedStatement, false);
102
    }
103
104
    public function getStatementsWithId()
105
    {
106
        return $this->getStatements(StatementFixtures::DEFAULT_STATEMENT_ID);
107
    }
108
109
    public function getStatementsWithoutId()
110
    {
111
        return $this->getStatements(null);
112
    }
113
114
    /**
115
     * @expectedException \XApi\Repository\Api\Exception\NotFoundException
116
     */
117
    public function testFetchingNonExistingVoidStatementThrowsException()
118
    {
119
        $this->statementRepository->findVoidedStatementById('12345678-1234-5678-8234-567812345678');
120
    }
121
122
    /**
123
     * @expectedException \XApi\Repository\Api\Exception\NotFoundException
124
     */
125
    public function testFetchingVoidStatementAsStatementThrowsException()
126
    {
127
        $statement = StatementFixtures::getVoidStatement(null);
128
        $statementId = $this->statementRepository->storeStatement($statement);
129
130
        $this->statementRepository->findStatementById($statementId);
131
    }
132
133
    public function testUuidIsGeneratedForNewVoidStatementIfNotPresent()
134
    {
135
        $statement = StatementFixtures::getVoidStatement(null);
136
        $statementId = $this->statementRepository->storeStatement($statement);
137
138
        $this->assertNull($statement->getId());
139
        $this->assertRegExp(self::UUID_REGEXP, $statementId);
140
    }
141
142
    public function testUuidIsNotGeneratedForNewVoidStatementIfPresent()
143
    {
144
        $statement = StatementFixtures::getVoidStatement();
145
        $statementId = $this->statementRepository->storeStatement($statement);
146
147
        $this->assertEquals($statement->getId(), $statementId);
148
    }
149
150
    public function testCreatedVoidStatementCanBeRetrievedByOriginalId()
151
    {
152
        $statement = StatementFixtures::getVoidStatement();
153
        $this->statementRepository->storeStatement($statement);
154
        $fetchedStatement = $this->statementRepository->findVoidedStatementById($statement->getId());
155
156
        $this->assertStatementEquals($statement, $fetchedStatement);
157
    }
158
159 View Code Duplication
    public function testCreatedVoidStatementCanBeRetrievedByGeneratedId()
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...
160
    {
161
        $statement = StatementFixtures::getVoidStatement(null);
162
        $statementId = $this->statementRepository->storeStatement($statement);
163
        $fetchedStatement = $this->statementRepository->findVoidedStatementById($statementId);
164
165
        $this->assertNull($statement->getId());
166
        $this->assertStatementEquals($statement, $fetchedStatement, false);
167
    }
168
169
    abstract protected function createStatementRepository();
170
171
    abstract protected function cleanDatabase();
172
173
    private function getStatements($id)
174
    {
175
        return array(
176
            'minimal-statement' => array(StatementFixtures::getMinimalStatement($id)),
177
            'statement-with-group-actor' => array(StatementFixtures::getStatementWithGroupActor($id)),
178
            'statement-with-group-actor-without-members' => array(StatementFixtures::getStatementWithGroupActorWithoutMembers($id)),
179
            'object-is-statement-reference' => array(StatementFixtures::getStatementWithStatementRef($id)),
180
            'statement-with-result' => array(StatementFixtures::getStatementWithResult($id)),
181
            'statement-with-agent-authority' => array(StatementFixtures::getStatementWithAgentAuthority($id)),
182
            'statement-with-group-authority' => array(StatementFixtures::getStatementWithGroupAuthority($id)),
183
        );
184
    }
185
186
    private function assertStatementEquals(Statement $expected, Statement $actual, $validateId = true)
187
    {
188
        if ($validateId) {
189
            $this->assertSame($expected->getId(), $actual->getId());
190
        }
191
192
        $this->assertTrue($actual->getActor()->equals($expected->getActor()));
193
        $this->assertTrue($actual->getVerb()->equals($expected->getVerb()));
194
        $this->assertTrue($actual->getObject()->equals($expected->getObject()));
195
196 View Code Duplication
        if (null === $expected->getResult()) {
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...
197
            $this->assertNull($actual->getResult());
198
        } else {
199
            $this->assertTrue($actual->getResult()->equals($expected->getResult()));
200
        }
201
202 View Code Duplication
        if (null === $expected->getAuthority()) {
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...
203
            $this->assertNull($actual->getAuthority());
204
        } else {
205
            $this->assertTrue($actual->getAuthority()->equals($expected->getAuthority()));
206
        }
207
    }
208
}
209