Completed
Push — master ( bd2868...16a7fa )
by Jeroen De
12s
created

HandleSubscriptionPaymentNotificationUseCaseTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 15
dl 0
loc 170
rs 9.1666
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMailer() 0 3 1
A getEventLogger() 0 3 1
A testWhenRepositoryThrowsException_request() 0 12 1
A testWhenApplicationDoesNotExist_requestIs() 0 16 1
A testWhenAuthorizationFails_requestIsNotHa() 0 16 1
A testWhenTransactionTypeIsForSubscriptionC() 0 13 1
B testGivenSubscriptionPaymentRequest_child() 0 33 1
A testGivenExistingTransactionId_requestIsN() 0 22 1
B testGivenSubscriptionPaymentRequest_paren() 0 34 1
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() {
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\...re\TemplateBasedMailer>, 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() {
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\...re\TemplateBasedMailer>, 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
			$this->getEventLogger()
0 ignored issues
show
Unused Code introduced by
The call to HandleSubscriptionPaymen...nUseCase::__construct() has too many arguments starting with $this->getEventLogger().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
51
		);
52
53
		$request = ValidPayPalNotificationRequest::newInstantPayment( 667 );
54
		$response = $useCase->handleNotification( $request );
55
		$this->assertFalse( $response->notificationWasHandled() );
56
	}
57
58
	public function testWhenAuthorizationFails_requestIsNotHandled() {
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\...re\TemplateBasedMailer>, 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
			$this->getEventLogger()
0 ignored issues
show
Unused Code introduced by
The call to HandleSubscriptionPaymen...nUseCase::__construct() has too many arguments starting with $this->getEventLogger().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
68
		);
69
70
		$request = ValidPayPalNotificationRequest::newInstantPayment( 1 );
71
		$response = $useCase->handleNotification( $request );
72
		$this->assertFalse( $response->notificationWasHandled() );
73
	}
74
75
	public function testWhenTransactionTypeIsForSubscriptionChanges_requestIsNotHandled() {
76
		$request = ValidPayPalNotificationRequest::newSubscriptionModification();
77
78
		$useCase = new HandleSubscriptionPaymentNotificationUseCase(
79
			new FakeApplicationRepository(),
80
			new SucceedingAuthorizer(),
81
			$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\...re\TemplateBasedMailer>, 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...
82
			new NullLogger(),
83
			$this->getEventLogger()
0 ignored issues
show
Unused Code introduced by
The call to HandleSubscriptionPaymen...nUseCase::__construct() has too many arguments starting with $this->getEventLogger().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
84
		);
85
		$response = $useCase->handleNotification( $request );
86
		$this->assertFalse( $response->notificationWasHandled() );
87
	}
88
89
	public function testGivenSubscriptionPaymentRequest_childDataSetIsCreated() {
90
		$application = ValidMembershipApplication::newConfirmedSubscriptionDomainEntity();
91
92
		$fakeRepository = new FakeApplicationRepository();
93
		$fakeRepository->storeApplication( $application );
94
95
		$request = ValidPayPalNotificationRequest::newRecurringPayment( $application->getId() );
96
97
		$useCase = new HandleSubscriptionPaymentNotificationUseCase(
98
			$fakeRepository,
99
			new SucceedingAuthorizer(),
100
			$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\...re\TemplateBasedMailer>, 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...
101
			new NullLogger(),
102
			$this->getEventLogger()
0 ignored issues
show
Unused Code introduced by
The call to HandleSubscriptionPaymen...nUseCase::__construct() has too many arguments starting with $this->getEventLogger().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103
		);
104
105
		$response = $useCase->handleNotification( $request );
106
		$this->assertTrue( $response->notificationWasHandled() );
107
		$this->assertFalse( $response->hasErrors() );
108
109
		$application = $fakeRepository->getApplicationById( $application->getId() );
110
		/** @var \WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PayPalPayment $payment */
111
		$payment = $application->getPayment()->getPaymentMethod();
112
		$childApplication = $fakeRepository->getApplicationById( $payment->getPayPalData()->getChildPaymentEntityId( ValidPayPalNotificationRequest::TRANSACTION_ID ) );
113
		$this->assertNotNull( $childApplication );
114
		/** @var \WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\PayPalPayment $childPayment */
115
		$childPayment = $childApplication->getPayment()->getPaymentMethod();
116
		$this->assertEquals( ValidPayPalNotificationRequest::TRANSACTION_ID, $childPayment->getPayPalData()->getPaymentId() );
117
		$this->assertEquals( $application->getPayment()->getAmount(), $childApplication->getPayment()->getAmount() );
118
		$this->assertEquals( $application->getApplicant(), $childApplication->getApplicant() );
119
		$this->assertEquals( $application->getPayment()->getIntervalInMonths(), $childApplication->getPayment()->getIntervalInMonths() );
120
		$this->assertTrue( $childApplication->isConfirmed() );
121
	}
