| 676 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Srmklive\PayPal\Tests;
|
|
|
4 |
|
|
|
5 |
use GuzzleHttp\Client as HttpClient;
|
|
|
6 |
use GuzzleHttp\Handler\MockHandler as HttpMockHandler;
|
|
|
7 |
use GuzzleHttp\HandlerStack as HttpHandlerStack;
|
|
|
8 |
use GuzzleHttp\Psr7\Response as HttpResponse;
|
|
|
9 |
use GuzzleHttp\Utils;
|
|
|
10 |
use Psr\Http\Message\ResponseInterface;
|
|
|
11 |
use Srmklive\PayPal\Services\PayPal as PayPalClient;
|
|
|
12 |
|
|
|
13 |
trait MockClientClasses
|
|
|
14 |
{
|
|
|
15 |
private function mock_http_client($response): HttpClient
|
|
|
16 |
{
|
|
|
17 |
$mock = new HttpMockHandler([
|
|
|
18 |
new HttpResponse(
|
|
|
19 |
200,
|
|
|
20 |
[],
|
|
|
21 |
($response === false) ? '' : Utils::jsonEncode($response)
|
|
|
22 |
),
|
|
|
23 |
]);
|
|
|
24 |
|
|
|
25 |
$handler = HttpHandlerStack::create($mock);
|
|
|
26 |
|
|
|
27 |
return new HttpClient(['handler' => $handler]);
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
private function mock_http_request($expectedResponse, $expectedEndpoint, $expectedParams, $expectedMethod = 'post')
|
|
|
31 |
{
|
|
|
32 |
$set_method_name = 'setMethods';
|
|
|
33 |
if (function_exists('onlyMethods')) {
|
|
|
34 |
$set_method_name = 'onlyMethods';
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
$mockResponse = $this->getMockBuilder(ResponseInterface::class)
|
|
|
38 |
->getMock();
|
|
|
39 |
$mockResponse->expects($this->exactly(1))
|
|
|
40 |
->method('getBody')
|
|
|
41 |
->willReturn($expectedResponse);
|
|
|
42 |
|
|
|
43 |
$mockHttpClient = $this->getMockBuilder(HttpClient::class)
|
|
|
44 |
->{$set_method_name}([$expectedMethod])
|
|
|
45 |
->getMock();
|
|
|
46 |
$mockHttpClient->expects($this->once())
|
|
|
47 |
->method($expectedMethod)
|
|
|
48 |
->with($expectedEndpoint, $expectedParams)
|
|
|
49 |
->willReturn($mockResponse);
|
|
|
50 |
|
|
|
51 |
return $mockHttpClient;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
private function mock_client($expectedResponse, $expectedMethod, $token = false)
|
|
|
55 |
{
|
|
|
56 |
$set_method_name = 'setMethods';
|
|
|
57 |
if (function_exists('onlyMethods')) {
|
|
|
58 |
$set_method_name = 'onlyMethods';
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$methods = [$expectedMethod, 'setApiCredentials'];
|
|
|
62 |
if ($token) {
|
|
|
63 |
$methods[] = 'getAccessToken';
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$mockClient = $this->getMockBuilder(PayPalClient::class)
|
|
|
67 |
->{$set_method_name}($methods)
|
|
|
68 |
->getMock();
|
|
|
69 |
|
|
|
70 |
if ($token) {
|
|
|
71 |
$mockClient->expects($this->exactly(1))
|
|
|
72 |
->method('getAccessToken');
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$mockClient->expects($this->exactly(1))
|
|
|
76 |
->method('setApiCredentials');
|
|
|
77 |
|
|
|
78 |
$mockClient->expects($this->exactly(1))
|
|
|
79 |
->method($expectedMethod)
|
|
|
80 |
->willReturn($expectedResponse);
|
|
|
81 |
|
|
|
82 |
return $mockClient;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private function getMockCredentials(): array
|
|
|
86 |
{
|
|
|
87 |
return [
|
|
|
88 |
'mode' => 'sandbox',
|
|
|
89 |
'sandbox' => [
|
|
|
90 |
'client_id' => 'some-client-id',
|
|
|
91 |
'client_secret' => 'some-access-token',
|
|
|
92 |
'app_id' => 'some-app-id',
|
|
|
93 |
],
|
|
|
94 |
'payment_action' => 'Sale',
|
|
|
95 |
'currency' => 'USD',
|
|
|
96 |
'notify_url' => '',
|
|
|
97 |
'locale' => 'en_US',
|
|
|
98 |
'validate_ssl' => true,
|
|
|
99 |
];
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
private function getApiCredentials(): array
|
|
|
103 |
{
|
|
|
104 |
return [
|
|
|
105 |
'mode' => 'sandbox',
|
|
|
106 |
'sandbox' => [
|
|
|
107 |
'client_id' => 'AbJgVQM6g57qPrXimGkBz1UaBOXn1dKLSdUj7BgiB3JhzJRCapzCnkPq6ycOOmgXHtnDZcjwLMJ2IdAI',
|
|
|
108 |
'client_secret' => 'EPd_XBNkfhU3-MlSw6gpa6EJj9x8QBdsC3o77jZZWjcFy_hrjR4kzBP8QN3MPPH4g52U_acG4-ogWUxI',
|
|
|
109 |
'app_id' => 'APP-80W284485P519543T',
|
|
|
110 |
],
|
|
|
111 |
'payment_action' => 'Sale',
|
|
|
112 |
'currency' => 'USD',
|
|
|
113 |
'notify_url' => '',
|
|
|
114 |
'locale' => 'en_US',
|
|
|
115 |
'validate_ssl' => true,
|
|
|
116 |
];
|
|
|
117 |
}
|
|
|
118 |
}
|