Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
7 
8 class DataTest extends \PHPUnit\Framework\TestCase
9 {
13  private $block;
14 
18  private $context;
19 
23  private $robots;
24 
28  private $storeResolver;
29 
33  private $storeManager;
34 
38  private $eventManagerMock;
39 
43  private $scopeConfigMock;
44 
45  protected function setUp()
46  {
47  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
48  ->getMockForAbstractClass();
49 
50  $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
51  ->getMockForAbstractClass();
52 
53  $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->context->expects($this->any())
58  ->method('getEventManager')
59  ->willReturn($this->eventManagerMock);
60 
61  $this->context->expects($this->any())
62  ->method('getScopeConfig')
63  ->willReturn($this->scopeConfigMock);
64 
65  $this->robots = $this->getMockBuilder(\Magento\Robots\Model\Robots::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68 
69  $this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72 
73  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
74  ->getMockForAbstractClass();
75 
76  $this->block = new \Magento\Robots\Block\Data(
77  $this->context,
78  $this->robots,
79  $this->storeResolver,
80  $this->storeManager
81  );
82  }
83 
87  public function testToHtml()
88  {
89  $data = 'test';
90 
92 
93  $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(false);
94 
95  $this->robots->expects($this->once())
96  ->method('getData')
97  ->willReturn($data);
98 
99  $this->assertEquals($data . PHP_EOL, $this->block->toHtml());
100  }
101 
105  public function testGetIdentities()
106  {
107  $storeId = 1;
108 
109  $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMock();
110 
111  $this->storeManager->expects($this->once())
112  ->method('getStore')
113  ->willReturn($storeMock);
114 
115  $storeMock->expects($this->once())
116  ->method('getId')
117  ->willReturn($storeId);
118 
119  $expected = [
121  ];
122  $this->assertEquals($expected, $this->block->getIdentities());
123  }
124 
131  protected function initEventManagerMock($data)
132  {
133  $this->eventManagerMock->expects($this->any())
134  ->method('dispatch')
135  ->willReturnMap([
136  [
137  'view_block_abstract_to_html_before',
138  [
139  'block' => $this->block,
140  ],
141  ],
142  [
143  'view_block_abstract_to_html_after',
144  [
145  'block' => $this->block,
146  'transport' => new \Magento\Framework\DataObject(['html' => $data]),
147  ],
148  ],
149  ]);
150  }
151 }