Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
XmlValidatorTest.php
Go to the documentation of this file.
1 <?php
8 
10 use PHPUnit_Framework_MockObject_MockObject as MockObject;
16 
17 class XmlValidatorTest extends \PHPUnit\Framework\TestCase
18 {
22  private $objectManager;
23 
27  private $xmlSecurityMock;
28 
32  private $errorProcessorMock;
33 
37  private $xmlValidator;
38 
42  protected function setUp()
43  {
44  $this->objectManager = new ObjectManager($this);
45 
46  // Mock XML Security object
47  $this->xmlSecurityMock = $this->getMockBuilder(Security::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50 
51  $this->errorProcessorMock = $this->getMockBuilder(ResponseErrorProcessor::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54 
55  $this->xmlValidator = $this->objectManager->getObject(
56  XmlValidator::class,
57  [
58  'xmlSecurity' => $this->xmlSecurityMock,
59  'errorProcessor' => $this->errorProcessorMock,
60  ]
61  );
62  }
63 
67  public function testValidateValidXml()
68  {
69  $rawXml = file_get_contents(__DIR__ . '/_files/validDHLResponse.xml');
70  $this->xmlSecurityMock->expects($this->once())->method('scan')->with($rawXml)->willReturn(true);
71 
72  try {
73  $this->xmlValidator->validate($rawXml);
74  } catch (DocumentValidationException $exception) {
75  $this->fail($exception->getMessage());
76  }
77  }
78 
85  public function testValidateInvalidXml($data)
86  {
87  $phrase = new \Magento\Framework\Phrase('Error #%1 : %2', ['111', 'Error in parsing request XML']);
88  $rawXml = file_get_contents(__DIR__ . '/_files/' . $data['file']);
89  $this->xmlSecurityMock->expects($this->any())
90  ->method('scan')
91  ->with($rawXml)
92  ->willReturn($data['isGenerateXml']);
93  $this->errorProcessorMock->expects($this->any())
94  ->method('process')
95  ->willReturn($phrase);
96 
97  try {
98  $this->xmlValidator->validate($rawXml);
99  } catch (\Magento\Sales\Exception\DocumentValidationException $exception) {
100  $this->assertEquals($data['errorMessage'], $exception->getMessage());
101  if (isset($data['code'])) {
102  $this->assertEquals($data['code'], $exception->getCode());
103  }
104  return;
105  }
106 
107  $this->fail('Exception not thrown for testValidateInvalidXml');
108  }
109 
113  public function invalidXmlResponseProvider()
114  {
115  return [
116  [
117  [
118  'file' => 'invalidDHLResponseWithMissingXmlTag.xml',
119  'errorMessage' => 'The response is in the wrong format',
120  'isGenerateXml' => false,
121  ],
122  ],
123  [
124  [
125  'file' => 'invalidDHLResponse.xml',
126  'errorMessage' => 'The security validation of the XML document has failed.',
127  'isGenerateXml' => false,
128  ],
129  ],
130  [
131  [
132  'file' => 'invalidDHLResponse.xml',
133  'errorMessage' => 'Error #111 : Error in parsing request XML',
134  'isGenerateXml' => true,
135  'code' => 111,
136  ],
137  ],
138 
139  ];
140  }
141 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60