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\ActivityFixtures; |
16
|
|
|
use Xabbuh\XApi\DataFixtures\ActorFixtures; |
17
|
|
|
use Xabbuh\XApi\DataFixtures\DocumentFixtures; |
18
|
|
|
use Xabbuh\XApi\Model\StateDocument; |
19
|
|
|
use Xabbuh\XApi\Model\StateDocumentsFilter; |
20
|
|
|
use XApi\Repository\Api\StateDocumentRepositoryInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Jérôme Parmentier <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class StateDocumentRepositoryTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var StateDocumentRepositoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $stateDocumentRepository; |
31
|
|
|
|
32
|
|
|
protected function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->stateDocumentRepository = $this->createStateDocumentRepositoryInterface(); |
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 testFetchingNonExistingStateDocumentThrowsException() |
47
|
|
|
{ |
48
|
|
|
$criteria = new StateDocumentsFilter(); |
49
|
|
|
$criteria |
50
|
|
|
->byActivity(ActivityFixtures::getIdActivity()) |
51
|
|
|
->byAgent(ActorFixtures::getTypicalAgent()); |
52
|
|
|
|
53
|
|
|
$this->stateDocumentRepository->find('unknown-state-id', $criteria); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @dataProvider getStateDocument |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function testCreatedStateDocumentCanBeRetrievedByOriginal(StateDocument $stateDocument) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this->stateDocumentRepository->save($stateDocument); |
62
|
|
|
|
63
|
|
|
$criteria = new StateDocumentsFilter(); |
64
|
|
|
$criteria |
65
|
|
|
->byActivity($stateDocument->getState()->getActivity()) |
66
|
|
|
->byAgent($stateDocument->getState()->getActor()); |
67
|
|
|
|
68
|
|
|
$fetchedStateDocument = $this->stateDocumentRepository->find($stateDocument->getState()->getStateId(), $criteria); |
69
|
|
|
|
70
|
|
|
$this->assertEquals($stateDocument->getState()->getStateId(), $fetchedStateDocument->getState()->getStateId()); |
71
|
|
|
$this->assertEquals($stateDocument->getState()->getRegistrationId(), $fetchedStateDocument->getState()->getRegistrationId()); |
72
|
|
|
$this->assertTrue($stateDocument->getState()->getActivity()->equals($fetchedStateDocument->getState()->getActivity())); |
73
|
|
|
$this->assertTrue($stateDocument->getState()->getActor()->equals($fetchedStateDocument->getState()->getActor())); |
74
|
|
|
$this->assertEquals($stateDocument->getData(), $fetchedStateDocument->getData()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @dataProvider getStateDocument |
79
|
|
|
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function testDeletedStateDocumentIsDeleted(StateDocument $stateDocument) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->stateDocumentRepository->save($stateDocument); |
84
|
|
|
$this->stateDocumentRepository->delete($stateDocument); |
85
|
|
|
|
86
|
|
|
$criteria = new StateDocumentsFilter(); |
87
|
|
|
$criteria |
88
|
|
|
->byActivity($stateDocument->getState()->getActivity()) |
89
|
|
|
->byAgent($stateDocument->getState()->getActor()); |
90
|
|
|
|
91
|
|
|
$this->stateDocumentRepository->find($stateDocument->getState()->getStateId(), $criteria); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @dataProvider getStateDocument |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function testCommitSaveDeferredStateDocument(StateDocument $stateDocument) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$this->stateDocumentRepository->saveDeferred($stateDocument); |
100
|
|
|
$this->stateDocumentRepository->commit(); |
101
|
|
|
|
102
|
|
|
$criteria = new StateDocumentsFilter(); |
103
|
|
|
$criteria |
104
|
|
|
->byActivity($stateDocument->getState()->getActivity()) |
105
|
|
|
->byAgent($stateDocument->getState()->getActor()); |
106
|
|
|
|
107
|
|
|
$fetchedStateDocument = $this->stateDocumentRepository->find($stateDocument->getState()->getStateId(), $criteria); |
108
|
|
|
|
109
|
|
|
$this->assertEquals($stateDocument->getState()->getStateId(), $fetchedStateDocument->getState()->getStateId()); |
110
|
|
|
$this->assertEquals($stateDocument->getState()->getRegistrationId(), $fetchedStateDocument->getState()->getRegistrationId()); |
111
|
|
|
$this->assertTrue($stateDocument->getState()->getActivity()->equals($fetchedStateDocument->getState()->getActivity())); |
112
|
|
|
$this->assertTrue($stateDocument->getState()->getActor()->equals($fetchedStateDocument->getState()->getActor())); |
113
|
|
|
$this->assertEquals($stateDocument->getData(), $fetchedStateDocument->getData()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @dataProvider getStateDocument |
118
|
|
|
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function testCommitDeleteDeferredStateDocument(StateDocument $stateDocument) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$this->stateDocumentRepository->save($stateDocument); |
123
|
|
|
$this->stateDocumentRepository->deleteDeferred($stateDocument); |
124
|
|
|
$this->stateDocumentRepository->commit(); |
125
|
|
|
|
126
|
|
|
$criteria = new StateDocumentsFilter(); |
127
|
|
|
$criteria |
128
|
|
|
->byActivity($stateDocument->getState()->getActivity()) |
129
|
|
|
->byAgent($stateDocument->getState()->getActor()); |
130
|
|
|
|
131
|
|
|
$this->stateDocumentRepository->find($stateDocument->getState()->getStateId(), $criteria); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getStateDocument() |
135
|
|
|
{ |
136
|
|
|
return array(DocumentFixtures::getStateDocument()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
abstract protected function createStateDocumentRepositoryInterface(); |
140
|
|
|
|
141
|
|
|
abstract protected function cleanDatabase(); |
142
|
|
|
} |
143
|
|
|
|
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.