123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Customer\Test\Unit\Controller\Section;
- use Magento\Customer\Controller\Section\Load;
- use Magento\Customer\CustomerData\Section\Identifier;
- use Magento\Customer\CustomerData\SectionPoolInterface;
- use Magento\Framework\App\Action\Context;
- use Magento\Framework\Controller\Result\Json;
- use Magento\Framework\Controller\Result\JsonFactory;
- use Magento\Framework\Escaper;
- use \PHPUnit_Framework_MockObject_MockObject as MockObject;
- use Magento\Framework\App\Request\Http as HttpRequest;
- class LoadTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Load
- */
- private $loadAction;
- /**
- * @var Context|MockObject
- */
- private $contextMock;
- /**
- * @var JsonFactory|MockObject
- */
- private $resultJsonFactoryMock;
- /**
- * @var Identifier|MockObject
- */
- private $sectionIdentifierMock;
- /**
- * @var SectionPoolInterface|MockObject
- */
- private $sectionPoolMock;
- /**
- * @var \Magento\Framework\Escaper|MockObject
- */
- private $escaperMock;
- /**
- * @var Json|MockObject
- */
- private $resultJsonMock;
- /**
- * @var HttpRequest|MockObject
- */
- private $httpRequestMock;
- protected function setUp()
- {
- $this->contextMock = $this->createMock(Context::class);
- $this->resultJsonFactoryMock = $this->createMock(JsonFactory::class);
- $this->sectionIdentifierMock = $this->createMock(Identifier::class);
- $this->sectionPoolMock = $this->getMockForAbstractClass(SectionPoolInterface::class);
- $this->escaperMock = $this->createMock(Escaper::class);
- $this->httpRequestMock = $this->createMock(HttpRequest::class);
- $this->resultJsonMock = $this->createMock(Json::class);
- $this->contextMock->expects($this->once())
- ->method('getRequest')
- ->willReturn($this->httpRequestMock);
- $this->loadAction = new Load(
- $this->contextMock,
- $this->resultJsonFactoryMock,
- $this->sectionIdentifierMock,
- $this->sectionPoolMock,
- $this->escaperMock
- );
- }
- /**
- * @param string $sectionNames
- * @param bool $forceNewSectionTimestamp
- * @param string[] $sectionNamesAsArray
- * @param bool $forceNewTimestamp
- * @dataProvider executeDataProvider
- */
- public function testExecute($sectionNames, $forceNewSectionTimestamp, $sectionNamesAsArray, $forceNewTimestamp)
- {
- $this->resultJsonFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->resultJsonMock);
- $this->resultJsonMock->expects($this->exactly(2))
- ->method('setHeader')
- ->withConsecutive(
- ['Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store'],
- ['Pragma', 'no-cache']
- );
- $this->httpRequestMock->expects($this->exactly(2))
- ->method('getParam')
- ->withConsecutive(['sections'], ['force_new_section_timestamp'])
- ->willReturnOnConsecutiveCalls($sectionNames, $forceNewSectionTimestamp);
- $this->sectionPoolMock->expects($this->once())
- ->method('getSectionsData')
- ->with($sectionNamesAsArray, $forceNewTimestamp)
- ->willReturn([
- 'message' => 'some message',
- 'someKey' => 'someValue'
- ]);
- $this->resultJsonMock->expects($this->once())
- ->method('setData')
- ->with([
- 'message' => 'some message',
- 'someKey' => 'someValue'
- ])
- ->willReturn($this->resultJsonMock);
- $this->loadAction->execute();
- }
- /**
- * @return array
- */
- public function executeDataProvider()
- {
- return [
- [
- 'sectionNames' => 'sectionName1,sectionName2,sectionName3',
- 'forceNewSectionTimestamp' => 'forceNewSectionTimestamp',
- 'sectionNamesAsArray' => ['sectionName1', 'sectionName2', 'sectionName3'],
- 'forceNewTimestamp' => true
- ],
- [
- 'sectionNames' => null,
- 'forceNewSectionTimestamp' => null,
- 'sectionNamesAsArray' => null,
- 'forceNewTimestamp' => false
- ],
- ];
- }
- public function testExecuteWithThrowException()
- {
- $this->resultJsonFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->resultJsonMock);
- $this->resultJsonMock->expects($this->exactly(2))
- ->method('setHeader')
- ->withConsecutive(
- ['Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store'],
- ['Pragma', 'no-cache']
- );
- $this->httpRequestMock->expects($this->once())
- ->method('getParam')
- ->with('sections')
- ->willThrowException(new \Exception('Some Message'));
- $this->resultJsonMock->expects($this->once())
- ->method('setStatusHeader')
- ->with(
- \Zend\Http\Response::STATUS_CODE_400,
- \Zend\Http\AbstractMessage::VERSION_11,
- 'Bad Request'
- );
- $this->escaperMock->expects($this->once())
- ->method('escapeHtml')
- ->with('Some Message')
- ->willReturn('Some Message');
- $this->resultJsonMock->expects($this->once())
- ->method('setData')
- ->with(['message' => 'Some Message'])
- ->willReturn($this->resultJsonMock);
- $this->loadAction->execute();
- }
- }
|