Passed
Push — main ( 061772...28b955 )
by Remco
07:49 queued 12s
created

GatewayTest::pre_http_request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gateway Test
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Payvision
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Payvision;
12
13
use Pronamic\WordPress\Money\TaxedMoney;
14
use Pronamic\WordPress\Pay\Core\PaymentMethods;
15
use Pronamic\WordPress\Pay\Payments\Payment;
16
use Pronamic\WordPress\Pay\Payments\PaymentStatus;
17
18
/**
19
 * Gateway Test
20
 *
21
 * @author  Remco Tolsma
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 */
25
class GatewayTest extends \WP_UnitTestCase {
26
	/**
27
	 * Mock HTTP responses.
28
	 *
29
	 * @var array<string, string>
30
	 */
31
	private $mock_http_responses;
32
33
	/**
34
	 * Setup.
35
	 */
36
	public function setUp() {
37
		parent::setUp();
38
39
		// Mock HTTP responses.
40
		$this->mock_http_responses = array();
41
42
		\add_filter( 'pre_http_request', array( $this, 'pre_http_request' ), 10, 3 );
43
	}
44
45
	/**
46
	 * Mock HTTP response.
47
	 *
48
	 * @param string $url  URL.
49
	 * @param string $file File with HTTP response.
50
	 */
51
	public function mock_http_response( $url, $file ) {
52
		$this->mock_http_responses[ $url ] = $file;
53
	}
54
55
	/**
56
	 * Pre HTTP request
57
	 *
58
	 * @link https://github.com/WordPress/WordPress/blob/3.9.1/wp-includes/class-http.php#L150-L164
59
	 * @param bool                 $preempt Whether to preempt an HTTP request's return value. Default false.
60
	 * @param array<string, mixed> $r       HTTP request arguments.
61
	 * @param string               $url     The request URL.
62
	 * @return array<string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
63
	 */
64
	public function pre_http_request( $preempt, $r, $url ) {
65
		if ( ! isset( $this->mock_http_responses[ $url ] ) ) {
66
			return $preempt;
67
		}
68
69
		$file = $this->mock_http_responses[ $url ];
70
71
		unset( $this->mock_http_responses[ $url ] );
72
73
		$response = \file_get_contents( $file, true );
74
75
		$processed_response = \WP_Http::processResponse( $response );
76
77
		$processed_headers = \WP_Http::processHeaders( $processed_response['headers'], $url );
78
79
		$processed_headers['body'] = $processed_response['body'];
80
81
		// The `arguments` key is not an officiale WordPress core key, added for SlevomatCodingStandard compliance.
82
		$processed_headers['arguments'] = $r;
83
84
		return $processed_headers;
85
	}
86
87
	/**
88
	 * Test gateway.
89
	 */
90
	public function test_gateway() {
91
		$config = new Config( Gateway::MODE_TEST, '123456', 'Test', '●●●●●●●●', '1' );
92
93
		$gateway = new Gateway( $config );
94
95
		$this->assertTrue( $gateway->payment_method_is_required() );
96
97
		// iDEAL issuers.
98
		$issuers = $gateway->get_issuers();
99
100
		$this->assertCount( 1, $issuers );
101
102
		// Payment methods.
103
		$methods = $gateway->get_supported_payment_methods();
104
105
		$this->assertCount( 1, $methods );
106
		$this->assertContains( PaymentMethods::IDEAL, $methods );
107
	}
108
109
	/**
110
	 * Test get payment status.
111
	 */
112 View Code Duplication
	public function test_get_payment_status() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
		$config = new Config( Gateway::MODE_TEST, '123456', 'Test', '●●●●●●●●', '1' );
114
115
		$gateway = new Gateway( $config );
116
117
		$this->mock_http_response(
118
			'https://stagconnect.acehubpaymentservices.com/gateway/v3/payments/00a502ba-d289-4ee1-a43e-3c4e1de76b4d',
119
			__DIR__ . '/../http/get-payment-result-0.http'
120
		);
121
122
		$payment = new Payment();
123
124
		$payment->set_transaction_id( '00a502ba-d289-4ee1-a43e-3c4e1de76b4d' );
125
126
		$gateway->update_status( $payment );
127
128
		$this->assertEquals( PaymentStatus::SUCCESS, $payment->get_status() );
129
	}
130
131
	/**
132
	 * Test get payment status.
133
	 */
134 View Code Duplication
	public function test_get_payment_status_fake() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
		$config = new Config( Gateway::MODE_TEST, '123456', 'Test', '●●●●●●●●', '1' );
136
137
		$gateway = new Gateway( $config );
