1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
namespace Behat\SoapExtension\Utils; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class SoapFaultProcessor. |
9
|
|
|
* |
10
|
|
|
* @package Behat\SoapExtension\Utils |
11
|
|
|
*/ |
12
|
|
|
class SoapFaultProcessor |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* An exception thrown by \SoapCall. |
16
|
|
|
* |
17
|
|
|
* @var \SoapFault |
18
|
|
|
*/ |
19
|
|
|
private $exception; |
20
|
|
|
/** |
21
|
|
|
* Messages collected during processing. |
22
|
|
|
* |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $errors = []; |
26
|
|
|
/** |
27
|
|
|
* Expected error code. |
28
|
|
|
* |
29
|
|
|
* @var null|string |
30
|
|
|
*/ |
31
|
|
|
private $code; |
32
|
|
|
/** |
33
|
|
|
* Expected error message. |
34
|
|
|
* |
35
|
|
|
* @var null|string |
36
|
|
|
*/ |
37
|
|
|
private $message; |
38
|
|
|
/** |
39
|
|
|
* Condition between comparison of error code and message. |
40
|
|
|
* |
41
|
|
|
* @var null|string |
42
|
|
|
*/ |
43
|
|
|
private $condition; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* SoapFaultProcessor constructor. |
47
|
|
|
* |
48
|
|
|
* @param \SoapFault $exception |
49
|
|
|
* @param null|string $code |
50
|
|
|
* Any numeric value. Type will be casted to "int". |
51
|
|
|
* @param null|string $message |
52
|
|
|
* Expected message. Inaccurate matching will be used. |
53
|
|
|
* @param null|string $condition |
54
|
|
|
* Allowed values: "or", "and". |
55
|
|
|
*/ |
56
|
|
|
public function __construct(\SoapFault $exception, $code = null, $message = null, $condition = null) |
57
|
|
|
{ |
58
|
|
|
$this->code = $code; |
59
|
|
|
$this->message = $message; |
60
|
|
|
$this->condition = empty($condition) ? '' : trim($condition); |
61
|
|
|
$this->exception = $exception; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Process the data. |
66
|
|
|
*/ |
67
|
|
|
public function __destruct() |
68
|
|
|
{ |
69
|
|
|
$this->processCode() |
70
|
|
|
->processMessage() |
71
|
|
|
->processCondition(); |
72
|
|
|
|
73
|
|
|
if (!empty($this->errors)) { |
74
|
|
|
throw new \RuntimeException(implode("\n", $this->errors)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Process expected exit code. |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
private function processCode() |
84
|
|
|
{ |
85
|
|
|
if (null !== $this->code) { |
86
|
|
|
$value = $this->exception->getCode(); |
87
|
|
|
|
88
|
|
|
if ($value !== (int) $this->code) { |
89
|
|
|
$this->errors[] = sprintf('Exit code "%s" does not match with expected.', $value); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Process expected error message. |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
private function processMessage() |
102
|
|
|
{ |
103
|
|
|
if (null !== $this->message) { |
104
|
|
|
$value = $this->exception->getMessage(); |
105
|
|
|
|
106
|
|
|
if (strpos($value, trim($this->message)) === false) { |
107
|
|
|
$this->errors[] = sprintf('Exception message "%s" does not contain expected value.', $value); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Filter messages depending on condition. |
116
|
|
|
*/ |
117
|
|
|
private function processCondition() |
118
|
|
|
{ |
119
|
|
|
if (!empty($this->condition)) { |
120
|
|
|
$errors = count($this->errors); |
121
|
|
|
|
122
|
|
|
// - At least one message needed to be able to choose one from and meet "or" condition. |
123
|
|
|
// - No failed assertions should be present in order to meet "and" condition. |
124
|
|
|
if (('or' === $this->condition && $errors < 2) || ('and' === $this->condition && $errors < 1)) { |
125
|
|
|
$this->errors = []; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|