FileTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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\Framework\View\Design\Theme\Customization\FileServiceFactory;
  8. use Magento\Framework\View\DesignInterface;
  9. use Magento\Theme\Model\Theme\File;
  10. class FileTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var File
  14. */
  15. protected $model;
  16. /**
  17. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $registry;
  20. /**
  21. * @var \Magento\Framework\View\Design\Theme\FlyweightFactory|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $themeFactory;
  24. /**
  25. * @var FileServiceFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $fileServiceFactory;
  28. /**
  29. * @var \Magento\Theme\Model\ResourceModel\Theme\File|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $resource;
  32. /**
  33. * @var \Magento\Theme\Model\ResourceModel\Theme\File\Collection|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $resourceCollection;
  36. protected function setUp()
  37. {
  38. $context = $this->getMockBuilder(
  39. \Magento\Framework\Model\Context::class
  40. )->disableOriginalConstructor()->getMock();
  41. $this->registry = $this->getMockBuilder(
  42. \Magento\Framework\Registry::class
  43. )->disableOriginalConstructor()->getMock();
  44. $this->themeFactory = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\FlyweightFactory::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->fileServiceFactory = $this->getMockBuilder(
  48. \Magento\Framework\View\Design\Theme\Customization\FileServiceFactory::class
  49. )->disableOriginalConstructor()->getMock();
  50. $this->resource = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\File::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->resourceCollection = $this->getMockBuilder(
  54. \Magento\Theme\Model\ResourceModel\Theme\File\Collection::class
  55. )->disableOriginalConstructor()->getMock();
  56. $context->expects($this->once())
  57. ->method('getEventDispatcher')
  58. ->willReturn($this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)->getMock());
  59. $validator = $this->getMockBuilder(\Magento\Framework\Model\ActionValidator\RemoveAction::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $validator->expects($this->any())
  63. ->method('isAllowed')
  64. ->willReturn(true);
  65. $context->expects($this->once())
  66. ->method('getActionValidator')
  67. ->willReturn($validator);
  68. /** @var $context \Magento\Framework\Model\Context */
  69. $this->model = new File(
  70. $context,
  71. $this->registry,
  72. $this->themeFactory,
  73. $this->fileServiceFactory,
  74. $this->resource,
  75. $this->resourceCollection
  76. );
  77. }
  78. /**
  79. * @test
  80. * @return void
  81. */
  82. public function testSetCustomizationService()
  83. {
  84. $customization = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\Customization\FileInterface::class)
  85. ->getMock();
  86. /** @var $customization \Magento\Framework\View\Design\Theme\Customization\FileInterface */
  87. $this->assertInstanceOf(get_class($this->model), $this->model->setCustomizationService($customization));
  88. }
  89. /**
  90. * @test
  91. * @return void
  92. * @expectedException \UnexpectedValueException
  93. */
  94. public function testGetFullPathWithoutFileType()
  95. {
  96. $this->model->getFullPath();
  97. }
  98. /**
  99. * @test
  100. * @return void
  101. */
  102. public function testGetFullPath()
  103. {
  104. $fileServiceName = 'file_service';
  105. $fullPath = '/full/path';
  106. $customization = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\Customization\FileInterface::class)
  107. ->getMock();
  108. $this->model->setData('file_type', $fileServiceName);
  109. $this->fileServiceFactory->expects($this->once())
  110. ->method('create')
  111. ->with($fileServiceName)
  112. ->willReturn($customization);
  113. $customization->expects($this->once())
  114. ->method('getFullPath')
  115. ->willReturn($fullPath);
  116. $this->assertEquals($fullPath, $this->model->getFullPath());
  117. }
  118. /**
  119. * @test
  120. * @return void
  121. */
  122. public function testSetTheme()
  123. {
  124. $themeId = 1;
  125. $themePath = '/path/to/theme';
  126. $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)->getMock();
  127. $theme->expects($this->once())
  128. ->method('getId')
  129. ->willReturn($themeId);
  130. $theme->expects($this->once())
  131. ->method('getThemePath')
  132. ->willReturn($themePath);
  133. /** @var $theme \Magento\Framework\View\Design\ThemeInterface */
  134. $this->model->setTheme($theme);
  135. $this->assertEquals($themeId, $this->model->getThemeId());
  136. $this->assertEquals($themePath, $this->model->getThemePath());
  137. }
  138. /**
  139. * @test
  140. * @return void
  141. */
  142. public function testGetTheme()
  143. {
  144. $themeId = 1;
  145. $this->model->setThemeId($themeId);
  146. $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)->getMock();
  147. $this->themeFactory->expects($this->once())
  148. ->method('create')
  149. ->with($themeId, DesignInterface::DEFAULT_AREA)
  150. ->willReturn($theme);
  151. $this->assertInstanceOf(\Magento\Framework\View\Design\ThemeInterface::class, $this->model->getTheme());
  152. }
  153. /**
  154. * @test
  155. * @return void
  156. * @expectedException \Magento\Framework\Exception\LocalizedException
  157. * @expectedExceptionMessage Theme id should be set
  158. */
  159. public function testGetThemeException()
  160. {
  161. $this->themeFactory->expects($this->once())
  162. ->method('create')
  163. ->with(null, DesignInterface::DEFAULT_AREA)
  164. ->willReturn(null);
  165. $this->model->getTheme();
  166. }
  167. /**
  168. * @test
  169. * @return void
  170. */
  171. public function testSetGetFileName()
  172. {
  173. $fileName = 'fileName';
  174. $this->assertInstanceOf(get_class($this->model), $this->model->setFileName($fileName));
  175. $this->assertEquals($fileName, $this->model->getFileName());
  176. }
  177. /**
  178. * @test
  179. * @return void
  180. */
  181. public function testGetContent()
  182. {
  183. $content = 'content';
  184. $this->model->setContent($content);
  185. $this->assertEquals($content, $this->model->getContent());
  186. }
  187. public function testGetFileInfo()
  188. {
  189. $fileId = 123;
  190. $fileName = 'fileName';
  191. $data = [
  192. 'id' => $fileId,
  193. 'name' => $fileName,
  194. 'temporary' => 0,
  195. ];
  196. $this->model->setId($fileId);
  197. $this->model->setFileName($fileName);
  198. $this->model->setIsTemporary(false);
  199. $this->assertEquals($data, $this->model->getFileInfo());
  200. }
  201. /**
  202. * @test
  203. * @return void
  204. */
  205. public function testBeforeSaveDelete()
  206. {
  207. $fileServiceName = 'service_name';
  208. $customization = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\Customization\FileInterface::class)
  209. ->getMock();
  210. $this->fileServiceFactory->expects($this->once())
  211. ->method('create')
  212. ->with($fileServiceName)
  213. ->willReturn($customization);
  214. $customization->expects($this->once())
  215. ->method('prepareFile')
  216. ->with($this->model)
  217. ->willReturnSelf();
  218. $customization->expects($this->once())
  219. ->method('save')
  220. ->with($this->model)
  221. ->willReturnSelf();
  222. $customization->expects($this->once())
  223. ->method('delete')
  224. ->with($this->model)
  225. ->willReturnSelf();
  226. $this->model->setData('file_type', $fileServiceName);
  227. $this->model->beforeSave();
  228. $this->model->beforeDelete();
  229. }
  230. }