layoutMock = $this->getMockBuilder(Layout::class) ->disableOriginalConstructor() ->getMock(); $this->layoutProcessorMock = $this->getMockForAbstractClass( ProcessorInterface::class ); $this->layoutCacheKeyMock = $this->getMockForAbstractClass( LayoutCacheKeyInterface::class ); $contextMock = $this->getMockBuilder(Context::class) ->disableOriginalConstructor() ->getMock(); $this->requestMock = $this->getMockBuilder( Http::class )->disableOriginalConstructor() ->getMock(); $this->responseMock = $this->getMockBuilder( \Magento\Framework\App\Response\Http::class )->disableOriginalConstructor() ->getMock(); $this->viewMock = $this->getMockBuilder(View::class) ->disableOriginalConstructor() ->getMock(); $this->layoutMock->expects($this->any()) ->method('getUpdate') ->willReturn($this->layoutProcessorMock); $contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock); $contextMock->expects($this->any())->method('getResponse')->willReturn($this->responseMock); $contextMock->expects($this->any())->method('getView')->willReturn($this->viewMock); $this->translateInline = $this->getMockForAbstractClass(InlineInterface::class); $regexFactoryMock = $this->getMockBuilder(RegexFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $regexObject = new Regex(self::VALIDATION_RULE_PATTERN); $regexFactoryMock->expects($this->any())->method('create') ->willReturn($regexObject); $helperObjectManager = new ObjectManager($this); $this->action = $helperObjectManager->getObject( Render::class, [ 'context' => $contextMock, 'translateInline' => $this->translateInline, 'jsonSerializer' => new Json(), 'base64jsonSerializer' => new Base64Json(), 'layoutCacheKey' => $this->layoutCacheKeyMock, 'regexValidatorFactory' => $regexFactoryMock ] ); } /** * @return void */ public function testExecuteNotAjax(): void { $this->requestMock->expects($this->once())->method('isAjax')->willReturn(false); $this->requestMock->expects($this->once())->method('setActionName')->willReturn('noroute'); $this->requestMock->expects($this->once())->method('setDispatched')->willReturn(false); $this->layoutCacheKeyMock->expects($this->never()) ->method('addCacheKeys'); $this->action->execute(); } /** * Test no params: blocks, handles. * * @return void */ public function testExecuteNoParams(): void { $this->requestMock->expects($this->once())->method('isAjax')->willReturn(true); $this->requestMock ->method('getParam') ->withConsecutive([], [], [], [], [], [], ['blocks', ''], ['handles', '']) ->willReturnOnConsecutiveCalls(null, null, null, null, null, null, '', ''); $this->layoutCacheKeyMock->expects($this->never()) ->method('addCacheKeys'); $this->action->execute(); } /** * @return void */ public function testExecute(): void { $blocks = ['block1', 'block2']; $handles = ['handle1', 'handle2']; $originalRequest = '{"route":"route","controller":"controller","action":"action","uri":"uri"}'; $expectedData = ['block1' => 'data1', 'block2' => 'data2']; $blockInstance1 = $this->createPartialMock( StubBlock::class, ['toHtml'] ); $blockInstance1->expects($this->once())->method('toHtml')->willReturn($expectedData['block1']); $blockInstance2 = $this->createPartialMock( StubBlock::class, ['toHtml'] ); $blockInstance2->expects($this->once())->method('toHtml')->willReturn($expectedData['block2']); $this->requestMock->expects($this->once())->method('isAjax')->willReturn(true); $this->requestMock ->method('getRouteName') ->willReturn('magento_pagecache'); $this->requestMock ->method('getParam') ->withConsecutive( ['originalRequest'], ['blocks', ''], ['handles', ''] ) ->willReturnOnConsecutiveCalls( $originalRequest, json_encode($blocks), base64_encode(json_encode($handles)) ); $this->requestMock ->method('getRequestUri') ->willReturn('uri'); $this->requestMock ->method('getActionName') ->willReturn('render'); $this->requestMock ->method('getControllerName') ->willReturn('block'); $this->viewMock->expects($this->once())->method('loadLayout')->with($handles); $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock); $this->layoutMock->expects($this->never()) ->method('getUpdate'); $this->layoutCacheKeyMock->expects($this->atLeastOnce()) ->method('addCacheKeys'); $this->layoutMock ->method('getBlock') ->withConsecutive([$blocks[0]], [$blocks[1]]) ->willReturnOnConsecutiveCalls($blockInstance1, $blockInstance2); $this->translateInline->expects($this->once()) ->method('processResponseBody') ->with($expectedData) ->willReturnSelf(); $this->responseMock->expects($this->once()) ->method('appendBody') ->with(json_encode($expectedData)); $this->action->execute(); } }