SingleFileTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Test\Unit\Model\Theme;
  7. use \Magento\Theme\Model\Theme\SingleFile;
  8. class SingleFileTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var SingleFile
  12. */
  13. protected $object;
  14. /**
  15. * @var \Magento\Framework\View\Design\Theme\Customization\FileInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $file;
  18. /**
  19. * Initialize testable object
  20. */
  21. protected function setUp()
  22. {
  23. $this->file = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\Customization\FileInterface::class)
  24. ->getMock();
  25. $this->object = new SingleFile($this->file);
  26. }
  27. /**
  28. * cover update method
  29. */
  30. public function testUpdate()
  31. {
  32. $fileContent = 'file content';
  33. $customFiles = [];
  34. $fileType = 'png';
  35. $customCss = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\FileInterface::class)
  36. ->disableOriginalConstructor()
  37. ->setMethods(
  38. [
  39. 'delete',
  40. 'save',
  41. 'getContent',
  42. 'getFileInfo',
  43. 'getFullPath',
  44. 'getFileName',
  45. 'setFileName',
  46. 'getTheme',
  47. 'setTheme',
  48. 'getCustomizationService',
  49. 'setCustomizationService',
  50. 'setData',
  51. 'getType',
  52. 'prepareFile',
  53. ]
  54. )
  55. ->getMock();
  56. $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
  57. ->setMethods(
  58. [
  59. 'getArea',
  60. 'getThemePath',
  61. 'getFullPath',
  62. 'getParentTheme',
  63. 'getCode',
  64. 'isPhysical',
  65. 'getInheritedThemes',
  66. 'getId',
  67. 'getCustomization',
  68. ]
  69. )
  70. ->getMock();
  71. $customization = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\CustomizationInterface::class)
  72. ->getMock();
  73. $customCss->expects($this->once())
  74. ->method('setData')
  75. ->with('content', $fileContent);
  76. $customCss->expects($this->once())
  77. ->method('setTheme')
  78. ->with($theme);
  79. $customCss->expects($this->once())
  80. ->method('save');
  81. $this->file->expects($this->once())
  82. ->method('create')
  83. ->willReturn($customCss);
  84. $this->file->expects($this->once())
  85. ->method('getType')
  86. ->willReturn($fileType);
  87. $customization->expects($this->once())
  88. ->method('getFilesByType')
  89. ->with($fileType)
  90. ->willReturn($customFiles);
  91. $theme->expects($this->once())
  92. ->method('getCustomization')
  93. ->willReturn($customization);
  94. /** @var \Magento\Framework\View\Design\ThemeInterface $theme */
  95. $this->assertInstanceOf(
  96. \Magento\Framework\View\Design\Theme\FileInterface::class,
  97. $this->object->update($theme, $fileContent)
  98. );
  99. }
  100. /**
  101. * cover update method when fileContent is empty
  102. */
  103. public function testUpdateWhenFileDelete()
  104. {
  105. $customCss = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\FileInterface::class)
  106. ->disableOriginalConstructor()
  107. ->setMethods(
  108. [
  109. 'delete',
  110. 'save',
  111. 'getContent',
  112. 'getFileInfo',
  113. 'getFullPath',
  114. 'getFileName',
  115. 'setFileName',
  116. 'getTheme',
  117. 'setTheme',
  118. 'getCustomizationService',
  119. 'setCustomizationService',
  120. 'setData',
  121. 'getType',
  122. 'prepareFile',
  123. ]
  124. )
  125. ->getMock();
  126. $fileContent = '';
  127. $customFiles = [$customCss];
  128. $fileType = 'png';
  129. $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
  130. ->setMethods(
  131. [
  132. 'getArea',
  133. 'getThemePath',
  134. 'getFullPath',
  135. 'getParentTheme',
  136. 'getCode',
  137. 'isPhysical',
  138. 'getInheritedThemes',
  139. 'getId',
  140. 'getCustomization',
  141. ]
  142. )
  143. ->getMock();
  144. $customization = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\CustomizationInterface::class)
  145. ->getMock();
  146. $customCss->expects($this->once())
  147. ->method('delete');
  148. $this->file->expects($this->once())
  149. ->method('getType')
  150. ->willReturn($fileType);
  151. $customization->expects($this->once())
  152. ->method('getFilesByType')
  153. ->with($fileType)
  154. ->willReturn($customFiles);
  155. $theme->expects($this->once())
  156. ->method('getCustomization')
  157. ->willReturn($customization);
  158. /** @var \Magento\Framework\View\Design\ThemeInterface $theme */
  159. $this->assertInstanceOf(
  160. \Magento\Framework\View\Design\Theme\FileInterface::class,
  161. $this->object->update($theme, $fileContent)
  162. );
  163. }
  164. }