LoadTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Customer\Test\Unit\Controller\Section;
  8. use Magento\Customer\Controller\Section\Load;
  9. use Magento\Customer\CustomerData\Section\Identifier;
  10. use Magento\Customer\CustomerData\SectionPoolInterface;
  11. use Magento\Framework\App\Action\Context;
  12. use Magento\Framework\Controller\Result\Json;
  13. use Magento\Framework\Controller\Result\JsonFactory;
  14. use Magento\Framework\Escaper;
  15. use \PHPUnit_Framework_MockObject_MockObject as MockObject;
  16. use Magento\Framework\App\Request\Http as HttpRequest;
  17. class LoadTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var Load
  21. */
  22. private $loadAction;
  23. /**
  24. * @var Context|MockObject
  25. */
  26. private $contextMock;
  27. /**
  28. * @var JsonFactory|MockObject
  29. */
  30. private $resultJsonFactoryMock;
  31. /**
  32. * @var Identifier|MockObject
  33. */
  34. private $sectionIdentifierMock;
  35. /**
  36. * @var SectionPoolInterface|MockObject
  37. */
  38. private $sectionPoolMock;
  39. /**
  40. * @var \Magento\Framework\Escaper|MockObject
  41. */
  42. private $escaperMock;
  43. /**
  44. * @var Json|MockObject
  45. */
  46. private $resultJsonMock;
  47. /**
  48. * @var HttpRequest|MockObject
  49. */
  50. private $httpRequestMock;
  51. protected function setUp()
  52. {
  53. $this->contextMock = $this->createMock(Context::class);
  54. $this->resultJsonFactoryMock = $this->createMock(JsonFactory::class);
  55. $this->sectionIdentifierMock = $this->createMock(Identifier::class);
  56. $this->sectionPoolMock = $this->getMockForAbstractClass(SectionPoolInterface::class);
  57. $this->escaperMock = $this->createMock(Escaper::class);
  58. $this->httpRequestMock = $this->createMock(HttpRequest::class);
  59. $this->resultJsonMock = $this->createMock(Json::class);
  60. $this->contextMock->expects($this->once())
  61. ->method('getRequest')
  62. ->willReturn($this->httpRequestMock);
  63. $this->loadAction = new Load(
  64. $this->contextMock,
  65. $this->resultJsonFactoryMock,
  66. $this->sectionIdentifierMock,
  67. $this->sectionPoolMock,
  68. $this->escaperMock
  69. );
  70. }
  71. /**
  72. * @param string $sectionNames
  73. * @param bool $forceNewSectionTimestamp
  74. * @param string[] $sectionNamesAsArray
  75. * @param bool $forceNewTimestamp
  76. * @dataProvider executeDataProvider
  77. */
  78. public function testExecute($sectionNames, $forceNewSectionTimestamp, $sectionNamesAsArray, $forceNewTimestamp)
  79. {
  80. $this->resultJsonFactoryMock->expects($this->once())
  81. ->method('create')
  82. ->willReturn($this->resultJsonMock);
  83. $this->resultJsonMock->expects($this->exactly(2))
  84. ->method('setHeader')
  85. ->withConsecutive(
  86. ['Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store'],
  87. ['Pragma', 'no-cache']
  88. );
  89. $this->httpRequestMock->expects($this->exactly(2))
  90. ->method('getParam')
  91. ->withConsecutive(['sections'], ['force_new_section_timestamp'])
  92. ->willReturnOnConsecutiveCalls($sectionNames, $forceNewSectionTimestamp);
  93. $this->sectionPoolMock->expects($this->once())
  94. ->method('getSectionsData')
  95. ->with($sectionNamesAsArray, $forceNewTimestamp)
  96. ->willReturn([
  97. 'message' => 'some message',
  98. 'someKey' => 'someValue'
  99. ]);
  100. $this->resultJsonMock->expects($this->once())
  101. ->method('setData')
  102. ->with([
  103. 'message' => 'some message',
  104. 'someKey' => 'someValue'
  105. ])
  106. ->willReturn($this->resultJsonMock);
  107. $this->loadAction->execute();
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function executeDataProvider()
  113. {
  114. return [
  115. [
  116. 'sectionNames' => 'sectionName1,sectionName2,sectionName3',
  117. 'forceNewSectionTimestamp' => 'forceNewSectionTimestamp',
  118. 'sectionNamesAsArray' => ['sectionName1', 'sectionName2', 'sectionName3'],
  119. 'forceNewTimestamp' => true
  120. ],
  121. [
  122. 'sectionNames' => null,
  123. 'forceNewSectionTimestamp' => null,
  124. 'sectionNamesAsArray' => null,
  125. 'forceNewTimestamp' => false
  126. ],
  127. ];
  128. }
  129. public function testExecuteWithThrowException()
  130. {
  131. $this->resultJsonFactoryMock->expects($this->once())
  132. ->method('create')
  133. ->willReturn($this->resultJsonMock);
  134. $this->resultJsonMock->expects($this->exactly(2))
  135. ->method('setHeader')
  136. ->withConsecutive(
  137. ['Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store'],
  138. ['Pragma', 'no-cache']
  139. );
  140. $this->httpRequestMock->expects($this->once())
  141. ->method('getParam')
  142. ->with('sections')
  143. ->willThrowException(new \Exception('Some Message'));
  144. $this->resultJsonMock->expects($this->once())
  145. ->method('setStatusHeader')
  146. ->with(
  147. \Zend\Http\Response::STATUS_CODE_400,
  148. \Zend\Http\AbstractMessage::VERSION_11,
  149. 'Bad Request'
  150. );
  151. $this->escaperMock->expects($this->once())
  152. ->method('escapeHtml')
  153. ->with('Some Message')
  154. ->willReturn('Some Message');
  155. $this->resultJsonMock->expects($this->once())
  156. ->method('setData')
  157. ->with(['message' => 'Some Message'])
  158. ->willReturn($this->resultJsonMock);
  159. $this->loadAction->execute();
  160. }
  161. }