ImporterTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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\Config;
  7. use Magento\Theme\Model\Config\Importer;
  8. use Magento\Theme\Model\ResourceModel\Theme as ThemeResourceModel;
  9. use Magento\Theme\Model\ResourceModel\Theme\Data\Collection as ThemeDbCollection;
  10. use Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory;
  11. use Magento\Theme\Model\Theme\Collection as ThemeFilesystemCollection;
  12. use Magento\Theme\Model\Theme\Data;
  13. use Magento\Theme\Model\Theme\Registration;
  14. class ImporterTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var ThemeFilesystemCollection|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $themeFilesystemCollectionMock;
  20. /**
  21. * @var ThemeDbCollection|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $themeDbCollectionMock;
  24. /**
  25. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $themecollectionFactoryMock;
  28. /**
  29. * @var Registration|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $themeRegistrationMock;
  32. /**
  33. * @var ThemeResourceModel|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $themeResourceModelMock;
  36. /**
  37. * @var Importer
  38. */
  39. private $importer;
  40. protected function setUp()
  41. {
  42. $this->themeFilesystemCollectionMock = $this->getMockBuilder(ThemeFilesystemCollection::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->themeDbCollectionMock = $this->getMockBuilder(ThemeDbCollection::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->themecollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
  49. ->setMethods(['create'])
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->themeRegistrationMock = $this->getMockBuilder(Registration::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->themeResourceModelMock = $this->getMockBuilder(ThemeResourceModel::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->importer = new Importer(
  59. $this->themeFilesystemCollectionMock,
  60. $this->themecollectionFactoryMock,
  61. $this->themeRegistrationMock,
  62. $this->themeResourceModelMock
  63. );
  64. }
  65. /**
  66. * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException
  67. * @expectedExceptionMessage Some error
  68. */
  69. public function testImportWithException()
  70. {
  71. $this->themeRegistrationMock->expects($this->once())
  72. ->method('register')
  73. ->willThrowException(new \Exception('Some error'));
  74. $this->importer->import([]);
  75. }
  76. public function testImport()
  77. {
  78. /** @var Data|\PHPUnit_Framework_MockObject_MockObject $firstThemeMock */
  79. $firstThemeMock = $this->getMockBuilder(Data::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $firstThemeMock->expects($this->atLeastOnce())
  83. ->method('getFullPath')
  84. ->willReturn('frontend/Magento/luma');
  85. /** @var Data|\PHPUnit_Framework_MockObject_MockObject $secondThemeMock */
  86. $secondThemeMock = $this->getMockBuilder(Data::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $secondThemeMock->expects($this->once())
  90. ->method('getFullPath')
  91. ->willReturn('frontend/Magento/blank');
  92. /** @var Data|\PHPUnit_Framework_MockObject_MockObject $thirdThemeMock */
  93. $thirdThemeMock = $this->getMockBuilder(Data::class)
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $thirdThemeMock->expects($this->once())
  97. ->method('getFullPath')
  98. ->willReturn('frontend/Magento/test');
  99. $this->themeRegistrationMock->expects($this->once())
  100. ->method('register')
  101. ->willReturnSelf();
  102. $this->themeDbCollectionMock->expects($this->once())
  103. ->method('getItems')
  104. ->willReturn([$firstThemeMock, $secondThemeMock, $thirdThemeMock]);
  105. $this->themecollectionFactoryMock->expects($this->once())
  106. ->method('create')
  107. ->willReturn($this->themeDbCollectionMock);
  108. $this->themeResourceModelMock->expects($this->once())
  109. ->method('delete')
  110. ->with($secondThemeMock)
  111. ->willReturnSelf();
  112. $this->themeRegistrationMock->expects($this->any())
  113. ->method('getThemeFromDb')
  114. ->willReturnMap([
  115. ['frontend/Magento/luma', $firstThemeMock],
  116. ['frontend/Magento/blank', $secondThemeMock],
  117. ]);
  118. $this->themeFilesystemCollectionMock->expects($this->once())
  119. ->method('getAllIds')
  120. ->willReturn(['frontend/Magento/luma']);
  121. $result = $this->importer->import([
  122. 'frontend/Magento/test' => [
  123. 'area' => 'frontend',
  124. 'parent_id' => 'Magento/blank',
  125. 'theme_path' => 'Magento/test',
  126. ],
  127. ]);
  128. $this->assertSame(
  129. [
  130. '<info>Theme import was started.</info>',
  131. '<info>Theme import finished.</info>'
  132. ],
  133. $result
  134. );
  135. }
  136. /**
  137. * @param array $inFile
  138. * @param array $inDb
  139. * @param array $inFs
  140. * @param array $expectedResult
  141. * @dataProvider getWarningMessagesDataProvider
  142. */
  143. public function testGetWarningMessages(array $inFile, array $inDb, array $inFs, array $expectedResult)
  144. {
  145. $themes = [];
  146. foreach ($inDb as $themePath) {
  147. /** @var Data|\PHPUnit_Framework_MockObject_MockObject $themeMock */
  148. $themeMock = $this->getMockBuilder(Data::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $themeMock->expects($this->any())
  152. ->method('getFullPath')
  153. ->willReturn($themePath);
  154. $themes[] = $themeMock;
  155. }
  156. $this->themeDbCollectionMock->expects($this->once())
  157. ->method('getItems')
  158. ->willReturn($themes);
  159. $this->themecollectionFactoryMock->expects($this->once())
  160. ->method('create')
  161. ->willReturn($this->themeDbCollectionMock);
  162. $this->themeFilesystemCollectionMock->expects($this->once())
  163. ->method('getAllIds')
  164. ->willReturn($inFs);
  165. $this->assertEquals($expectedResult, $this->importer->getWarningMessages($inFile));
  166. }
  167. /**
  168. * @return array
  169. */
  170. public function getWarningMessagesDataProvider()
  171. {
  172. return [
  173. [[], [], [], []],
  174. [
  175. ['frontend/Magento/luma' => ['Data of theme']],
  176. ['frontend/Magento/luma'],
  177. ['frontend/Magento/luma'],
  178. []
  179. ],
  180. [
  181. ['frontend/Magento/luma' => ['Data of theme']],
  182. ['frontend/Magento/luma'],
  183. [],
  184. []
  185. ],
  186. [
  187. [
  188. 'frontend/Magento/luma' => ['Data of theme'],
  189. 'frontend/Magento/blank' => ['Data of theme']
  190. ],
  191. [],
  192. ['frontend/Magento/luma', 'frontend/Magento/blank'],
  193. [
  194. '<info>The following themes will be registered:</info>'
  195. . ' frontend/Magento/luma, frontend/Magento/blank',
  196. ]
  197. ],
  198. [
  199. [
  200. 'frontend/Magento/luma' => ['Data of theme'],
  201. 'frontend/Magento/blank' => ['Data of theme']
  202. ],
  203. [],
  204. [],
  205. []
  206. ],
  207. [
  208. [],
  209. [],
  210. ['frontend/Magento/luma'],
  211. [
  212. '<info>The following themes will be registered:</info> frontend/Magento/luma',
  213. ]
  214. ],
  215. [
  216. [],
  217. ['frontend/Magento/luma', 'frontend/Magento/blank'],
  218. [],
  219. [
  220. '<info>The following themes will be removed:</info> frontend/Magento/luma, frontend/Magento/blank',
  221. ]
  222. ],
  223. [
  224. [],
  225. ['frontend/Magento/luma'],
  226. ['frontend/Magento/luma'],
  227. []
  228. ],
  229. ];
  230. }
  231. }