Completed
Pull Request — master (#976)
by wiese
61:34
created

testWhenAuthorizationFails_requestIsNotHa()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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