|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\DonationContext\Tests\Integration\DataAccess; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use Doctrine\ORM\EntityManager; |
|
9
|
|
|
use Doctrine\ORM\ORMException; |
|
10
|
|
|
use WMDE\Fundraising\Entities\Donation; |
|
11
|
|
|
use WMDE\Fundraising\Frontend\DonationContext\DataAccess\DoctrineCommentFinder; |
|
12
|
|
|
use WMDE\Fundraising\Frontend\DonationContext\Domain\Repositories\CommentListingException; |
|
13
|
|
|
use WMDE\Fundraising\Frontend\DonationContext\Domain\Repositories\CommentWithAmount; |
|
14
|
|
|
use WMDE\Fundraising\Frontend\Tests\TestEnvironment; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @covers WMDE\Fundraising\Frontend\DonationContext\DataAccess\DoctrineCommentFinder |
|
18
|
|
|
* |
|
19
|
|
|
* @licence GNU GPL v2+ |
|
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
21
|
|
|
*/ |
|
22
|
|
|
class DoctrineCommentFinderTest extends \PHPUnit_Framework_TestCase { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntityManager |
|
26
|
|
|
*/ |
|
27
|
|
|
private $entityManager; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() { |
|
30
|
|
|
$this->entityManager = TestEnvironment::newInstance()->getFactory()->getEntityManager(); |
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function newDbalCommentRepository(): DoctrineCommentFinder { |
|
35
|
|
|
return new DoctrineCommentFinder( $this->entityManager ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testWhenThereAreNoComments_anEmptyListIsReturned() { |
|
39
|
|
|
$repository = $this->newDbalCommentRepository(); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertEmpty( $repository->getPublicComments( 10 ) ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testWhenThereAreLessCommentsThanTheLimit_theyAreAllReturned() { |
|
45
|
|
|
$this->persistFirstDonationWithComment(); |
|
46
|
|
|
$this->persistSecondDonationWithComment(); |
|
47
|
|
|
$this->persistThirdDonationWithComment(); |
|
48
|
|
|
$this->entityManager->flush(); |
|
49
|
|
|
|
|
50
|
|
|
$repository = $this->newDbalCommentRepository(); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertEquals( |
|
53
|
|
|
[ |
|
54
|
|
|
$this->getThirdComment( 3 ), |
|
55
|
|
|
$this->getSecondComment(), |
|
56
|
|
|
$this->getFirstComment(), |
|
57
|
|
|
], |
|
58
|
|
|
$repository->getPublicComments( 10 ) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testWhenThereAreMoreCommentsThanTheLimit_aLimitedNumberAreReturned() { |
|
63
|
|
|
$this->persistFirstDonationWithComment(); |
|
64
|
|
|
$this->persistSecondDonationWithComment(); |
|
65
|
|
|
$this->persistThirdDonationWithComment(); |
|
66
|
|
|
$this->entityManager->flush(); |
|
67
|
|
|
|
|
68
|
|
|
$repository = $this->newDbalCommentRepository(); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertEquals( |
|
71
|
|
|
[ |
|
72
|
|
|
$this->getThirdComment( 3 ), |
|
73
|
|
|
$this->getSecondComment(), |
|
74
|
|
|
], |
|
75
|
|
|
$repository->getPublicComments( 2 ) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testOnlyPublicCommentsGetReturned() { |
|
80
|
|
|
$this->persistFirstDonationWithComment(); |
|
81
|
|
|
$this->persistSecondDonationWithComment(); |
|
82
|
|
|
$this->persistDonationWithPrivateComment(); |
|
83
|
|
|
$this->persistThirdDonationWithComment(); |
|
84
|
|
|
$this->entityManager->flush(); |
|
85
|
|
|
|
|
86
|
|
|
$repository = $this->newDbalCommentRepository(); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertEquals( |
|
89
|
|
|
[ |
|
90
|
|
|
$this->getThirdComment( 4 ), |
|
91
|
|
|
$this->getSecondComment(), |
|
92
|
|
|
$this->getFirstComment(), |
|
93
|
|
|
], |
|
94
|
|
|
$repository->getPublicComments( 10 ) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testOnlyNonDeletedCommentsGetReturned() { |
|
99
|
|
|
$this->persistFirstDonationWithComment(); |
|
100
|
|
|
$this->persistSecondDonationWithComment(); |
|
101
|
|
|
$this->persistDeletedDonationWithComment(); |
|
102
|
|
|
$this->persistThirdDonationWithComment(); |
|
103
|
|
|
$this->persistDeletedDonationWithoutDeletedTimestamp(); |
|
104
|
|
|
$this->entityManager->flush(); |
|
105
|
|
|
|
|
106
|
|
|
$repository = $this->newDbalCommentRepository(); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertEquals( |
|
109
|
|
|
[ |
|
110
|
|
|
$this->getThirdComment( 4 ), |
|
111
|
|
|
$this->getSecondComment(), |
|
112
|
|
|
$this->getFirstComment(), |
|
113
|
|
|
], |
|
114
|
|
|
$repository->getPublicComments( 10 ) |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function persistFirstDonationWithComment() { |
|
119
|
|
|
$firstDonation = new Donation(); |
|
120
|
|
|
$firstDonation->setPublicRecord( 'First name' ); |
|
121
|
|
|
$firstDonation->setComment( 'First comment' ); |
|
122
|
|
|
$firstDonation->setAmount( '100' ); |
|
123
|
|
|
$firstDonation->setCreationTime( new DateTime( '1984-01-01' ) ); |
|
124
|
|
|
$firstDonation->setIsPublic( true ); |
|
125
|
|
|
$this->entityManager->persist( $firstDonation ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function persistSecondDonationWithComment() { |
|
129
|
|
|
$secondDonation = new Donation(); |
|
130
|
|
|
$secondDonation->setPublicRecord( 'Second name' ); |
|
131
|
|
|
$secondDonation->setComment( 'Second comment' ); |
|
132
|
|
|
$secondDonation->setAmount( '200' ); |
|
133
|
|
|
$secondDonation->setCreationTime( new DateTime( '1984-02-02' ) ); |
|
134
|
|
|
$secondDonation->setIsPublic( true ); |
|
135
|
|
|
$this->entityManager->persist( $secondDonation ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function persistThirdDonationWithComment() { |
|
139
|
|
|
$thirdDonation = new Donation(); |
|
140
|
|
|
$thirdDonation->setPublicRecord( 'Third name' ); |
|
141
|
|
|
$thirdDonation->setComment( 'Third comment' ); |
|
142
|
|
|
$thirdDonation->setAmount( '300' ); |
|
143
|
|
|
$thirdDonation->setCreationTime( new DateTime( '1984-03-03' ) ); |
|
144
|
|
|
$thirdDonation->setIsPublic( true ); |
|
145
|
|
|
$this->entityManager->persist( $thirdDonation ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
private function persistDonationWithPrivateComment() { |
|
149
|
|
|
$privateDonation = new Donation(); |
|
150
|
|
|
$privateDonation->setPublicRecord( 'Private name' ); |
|
151
|
|
|
$privateDonation->setComment( 'Private comment' ); |
|
152
|
|
|
$privateDonation->setAmount( '1337' ); |
|
153
|
|
|
$privateDonation->setCreationTime( new DateTime( '1984-12-12' ) ); |
|
154
|
|
|
$privateDonation->setIsPublic( false ); |
|
155
|
|
|
$this->entityManager->persist( $privateDonation ); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
private function persistDeletedDonationWithComment() { |
|
159
|
|
|
$deletedDonation = new Donation(); |
|
160
|
|
|
$deletedDonation->setPublicRecord( 'Deleted name' ); |
|
161
|
|
|
$deletedDonation->setComment( 'Deleted comment' ); |
|
162
|
|
|
$deletedDonation->setAmount( '31337' ); |
|
163
|
|
|
$deletedDonation->setCreationTime( new DateTime( '1984-11-11' ) ); |
|
164
|
|
|
$deletedDonation->setIsPublic( true ); |
|
165
|
|
|
$deletedDonation->setDeletionTime( new DateTime( '2000-01-01' ) ); |
|
166
|
|
|
$this->entityManager->persist( $deletedDonation ); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
private function persistDeletedDonationWithoutDeletedTimestamp() { |
|
170
|
|
|
$deletedDonation = new Donation(); |
|
171
|
|
|
$deletedDonation->setPublicRecord( 'Deleted name' ); |
|
172
|
|
|
$deletedDonation->setComment( 'Deleted comment' ); |
|
173
|
|
|
$deletedDonation->setAmount( '31337' ); |
|
174
|
|
|
$deletedDonation->setCreationTime( new DateTime( '1984-11-11' ) ); |
|
175
|
|
|
$deletedDonation->setIsPublic( true ); |
|
176
|
|
|
$deletedDonation->setStatus( Donation::STATUS_CANCELLED ); |
|
177
|
|
|
$this->entityManager->persist( $deletedDonation ); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
private function getFirstComment(): CommentWithAmount { |
|
181
|
|
|
return CommentWithAmount::newInstance() |
|
182
|
|
|
->setAuthorName( 'First name' ) |
|
183
|
|
|
->setCommentText( 'First comment' ) |
|
184
|
|
|
->setDonationAmount( 100 ) |
|
185
|
|
|
->setDonationTime( new \DateTime( '1984-01-01' ) ) |
|
186
|
|
|
->setDonationId( 1 ) |
|
187
|
|
|
->freeze()->assertNoNullFields(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
private function getSecondComment(): CommentWithAmount { |
|
191
|
|
|
return CommentWithAmount::newInstance() |
|
192
|
|
|
->setAuthorName( 'Second name' ) |
|
193
|
|
|
->setCommentText( 'Second comment' ) |
|
194
|
|
|
->setDonationAmount( 200 ) |
|
195
|
|
|
->setDonationTime( new \DateTime( '1984-02-02' ) ) |
|
196
|
|
|
->setDonationId( 2 ) |
|
197
|
|
|
->freeze()->assertNoNullFields(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
private function getThirdComment( int $donationId ): CommentWithAmount { |
|
201
|
|
|
return CommentWithAmount::newInstance() |
|
202
|
|
|
->setAuthorName( 'Third name' ) |
|
203
|
|
|
->setCommentText( 'Third comment' ) |
|
204
|
|
|
->setDonationAmount( 300 ) |
|
205
|
|
|
->setDonationTime( new \DateTime( '1984-03-03' ) ) |
|
206
|
|
|
->setDonationId( $donationId ) |
|
207
|
|
|
->freeze()->assertNoNullFields(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function testDoctrineThrowsException_getPublicCommentsRethrowsAsDomainException() { |
|
211
|
|
|
$repository = new DoctrineCommentFinder( $this->newThrowingEntityManager() ); |
|
212
|
|
|
|
|
213
|
|
|
$this->expectException( CommentListingException::class ); |
|
214
|
|
|
$repository->getPublicComments( 10 ); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
private function newThrowingEntityManager(): EntityManager { |
|
218
|
|
|
$entityManager = $this->createMock( EntityManager::class ); |
|
219
|
|
|
|
|
220
|
|
|
$entityManager->expects( $this->any() ) |
|
221
|
|
|
->method( $this->anything() ) |
|
222
|
|
|
->willThrowException( new ORMException( 'Such error!' ) ); |
|
223
|
|
|
|
|
224
|
|
|
return $entityManager; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testGivenOffsetOfOneCausesOneCommentToBeSkipped() { |
|
228
|
|
|
$this->persistFirstDonationWithComment(); |
|
229
|
|
|
$this->persistSecondDonationWithComment(); |
|
230
|
|
|
$this->persistThirdDonationWithComment(); |
|
231
|
|
|
$this->entityManager->flush(); |
|
232
|
|
|
|
|
233
|
|
|
$this->assertEquals( |
|
234
|
|
|
[ |
|
235
|
|
|
$this->getSecondComment(), |
|
236
|
|
|
$this->getFirstComment(), |
|
237
|
|
|
], |
|
238
|
|
|
$this->newDbalCommentRepository()->getPublicComments( 10, 1 ) |
|
239
|
|
|
); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function testGivenOffsetBeyondResultSetCausesEmptyResult() { |
|
243
|
|
|
$this->persistFirstDonationWithComment(); |
|
244
|
|
|
$this->persistSecondDonationWithComment(); |
|
245
|
|
|
$this->persistThirdDonationWithComment(); |
|
246
|
|
|
$this->entityManager->flush(); |
|
247
|
|
|
|
|
248
|
|
|
$this->assertEquals( |
|
249
|
|
|
[], |
|
250
|
|
|
$this->newDbalCommentRepository()->getPublicComments( 10, 10 ) |
|
251
|
|
|
); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
|