|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\MembershipContext\Tests\Integration\UseCases\HandleSubscriptionPaymentNotification; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Log\NullLogger; |
|
8
|
|
|
use WMDE\Fundraising\Frontend\DonationContext\Infrastructure\DonationEventLogger; |
|
9
|
|
|
use WMDE\Fundraising\Frontend\DonationContext\Tests\Data\ValidPayPalNotificationRequest; |
|
10
|
|
|
use WMDE\Fundraising\Frontend\Infrastructure\TemplateBasedMailer; |
|
11
|
|
|
use WMDE\Fundraising\Frontend\MembershipContext\DataAccess\DoctrineApplicationRepository; |
|
12
|
|
|
use WMDE\Fundraising\Frontend\MembershipContext\Tests\Fixtures\FailingAuthorizer; |
|
13
|
|
|
use WMDE\Fundraising\Frontend\MembershipContext\Tests\Fixtures\FakeApplicationRepository; |
|
14
|
|
|
use WMDE\Fundraising\Frontend\MembershipContext\Tests\Fixtures\SucceedingAuthorizer; |
|
15
|
|
|
use WMDE\Fundraising\Frontend\MembershipContext\UseCases\HandleSubscriptionPaymentNotification\HandleSubscriptionPaymentNotificationUseCase; |
|
16
|
|
|
use WMDE\Fundraising\Frontend\MembershipContext\Tests\Data\ValidMembershipApplication; |
|
17
|
|
|
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PayPalPayment; |
|
18
|
|
|
use WMDE\Fundraising\Frontend\Tests\Fixtures\ThrowingEntityManager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @covers WMDE\Fundraising\Frontend\MembershipContext\UseCases\HandleSubscriptionPaymentNotification\HandleSubscriptionPaymentNotificationUseCase |
|
22
|
|
|
* |
|
23
|
|
|
* @licence GNU GPL v2+ |
|
24
|
|
|
* @author Kai Nissen < [email protected] > |
|
25
|
|
|
*/ |
|
26
|
|
|
class HandleSubscriptionPaymentNotificationUseCaseTest extends \PHPUnit\Framework\TestCase { |
|
27
|
|
|
|
|
28
|
|
|
public function testWhenRepositoryThrowsException_requestIsNotHandled(): void { |
|
29
|
|
|
$useCase = new HandleSubscriptionPaymentNotificationUseCase( |
|
30
|
|
|
new DoctrineApplicationRepository( ThrowingEntityManager::newInstance( $this ) ), |
|
31
|
|
|
new SucceedingAuthorizer(), |
|
32
|
|
|
$this->getMailer(), |
|
|
|
|
|
|
33
|
|
|
new NullLogger() |
|
34
|
|
|
); |
|
35
|
|
|
$request = ValidPayPalNotificationRequest::newInstantPayment( 1 ); |
|
36
|
|
|
$response = $useCase->handleNotification( $request ); |
|
37
|
|
|
$this->assertFalse( $response->notificationWasHandled() ); |
|
38
|
|
|
$this->assertTrue( $response->hasErrors() ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testWhenApplicationDoesNotExist_requestIsNotHandled(): void { |
|
42
|
|
|
$fakeRepository = new FakeApplicationRepository(); |
|
43
|
|
|
$fakeRepository->storeApplication( ValidMembershipApplication::newDomainEntityUsingPayPal() ); |
|
44
|
|
|
|
|
45
|
|
|
$useCase = new HandleSubscriptionPaymentNotificationUseCase( |
|
46
|
|
|
$fakeRepository, |
|
47
|
|
|
new FailingAuthorizer(), |
|
48
|
|
|
$this->getMailer(), |
|
|
|
|
|
|
49
|
|
|
new NullLogger() |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$request = ValidPayPalNotificationRequest::newInstantPayment( 667 ); |
|
53
|
|
|
$response = $useCase->handleNotification( $request ); |
|
54
|
|
|
$this->assertFalse( $response->notificationWasHandled() ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testWhenAuthorizationFails_requestIsNotHandled(): void { |
|
58
|
|
|
$fakeRepository = new FakeApplicationRepository(); |
|
59
|
|
|
$fakeRepository->storeApplication( ValidMembershipApplication::newDomainEntityUsingPayPal() ); |
|
60
|
|
|
|
|
61
|
|
|
$useCase = new HandleSubscriptionPaymentNotificationUseCase( |
|
62
|
|
|
$fakeRepository, |
|
63
|
|
|
new FailingAuthorizer(), |
|
64
|
|
|
$this->getMailer(), |
|
|
|
|
|
|
65
|
|
|
new NullLogger() |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$request = ValidPayPalNotificationRequest::newInstantPayment( 1 ); |
|
69
|
|
|
$response = $useCase->handleNotification( $request ); |
|
70
|
|
|
$this->assertFalse( $response->notificationWasHandled() ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testWhenTransactionTypeIsForSubscriptionChanges_requestIsNotHandled(): void { |
|
74
|
|
|
$request = ValidPayPalNotificationRequest::newSubscriptionModification(); |
|
75
|
|
|
|
|
76
|
|
|
$useCase = new HandleSubscriptionPaymentNotificationUseCase( |
|
77
|
|
|
new FakeApplicationRepository(), |
|
78
|
|
|
new SucceedingAuthorizer(), |
|
79
|
|
|
$this->getMailer(), |
|
|
|
|
|
|
80
|
|
|
new NullLogger() |
|
81
|
|
|
); |
|
82
|
|
|
$response = $useCase->handleNotification( $request ); |
|
83
|
|
|
$this->assertFalse( $response->notificationWasHandled() ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testGivenSubscriptionPaymentRequest_childDataSetIsCreated(): void { |
|
87
|
|
|
$application = ValidMembershipApplication::newConfirmedSubscriptionDomainEntity(); |
|
88
|
|
|
|
|
89
|
|
|
$fakeRepository = new FakeApplicationRepository(); |
|
90
|
|
|
$fakeRepository->storeApplication( $application ); |
|
91
|
|
|
|
|
92
|
|
|
$request = ValidPayPalNotificationRequest::newRecurringPayment( $application->getId() ); |
|
93
|
|
|
|
|
94
|
|
|
$useCase = new HandleSubscriptionPaymentNotificationUseCase( |
|
95
|
|
|
$fakeRepository, |
|
96
|
|
|
new SucceedingAuthorizer(), |
|
97
|
|
|
$this->getMailer(), |
|
|
|
|
|
|
98
|
|
|
new NullLogger() |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$response = $useCase->handleNotification( $request ); |
|
102
|
|
|
$this->assertTrue( $response->notificationWasHandled() ); |
|
103
|
|
|
$this->assertFalse( $response->hasErrors() ); |
|
104
|
|
|
|
|
105
|
|
|
$application = $fakeRepository->getApplicationById( $application->getId() ); |
|
106
|
|
|
/** @var \WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PayPalPayment $payment */ |
|
107
|
|
|
$payment = $application->getPayment()->getPaymentMethod(); |
|
108
|
|
|
$childApplication = $fakeRepository->getApplicationById( $payment->getPayPalData()->getChildPaymentEntityId( ValidPayPalNotificationRequest::TRANSACTION_ID ) ); |
|
109
|
|
|
$this->assertNotNull( $childApplication ); |
|
110
|
|
|
/** @var \WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PayPalPayment $childPayment */ |
|
111
|
|
|
$childPayment = $childApplication->getPayment()->getPaymentMethod(); |
|
112
|
|
|
$this->assertEquals( ValidPayPalNotificationRequest::TRANSACTION_ID, $childPayment->getPayPalData()->getPaymentId() ); |
|
113
|
|
|
$this->assertEquals( $application->getPayment()->getAmount(), $childApplication->getPayment()->getAmount() ); |
|
114
|
|
|
$this->assertEquals( $application->getApplicant(), $childApplication->getApplicant() ); |
|
115
|
|
|
$this->assertEquals( $application->getPayment()->getIntervalInMonths(), $childApplication->getPayment()->getIntervalInMonths() ); |
|
116
|
|
|
$this->assertTrue( $childApplication->isConfirmed() ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testGivenExistingTransactionId_requestIsNotHandled(): void { |
|
120
|
|
|
$application = ValidMembershipApplication::newConfirmedSubscriptionDomainEntity(); |
|
121
|
|
|
/** @var PayPalPayment $payment */ |
|
122
|
|
|
$payment = $application->getPayment()->getPaymentMethod(); |
|
123
|
|
|
$payment->getPayPalData()->addChildPayment( ValidPayPalNotificationRequest::TRANSACTION_ID, 1 ); |
|
124
|
|
|
|
|
125
|
|
|
$fakeRepository = new FakeApplicationRepository(); |
|
126
|
|
|
$fakeRepository->storeApplication( $application ); |
|
127
|
|
|
|
|
128
|
|
|
$request = ValidPayPalNotificationRequest::newRecurringPayment( 1 ); |
|
129
|
|
|
|
|
130
|
|
|
$useCase = new HandleSubscriptionPaymentNotificationUseCase( |
|
131
|
|
|
$fakeRepository, |
|
132
|
|
|
new SucceedingAuthorizer(), |
|
133
|
|
|
$this->getMailer(), |
|
|
|
|
|
|
134
|
|
|
new NullLogger() |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
$response = $useCase->handleNotification( $request ); |
|
138
|
|
|
$this->assertFalse( $response->notificationWasHandled() ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return TemplateBasedMailer|\PHPUnit_Framework_MockObject_MockObject |
|
143
|
|
|
*/ |
|
144
|
|
|
private function getMailer(): TemplateBasedMailer { |
|
145
|
|
|
return $this->getMockBuilder( TemplateBasedMailer::class )->disableOriginalConstructor()->getMock(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return DonationEventLogger|\PHPUnit_Framework_MockObject_MockObject |
|
150
|
|
|
*/ |
|
151
|
|
|
private function getEventLogger(): DonationEventLogger { |
|
152
|
|
|
return $this->createMock( DonationEventLogger::class ); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testGivenSubscriptionPaymentRequest_parentDataSetReferencesChildPaymentId(): void { |
|
156
|
|
|
$application = ValidMembershipApplication::newConfirmedSubscriptionDomainEntity(); |
|
157
|
|
|
|
|
158
|
|
|
$fakeRepository = new FakeApplicationRepository(); |
|
159
|
|
|
$fakeRepository->storeApplication( $application ); |
|
160
|
|
|
|
|
161
|
|
|
$request = ValidPayPalNotificationRequest::newRecurringPayment( $application->getId() ); |
|
162
|
|
|
|
|
163
|
|
|
$useCase = new HandleSubscriptionPaymentNotificationUseCase( |
|
164
|
|
|
$fakeRepository, |
|
165
|
|
|
new SucceedingAuthorizer(), |
|
166
|
|
|
$this->getMailer(), |
|
|
|
|
|
|
167
|
|
|
new NullLogger() |
|
168
|
|
|
); |
|
169
|
|
|
$useCase->handleNotification( $request ); |
|
170
|
|
|
|
|
171
|
|
|
/** @var PayPalPayment $payment */ |
|
172
|
|
|
$payment = $application->getPayment()->getPaymentMethod(); |
|
173
|
|
|
|
|
174
|
|
|
$storedApplication = $fakeRepository->getApplicationById( $application->getId() ); |
|
175
|
|
|
/** @var PayPalPayment $storedpayment */ |
|
176
|
|
|
$storedpayment = $storedApplication->getPayment()->getPaymentMethod(); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertSame( |
|
179
|
|
|
2, |
|
180
|
|
|
$storedpayment->getPayPalData()->getChildPaymentEntityId( ValidPayPalNotificationRequest::TRANSACTION_ID ) |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
|
|
$this->assertEquals( |
|
184
|
|
|
$payment->getPayPalData()->addChildPayment( ValidPayPalNotificationRequest::TRANSACTION_ID, 2 ), |
|
185
|
|
|
$storedpayment->getPayPalData() |
|
186
|
|
|
); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.