ThemeTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Design\Backend;
  7. use Magento\Framework\App\Area;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Theme\Model\Design\Backend\Theme;
  12. class ThemeTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Theme\Model\Design\Backend\Theme
  16. */
  17. protected $model;
  18. /**
  19. * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $contextMock;
  22. /**
  23. * @var \Magento\Framework\View\DesignInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $designMock;
  26. /**
  27. * @var \Magento\Framework\App\Cache\TypeListInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $cacheTypeListMock;
  30. /**
  31. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $configMock;
  34. protected function setUp()
  35. {
  36. $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->designMock = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)->getMock();
  40. $this->cacheTypeListMock = $this->getMockBuilder(\Magento\Framework\App\Cache\TypeListInterface::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->contextMock->expects($this->once())
  44. ->method('getEventDispatcher')
  45. ->willReturn($this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)->getMock());
  46. $this->configMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)->getMock();
  47. $this->model = (new ObjectManager($this))->getObject(
  48. \Magento\Theme\Model\Design\Backend\Theme::class,
  49. [
  50. 'design' => $this->designMock,
  51. 'context' => $this->contextMock,
  52. 'cacheTypeList' => $this->cacheTypeListMock,
  53. 'config' => $this->configMock,
  54. ]
  55. );
  56. }
  57. /**
  58. * @test
  59. * @return void
  60. * @covers \Magento\Theme\Model\Design\Backend\Theme::beforeSave
  61. * @covers \Magento\Theme\Model\Design\Backend\Theme::__construct
  62. */
  63. public function testBeforeSave()
  64. {
  65. $this->designMock->expects($this->once())
  66. ->method('setDesignTheme')
  67. ->with('some_value', Area::AREA_FRONTEND);
  68. $this->model->setValue('some_value');
  69. $this->assertInstanceOf(get_class($this->model), $this->model->beforeSave());
  70. }
  71. /**
  72. * @param int $callNumber
  73. * @param string $oldValue
  74. * @dataProvider afterSaveDataProvider
  75. */
  76. public function testAfterSave($callNumber, $oldValue)
  77. {
  78. $this->cacheTypeListMock->expects($this->exactly($callNumber))
  79. ->method('invalidate');
  80. $this->configMock->expects($this->any())
  81. ->method('getValue')
  82. ->willReturnMap(
  83. [
  84. [
  85. Theme::XML_PATH_INVALID_CACHES,
  86. ScopeInterface::SCOPE_STORE,
  87. null,
  88. ['block_html' => 1, 'layout' => 1, 'translate' => 1]
  89. ],
  90. [
  91. null,
  92. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  93. null,
  94. $oldValue
  95. ],
  96. ]
  97. );
  98. $this->model->setValue('some_value');
  99. $this->assertInstanceOf(get_class($this->model), $this->model->afterSave());
  100. }
  101. /**
  102. * @param string|null $value
  103. * @param string $expectedResult
  104. * @return void
  105. * @dataProvider getValueDataProvider
  106. */
  107. public function testGetValue($value, $expectedResult)
  108. {
  109. $this->model->setValue($value);
  110. $this->assertEquals($expectedResult, $this->model->getValue());
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function getValueDataProvider()
  116. {
  117. return [
  118. [null, ''],
  119. ['value', 'value']
  120. ];
  121. }
  122. /**
  123. * @return array
  124. */
  125. public function afterSaveDataProvider()
  126. {
  127. return [
  128. [0, 'some_value'],
  129. [2, 'other_value'],
  130. ];
  131. }
  132. public function testAfterDelete()
  133. {
  134. $this->configMock->expects($this->any())
  135. ->method('getValue')
  136. ->willReturnMap(
  137. [
  138. [
  139. Theme::XML_PATH_INVALID_CACHES,
  140. ScopeInterface::SCOPE_STORE,
  141. null,
  142. ['block_html' => 1, 'layout' => 1, 'translate' => 1]
  143. ],
  144. [
  145. null,
  146. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  147. null,
  148. 'old_value'
  149. ],
  150. ]
  151. );
  152. $this->cacheTypeListMock->expects($this->exactly(2))
  153. ->method('invalidate');
  154. $this->model->setValue('some_value');
  155. $this->assertSame($this->model, $this->model->afterDelete());
  156. }
  157. }