Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BlockTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
20 class BlockTest extends \PHPUnit\Framework\TestCase
21 {
27  private $blockModel;
28 
34  private $objectManager;
35 
39  private $eventManagerMock;
40 
44  private $contextMock;
45 
49  private $resourceMock;
50 
56  protected function setUp()
57  {
58  $this->resourceMock = $this->createMock(BlockResource::class);
59  $this->eventManagerMock = $this->createMock(ManagerInterface::class);
60  $this->contextMock = $this->createMock(Context::class);
61  $this->contextMock->expects($this->any())->method('getEventDispatcher')->willReturn($this->eventManagerMock);
62  $this->objectManager = new ObjectManager($this);
63  $this->blockModel = $this->objectManager->getObject(
64  Block::class,
65  [
66  'context' => $this->contextMock,
67  'resource' => $this->resourceMock,
68  ]
69  );
70  }
71 
79  public function testBeforeSave()
80  {
81  $blockId = 7;
82  $this->blockModel->setData(Block::BLOCK_ID, $blockId);
83  $this->blockModel->setData(Block::CONTENT, 'test');
84  $this->objectManager->setBackwardCompatibleProperty($this->blockModel, '_hasDataChanges', true);
85  $this->eventManagerMock->expects($this->atLeastOnce())->method('dispatch');
86  $expected = $this->blockModel;
87  $actual = $this->blockModel->beforeSave();
88  self::assertEquals($expected, $actual);
89  }
90 
98  public function testBeforeSaveWithException()
99  {
100  $blockId = 10;
101  $this->blockModel->setData(Block::BLOCK_ID, $blockId);
102  $this->blockModel->setData(Block::CONTENT, 'Test block_id="' . $blockId . '".');
103  $this->objectManager->setBackwardCompatibleProperty($this->blockModel, '_hasDataChanges', false);
104  $this->eventManagerMock->expects($this->never())->method('dispatch');
105  $this->expectException(LocalizedException::class);
106  $this->blockModel->beforeSave();
107  }
108 
114  public function testGetIdentities()
115  {
116  $result = $this->blockModel->getIdentities();
117  self::assertInternalType('array', $result);
118  }
119 
125  public function testGetId()
126  {
127  $blockId = 12;
128  $this->blockModel->setData(Block::BLOCK_ID, $blockId);
129  $expected = $blockId;
130  $actual = $this->blockModel->getId();
131  self::assertEquals($expected, $actual);
132  }
133 
139  public function testGetIdentifier()
140  {
141  $identifier = 'test01';
142  $this->blockModel->setData(Block::IDENTIFIER, $identifier);
143  $expected = $identifier;
144  $actual = $this->blockModel->getIdentifier();
145  self::assertEquals($expected, $actual);
146  }
147 
153  public function testGetTitle()
154  {
155  $title = 'test02';
156  $this->blockModel->setData(Block::TITLE, $title);
157  $expected = $title;
158  $actual = $this->blockModel->getTitle();
159  self::assertEquals($expected, $actual);
160  }
161 
167  public function testGetContent()
168  {
169  $content = 'test03';
170  $this->blockModel->setData(Block::CONTENT, $content);
171  $expected = $content;
172  $actual = $this->blockModel->getContent();
173  self::assertEquals($expected, $actual);
174  }
175 
181  public function testGetCreationTime()
182  {
183  $creationTime = 'test04';
184  $this->blockModel->setData(Block::CREATION_TIME, $creationTime);
185  $expected = $creationTime;
186  $actual = $this->blockModel->getCreationTime();
187  self::assertEquals($expected, $actual);
188  }
189 
195  public function testGetUpdateTime()
196  {
197  $updateTime = 'test05';
198  $this->blockModel->setData(Block::UPDATE_TIME, $updateTime);
199  $expected = $updateTime;
200  $actual = $this->blockModel->getUpdateTime();
201  self::assertEquals($expected, $actual);
202  }
203 
209  public function testIsActive()
210  {
211  $isActive = true;
212  $this->blockModel->setData(Block::IS_ACTIVE, $isActive);
213  $result = $this->blockModel->isActive();
214  self::assertTrue($result);
215  }
216 
222  public function testSetId()
223  {
224  $blockId = 15;
225  $this->blockModel->setId($blockId);
226  $expected = $blockId;
227  $actual = $this->blockModel->getData(Block::BLOCK_ID);
228  self::assertEquals($expected, $actual);
229  }
230 
236  public function testSetIdentifier()
237  {
238  $identifier = 'test06';
239  $this->blockModel->setIdentifier($identifier);
240  $expected = $identifier;
241  $actual = $this->blockModel->getData(Block::IDENTIFIER);
242  self::assertEquals($expected, $actual);
243  }
244 
250  public function testSetTitle()
251  {
252  $title = 'test07';
253  $this->blockModel->setTitle($title);
254  $expected = $title;
255  $actual = $this->blockModel->getData(Block::TITLE);
256  self::assertEquals($expected, $actual);
257  }
258 
264  public function testSetContent()
265  {
266  $content = 'test08';
267  $this->blockModel->setContent($content);
268  $expected = $content;
269  $actual = $this->blockModel->getData(Block::CONTENT);
270  self::assertEquals($expected, $actual);
271  }
272 
278  public function testSetCreationTime()
279  {
280  $creationTime = 'test09';
281  $this->blockModel->setCreationTime($creationTime);
282  $expected = $creationTime;
283  $actual = $this->blockModel->getData(Block::CREATION_TIME);
284  self::assertEquals($expected, $actual);
285  }
286 
292  public function testSetUpdateTime()
293  {
294  $updateTime = 'test10';
295  $this->blockModel->setUpdateTime($updateTime);
296  $expected = $updateTime;
297  $actual = $this->blockModel->getData(Block::UPDATE_TIME);
298  self::assertEquals($expected, $actual);
299  }
300 
306  public function testSetIsActive()
307  {
308  $this->blockModel->setIsActive(false);
309  $result = $this->blockModel->getData(Block::IS_ACTIVE);
310  self::assertFalse($result);
311  }
312 
318  public function testGetStores()
319  {
320  $stores = [1, 4, 9];
321  $this->blockModel->setData('stores', $stores);
322  $expected = $stores;
323  $actual = $this->blockModel->getStores();
324  self::assertEquals($expected, $actual);
325  }
326 
332  public function testGetAvailableStatuses()
333  {
334  $result = $this->blockModel->getAvailableStatuses();
335  self::assertInternalType('array', $result);
336  }
337 }
$title
Definition: default.phtml:14