Test Failed
Push — develop ( f1c9c4...c399d3 )
by Reüel
04:34
created

ClientTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 * Client test
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Buckaroo
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Buckaroo;
12
13
use WP_Error;
14
use WP_Http;
15
16
/**
17
 * Title: Buckaroo client tests
18
 * Description:
19
 * Copyright: 2005-2019 Pronamic
20
 * Company: Pronamic
21
 *
22
 * @author  Remco Tolsma
23
 * @version 2.0.0
24
 */
25
class ClientTest extends \WP_UnitTestCase {
26
	/**
27
	 * Mock HTTP responses.
28
	 *
29
	 * @var array
30
	 */
31
	private $mock_http_responses;
32
33
	/**
34
	 * Setup.
35
	 */
36
	public function setUp() {
37
		parent::setUp();
38
39
		$this->mock_http_responses = array();
40
41
		// Mock HTTP response.
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
	 *
60
	 * @param false|array|WP_Error $preempt Whether to preempt an HTTP request's return value. Default false.
61
	 * @param array                $r       HTTP request arguments.
62
	 * @param string               $url     The request URL.
63
	 *
64
	 * @return array
65
	 */
66
	public function pre_http_request( $preempt, $r, $url ) {
67
		if ( ! isset( $this->mock_http_responses[ $url ] ) ) {
68
			return $preempt;
0 ignored issues
show
Bug Best Practice introduced by Reüel van der Steege
The expression return $preempt also could return the type WP_Error|false which is incompatible with the documented return type array.
Loading history...
69
		}
70
71
		$file = $this->mock_http_responses[ $url ];
72
73
		unset( $this->mock_http_responses[ $url ] );
74
75
		$response = file_get_contents( $file, true );
76
77
		$processed_response = WP_Http::processResponse( $response );
78
79
		$processed_headers = WP_Http::processHeaders( $processed_response['headers'], $url );
80
81
		$processed_headers['body'] = $processed_response['body'];
82
83
		return $processed_headers;
84
	}
85
86
	/**
87
	 * Test client.
88
	 */
89
	public function test_client() {
90
		$client = new Client();
91
		$client->set_website_key( getenv( 'BUCKAROO_WEBSITE_KEY' ) );
92
		$client->set_secret_key( getenv( 'BUCKAROO_SECRET_KEY' ) );
93
94
		$this->mock_http_response( 'https://testcheckout.buckaroo.nl/nvp/?op=TransactionRequestSpecification', __DIR__ . '/../http/testcheckout-buckaroo-nl-nvp-op-TransactionRequestSpecification-ok.http' );
95
96
		$issuers = $client->get_issuers();
97
98
		$this->assertInternalType( 'array', $issuers );
99
	}
100
}
101