122
123
	public function testGivenExistingTransactionId_requestIsNotHandled() {
124
		$application = ValidMembershipApplication::newConfirmedSubscriptionDomainEntity();
125
		/** @var PayPalPayment $payment */
126
		$payment = $application->getPayment()->getPaymentMethod();
127
		$payment->getPayPalData()->addChildPayment( ValidPayPalNotificationRequest::TRANSACTION_ID, 1 );
128
129
		$fakeRepository = new FakeApplicationRepository();
130
		$fakeRepository->storeApplication( $application );
131
132
		$request = ValidPayPalNotificationRequest::newRecurringPayment( 1 );
133
134
		$useCase = new HandleSubscriptionPaymentNotificationUseCase(
135
			$fakeRepository,
136
			new SucceedingAuthorizer(),
137
			$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\...re\TemplateBasedMailer>, 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...
138
			new NullLogger(),
139
			$this->getEventLogger()
0 ignored issues
show
Unused Code introduced by
The call to HandleSubscriptionPaymen...nUseCase::__construct() has too many arguments starting with $this->getEventLogger().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
140
		);
141
142
		$response = $useCase->handleNotification( $request );
143
		$this->assertFalse( $response->notificationWasHandled() );
144
	}
145
146
	/**
147
	 * @return TemplateBasedMailer|\PHPUnit_Framework_MockObject_MockObject
148
	 */
149
	private function getMailer(): TemplateBasedMailer {
150
		return $this->getMockBuilder( TemplateBasedMailer::class )->disableOriginalConstructor()->getMock();
151
	}
152
153
	/**
154
	 * @return DonationEventLogger|\PHPUnit_Framework_MockObject_MockObject
155
	 */
156
	private function getEventLogger(): DonationEventLogger {
157
		return $this->createMock( DonationEventLogger::class );
158
	}
159
160
	public function testGivenSubscriptionPaymentRequest_parentDataSetReferencesChildPaymentId() {
161
		$application = ValidMembershipApplication::newConfirmedSubscriptionDomainEntity();
162
163
		$fakeRepository = new FakeApplicationRepository();
164
		$fakeRepository->storeApplication( $application );
165
166
		$request = ValidPayPalNotificationRequest::newRecurringPayment( $application->getId() );
167
168
		$useCase = new HandleSubscriptionPaymentNotificationUseCase(
169
			$fakeRepository,
170
			new SucceedingAuthorizer(),
171
			$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\...re\TemplateBasedMailer>, 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...
172
			new NullLogger(),
173
			$this->getEventLogger()
0 ignored issues
show
Unused Code introduced by
The call to HandleSubscriptionPaymen...nUseCase::__construct() has too many arguments starting with $this->getEventLogger().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
174
		);
175
		$useCase->handleNotification( $request );
176
177
		/** @var PayPalPayment $payment */
178
		$payment = $application->getPayment()->getPaymentMethod();
179
180
		$storedApplication = $fakeRepository->getApplicationById( $application->getId() );
181
		/** @var PayPalPayment $storedpayment */
182
		$storedpayment = $storedApplication->getPayment()->getPaymentMethod();
183
184
		$this->assertSame(
185
			2,
186
			$storedpayment->getPayPalData()->getChildPaymentEntityId( ValidPayPalNotificationRequest::TRANSACTION_ID )
187
		);
188
189
		$this->assertEquals(
190
			$payment->getPayPalData()->addChildPayment( ValidPayPalNotificationRequest::TRANSACTION_ID, 2 ),
191
			$storedpayment->getPayPalData()
192
		);
193
	}
194
195
}
196