Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StateTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\App\Area;
10 use \Magento\Framework\App\AreaList;
11 use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
12 
13 class StateTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $model;
19 
23  protected $scopeMock;
24 
28  protected $areaListMock;
29 
30  protected function setUp()
31  {
32  $objectManager = new ObjectManagerHelper($this);
33  $this->scopeMock = $this->getMockForAbstractClass(
34  \Magento\Framework\Config\ScopeInterface::class,
35  ['setCurrentScope'],
36  '',
37  false
38  );
39 
40  $this->areaListMock = $this->createMock(AreaList::class);
41  $this->areaListMock->expects($this->any())
42  ->method('getCodes')
44 
45  $this->model = $objectManager->getObject(
46  \Magento\Framework\App\State::class,
47  ['configScope' => $this->scopeMock]
48  );
49 
50  $objectManager->setBackwardCompatibleProperty($this->model, 'areaList', $this->areaListMock);
51  }
52 
53  public function testSetAreaCode()
54  {
55  $areaCode = Area::AREA_FRONTEND;
56  $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
57  $this->model->setAreaCode($areaCode);
58  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
59  $this->model->setAreaCode(Area::AREA_ADMINHTML);
60  }
61 
62  public function testGetAreaCodeException()
63  {
64  $this->scopeMock->expects($this->never())->method('setCurrentScope');
65  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
66  $this->model->getAreaCode();
67  }
68 
69  public function testGetAreaCode()
70  {
71  $areaCode = Area::AREA_FRONTEND;
72  $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
73  $this->model->setAreaCode($areaCode);
74  $this->assertEquals($areaCode, $this->model->getAreaCode());
75  }
76 
77  public function testEmulateAreaCode()
78  {
79  $areaCode = Area::AREA_FRONTEND;
80  $emulatedCode = Area::AREA_ADMINHTML;
81  $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
82  $this->model->setAreaCode($areaCode);
83  $this->assertEquals(
84  $emulatedCode,
85  $this->model->emulateAreaCode($emulatedCode, [$this, 'emulateAreaCodeCallback'])
86  );
87  $this->assertEquals($this->model->getAreaCode(), $areaCode);
88  }
89 
94  public function emulateAreaCodeCallback()
95  {
96  return $this->model->getAreaCode();
97  }
98 
99  public function testIsAreaCodeEmulated()
100  {
101  $areaCode = Area::AREA_ADMINHTML;
102  $emulatedCode = Area::AREA_FRONTEND;
103  $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
104  $this->model->setAreaCode($areaCode);
105  $this->assertFalse(
106  $this->model->isAreaCodeEmulated(),
107  'By default, area code is not emulated'
108  );
109  $this->assertTrue(
110  $this->model->emulateAreaCode($emulatedCode, [$this, 'isAreaCodeEmulatedCallback']),
111  'isAreaCodeEmulated should return true when being called within the context of an emulated method'
112  );
113  $this->assertFalse(
114  $this->model->isAreaCodeEmulated(),
115  'Now that emulateAreaCode execution has finished, this should return false again'
116  );
117  }
118 
124  public function isAreaCodeEmulatedCallback()
125  {
126  return $this->model->isAreaCodeEmulated();
127  }
128 
134  {
135  $areaCode = Area::AREA_FRONTEND;
136  $emulatedCode = Area::AREA_ADMINHTML;
137  $this->scopeMock->expects($this->once())->method('setCurrentScope')->with($areaCode);
138  $this->model->setAreaCode($areaCode);
139  $this->model->emulateAreaCode($emulatedCode, [$this, 'emulateAreaCodeCallbackException']);
140  $this->assertEquals($this->model->getAreaCode(), $areaCode);
141  }
142 
144  {
145  throw new \Exception('Some error');
146  }
147 
152  public function testConstructor($mode)
153  {
154  $model = new \Magento\Framework\App\State(
155  $this->getMockForAbstractClass(\Magento\Framework\Config\ScopeInterface::class, [], '', false),
156  $mode
157  );
158  $this->assertEquals($mode, $model->getMode());
159  }
160 
164  public static function constructorDataProvider()
165  {
166  return [
167  'default mode' => [\Magento\Framework\App\State::MODE_DEFAULT],
168  'production mode' => [\Magento\Framework\App\State::MODE_PRODUCTION],
170  ];
171  }
172 
177  public function testConstructorException()
178  {
179  new \Magento\Framework\App\State(
180  $this->getMockForAbstractClass(\Magento\Framework\Config\ScopeInterface::class, [], '', false),
181  "unknown mode"
182  );
183  }
184 
189  public function testCheckAreaCodeException()
190  {
191  $this->model->setAreaCode('any code');
192  }
193 }
$objectManager
Definition: bootstrap.php:17
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15