NoCookiesTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cookie\Test\Unit\Controller\Index;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class NoCookiesTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Cookie\Controller\Index\NoCookies
  12. */
  13. private $controller;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Event\ManagerInterface
  16. */
  17. private $eventManagerMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Request\Http
  20. */
  21. private $requestMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Response\Http
  24. */
  25. private $responseMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Response\RedirectInterface
  28. */
  29. private $redirectResponseMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\ViewInterface
  32. */
  33. protected $viewMock;
  34. const REDIRECT_URL = 'http://www.example.com/redirect';
  35. const REDIRECT_PATH = '\a\path';
  36. const REDIRECT_ARGUMENTS = '&arg1key=arg1value';
  37. public function setup()
  38. {
  39. $objectManager = new ObjectManager($this);
  40. $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)->getMock();
  41. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->redirectResponseMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
  48. ->getMock();
  49. $this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
  50. $this->controller = $objectManager->getObject(
  51. \Magento\Cookie\Controller\Index\NoCookies::class,
  52. [
  53. 'eventManager' => $this->eventManagerMock,
  54. 'request' => $this->requestMock,
  55. 'response' => $this->responseMock,
  56. 'redirect' => $this->redirectResponseMock,
  57. 'view' => $this->viewMock,
  58. ]
  59. );
  60. }
  61. public function testExecuteRedirectUrl()
  62. {
  63. // redirect is new'ed in the execute function, so need to set the redirect URL in dispatch call
  64. $this->eventManagerMock->expects($this->once())
  65. ->method('dispatch')
  66. ->with(
  67. $this->equalTo('controller_action_nocookies'),
  68. $this->callback(
  69. function ($dataArray) {
  70. $redirect = $dataArray['redirect'];
  71. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $redirect);
  72. $redirect->setRedirectUrl(self::REDIRECT_URL);
  73. return true;
  74. }
  75. )
  76. );
  77. // Verify response is set with redirect url
  78. $this->responseMock->expects($this->once())
  79. ->method('setRedirect')
  80. ->with(self::REDIRECT_URL);
  81. // Verify request is set to dispatched
  82. $this->requestMock->expects($this->once())->method('setDispatched')->with($this->isTrue());
  83. // Make the call to test
  84. $this->controller->execute();
  85. }
  86. public function testExecuteRedirectPath()
  87. {
  88. // redirect is new'ed in the execute function, so need to set the redirect in dispatch call
  89. $this->eventManagerMock->expects($this->once())
  90. ->method('dispatch')
  91. ->with(
  92. $this->equalTo('controller_action_nocookies'),
  93. $this->callback(
  94. function ($dataArray) {
  95. $redirect = $dataArray['redirect'];
  96. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $redirect);
  97. $redirect->setArguments(self::REDIRECT_ARGUMENTS);
  98. $redirect->setPath(self::REDIRECT_PATH);
  99. $redirect->setRedirect(self::REDIRECT_URL);
  100. return true;
  101. }
  102. )
  103. );
  104. // Verify response is set with redirect, which
  105. $this->redirectResponseMock->expects($this->once())
  106. ->method('redirect')
  107. ->with($this->responseMock, $this->equalTo('\a\path'), $this->equalTo('&arg1key=arg1value'));
  108. // Verify request is set to dispatched
  109. $this->requestMock->expects($this->once())->method('setDispatched')->with($this->isTrue());
  110. // Make the call to test
  111. $this->controller->execute();
  112. }
  113. public function testExecuteDefault()
  114. {
  115. // Verify view is called to load and render
  116. $this->viewMock->expects($this->once())->method('loadLayout')->with(['default', 'noCookie']);
  117. $this->viewMock->expects($this->once())->method('renderLayout');
  118. // Verify request is set to dispatched
  119. $this->requestMock->expects($this->once())->method('setDispatched')->with($this->isTrue());
  120. // Make the call to test
  121. $this->controller->execute();
  122. }
  123. }