3434
3535class ApplepayAdapterTest extends BaseIntegrationTest
3636{
37+ /** @var string $appleCaCertificatePath Path to ca Certificate file. */
38+ protected $ appleCaCertificatePath ;
39+ /** @var string $merchantValidationUrl merchant validation url for testing. */
3740 private $ merchantValidationUrl ;
38- private $ merchantValidationCertificatePath ;
39- private $ appleCaCertificatePath ;
40-
41- public function domainShouldBeValidatedCorrectlyDP ()
42- {
43- return [
44- 'invalid: example.domain.com ' => ['https://example.domain.com ' , false ],
45- 'valid: https://apple-pay-gateway.apple.com/some/path ' => ['https://apple-pay-gateway.apple.com/some/path ' , true ],
46- 'valid: https://cn-apple-pay-gateway.apple.com ' => ['https://cn-apple-pay-gateway.apple.com ' , true ],
47- 'invalid: apple-pay-gateway-nc-pod1.apple.com ' => ['apple-pay-gateway-nc-pod1.apple.com ' , false ],
48- 'invalid: (empty) ' => ['' , false ],
49- ];
50- }
41+ /** @var string $applepayCombinedCertPath Path to combined certificate file. */
42+ private $ applepayCombinedCertPath ;
43+ /** @var string $applepayCertPath Path to merchant ID certificate file. */
44+ private $ applepayCertPath ;
45+ /** @var string $applepayKeyPath Path to merchant ID key file. */
46+ private $ applepayKeyPath ;
5147
5248 /**
5349 * @inheritDoc
5450 */
5551 protected function setUp (): void
5652 {
5753 $ this ->merchantValidationUrl = 'https://apple-pay-gateway-cert.apple.com/paymentservices/startSession ' ;
58- $ this ->merchantValidationCertificatePath = EnvironmentService::getAppleMerchantCertificatePath ();
54+
55+ $ appleMerchantIdPath = EnvironmentService::getAppleMerchantIdPath ();
56+ $ this ->applepayCertPath = $ appleMerchantIdPath . 'merchant_id.pem ' ;
57+ $ this ->applepayKeyPath = $ appleMerchantIdPath . 'merchant_id.key ' ;
58+ $ this ->applepayCombinedCertPath = $ appleMerchantIdPath . 'apple-pay-cert.pem ' ;
5959 $ this ->appleCaCertificatePath = EnvironmentService::getAppleCaCertificatePath ();
6060 }
6161
6262 /**
63- * test merchant validation request.
63+ * Test merchant validation request.
6464 *
6565 * @test
6666 *
@@ -70,7 +70,7 @@ public function verifyMerchantValidationRequest(): void
7070 {
7171 $ applepaySession = $ this ->createApplepaySession ();
7272 $ appleAdapter = new ApplepayAdapter ();
73- $ appleAdapter ->init ($ this ->merchantValidationCertificatePath , null , $ this ->appleCaCertificatePath );
73+ $ appleAdapter ->init ($ this ->applepayCertPath , $ this -> applepayKeyPath , $ this ->appleCaCertificatePath );
7474
7575 $ validationResponse = $ appleAdapter ->validateApplePayMerchant (
7676 $ this ->merchantValidationUrl ,
@@ -81,17 +81,25 @@ public function verifyMerchantValidationRequest(): void
8181 }
8282
8383 /**
84- * test merchant validation request without ca certificate.
84+ * @return ApplepaySession
85+ */
86+ private function createApplepaySession (): ApplepaySession
87+ {
88+ return new ApplepaySession ('merchantIdentifier ' , 'displayName ' , 'domainName ' );
89+ }
90+
91+ /**
92+ * Test merchant validation request without ca certificate.
8593 *
8694 * @test
8795 *
8896 * @throws ApplepayMerchantValidationException
8997 */
90- public function merchantValidationWorksWithoutCaCert (): void
98+ public function merchantValidationWorksWithApplepayCertOnly (): void
9199 {
92100 $ applepaySession = $ this ->createApplepaySession ();
93101 $ appleAdapter = new ApplepayAdapter ();
94- $ appleAdapter ->init ($ this ->merchantValidationCertificatePath );
102+ $ appleAdapter ->init ($ this ->applepayCombinedCertPath );
95103
96104 $ validationResponse = $ appleAdapter ->validateApplePayMerchant (
97105 $ this ->merchantValidationUrl ,
@@ -101,6 +109,68 @@ public function merchantValidationWorksWithoutCaCert(): void
101109 $ this ->assertNotNull ($ validationResponse );
102110 }
103111
112+ /**
113+ * Test merchant validation request without ca certificate.
114+ *
115+ * @test
116+ *
117+ * @throws ApplepayMerchantValidationException
118+ */
119+ public function merchantValidationWorksWithCertAndKey (): void
120+ {
121+ $ applepaySession = $ this ->createApplepaySession ();
122+ $ appleAdapter = new ApplepayAdapter ();
123+ $ appleAdapter ->init ($ this ->applepayCertPath , $ this ->applepayKeyPath );
124+
125+ $ validationResponse = $ appleAdapter ->validateApplePayMerchant (
126+ $ this ->merchantValidationUrl ,
127+ $ applepaySession
128+ );
129+
130+ $ this ->assertNotNull ($ validationResponse );
131+ }
132+
133+ /**
134+ * Test merchant validation request without key and only the merchant id certificate should throw exception.
135+ *
136+ * @test
137+ *
138+ * @throws ApplepayMerchantValidationException
139+ */
140+ public function missingKeyShouldThrowException (): void
141+ {
142+ $ applepaySession = $ this ->createApplepaySession ();
143+ $ appleAdapter = new ApplepayAdapter ();
144+ $ appleAdapter ->init ($ this ->applepayCertPath );
145+
146+ $ this ->expectException (ApplepayMerchantValidationException::class);
147+ $ appleAdapter ->validateApplePayMerchant (
148+ $ this ->merchantValidationUrl ,
149+ $ applepaySession
150+ );
151+ }
152+
153+ /**
154+ * Test merchant validation request without init() call should throw exception.
155+ *
156+ * @test
157+ *
158+ * @throws ApplepayMerchantValidationException
159+ */
160+ public function missingInitCallThrowsException (): void
161+ {
162+ $ applepaySession = $ this ->createApplepaySession ();
163+ $ appleAdapter = new ApplepayAdapter ();
164+
165+ $ this ->expectException (ApplepayMerchantValidationException::class);
166+ $ this ->expectExceptionMessage ('No curl adapter initiated yet. Make sure to cal init() function before. ' );
167+
168+ $ appleAdapter ->validateApplePayMerchant (
169+ $ this ->merchantValidationUrl ,
170+ $ applepaySession
171+ );
172+ }
173+
104174 /**
105175 * Merchant validation call should throw Exception if domain of Validation url is invalid.
106176 *
@@ -111,7 +181,7 @@ public function merchantValidationThrowsErrorForInvalidDomain(): void
111181 {
112182 $ applepaySession = $ this ->createApplepaySession ();
113183 $ appleAdapter = new ApplepayAdapter ();
114- $ appleAdapter ->init ($ this ->merchantValidationCertificatePath );
184+ $ appleAdapter ->init ($ this ->applepayCombinedCertPath );
115185
116186 $ this ->expectException (ApplepayMerchantValidationException::class);
117187 $ this ->expectExceptionMessage ('Invalid URL used for merchantValidation request. ' );
@@ -140,11 +210,18 @@ public function domainShouldBeValidatedCorrectly($validationUrl, $expectedResult
140210 $ this ->assertEquals ($ expectedResult , $ domainValidation );
141211 }
142212
143- /**
144- * @return ApplepaySession
213+ /** Provides different urls to test domain validation.
214+ *
215+ * @return array[]
145216 */
146- private function createApplepaySession (): ApplepaySession
217+ public function domainShouldBeValidatedCorrectlyDP (): array
147218 {
148- return new ApplepaySession ('merchantIdentifier ' , 'displayName ' , 'domainName ' );
219+ return [
220+ 'invalid: example.domain.com ' => ['https://example.domain.com ' , false ],
221+ 'valid: https://apple-pay-gateway.apple.com/some/path ' => ['https://apple-pay-gateway.apple.com/some/path ' , true ],
222+ 'valid: https://cn-apple-pay-gateway.apple.com ' => ['https://cn-apple-pay-gateway.apple.com ' , true ],
223+ 'invalid: apple-pay-gateway-nc-pod1.apple.com ' => ['apple-pay-gateway-nc-pod1.apple.com ' , false ],
224+ 'invalid: (empty) ' => ['' , false ],
225+ ];
149226 }
150227}
0 commit comments