ReaderTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Email\Test\Unit\Model\Template\Config;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class ReaderTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Email\Model\Template\Config\Reader
  14. */
  15. protected $_model;
  16. /**
  17. * @var \Magento\Catalog\Model\Attribute\Config\Converter|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $_converter;
  20. /**
  21. * @var \Magento\Framework\Module\Dir\ReverseResolver|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $_moduleDirResolver;
  24. /**
  25. * @var \Magento\Framework\Filesystem\File\Read|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $read;
  28. /**
  29. * Paths to fixtures
  30. *
  31. * @var array
  32. */
  33. protected $_paths;
  34. protected function setUp()
  35. {
  36. $fileResolver = $this->createMock(\Magento\Email\Model\Template\Config\FileResolver::class);
  37. $this->_paths = [
  38. __DIR__ . '/_files/Fixture/ModuleOne/etc/email_templates_one.xml',
  39. __DIR__ . '/_files/Fixture/ModuleTwo/etc/email_templates_two.xml',
  40. ];
  41. $this->_converter = $this->createPartialMock(
  42. \Magento\Email\Model\Template\Config\Converter::class,
  43. ['convert']
  44. );
  45. $moduleReader = $this->createPartialMock(\Magento\Framework\Module\Dir\Reader::class, ['getModuleDir']);
  46. $moduleReader->expects(
  47. $this->once()
  48. )->method(
  49. 'getModuleDir'
  50. )->with(
  51. 'etc',
  52. 'Magento_Email'
  53. )->will(
  54. $this->returnValue('stub')
  55. );
  56. $schemaLocator = new \Magento\Email\Model\Template\Config\SchemaLocator($moduleReader);
  57. $validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
  58. $validationStateMock->expects($this->any())
  59. ->method('isValidationRequired')
  60. ->willReturn(false);
  61. $this->_moduleDirResolver = $this->createMock(\Magento\Framework\Module\Dir\ReverseResolver::class);
  62. $readFactory = $this->createMock(\Magento\Framework\Filesystem\File\ReadFactory::class);
  63. $this->read = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
  64. $readFactory->expects($this->any())->method('create')->willReturn($this->read);
  65. $fileIterator = new \Magento\Email\Model\Template\Config\FileIterator(
  66. $readFactory,
  67. $this->_paths,
  68. $this->_moduleDirResolver
  69. );
  70. $fileResolver->expects(
  71. $this->once()
  72. )->method(
  73. 'get'
  74. )->with(
  75. 'email_templates.xml',
  76. 'scope'
  77. )->will(
  78. $this->returnValue($fileIterator)
  79. );
  80. $this->_model = new \Magento\Email\Model\Template\Config\Reader(
  81. $fileResolver,
  82. $this->_converter,
  83. $schemaLocator,
  84. $validationStateMock
  85. );
  86. }
  87. public function testRead()
  88. {
  89. $this->read->expects(
  90. $this->at(0)
  91. )->method(
  92. 'readAll'
  93. )->will(
  94. $this->returnValue(file_get_contents($this->_paths[0]))
  95. );
  96. $this->read->expects(
  97. $this->at(1)
  98. )->method(
  99. 'readAll'
  100. )->will(
  101. $this->returnValue(file_get_contents($this->_paths[1]))
  102. );
  103. $this->_moduleDirResolver->expects(
  104. $this->at(0)
  105. )->method(
  106. 'getModuleName'
  107. )->with(
  108. __DIR__ . '/_files/Fixture/ModuleOne/etc/email_templates_one.xml'
  109. )->will(
  110. $this->returnValue('Fixture_ModuleOne')
  111. );
  112. $this->_moduleDirResolver->expects(
  113. $this->at(1)
  114. )->method(
  115. 'getModuleName'
  116. )->with(
  117. __DIR__ . '/_files/Fixture/ModuleTwo/etc/email_templates_two.xml'
  118. )->will(
  119. $this->returnValue('Fixture_ModuleTwo')
  120. );
  121. $constraint = function (\DOMDocument $actual) {
  122. try {
  123. $expected = file_get_contents(__DIR__ . '/_files/email_templates_merged.xml');
  124. $expectedNorm = preg_replace('/xsi:noNamespaceSchemaLocation="[^"]*"/', '', $expected, 1);
  125. $actualNorm = preg_replace('/xsi:noNamespaceSchemaLocation="[^"]*"/', '', $actual->saveXML(), 1);
  126. \PHPUnit\Framework\Assert::assertXmlStringEqualsXmlString($expectedNorm, $actualNorm);
  127. return true;
  128. } catch (\PHPUnit\Framework\AssertionFailedError $e) {
  129. return false;
  130. }
  131. };
  132. $expectedResult = new \stdClass();
  133. $this->_converter->expects(
  134. $this->once()
  135. )->method(
  136. 'convert'
  137. )->with(
  138. $this->callback($constraint)
  139. )->will(
  140. $this->returnValue($expectedResult)
  141. );
  142. $this->assertSame($expectedResult, $this->_model->read('scope'));
  143. }
  144. /**
  145. * @expectedException \UnexpectedValueException
  146. * @expectedExceptionMessage Unable to determine a module
  147. */
  148. public function testReadUnknownModule()
  149. {
  150. $this->_moduleDirResolver->expects($this->once())->method('getModuleName')->will($this->returnValue(null));
  151. $this->_converter->expects($this->never())->method('convert');
  152. $this->_model->read('scope');
  153. }
  154. }