CollectionTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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;
  9. use Magento\Theme\Model\Theme\Collection;
  10. class CollectionTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Collection
  14. */
  15. private $model;
  16. /**
  17. * @var \Magento\Framework\Config\ThemeFactory|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $themeConfigFactory;
  20. /**
  21. * @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $directory;
  24. /**
  25. * @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $entityFactory;
  28. /**
  29. * @var \Magento\Framework\View\Design\Theme\ThemePackageList|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $themePackageList;
  32. /**
  33. * @var \Magento\Framework\Filesystem\Directory\ReadFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $readDirFactory;
  36. protected function setUp()
  37. {
  38. $this->entityFactory = $this->getMockBuilder(\Magento\Framework\Data\Collection\EntityFactory::class)
  39. ->disableOriginalConstructor()
  40. ->setMethods(['create'])
  41. ->getMock();
  42. $this->themeConfigFactory = $this->getMockBuilder(\Magento\Framework\Config\ThemeFactory::class)
  43. ->setMethods(['create'])
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->directory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->themePackageList = $this->createMock(\Magento\Framework\View\Design\Theme\ThemePackageList::class);
  50. $this->readDirFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
  51. $this->readDirFactory->expects($this->any())
  52. ->method('create')
  53. ->will($this->returnValue($this->directory));
  54. $this->model = new Collection(
  55. $this->entityFactory,
  56. $this->themeConfigFactory,
  57. $this->themePackageList,
  58. $this->readDirFactory
  59. );
  60. }
  61. /**
  62. * @test
  63. * @return void
  64. */
  65. public function testLoadData()
  66. {
  67. $fileContent = 'content file';
  68. $media = ['preview_image' => 'preview.jpg'];
  69. $themeTitle = 'Theme title';
  70. $themeConfigFile = 'theme.xml';
  71. $themeConfig = $this->getMockBuilder(
  72. \Magento\Framework\Config\Theme::class
  73. )->disableOriginalConstructor()->getMock();
  74. $theme = $this->getMockBuilder(Theme::class)->disableOriginalConstructor()->getMock();
  75. $parentTheme = ['parentThemeCode'];
  76. $parentThemePath = 'frontend/parent/theme';
  77. $themePackage = $this->createMock(\Magento\Framework\View\Design\Theme\ThemePackage::class);
  78. $themePackage->expects($this->any())
  79. ->method('getArea')
  80. ->will($this->returnValue('frontend'));
  81. $themePackage->expects($this->any())
  82. ->method('getVendor')
  83. ->will($this->returnValue('theme'));
  84. $themePackage->expects($this->any())
  85. ->method('getName')
  86. ->will($this->returnValue('code'));
  87. $this->themePackageList->expects($this->once())
  88. ->method('getThemes')
  89. ->will($this->returnValue([$themePackage]));
  90. $this->directory->expects($this->once())
  91. ->method('isExist')
  92. ->with($themeConfigFile)
  93. ->willReturn(true);
  94. $this->directory->expects($this->once())
  95. ->method('readFile')
  96. ->with($themeConfigFile)
  97. ->willReturn($fileContent);
  98. $this->themeConfigFactory->expects($this->once())
  99. ->method('create')
  100. ->with(['configContent' => $fileContent])
  101. ->willReturn($themeConfig);
  102. $this->entityFactory->expects($this->any())
  103. ->method('create')
  104. ->with(ThemeInterface::class)
  105. ->willReturn($theme);
  106. $themeConfig->expects($this->once())
  107. ->method('getMedia')
  108. ->willReturn($media);
  109. $themeConfig->expects($this->once())
  110. ->method('getParentTheme')
  111. ->willReturn($parentTheme);
  112. $themeConfig->expects($this->once())
  113. ->method('getThemeTitle')
  114. ->willReturn($themeTitle);
  115. $theme->expects($this->once())
  116. ->method('addData')
  117. ->with(
  118. [
  119. 'parent_id' => null,
  120. 'type' => ThemeInterface::TYPE_PHYSICAL,
  121. 'area' => 'frontend',
  122. 'theme_path' => 'theme/code',
  123. 'code' => 'theme/code',
  124. 'theme_title' => $themeTitle,
  125. 'preview_image' => $media['preview_image'],
  126. 'parent_theme_path' => 'theme/parentThemeCode'
  127. ]
  128. )
  129. ->willReturnSelf();
  130. $theme->expects($this->once())
  131. ->method('getData')
  132. ->with('parent_theme_path')
  133. ->willReturn($parentThemePath);
  134. $theme->expects($this->once())
  135. ->method('getArea')
  136. ->willReturn('frontend');
  137. $this->assertInstanceOf(get_class($this->model), $this->model->loadData());
  138. }
  139. /**
  140. * @expectedException \UnexpectedValueException
  141. * @expectedExceptionMessage Constraint 'unsupported_type' is not supported
  142. */
  143. public function testAddConstraintUnsupportedType()
  144. {
  145. $this->model->addConstraint('unsupported_type', 'value');
  146. }
  147. /**
  148. * @param array $inputValues
  149. * @param array $expected
  150. *
  151. * @dataProvider addConstraintDataProvider
  152. */
  153. public function testAddConstraint(array $inputValues, array $expected)
  154. {
  155. foreach ($inputValues as $data) {
  156. $type = $data[0];
  157. $value = $data[1];
  158. $this->model->addConstraint($type, $value);
  159. }
  160. $default = [
  161. Collection::CONSTRAINT_AREA => [],
  162. Collection::CONSTRAINT_VENDOR => [],
  163. Collection::CONSTRAINT_THEME_NAME => []
  164. ];
  165. $expected = array_merge($default, $expected);
  166. $this->assertAttributeSame($expected, 'constraints', $this->model);
  167. }
  168. /**
  169. * @return array
  170. */
  171. public function addConstraintDataProvider()
  172. {
  173. return [
  174. 'area' => [
  175. [[Collection::CONSTRAINT_AREA, 'area']],
  176. [Collection::CONSTRAINT_AREA => ['area']]
  177. ],
  178. 'vendor' => [
  179. [[Collection::CONSTRAINT_VENDOR, 'Vendor']],
  180. [Collection::CONSTRAINT_VENDOR => ['Vendor']]
  181. ],
  182. 'theme name' => [
  183. [[Collection::CONSTRAINT_THEME_NAME, 'theme_name']],
  184. [Collection::CONSTRAINT_THEME_NAME => ['theme_name']]
  185. ],
  186. 'area, vendor and theme name' => [
  187. [
  188. [Collection::CONSTRAINT_AREA, 'area_one'],
  189. [Collection::CONSTRAINT_AREA, 'area_two'],
  190. [Collection::CONSTRAINT_VENDOR, 'Vendor'],
  191. [Collection::CONSTRAINT_VENDOR, 'Vendor'],
  192. [Collection::CONSTRAINT_THEME_NAME, 'theme_name']
  193. ],
  194. [
  195. Collection::CONSTRAINT_AREA => ['area_one', 'area_two'],
  196. Collection::CONSTRAINT_VENDOR => ['Vendor'],
  197. Collection::CONSTRAINT_THEME_NAME => ['theme_name']
  198. ]
  199. ],
  200. ];
  201. }
  202. }