DataProviderTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Test\Unit\Model\Js;
  7. use Magento\Framework\App\State;
  8. use Magento\Framework\App\Utility\Files;
  9. use Magento\Framework\Filesystem;
  10. use Magento\Framework\Filesystem\File\ReadInterface;
  11. use Magento\Translation\Model\Js\DataProvider;
  12. use Magento\Translation\Model\Js\Config;
  13. use Magento\Framework\Phrase\Renderer\Translate;
  14. /**
  15. * Class DataProviderTest
  16. *
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class DataProviderTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var DataProvider
  23. */
  24. protected $model;
  25. /**
  26. * @var State|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $appStateMock;
  29. /**
  30. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $configMock;
  33. /**
  34. * @var Files|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $filesUtilityMock;
  37. /**
  38. * @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $fileReadMock;
  41. /**
  42. * @var Translate|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $translateMock;
  45. /**
  46. * @return void
  47. */
  48. protected function setUp()
  49. {
  50. $this->appStateMock = $this->createMock(\Magento\Framework\App\State::class);
  51. $this->configMock = $this->createMock(\Magento\Translation\Model\Js\Config::class);
  52. $this->filesUtilityMock = $this->createMock(\Magento\Framework\App\Utility\Files::class);
  53. $fileReadFactory = $this->createMock(\Magento\Framework\Filesystem\File\ReadFactory::class);
  54. $this->fileReadMock = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
  55. $this->translateMock = $this->createMock(\Magento\Framework\Phrase\Renderer\Translate::class);
  56. $fileReadFactory->expects($this->atLeastOnce())
  57. ->method('create')
  58. ->willReturn($this->fileReadMock);
  59. $dirSearch = $this->createMock(\Magento\Framework\Component\DirSearch::class);
  60. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  61. $this->model = $objectManager->getObject(
  62. \Magento\Translation\Model\Js\DataProvider::class,
  63. [
  64. 'appState' => $this->appStateMock,
  65. 'config' => $this->configMock,
  66. 'fileReadFactory' => $fileReadFactory,
  67. 'translate' => $this->translateMock,
  68. 'dirSearch' => $dirSearch,
  69. 'filesUtility' => $this->filesUtilityMock,
  70. 'componentRegistrar' =>
  71. $this->createMock(\Magento\Framework\Component\ComponentRegistrar::class)
  72. ]
  73. );
  74. }
  75. /**
  76. * @return void
  77. */
  78. public function testGetData()
  79. {
  80. $themePath = 'blank';
  81. $areaCode = 'adminhtml';
  82. $filePaths = [['path1'], ['path2'], ['path3'], ['path4']];
  83. $jsFilesMap = [
  84. ['base', $themePath, '*', '*', [$filePaths[0]]],
  85. [$areaCode, $themePath, '*', '*', [$filePaths[1]]]
  86. ];
  87. $staticFilesMap = [
  88. ['base', $themePath, '*', '*', [$filePaths[2]]],
  89. [$areaCode, $themePath, '*', '*', [$filePaths[3]]]
  90. ];
  91. $expectedResult = [
  92. 'hello1' => 'hello1translated',
  93. 'hello2' => 'hello2translated',
  94. 'hello3' => 'hello3translated',
  95. 'hello4' => 'hello4translated'
  96. ];
  97. $contentsMap = [
  98. 'content1$.mage.__("hello1")content1',
  99. 'content2$.mage.__("hello2")content2',
  100. 'content2$.mage.__("hello3")content3',
  101. 'content2$.mage.__("hello4")content4'
  102. ];
  103. $translateMap = [
  104. [['hello1'], [], 'hello1translated'],
  105. [['hello2'], [], 'hello2translated'],
  106. [['hello3'], [], 'hello3translated'],
  107. [['hello4'], [], 'hello4translated']
  108. ];
  109. $patterns = ['~\$\.mage\.__\(([\'"])(.+?)\1\)~'];
  110. $this->appStateMock->expects($this->once())
  111. ->method('getAreaCode')
  112. ->willReturn($areaCode);
  113. $this->filesUtilityMock->expects($this->any())
  114. ->method('getJsFiles')
  115. ->willReturnMap($jsFilesMap);
  116. $this->filesUtilityMock->expects($this->any())
  117. ->method('getStaticHtmlFiles')
  118. ->willReturnMap($staticFilesMap);
  119. foreach ($contentsMap as $index => $content) {
  120. $this->fileReadMock->expects($this->at($index))
  121. ->method('readAll')
  122. ->willReturn($content);
  123. }
  124. $this->configMock->expects($this->any())
  125. ->method('getPatterns')
  126. ->willReturn($patterns);
  127. $this->translateMock->expects($this->any())
  128. ->method('render')
  129. ->willReturnMap($translateMap);
  130. $this->assertEquals($expectedResult, $this->model->getData($themePath));
  131. }
  132. /**
  133. * @expectedException \Magento\Framework\Exception\LocalizedException
  134. */
  135. public function testGetDataThrowingException()
  136. {
  137. $themePath = 'blank';
  138. $areaCode = 'adminhtml';
  139. $patterns = ['~\$\.mage\.__\(([\'"])(.+?)\1\)~'];
  140. $this->fileReadMock->expects($this->once())
  141. ->method('readAll')
  142. ->willReturn('content1$.mage.__("hello1")content1');
  143. $this->appStateMock->expects($this->once())
  144. ->method('getAreaCode')
  145. ->willReturn($areaCode);
  146. $this->filesUtilityMock->expects($this->any())
  147. ->method('getJsFiles')
  148. ->willReturn(['test']);
  149. $this->filesUtilityMock->expects($this->any())
  150. ->method('getStaticHtmlFiles')
  151. ->willReturn(['test']);
  152. $this->configMock->expects($this->any())
  153. ->method('getPatterns')
  154. ->willReturn($patterns);
  155. $this->translateMock->expects($this->once())
  156. ->method('render')
  157. ->willThrowException(new \Exception('Test exception'));
  158. $this->model->getData($themePath);
  159. }
  160. }