138
139
		$this->mock_http_response(
140
			'https://stagconnect.acehubpaymentservices.com/gateway/v3/payments/00a502ba-d289-4ee1-a43e-3c4e1de76b4e',
141
			__DIR__ . '/../http/get-payment-by-fake-id.http'
142
		);
143
144
		$payment = new Payment();
145
146
		$payment->set_transaction_id( '00a502ba-d289-4ee1-a43e-3c4e1de76b4e' );
147
148
		$this->expectException( \Throwable::class );
149
		$this->expectExceptionMessage(
150
			'Could not JSON decode Payvision response, HTTP response: "404 Not Found", HTTP body length: "2", JSON error: "Syntax error".'
151
		);
152
153
		$gateway->update_status( $payment );
154
	}
155
156
	/**
157
	 * Test get payment empty body.
158
	 */
159 View Code Duplication
	public function test_get_payment_empty_body() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
		$config = new Config( Gateway::MODE_TEST, '123456', 'Test', '●●●●●●●●', '1' );
161
162
		$gateway = new Gateway( $config );
163
164
		$this->mock_http_response(
165
			'https://stagconnect.acehubpaymentservices.com/gateway/v3/payments/00a502ba-d289-4ee1-a43e-3c4e1de76b4e',
166
			__DIR__ . '/../http/get-payment-body-empty.http'
167
		);
168
169
		$payment = new Payment();
170
171
		$payment->set_transaction_id( '00a502ba-d289-4ee1-a43e-3c4e1de76b4e' );
172
173
		$this->expectException( \Throwable::class );
174
		$this->expectExceptionMessage( 'Payvision response is empty, HTTP response: "200 OK".' );
175
176
		$gateway->update_status( $payment );
177
	}
178
179
	/**
180
	 * Test get payment no object.
181
	 */
182 View Code Duplication
	public function test_get_payment_no_object() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
		$config = new Config( Gateway::MODE_TEST, '123456', 'Test', '●●●●●●●●', '1' );
184
185
		$gateway = new Gateway( $config );
186
187
		$this->mock_http_response(
188
			'https://stagconnect.acehubpaymentservices.com/gateway/v3/payments/00a502ba-d289-4ee1-a43e-3c4e1de76b4e',
189
			__DIR__ . '/../http/get-payment-no-object.http'
190
		);
191
192
		$payment = new Payment();
193
194
		$payment->set_transaction_id( '00a502ba-d289-4ee1-a43e-3c4e1de76b4e' );
195
196
		$this->expectException( \Throwable::class );
197
		$this->expectExceptionMessage(
198
			'Could not JSON decode Payvision response to an object, HTTP response: "200 OK", HTTP body: "[]".'
199
		);
200
201
		$gateway->update_status( $payment );
202
	}
203
204
	/**
205
	 * Test WordPress error.
206
	 */
207
	public function test_wp_error() {
208
		$config = new Config( Gateway::MODE_TEST, '123456', 'Test', '●●●●●●●●', '1' );
209
210
		$gateway = new Gateway( $config );
211
212
		$filter = function() {
213
			return new \WP_Error( 'http_request_failed', 'A valid URL was not provided.' );
214
		};
215
216
		\add_filter( 'pre_http_request', $filter );
217
218
		$payment = new Payment();
219
220
		$payment->set_transaction_id( '00a502ba-d289-4ee1-a43e-3c4e1de76b4e' );
221
222
		$this->expectException( \Throwable::class );
223
		$this->expectExceptionMessage( 'A valid URL was not provided.' );
224
225
		$gateway->update_status( $payment );
226
227
		\remove_filter( 'pre_http_request', $filter );
228
	}
229
230
	/**
231
	 * Test post payment.
232
	 */
233
	public function test_post_payment() {
234
		$config = new Config( Gateway::MODE_TEST, '123456', 'Test', '●●●●●●●●', '1' );
235
236
		$gateway = new Gateway( $config );
237
238
		$this->mock_http_response(
239
			'https://stagconnect.acehubpaymentservices.com/gateway/v3/payments',
240
			__DIR__ . '/../http/post-payment.http'
241
		);
242
243
		$payment = new Payment();
244
245
		$payment->set_id( 1 );
246
247
		$payment->set_total_amount( new TaxedMoney( 50, 'EUR' ) );
248
249
		$gateway->start( $payment );
250
251
		$this->assertEquals(
252
			'https://test.acaptureservices.com/connectors/demo/ideal/simulator/paymentSimulation.ftl;jsessionid=5D94958C082AE32D58841306599361A5.uat01-vm-con02',
253
			$payment->get_action_url()
254
		);
255
256
		$this->assertEquals( '0c5b2580-45b9-440b-bef4-1ca016854afd', $payment->get_transaction_id() );
257
	}
258
}
259