DesignTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test design model
  8. */
  9. namespace Magento\Theme\Test\Unit\Model;
  10. use Magento\Framework\Serialize\SerializerInterface;
  11. use Magento\Theme\Model\Design;
  12. class DesignTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var Design
  16. */
  17. protected $model;
  18. /**
  19. * @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $cacheManager;
  22. /**
  23. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $localeDate;
  26. /**
  27. * @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $dateTime;
  30. /**
  31. * @var \Magento\Framework\Model\ResourceModel\AbstractResource|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $resource;
  34. /**
  35. * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $serializerMock;
  38. protected function setUp()
  39. {
  40. $context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->localeDate = $this->getMockBuilder(
  44. \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class
  45. )->getMock();
  46. $this->dateTime = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->resource = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Design::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->cacheManager = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)->getMock();
  53. $context->expects($this->any())
  54. ->method('getCacheManager')
  55. ->willReturn($this->cacheManager);
  56. $this->serializerMock = $this->createMock(SerializerInterface::class);
  57. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  58. $this->model = $objectManager->getObject(
  59. Design::class,
  60. [
  61. 'context' => $context,
  62. 'localeDate' => $this->localeDate,
  63. 'dateTime' => $this->dateTime,
  64. 'resource' => $this->resource,
  65. 'serializer' => $this->serializerMock,
  66. ]
  67. );
  68. }
  69. protected function tearDown()
  70. {
  71. $this->model = null;
  72. }
  73. /**
  74. * @test
  75. * @return void
  76. * @covers \Magento\Theme\Model\Design::loadChange
  77. */
  78. public function testLoadChange()
  79. {
  80. $storeId = 1;
  81. $localDate = '2\28\2000';
  82. $date = '28-02-2000';
  83. $cacheId = 'design_change_' . md5($storeId . $date);
  84. $this->localeDate->expects($this->once())
  85. ->method('scopeTimeStamp')
  86. ->with($storeId)
  87. ->willReturn($localDate);
  88. $this->dateTime->expects($this->once())
  89. ->method('formatDate')
  90. ->with($localDate, false)
  91. ->willReturn($date);
  92. $this->cacheManager->expects($this->once())
  93. ->method('load')
  94. ->with($cacheId)
  95. ->willReturn(false);
  96. $this->resource->expects($this->once())
  97. ->method('loadChange')
  98. ->with($storeId, $date)
  99. ->willReturn(false);
  100. $this->serializerMock->expects($this->once())
  101. ->method('serialize')
  102. ->willReturn('serializedData');
  103. $this->cacheManager->expects($this->once())
  104. ->method('save')
  105. ->with('serializedData', $cacheId, [Design::CACHE_TAG], 86400)
  106. ->willReturnSelf();
  107. $this->assertInstanceOf(get_class($this->model), $this->model->loadChange($storeId));
  108. }
  109. /**
  110. * @test
  111. * @return void
  112. * @covers \Magento\Theme\Model\Design::loadChange
  113. * @covers \Magento\Theme\Model\Design::__construct
  114. * @covers \Magento\Theme\Model\Design::_construct
  115. */
  116. public function testLoadChangeFromCache()
  117. {
  118. $storeId = 1;
  119. $localDate = '2\28\2000';
  120. $date = '28-02-2000';
  121. $cacheId = 'design_change_' . md5($storeId . $date);
  122. $this->localeDate->expects($this->once())
  123. ->method('scopeTimeStamp')
  124. ->with($storeId)
  125. ->willReturn($localDate);
  126. $this->dateTime->expects($this->once())
  127. ->method('formatDate')
  128. ->with($localDate, false)
  129. ->willReturn($date);
  130. $this->cacheManager->expects($this->once())
  131. ->method('load')
  132. ->with($cacheId)
  133. ->willReturn('serializedData');
  134. $data = ['test' => 'data'];
  135. $this->serializerMock->expects($this->once())
  136. ->method('unserialize')
  137. ->with('serializedData')
  138. ->willReturn($data);
  139. $change = $this->model->loadChange($storeId);
  140. $this->assertInstanceOf(get_class($this->model), $change);
  141. $this->assertEquals($data, $change->getData());
  142. }
  143. /**
  144. * @test
  145. * @return void
  146. * @covers \Magento\Theme\Model\Design::getIdentities
  147. * @covers \Magento\Theme\Model\Design::__construct
  148. * @covers \Magento\Theme\Model\Design::_construct
  149. */
  150. public function testGetIdentities()
  151. {
  152. $id = 1;
  153. $this->model->setId($id);
  154. $this->assertEquals([Design::CACHE_TAG . '_' . $id], $this->model->getIdentities());
  155. }
  156. /**
  157. * @test
  158. * @return void
  159. * @covers \Magento\Theme\Model\Design::changeDesign
  160. * @covers \Magento\Theme\Model\Design::__construct
  161. * @covers \Magento\Theme\Model\Design::_construct
  162. */
  163. public function testChangeDesign()
  164. {
  165. $design = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)->getMock();
  166. $this->model->setDesign('test');
  167. /** @var $design \Magento\Framework\View\DesignInterface */
  168. $this->assertInstanceOf(get_class($this->model), $this->model->changeDesign($design));
  169. }
  170. }