1 | <?php |
||
2 | |||
3 | namespace Pronamic\WordPress\Pay\Gateways\PayNL; |
||
4 | |||
5 | use WP_UnitTestCase; |
||
6 | use WP_Http; |
||
7 | |||
8 | /** |
||
9 | * Title: Pay.nl client test |
||
10 | * Description: |
||
11 | * Copyright: 2005-2020 Pronamic |
||
12 | * Company: Pronamic |
||
13 | * |
||
14 | * @author Remco Tolsma |
||
15 | * @version 2.0.4 |
||
16 | * @since 1.0.0 |
||
17 | */ |
||
18 | class ClientTest extends \WP_UnitTestCase { |
||
19 | /** |
||
20 | * Mock HTTP responses. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | private $mock_http_responses; |
||
25 | |||
26 | /** |
||
27 | * Setup. |
||
28 | */ |
||
29 | public function setUp() { |
||
30 | parent::setUp(); |
||
31 | |||
32 | $this->mock_http_responses = array(); |
||
33 | |||
34 | // Mock HTTP response. |
||
35 | add_filter( 'pre_http_request', array( $this, 'pre_http_request' ), 10, 3 ); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Mock HTTP response. |
||
40 | * |
||
41 | * @param string $url URL. |
||
42 | * @param string $file File with HTTP response. |
||
43 | */ |
||
44 | public function mock_http_response( $url, $file ) { |
||
45 | $this->mock_http_responses[ $url ] = $file; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Pre HTTP request |
||
50 | * |
||
51 | * @link https://github.com/WordPress/WordPress/blob/3.9.1/wp-includes/class-http.php#L150-L164 |
||
52 | * |
||
53 | * @param false|array|\WP_Error $preempt Whether to preempt an HTTP request's return value. Default false. |
||
54 | * @param array $r HTTP request arguments. |
||
55 | * @param string $url The request URL. |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | public function pre_http_request( $preempt, $r, $url ) { |
||
60 | if ( ! isset( $this->mock_http_responses[ $url ] ) ) { |
||
61 | return $preempt; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
62 | } |
||
63 | |||
64 | $file = $this->mock_http_responses[ $url ]; |
||
65 | |||
66 | $response = file_get_contents( $file, true ); |
||
67 | |||
68 | $processed_response = WP_Http::processResponse( $response ); |
||
69 | |||
70 | $processed_headers = WP_Http::processHeaders( $processed_response['headers'], $url ); |
||
71 | |||
72 | $processed_headers['body'] = $processed_response['body']; |
||
73 | |||
74 | return $processed_headers; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Test get issuers. |
||
79 | * |
||
80 | * @throws \Exception Throws exception if service can not be found. |
||
81 | */ |
||
82 | public function test_get_issuers() { |
||
83 | $this->mock_http_response( 'https://rest-api.pay.nl/v4/Transaction/getService/json/?token&serviceId&paymentMethodId=10', dirname( dirname( __FILE__ ) ) . '/http/transaction-get-service-json-ideal-service-not-found.http' ); |
||
84 | |||
85 | $client = new Client( '', '' ); |
||
86 | |||
87 | $this->expectException( \Exception::class ); |
||
88 | $this->expectExceptionMessage( 'PAY-404 - Service not found' ); |
||
89 | |||
90 | $client->get_issuers(); |
||
91 | } |
||
92 | } |
||
93 |