Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
XmlTest Class Reference
Inheritance diagram for XmlTest:

Public Member Functions

 testDeserializeInvalidArgumentException ()
 
 testDeserialize ()
 
 testHandleErrors ()
 
 testDeserializeMagentoWebapiExceptionDeveloperModeOn ()
 
 testDeserializeMagentoWebapiExceptionDeveloperModeOff ()
 

Protected Member Functions

 setUp ()
 
 tearDown ()
 

Protected Attributes

 $_xmlParserMock
 
 $_xmlDeserializer
 
 $_appStateMock
 

Detailed Description

Definition at line 8 of file XmlTest.php.

Member Function Documentation

◆ setUp()

setUp ( )
protected

Prepare mocks for SUT constructor.

Initialize SUT.

Definition at line 19 of file XmlTest.php.

20  {
22  $this->_xmlParserMock = $this->createPartialMock(
23  \Magento\Framework\Xml\Parser::class,
24  ['xmlToArray', 'loadXML']
25  );
26  $this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
28  $this->_xmlDeserializer = new \Magento\Framework\Webapi\Rest\Request\Deserializer\Xml(
29  $this->_xmlParserMock,
30  $this->_appStateMock
31  );
32  parent::setUp();
33  }

◆ tearDown()

tearDown ( )
protected

Definition at line 35 of file XmlTest.php.

36  {
37  unset($this->_xmlDeserializer);
38  unset($this->_xmlParserMock);
39  unset($this->_appStateMock);
40  parent::tearDown();
41  }

◆ testDeserialize()

testDeserialize ( )

Prepare mocks for SUT constructor.

Initialize SUT.

Definition at line 50 of file XmlTest.php.

51  {
53  $this->_xmlParserMock->expects($this->once())->method('loadXML');
54  $validInputXml = '<?xml version="1.0"?><xml><key1>test1</key1><key2>test2</key2></xml>';
55  $returnArray = ['xml' => ['key1' => 'test1', 'key2' => 'test2']];
56  $this->_xmlParserMock->expects($this->once())->method('xmlToArray')->will($this->returnValue($returnArray));
57  $expectedArray = ['key1' => 'test1', 'key2' => 'test2'];
59  $this->assertEquals(
60  $expectedArray,
61  $this->_xmlDeserializer->deserialize($validInputXml),
62  'Request XML body was parsed incorrectly into array of params.'
63  );
64  }

◆ testDeserializeInvalidArgumentException()

testDeserializeInvalidArgumentException ( )

Definition at line 43 of file XmlTest.php.

44  {
45  $this->expectException('InvalidArgumentException');
46  $this->expectExceptionMessage('"boolean" data type is invalid. String is expected.');
47  $this->_xmlDeserializer->deserialize(false);
48  }

◆ testDeserializeMagentoWebapiExceptionDeveloperModeOff()

testDeserializeMagentoWebapiExceptionDeveloperModeOff ( )

Prepare mocks for SUT constructor.

Initialize SUT.

Definition at line 117 of file XmlTest.php.

118  {
120  $this->_appStateMock->expects($this->once())
121  ->method('getMode')
122  ->will($this->returnValue('production'));
123  $errorMessage = 'End tag for "key1" was omitted.';
124  $this->_xmlDeserializer->handleErrors(null, $errorMessage, null, null);
125  $this->_xmlParserMock->expects($this->once())->method('loadXML');
126  $invalidXml = '<?xml version="1.0"?><xml><key1>test1</xml>';
128  try {
129  $this->_xmlDeserializer->deserialize($invalidXml);
130  $this->fail("Exception is expected to be raised");
131  } catch (\Magento\Framework\Webapi\Exception $e) {
132  $this->assertInstanceOf(\Magento\Framework\Webapi\Exception::class, $e, 'Exception type is invalid');
133  $this->assertEquals('Decoding error.', $e->getMessage(), 'Exception message is invalid');
134  $this->assertEquals(
135  \Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST,
136  $e->getHttpCode(),
137  'HTTP code is invalid'
138  );
139  }
140  }

◆ testDeserializeMagentoWebapiExceptionDeveloperModeOn()

testDeserializeMagentoWebapiExceptionDeveloperModeOn ( )

Prepare mocks for SUT constructor.

Initialize SUT.

Definition at line 91 of file XmlTest.php.

92  {
94  $this->_appStateMock->expects($this->once())
95  ->method('getMode')
96  ->will($this->returnValue('developer'));
97  $errorMessage = 'End tag for "key1" was omitted.';
98  $this->_xmlDeserializer->handleErrors(null, $errorMessage, null, null);
99  $this->_xmlParserMock->expects($this->once())->method('loadXML');
100  $invalidXml = '<?xml version="1.0"?><xml><key1>test1</xml>';
102  try {
103  $this->_xmlDeserializer->deserialize($invalidXml);
104  $this->fail("Exception is expected to be raised");
105  } catch (\Magento\Framework\Webapi\Exception $e) {
106  $exceptionMessage = 'Decoding Error: End tag for "key1" was omitted.';
107  $this->assertInstanceOf(\Magento\Framework\Webapi\Exception::class, $e, 'Exception type is invalid');
108  $this->assertEquals($exceptionMessage, $e->getMessage(), 'Exception message is invalid');
109  $this->assertEquals(
110  \Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST,
111  $e->getHttpCode(),
112  'HTTP code is invalid'
113  );
114  }
115  }

◆ testHandleErrors()

testHandleErrors ( )

Add error message

Assert that first error message was added

Add error message

Assert that both error messages were added

Definition at line 66 of file XmlTest.php.

67  {
69  $firstErrorMessage = "No document type declaration. ";
70  $this->_xmlDeserializer->handleErrors(null, $firstErrorMessage, null, null);
72  $this->assertAttributeEquals(
73  $firstErrorMessage,
74  '_errorMessage',
75  $this->_xmlDeserializer,
76  'Error message was not set to xml deserializer.'
77  );
79  $secondErrorMessage = "Strings should be wrapped in double quotes.";
80  $expectedMessages = $firstErrorMessage . $secondErrorMessage;
81  $this->_xmlDeserializer->handleErrors(null, $secondErrorMessage, null, null);
83  $this->assertAttributeEquals(
84  $expectedMessages,
85  '_errorMessage',
86  $this->_xmlDeserializer,
87  'Error messages were not set to xml deserializer.'
88  );
89  }

Field Documentation

◆ $_appStateMock

$_appStateMock
protected

Definition at line 17 of file XmlTest.php.

◆ $_xmlDeserializer

$_xmlDeserializer
protected

Definition at line 14 of file XmlTest.php.

◆ $_xmlParserMock

$_xmlParserMock
protected

Definition at line 11 of file XmlTest.php.


The documentation for this class was generated from the following file: