Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ContextTest.php
Go to the documentation of this file.
1 <?php
11 
12 use \Magento\Framework\View\Context;
13 
17 class ContextTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $context;
23 
27  protected $appState;
28 
32  protected $request;
33 
37  protected $design;
38 
39  protected function setUp()
40  {
41  $this->markTestSkipped('Testcase needs to be refactored.');
42  $this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45 
46  $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49 
50  $this->design = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53 
54  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
55  $this->context = $objectManager->getObject(
56  \Magento\Framework\View\Context::class,
57  [
58  'appState' => $this->appState,
59  'request' => $this->request,
60  'design' => $this->design
61  ]
62  );
63  }
64 
65  public function testGetCache()
66  {
67  $this->assertInstanceOf(\Magento\Framework\App\CacheInterface::class, $this->context->getCache());
68  }
69 
70  public function testGetDesignPackage()
71  {
72  $this->assertInstanceOf(\Magento\Framework\View\DesignInterface::class, $this->context->getDesignPackage());
73  }
74 
75  public function testGetEventManager()
76  {
77  $this->assertInstanceOf(\Magento\Framework\Event\ManagerInterface::class, $this->context->getEventManager());
78  }
79 
80  public function testGetFrontController()
81  {
82  $this->assertInstanceOf(
83  \Magento\Framework\App\FrontControllerInterface::class,
84  $this->context->getFrontController()
85  );
86  }
87 
88  public function testGetLayout()
89  {
90  $this->assertInstanceOf(\Magento\Framework\View\LayoutInterface::class, $this->context->getLayout());
91  }
92 
93  public function testGetRequest()
94  {
95  $this->assertInstanceOf(\Magento\Framework\App\Request\Http::class, $this->context->getRequest());
96  }
97 
98  public function testGetSession()
99  {
100  $this->assertInstanceOf(
101  \Magento\Framework\Session\SessionManagerInterface::class,
102  $this->context->getSession()
103  );
104  }
105 
106  public function testGetScopeConfig()
107  {
108  $this->assertInstanceOf(
109  \Magento\Framework\App\Config\ScopeConfigInterface::class,
110  $this->context->getScopeConfig()
111  );
112  }
113 
114  public function testGetTranslator()
115  {
116  $this->assertInstanceOf(\Magento\Framework\TranslateInterface::class, $this->context->getTranslator());
117  }
118 
119  public function testGetUrlBuilder()
120  {
121  $this->assertInstanceOf(\Magento\Framework\UrlInterface::class, $this->context->getUrlBuilder());
122  }
123 
124  public function testGetViewConfig()
125  {
126  $this->assertInstanceOf(\Magento\Framework\View\ConfigInterface::class, $this->context->getViewConfig());
127  }
128 
129  public function testGetCacheState()
130  {
131  $this->assertInstanceOf(\Magento\Framework\App\Cache\StateInterface::class, $this->context->getCacheState());
132  }
133 
134  public function testGetLogger()
135  {
136  $this->assertInstanceOf(\Psr\Log\LoggerInterface::class, $this->context->getLogger());
137  }
138 
139  public function testGetAppState()
140  {
141  $this->assertInstanceOf(\Magento\Framework\App\State::class, $this->context->getAppState());
142  }
143 
144  public function testGetArea()
145  {
146  $area = 'frontendArea';
147 
148  $this->appState->expects($this->once())
149  ->method('getAreaCode')
150  ->will($this->returnValue($area));
151 
152  $this->assertEquals($area, $this->context->getArea());
153  }
154 
155  public function testGetModuleName()
156  {
157  $moduleName = 'testModuleName';
158 
159  $this->request->expects($this->once())
160  ->method('getModuleName')
161  ->will($this->returnValue($moduleName));
162 
163  $this->assertEquals($moduleName, $this->context->getModuleName());
164  }
165 
166  public function testGetFrontName()
167  {
168  $frontName = 'testFrontName';
169 
170  $this->request->expects($this->once())
171  ->method('getModuleName')
172  ->will($this->returnValue($frontName));
173 
174  $this->assertEquals($frontName, $this->context->getFrontName());
175  }
176 
177  public function testGetControllerName()
178  {
179  $controllerName = 'testControllerName';
180 
181  $this->request->expects($this->once())
182  ->method('getControllerName')
183  ->will($this->returnValue($controllerName));
184 
185  $this->assertEquals($controllerName, $this->context->getControllerName());
186  }
187 
188  public function testGetActionName()
189  {
190  $actionName = 'testActionName';
191 
192  $this->request->expects($this->once())
193  ->method('getActionName')
194  ->will($this->returnValue($actionName));
195 
196  $this->assertEquals($actionName, $this->context->getActionName());
197  }
198 
199  public function testGetFullActionName()
200  {
201  $frontName = 'testFrontName';
202  $controllerName = 'testControllerName';
203  $actionName = 'testActionName';
204  $fullActionName = 'testfrontname_testcontrollername_testactionname';
205 
206  $this->request->expects($this->once())
207  ->method('getModuleName')
208  ->will($this->returnValue($frontName));
209 
210  $this->request->expects($this->once())
211  ->method('getControllerName')
212  ->will($this->returnValue($controllerName));
213 
214  $this->request->expects($this->once())
215  ->method('getActionName')
216  ->will($this->returnValue($actionName));
217 
218  $this->assertEquals($fullActionName, $this->context->getFullActionName());
219  }
220 
227  public function testGetAcceptType($headerAccept, $acceptType)
228  {
229  $this->request->expects($this->once())
230  ->method('getHeader')
231  ->with('Accept')
232  ->will($this->returnValue($headerAccept));
233 
234  $this->assertEquals($acceptType, $this->context->getAcceptType());
235  }
236 
240  public function getAcceptTypeDataProvider()
241  {
242  return [
243  ['json', 'json'],
244  ['testjson', 'json'],
245  ['soap', 'soap'],
246  ['testsoap', 'soap'],
247  ['text/html', 'html'],
248  ['testtext/html', 'html'],
249  ['xml', 'xml'],
250  ['someElse', 'xml'],
251  ];
252  }
253 
254  public function testGetPost()
255  {
256  $key = 'getParamName';
257  $default = 'defaultGetParamValue';
258  $postValue = 'someGetParamValue';
259 
260  $this->request->expects($this->once())
261  ->method('getPost')
262  ->with($key, $default)
263  ->will($this->returnValue($postValue));
264 
265  $this->assertEquals($postValue, $this->context->getPost($key, $default));
266  }
267 
268  public function testGetQuery()
269  {
270  $key = 'getParamName';
271  $default = 'defaultGetParamValue';
272  $queryValue = 'someGetParamValue';
273 
274  $this->request->expects($this->once())
275  ->method('getPost')
276  ->with($key, $default)
277  ->will($this->returnValue($queryValue));
278 
279  $this->assertEquals($queryValue, $this->context->getQuery($key, $default));
280  }
281 
282  public function testGetParam()
283  {
284  $key = 'paramName';
285  $default = 'defaultParamValue';
286  $paramValue = 'someParamValue';
287 
288  $this->request->expects($this->once())
289  ->method('getParam')
290  ->with($key, $default)
291  ->will($this->returnValue($paramValue));
292 
293  $this->assertEquals($paramValue, $this->context->getParam($key, $default));
294  }
295 
296  public function testGetParams()
297  {
298  $params = ['paramName' => 'value'];
299 
300  $this->request->expects($this->once())
301  ->method('getParams')
302  ->will($this->returnValue($params));
303 
304  $this->assertEquals($params, $this->context->getParams());
305  }
306 
307  public function testGetHeader()
308  {
309  $headerName = 'headerName';
310  $headerValue = 'headerValue';
311 
312  $this->request->expects($this->once())
313  ->method('getHeader')
314  ->with($headerName)
315  ->will($this->returnValue($headerValue));
316 
317  $this->assertEquals($headerValue, $this->context->getHeader($headerName));
318  }
319 
320  public function testContent()
321  {
322  $content = 'body string';
323 
324  $this->request->expects($this->once())
325  ->method('getContent')
326  ->will($this->returnValue($content));
327 
328  $this->assertEquals($content, $this->context->getContent());
329  }
330 }
$objectManager
Definition: bootstrap.php:17
testGetAcceptType($headerAccept, $acceptType)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18