FilesystemTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Test\Unit\Reader;
  7. use \Magento\Framework\Config\Reader\Filesystem;
  8. class FilesystemTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $_fileResolverMock;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $_converterMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $_schemaLocatorMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $_validationStateMock;
  26. /**
  27. * @var \Magento\Framework\Config\Dom\UrnResolver
  28. */
  29. protected $urnResolver;
  30. /**
  31. * @var string
  32. */
  33. protected $_file;
  34. protected function setUp()
  35. {
  36. if (!function_exists('libxml_set_external_entity_loader')) {
  37. $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
  38. }
  39. $this->_file = file_get_contents(__DIR__ . '/../_files/reader/config.xml');
  40. $this->_fileResolverMock = $this->createMock(\Magento\Framework\Config\FileResolverInterface::class);
  41. $this->_converterMock = $this->createMock(\Magento\Framework\Config\ConverterInterface::class);
  42. $this->_schemaLocatorMock = $this->createMock(\Magento\Framework\Config\SchemaLocatorInterface::class);
  43. $this->_validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
  44. $this->urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
  45. }
  46. public function testRead()
  47. {
  48. $model = new Filesystem(
  49. $this->_fileResolverMock,
  50. $this->_converterMock,
  51. $this->_schemaLocatorMock,
  52. $this->_validationStateMock,
  53. 'fileName',
  54. []
  55. );
  56. $this->_fileResolverMock->expects($this->once())->method('get')->will($this->returnValue([$this->_file]));
  57. $dom = new \DomDocument();
  58. $dom->loadXML($this->_file);
  59. $this->_converterMock->expects($this->once())->method('convert')->with($dom);
  60. $model->read('scope');
  61. }
  62. public function testReadWithoutFiles()
  63. {
  64. $model = new Filesystem(
  65. $this->_fileResolverMock,
  66. $this->_converterMock,
  67. $this->_schemaLocatorMock,
  68. $this->_validationStateMock,
  69. 'fileName',
  70. []
  71. );
  72. $this->_fileResolverMock
  73. ->expects($this->once())->method('get')->will($this->returnValue([]));
  74. $this->assertEmpty($model->read('scope'));
  75. }
  76. /**
  77. * @expectedException \Magento\Framework\Exception\LocalizedException
  78. * @expectedExceptionMessage Invalid Document
  79. */
  80. public function testReadWithInvalidDom()
  81. {
  82. $this->_schemaLocatorMock->expects(
  83. $this->once()
  84. )->method(
  85. 'getSchema'
  86. )->will(
  87. $this->returnValue(
  88. $this->urnResolver->getRealPath('urn:magento:framework:Config/Test/Unit/_files/reader/schema.xsd')
  89. )
  90. );
  91. $this->_validationStateMock->expects($this->any())
  92. ->method('isValidationRequired')
  93. ->willReturn(true);
  94. $model = new Filesystem(
  95. $this->_fileResolverMock,
  96. $this->_converterMock,
  97. $this->_schemaLocatorMock,
  98. $this->_validationStateMock,
  99. 'fileName',
  100. []
  101. );
  102. $this->_fileResolverMock->expects($this->once())->method('get')->will($this->returnValue([$this->_file]));
  103. $model->read('scope');
  104. }
  105. /**
  106. * @expectedException \Magento\Framework\Exception\LocalizedException
  107. * @expectedExceptionMessage The XML in file "0" is invalid:
  108. */
  109. public function testReadWithInvalidXml()
  110. {
  111. $this->_schemaLocatorMock->expects(
  112. $this->any()
  113. )->method(
  114. 'getPerFileSchema'
  115. )->will(
  116. $this->returnValue(
  117. $this->urnResolver->getRealPath('urn:magento:framework:Config/Test/Unit/_files/reader/schema.xsd')
  118. )
  119. );
  120. $this->_validationStateMock->expects($this->any())
  121. ->method('isValidationRequired')
  122. ->willReturn(true);
  123. $model = new Filesystem(
  124. $this->_fileResolverMock,
  125. $this->_converterMock,
  126. $this->_schemaLocatorMock,
  127. $this->_validationStateMock,
  128. 'fileName',
  129. []
  130. );
  131. $this->_fileResolverMock->expects($this->once())->method('get')->will($this->returnValue([$this->_file]));
  132. $model->read('scope');
  133. }
  134. /**
  135. * @expectedException \UnexpectedValueException
  136. * @expectedExceptionMessage Instance of the DOM config merger is expected, got StdClass instead.
  137. */
  138. public function testReadException()
  139. {
  140. $this->_fileResolverMock->expects($this->once())->method('get')->will($this->returnValue([$this->_file]));
  141. $model = new Filesystem(
  142. $this->_fileResolverMock,
  143. $this->_converterMock,
  144. $this->_schemaLocatorMock,
  145. $this->_validationStateMock,
  146. 'fileName',
  147. [],
  148. 'StdClass'
  149. );
  150. $model->read();
  151. }
  152. }