Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DesignTest.php
Go to the documentation of this file.
1 <?php
11 
14 
15 class DesignTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $model;
21 
25  protected $cacheManager;
26 
30  protected $localeDate;
31 
35  protected $dateTime;
36 
40  protected $resource;
41 
45  private $serializerMock;
46 
47  protected function setUp()
48  {
49  $context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
50  ->disableOriginalConstructor()
51  ->getMock();
52  $this->localeDate = $this->getMockBuilder(
53  \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class
54  )->getMock();
55  $this->dateTime = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
56  ->disableOriginalConstructor()
57  ->getMock();
58  $this->resource = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Design::class)
59  ->disableOriginalConstructor()
60  ->getMock();
61  $this->cacheManager = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)->getMock();
62 
63  $context->expects($this->any())
64  ->method('getCacheManager')
65  ->willReturn($this->cacheManager);
66 
67  $this->serializerMock = $this->createMock(SerializerInterface::class);
68 
69  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
70  $this->model = $objectManager->getObject(
71  Design::class,
72  [
73  'context' => $context,
74  'localeDate' => $this->localeDate,
75  'dateTime' => $this->dateTime,
76  'resource' => $this->resource,
77  'serializer' => $this->serializerMock,
78  ]
79  );
80  }
81 
82  protected function tearDown()
83  {
84  $this->model = null;
85  }
86 
92  public function testLoadChange()
93  {
94  $storeId = 1;
95  $localDate = '2\28\2000';
96  $date = '28-02-2000';
97  $cacheId = 'design_change_' . md5($storeId . $date);
98  $this->localeDate->expects($this->once())
99  ->method('scopeTimeStamp')
100  ->with($storeId)
101  ->willReturn($localDate);
102  $this->dateTime->expects($this->once())
103  ->method('formatDate')
104  ->with($localDate, false)
105  ->willReturn($date);
106  $this->cacheManager->expects($this->once())
107  ->method('load')
108  ->with($cacheId)
109  ->willReturn(false);
110  $this->resource->expects($this->once())
111  ->method('loadChange')
112  ->with($storeId, $date)
113  ->willReturn(false);
114  $this->serializerMock->expects($this->once())
115  ->method('serialize')
116  ->willReturn('serializedData');
117  $this->cacheManager->expects($this->once())
118  ->method('save')
119  ->with('serializedData', $cacheId, [Design::CACHE_TAG], 86400)
120  ->willReturnSelf();
121 
122  $this->assertInstanceOf(get_class($this->model), $this->model->loadChange($storeId));
123  }
124 
132  public function testLoadChangeFromCache()
133  {
134  $storeId = 1;
135  $localDate = '2\28\2000';
136  $date = '28-02-2000';
137  $cacheId = 'design_change_' . md5($storeId . $date);
138  $this->localeDate->expects($this->once())
139  ->method('scopeTimeStamp')
140  ->with($storeId)
141  ->willReturn($localDate);
142  $this->dateTime->expects($this->once())
143  ->method('formatDate')
144  ->with($localDate, false)
145  ->willReturn($date);
146  $this->cacheManager->expects($this->once())
147  ->method('load')
148  ->with($cacheId)
149  ->willReturn('serializedData');
150  $data = ['test' => 'data'];
151  $this->serializerMock->expects($this->once())
152  ->method('unserialize')
153  ->with('serializedData')
154  ->willReturn($data);
155 
156  $change = $this->model->loadChange($storeId);
157  $this->assertInstanceOf(get_class($this->model), $change);
158  $this->assertEquals($data, $change->getData());
159  }
160 
168  public function testGetIdentities()
169  {
170  $id = 1;
171  $this->model->setId($id);
172  $this->assertEquals([Design::CACHE_TAG . '_' . $id], $this->model->getIdentities());
173  }
174 
182  public function testChangeDesign()
183  {
184  $design = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)->getMock();
185 
186  $this->model->setDesign('test');
188  $this->assertInstanceOf(get_class($this->model), $this->model->changeDesign($design));
189  }
190 }
$objectManager
Definition: bootstrap.php:17
$id
Definition: fieldset.phtml:14
$change