RegistrationTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\ThemeInterface;
  8. use Magento\Theme\Model\Theme\Registration;
  9. class RegistrationTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var Registration
  13. */
  14. protected $model;
  15. /**
  16. * @var \Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $collectionFactory;
  19. /**
  20. * @var \Magento\Theme\Model\Theme\Data\Collection|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $filesystemCollection;
  23. protected function setUp()
  24. {
  25. $this->collectionFactory =
  26. $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory::class)
  27. ->setMethods(['create'])
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->filesystemCollection = $this->getMockBuilder(\Magento\Theme\Model\Theme\Data\Collection::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->model = new Registration(
  34. $this->collectionFactory,
  35. $this->filesystemCollection
  36. );
  37. }
  38. /**
  39. * @test
  40. * @return void
  41. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  42. */
  43. public function testRegister()
  44. {
  45. $image = 'preview.jpg';
  46. $themeFilePath = 'any/path';
  47. $parentId = 1;
  48. $fullPath = '/full/path';
  49. $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
  50. ->setMethods(
  51. [
  52. 'setParentId',
  53. 'getId',
  54. 'getFullPath',
  55. 'getParentTheme',
  56. 'getCustomization',
  57. 'getPreviewImage',
  58. 'getThemeImage',
  59. 'setType',
  60. 'save',
  61. ]
  62. )
  63. ->getMockForAbstractClass();
  64. $parentTheme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)->getMock();
  65. $parentThemeFromCollectionId = 123;
  66. $parentThemeFromCollection = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
  67. ->setMethods(['getType', 'getId'])
  68. ->getMockForAbstractClass();
  69. $themeFromCollection = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
  70. ->setMethods(['setType', 'save', 'getParentTheme', 'getType', 'getParentId', 'setParentId'])
  71. ->getMockForAbstractClass();
  72. $collection = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\Data\Collection::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $customization = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\CustomizationInterface::class)
  76. ->getMock();
  77. $imageModel = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\Image::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $theme->expects($this->once())
  81. ->method('save')
  82. ->willReturnSelf();
  83. $theme->expects($this->once())
  84. ->method('setType')
  85. ->willReturn(ThemeInterface::TYPE_PHYSICAL);
  86. $theme->expects($this->any())
  87. ->method('setParentId')
  88. ->willReturn($parentId);
  89. $theme->expects($this->any())
  90. ->method('getFullPath')
  91. ->willReturn($fullPath);
  92. $theme->expects($this->any())
  93. ->method('getParentTheme')
  94. ->willReturn($parentTheme);
  95. $parentTheme->expects($this->any())
  96. ->method('getId')
  97. ->willReturn($parentId);
  98. $this->collectionFactory->expects($this->any())
  99. ->method('create')
  100. ->willReturn($collection);
  101. $this->filesystemCollection->expects($this->once())
  102. ->method('clear');
  103. $this->filesystemCollection->expects($this->once())
  104. ->method('hasTheme')
  105. ->with($themeFromCollection)
  106. ->willReturn(false);
  107. $this->filesystemCollection->expects($this->once())
  108. ->method('getIterator')
  109. ->willReturn(new \ArrayIterator([$theme]));
  110. $collection->expects($this->once())
  111. ->method('getThemeByFullPath')
  112. ->with($fullPath)
  113. ->willReturn($theme);
  114. $theme->expects($this->once())
  115. ->method('getCustomization')
  116. ->willReturn($customization);
  117. $customization->expects($this->once())
  118. ->method('getThemeFilesPath')
  119. ->willReturn($themeFilePath);
  120. $theme->expects($this->any())
  121. ->method('getPreviewImage')
  122. ->willReturn($image);
  123. $theme->expects($this->once())
  124. ->method('getThemeImage')
  125. ->willReturn($imageModel);
  126. $imageModel->expects($this->once())
  127. ->method('createPreviewImage')
  128. ->with($themeFilePath . '/' . $image)
  129. ->willReturnSelf();
  130. $collection->expects($this->once())
  131. ->method('addTypeFilter')
  132. ->with(ThemeInterface::TYPE_PHYSICAL)
  133. ->willReturnSelf();
  134. $collection->expects($this->any())
  135. ->method('getIterator')
  136. ->willReturn(new \ArrayIterator([$themeFromCollection]));
  137. $collection->expects($this->any())
  138. ->method('addTypeRelationFilter')
  139. ->willReturnSelf();
  140. $themeFromCollection->expects($this->once())
  141. ->method('setType')
  142. ->with(ThemeInterface::TYPE_VIRTUAL)
  143. ->willReturnSelf();
  144. $themeFromCollection->expects($this->any())
  145. ->method('save')
  146. ->willReturnSelf();
  147. $themeFromCollection->expects($this->any())
  148. ->method('getParentTheme')
  149. ->willReturn($parentThemeFromCollection);
  150. $themeFromCollection->expects($this->any())
  151. ->method('getType')
  152. ->willReturn(ThemeInterface::TYPE_STAGING);
  153. $themeFromCollection->expects($this->any())
  154. ->method('getParentId')
  155. ->willReturn(null);
  156. $themeFromCollection->expects($this->any())
  157. ->method('setParentId')
  158. ->with($parentThemeFromCollectionId)
  159. ->willReturnSelf();
  160. $parentThemeFromCollection->expects($this->any())
  161. ->method('getType')
  162. ->willReturn(ThemeInterface::TYPE_VIRTUAL);
  163. $parentThemeFromCollection->expects($this->any())
  164. ->method('getId')
  165. ->willReturn($parentThemeFromCollectionId);
  166. $this->assertInstanceOf(get_class($this->model), $this->model->register());
  167. }
  168. }