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