ContextTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test for view Context model
  8. */
  9. namespace Magento\Framework\View\Test\Unit;
  10. use \Magento\Framework\View\Context;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class ContextTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var Context
  18. */
  19. protected $context;
  20. /**
  21. * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $appState;
  24. /**
  25. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $request;
  28. /**
  29. * @var \Magento\Framework\View\DesignInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $design;
  32. protected function setUp()
  33. {
  34. $this->markTestSkipped('Testcase needs to be refactored.');
  35. $this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->design = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  45. $this->context = $objectManager->getObject(
  46. \Magento\Framework\View\Context::class,
  47. [
  48. 'appState' => $this->appState,
  49. 'request' => $this->request,
  50. 'design' => $this->design
  51. ]
  52. );
  53. }
  54. public function testGetCache()
  55. {
  56. $this->assertInstanceOf(\Magento\Framework\App\CacheInterface::class, $this->context->getCache());
  57. }
  58. public function testGetDesignPackage()
  59. {
  60. $this->assertInstanceOf(\Magento\Framework\View\DesignInterface::class, $this->context->getDesignPackage());
  61. }
  62. public function testGetEventManager()
  63. {
  64. $this->assertInstanceOf(\Magento\Framework\Event\ManagerInterface::class, $this->context->getEventManager());
  65. }
  66. public function testGetFrontController()
  67. {
  68. $this->assertInstanceOf(
  69. \Magento\Framework\App\FrontControllerInterface::class,
  70. $this->context->getFrontController()
  71. );
  72. }
  73. public function testGetLayout()
  74. {
  75. $this->assertInstanceOf(\Magento\Framework\View\LayoutInterface::class, $this->context->getLayout());
  76. }
  77. public function testGetRequest()
  78. {
  79. $this->assertInstanceOf(\Magento\Framework\App\Request\Http::class, $this->context->getRequest());
  80. }
  81. public function testGetSession()
  82. {
  83. $this->assertInstanceOf(
  84. \Magento\Framework\Session\SessionManagerInterface::class,
  85. $this->context->getSession()
  86. );
  87. }
  88. public function testGetScopeConfig()
  89. {
  90. $this->assertInstanceOf(
  91. \Magento\Framework\App\Config\ScopeConfigInterface::class,
  92. $this->context->getScopeConfig()
  93. );
  94. }
  95. public function testGetTranslator()
  96. {
  97. $this->assertInstanceOf(\Magento\Framework\TranslateInterface::class, $this->context->getTranslator());
  98. }
  99. public function testGetUrlBuilder()
  100. {
  101. $this->assertInstanceOf(\Magento\Framework\UrlInterface::class, $this->context->getUrlBuilder());
  102. }
  103. public function testGetViewConfig()
  104. {
  105. $this->assertInstanceOf(\Magento\Framework\View\ConfigInterface::class, $this->context->getViewConfig());
  106. }
  107. public function testGetCacheState()
  108. {
  109. $this->assertInstanceOf(\Magento\Framework\App\Cache\StateInterface::class, $this->context->getCacheState());
  110. }
  111. public function testGetLogger()
  112. {
  113. $this->assertInstanceOf(\Psr\Log\LoggerInterface::class, $this->context->getLogger());
  114. }
  115. public function testGetAppState()
  116. {
  117. $this->assertInstanceOf(\Magento\Framework\App\State::class, $this->context->getAppState());
  118. }
  119. public function testGetArea()
  120. {
  121. $area = 'frontendArea';
  122. $this->appState->expects($this->once())
  123. ->method('getAreaCode')
  124. ->will($this->returnValue($area));
  125. $this->assertEquals($area, $this->context->getArea());
  126. }
  127. public function testGetModuleName()
  128. {
  129. $moduleName = 'testModuleName';
  130. $this->request->expects($this->once())
  131. ->method('getModuleName')
  132. ->will($this->returnValue($moduleName));
  133. $this->assertEquals($moduleName, $this->context->getModuleName());
  134. }
  135. public function testGetFrontName()
  136. {
  137. $frontName = 'testFrontName';
  138. $this->request->expects($this->once())
  139. ->method('getModuleName')
  140. ->will($this->returnValue($frontName));
  141. $this->assertEquals($frontName, $this->context->getFrontName());
  142. }
  143. public function testGetControllerName()
  144. {
  145. $controllerName = 'testControllerName';
  146. $this->request->expects($this->once())
  147. ->method('getControllerName')
  148. ->will($this->returnValue($controllerName));
  149. $this->assertEquals($controllerName, $this->context->getControllerName());
  150. }
  151. public function testGetActionName()
  152. {
  153. $actionName = 'testActionName';
  154. $this->request->expects($this->once())
  155. ->method('getActionName')
  156. ->will($this->returnValue($actionName));
  157. $this->assertEquals($actionName, $this->context->getActionName());
  158. }
  159. public function testGetFullActionName()
  160. {
  161. $frontName = 'testFrontName';
  162. $controllerName = 'testControllerName';
  163. $actionName = 'testActionName';
  164. $fullActionName = 'testfrontname_testcontrollername_testactionname';
  165. $this->request->expects($this->once())
  166. ->method('getModuleName')
  167. ->will($this->returnValue($frontName));
  168. $this->request->expects($this->once())
  169. ->method('getControllerName')
  170. ->will($this->returnValue($controllerName));
  171. $this->request->expects($this->once())
  172. ->method('getActionName')
  173. ->will($this->returnValue($actionName));
  174. $this->assertEquals($fullActionName, $this->context->getFullActionName());
  175. }
  176. /**
  177. * @param string $headerAccept
  178. * @param string $acceptType
  179. *
  180. * @dataProvider getAcceptTypeDataProvider
  181. */
  182. public function testGetAcceptType($headerAccept, $acceptType)
  183. {
  184. $this->request->expects($this->once())
  185. ->method('getHeader')
  186. ->with('Accept')
  187. ->will($this->returnValue($headerAccept));
  188. $this->assertEquals($acceptType, $this->context->getAcceptType());
  189. }
  190. /**
  191. * @return array
  192. */
  193. public function getAcceptTypeDataProvider()
  194. {
  195. return [
  196. ['json', 'json'],
  197. ['testjson', 'json'],
  198. ['soap', 'soap'],
  199. ['testsoap', 'soap'],
  200. ['text/html', 'html'],
  201. ['testtext/html', 'html'],
  202. ['xml', 'xml'],
  203. ['someElse', 'xml'],
  204. ];
  205. }
  206. public function testGetPost()
  207. {
  208. $key = 'getParamName';
  209. $default = 'defaultGetParamValue';
  210. $postValue = 'someGetParamValue';
  211. $this->request->expects($this->once())
  212. ->method('getPost')
  213. ->with($key, $default)
  214. ->will($this->returnValue($postValue));
  215. $this->assertEquals($postValue, $this->context->getPost($key, $default));
  216. }
  217. public function testGetQuery()
  218. {
  219. $key = 'getParamName';
  220. $default = 'defaultGetParamValue';
  221. $queryValue = 'someGetParamValue';
  222. $this->request->expects($this->once())
  223. ->method('getPost')
  224. ->with($key, $default)
  225. ->will($this->returnValue($queryValue));
  226. $this->assertEquals($queryValue, $this->context->getQuery($key, $default));
  227. }
  228. public function testGetParam()
  229. {
  230. $key = 'paramName';
  231. $default = 'defaultParamValue';
  232. $paramValue = 'someParamValue';
  233. $this->request->expects($this->once())
  234. ->method('getParam')
  235. ->with($key, $default)
  236. ->will($this->returnValue($paramValue));
  237. $this->assertEquals($paramValue, $this->context->getParam($key, $default));
  238. }
  239. public function testGetParams()
  240. {
  241. $params = ['paramName' => 'value'];
  242. $this->request->expects($this->once())
  243. ->method('getParams')
  244. ->will($this->returnValue($params));
  245. $this->assertEquals($params, $this->context->getParams());
  246. }
  247. public function testGetHeader()
  248. {
  249. $headerName = 'headerName';
  250. $headerValue = 'headerValue';
  251. $this->request->expects($this->once())
  252. ->method('getHeader')
  253. ->with($headerName)
  254. ->will($this->returnValue($headerValue));
  255. $this->assertEquals($headerValue, $this->context->getHeader($headerName));
  256. }
  257. public function testContent()
  258. {
  259. $content = 'body string';
  260. $this->request->expects($this->once())
  261. ->method('getContent')
  262. ->will($this->returnValue($content));
  263. $this->assertEquals($content, $this->context->getContent());
  264. }
  265. }