| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Sergii Bondarenko, <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Behat\SoapExtension\Utils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Trait SoapManager. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package Behat\SoapExtension\Utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | trait SoapManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     use ArrayManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * URL of WSDL service to consume. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var string|null $wsdl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $wsdl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Set of options for SOAP request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Response of SOAP method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $response = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Last SOAP response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $rawResponse = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * The URIs of the namespaces. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     private $namespaces = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Latest exception thrown out during SOAP call. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var null|\SoapFault | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private $exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Make SOAP call to a function with params. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @link http://php.net/manual/en/soapclient.getlastrequest.php#example-5896 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      *   SOAP function name to execute. Use MethodNameIsIgnored if function name is in the XML body. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param array $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      *   Arguments array to pass to soap call function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function sendRequest($function, array $arguments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         // These values can be easily overridden inside of configuration file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->options += [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             // Important for raw response steps. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'trace' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             'exceptions' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             'cache_wsdl' => WSDL_CACHE_NONE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $client = new \SoapClient($this->wsdl, $this->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->response = $client->__soapCall($function, $arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->rawResponse = $client->__getLastResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } catch (\SoapFault $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $this->exception = $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Extracts first value matching provided XPATH expression. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      *   XPATH expression used to extract value from $this->rawResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return \DOMNode|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     protected function extractResponseValueMatchingXPATH($query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         // @todo: Allow users to ignore namespaces via config or steps. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         // @example: $this->rawResponse = str_replace('xmlns=', 'ns=', $this->rawResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $dom = new \DOMDocument(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $dom->loadXML($this->rawResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $xpath = new \DOMXpath($dom); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         foreach ($this->namespaces as $prefix => $uri) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $xpath->registerNamespace($prefix, $uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $nodeList = $xpath->query($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $nodeList->length > 0 ? $nodeList->item(0)->nodeValue : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * Helper to extract a property value from the response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 4 |  |      * @param string $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 4 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     protected function extractResponseProperty($property) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 4 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return static::arrayValue(static::objectToArray($this->response), explode('][', $property)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 4 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return null|\SoapFault | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 4 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     protected function getException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         // When this method was called, this means thrown exception was read and won't be available anymore. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $exception = $this->exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         // Reset the exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->exception = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return $exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param string $wsdl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     protected function setWSDL($wsdl) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         // Allow "null" and valid URLs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $isWsdlValid = null === $wsdl || filter_var($wsdl, FILTER_VALIDATE_URL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         // Set the URL if it is validated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $this->wsdl = $isWsdlValid ? $wsdl : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         // Throw deferred exception when WSDL was reset due to it invalidation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         if (!$isWsdlValid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             throw new \InvalidArgumentException(sprintf('You must pass a correct WSDL or null to %s.', __METHOD__)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     protected function setOptions(array $options = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         if (null === $options) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             $this->options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             foreach ($options as $option => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 $this->setOption($option, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param string $option | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     protected function setOption($option, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $this->options[$option] = is_string($value) && defined($value) ? constant($value) : $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @param array $namespaces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     protected function setNamespaces(array $namespaces = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         if (null === $namespaces) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             $this->namespaces = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             foreach ($namespaces as $prefix => $uri) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                 $this->setNamespace($prefix, $uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * @param string $prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     protected function setNamespace($prefix, $uri) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         $this->namespaces[$prefix] = $uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     public function getResponse() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         return $this->response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |     public function getRawResponse() { | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |         return $this->rawResponse; | 
            
                                                        
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 208 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |  |