1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\MockSoapClient; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use SoapClient; |
9
|
|
|
use SoapFault; |
10
|
|
|
use SoapHeader; |
11
|
|
|
|
12
|
|
|
use function array_key_exists; |
13
|
|
|
use function count; |
14
|
|
|
use function func_get_args; |
15
|
|
|
use function is_callable; |
16
|
|
|
|
17
|
|
|
use const ARRAY_FILTER_USE_KEY; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class MockSoapClient. |
21
|
|
|
*/ |
22
|
|
|
class MockSoapClient extends SoapClient |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array<string, int> |
26
|
|
|
*/ |
27
|
|
|
private $indexes; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array<mixed> |
31
|
|
|
*/ |
32
|
|
|
private $responses; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* MockSoapClient constructor. |
36
|
|
|
* |
37
|
|
|
* @param array<mixed>|callable $responses |
38
|
|
|
*/ |
39
|
9 |
|
public function __construct($responses = null) |
40
|
|
|
{ |
41
|
9 |
|
$responses = (array) $responses; |
42
|
|
|
|
43
|
9 |
|
if ([] === $responses) { |
44
|
3 |
|
throw new InvalidArgumentException('The response argument cannot be empty.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
$this->responses = $responses; |
48
|
6 |
|
$this->indexes = [ |
49
|
|
|
'*' => 0, |
50
|
|
|
]; |
51
|
6 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $function_name |
55
|
|
|
* @param array<mixed> $arguments |
56
|
|
|
* |
57
|
|
|
* @throws \SoapFault |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
3 |
|
public function __call($function_name, $arguments = []) |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
3 |
|
$response = $this->__soapCall($function_name, $arguments); |
65
|
1 |
|
} catch (SoapFault $exception) { |
66
|
1 |
|
throw $exception; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return $response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $function_name |
74
|
|
|
* @param array<mixed> $arguments |
75
|
|
|
* @param array<mixed>|null $options |
76
|
|
|
* @param array<mixed>|SoapHeader|null $input_headers |
77
|
|
|
* @param array<mixed>|null $output_headers |
78
|
|
|
* |
79
|
|
|
* @throws SoapFault |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
6 |
|
public function __soapCall( |
84
|
|
|
$function_name, |
85
|
|
|
$arguments, |
86
|
|
|
$options = null, |
87
|
|
|
$input_headers = null, |
88
|
|
|
&$output_headers = null |
89
|
|
|
) { |
90
|
6 |
|
if (false === array_key_exists($function_name, $this->indexes)) { |
91
|
6 |
|
$this->indexes[$function_name] = 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
$index = &$this->indexes['*']; |
95
|
6 |
|
$responses = $this->responses; |
96
|
|
|
|
97
|
6 |
|
if (true === array_key_exists($function_name, $responses)) { |
98
|
1 |
|
$responses = (array) $responses[$function_name]; |
99
|
1 |
|
$index = &$this->indexes[$function_name]; |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
$index %= count( |
103
|
6 |
|
array_filter( |
104
|
6 |
|
$responses, |
105
|
|
|
static function (string $key) { |
106
|
6 |
|
return is_numeric($key); |
107
|
6 |
|
}, |
108
|
6 |
|
ARRAY_FILTER_USE_KEY |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
|
112
|
6 |
|
$response = $responses[$index]; |
113
|
|
|
|
114
|
6 |
|
if (is_callable($response)) { |
115
|
4 |
|
++$index; |
116
|
|
|
|
117
|
4 |
|
return ($response)(...func_get_args()); |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
if ($response instanceof SoapFault) { |
121
|
1 |
|
throw $response; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
++$index; |
125
|
|
|
|
126
|
2 |
|
return $response; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|