Completed
Push — master ( 00092a...39103d )
by wiese
86:17 queued 21:06
created

testGivenExistingTransactionId_requestIsN()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
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(),
0 ignored issues
show
Bug introduced by
It seems like $this->getMailer() targeting WMDE\Fundraising\Fronten...seCaseTest::getMailer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...nUseCase::__construct() does only seem to accept object<WMDE\Fundraising\...emplateMailerInterface>, maybe add an additional type check?

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.

Loading history...
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(),
0 ignored issues
show
Bug introduced by
It seems like $this->getMailer() targeting WMDE\Fundraising\Fronten...seCaseTest::getMailer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...nUseCase::__construct() does only seem to accept object<WMDE\Fundraising\...emplateMailerInterface>, maybe add an additional type check?

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.

Loading history...
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(),
0 ignored issues
show
Bug introduced by
It seems like $this->getMailer() targeting WMDE\Fundraising\Fronten...seCaseTest::getMailer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...nUseCase::__construct() does only seem to accept object<WMDE\Fundraising\...emplateMailerInterface>, maybe add an additional type check?

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.

Loading history...
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(),
0 ignored issues
show
Bug introduced by
It seems like $this->getMailer() targeting WMDE\Fundraising\Fronten...seCaseTest::getMailer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...nUseCase::__construct() does only seem to accept object<WMDE\Fundraising\...emplateMailerInterface>, maybe add an additional type check?

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.

Loading history...
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(),
0 ignored issues
show
Bug introduced by
It seems like $this->getMailer() targeting WMDE\Fundraising\Fronten...seCaseTest::getMailer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...nUseCase::__construct() does only seem to accept object<WMDE\Fundraising\...emplateMailerInterface>, maybe add an additional type check?

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.

Loading history...
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(),
0 ignored issues
show
Bug introduced by
It seems like $this->getMailer() targeting WMDE\Fundraising\Fronten...seCaseTest::getMailer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...nUseCase::__construct() does only seem to accept object<WMDE\Fundraising\...emplateMailerInterface>, maybe add an additional type check?

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.

Loading history...
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(),
0 ignored issues
show
Bug introduced by
It seems like $this->getMailer() targeting WMDE\Fundraising\Fronten...seCaseTest::getMailer() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...nUseCase::__construct() does only seem to accept object<WMDE\Fundraising\...emplateMailerInterface>, maybe add an additional type check?

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.

Loading history...
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