public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOn() { /** Prepare mocks for SUT constructor. */ $this->decoderMock->expects($this->once())->method('decode')->will($this->throwException(new \Zend_Json_Exception('Decoding error:' . PHP_EOL . 'Decoding failed: Syntax error'))); $this->_appStateMock->expects($this->once())->method('getMode')->will($this->returnValue('developer')); /** Initialize SUT. */ $inputInvalidJson = '{"key1":"test1"."key2":"test2"}'; try { $this->_jsonDeserializer->deserialize($inputInvalidJson); $this->fail("Exception is expected to be raised"); } catch (\Magento\Framework\Webapi\Exception $e) { $this->assertInstanceOf('Magento\\Framework\\Webapi\\Exception', $e, 'Exception type is invalid'); $this->assertContains('Decoding error:', $e->getMessage(), 'Exception message is invalid'); $this->assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getHttpCode(), 'HTTP code is invalid'); } }
/** * Parse Request body into array of params. * * @param string $encodedBody Posted content from request. * @return array|null Return NULL if content is invalid. * @throws \InvalidArgumentException * @throws \Magento\Framework\Webapi\Exception If decoding error was encountered. */ public function deserialize($encodedBody) { if (!is_string($encodedBody)) { throw new \InvalidArgumentException(sprintf('"%s" data type is invalid. String is expected.', gettype($encodedBody))); } try { $decodedBody = $this->decoder->decode($encodedBody); } catch (\Zend_Json_Exception $e) { if ($this->_appState->getMode() !== State::MODE_DEVELOPER) { throw new \Magento\Framework\Webapi\Exception(new Phrase('Decoding error.')); } else { throw new \Magento\Framework\Webapi\Exception(new Phrase('Decoding error: %1%2%3%4', [PHP_EOL, $e->getMessage(), PHP_EOL, $e->getTraceAsString()])); } } return $decodedBody; }
/** * Filters parser by language. * * @param \DOMXPath $xpath XPath access to the document parsed. * @param string $rootNodeName Parsing root node. * @param string $nodeName Name of the nodes look up. * @param string $language Language searched. * * @return array */ private function parseFilters(\DOMXPath $xpath, $rootNodeName, $nodeName, $language = 'default') { $filters = []; $languagePath = sprintf("[@language='%s']", $language); $searchPath = sprintf("/%s/%s/%s%s", self::ROOT_NODE_NAME, $rootNodeName, $nodeName, $languagePath); $filterNodes = $xpath->query($searchPath); foreach ($filterNodes as $filterNode) { $filterName = $filterNode->getAttribute('name'); $filter = ['type' => $filterNode->getAttribute('type')]; foreach ($filterNode->childNodes as $childNode) { if ($childNode instanceof \DOMElement) { try { $filter[$childNode->tagName] = $this->jsonDecoder->decode($childNode->nodeValue); } catch (\Exception $e) { $filter[$childNode->tagName] = $childNode->nodeValue; } } } $filters[$filterName] = $filter; } return $filters; }
/** * Get config * * @return array */ public function getConfig() { return $this->jsonDecoder->decode($this->getData(self::CONFIG)); }