![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-cms/Test/Unit/Controller/Block/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\Cms\Test\Unit\Controller\Block; use Magento\Backend\App\Action\Context; use Magento\Cms\Api\BlockRepositoryInterface; use Magento\Cms\Controller\Adminhtml\Block\InlineEdit; use Magento\Cms\Model\Block; use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\Result\Json; use Magento\Framework\Controller\Result\JsonFactory; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; class InlineEditTest extends TestCase { /** * @var RequestInterface|MockObject */ protected $request; /** * @var Block|MockObject */ protected $cmsBlock; /** * @var Context|MockObject */ protected $context; /** * @var BlockRepositoryInterface|MockObject */ protected $blockRepository; /** * @var JsonFactory|MockObject */ protected $jsonFactory; /** * @var Json|MockObject */ protected $resultJson; /** * @var InlineEdit */ protected $controller; /** * @inheritdoc */ protected function setUp(): void { $helper = new ObjectManager($this); $this->request = $this->getMockForAbstractClass( RequestInterface::class, [], '', false ); $this->cmsBlock = $this->createMock(Block::class); $this->context = $helper->getObject( Context::class, [ 'request' => $this->request ] ); $this->blockRepository = $this->getMockForAbstractClass( BlockRepositoryInterface::class, [], '', false ); $this->resultJson = $this->createMock(Json::class); $this->jsonFactory = $this->createPartialMock( JsonFactory::class, ['create'] ); $this->controller = new InlineEdit( $this->context, $this->blockRepository, $this->jsonFactory ); } /** * @return void */ public function prepareMocksForTestExecute(): void { $postData = [ 1 => [ 'title' => 'Catalog Events Lister', 'identifier' => 'Catalog Events Lister' ] ]; $this->request ->method('getParam') ->withConsecutive(['isAjax'], ['items', []]) ->willReturnOnConsecutiveCalls(true, $postData); $this->blockRepository->expects($this->once()) ->method('getById') ->with(1) ->willReturn($this->cmsBlock); $this->cmsBlock->expects($this->atLeastOnce()) ->method('getId') ->willReturn('1'); $this->cmsBlock->expects($this->once()) ->method('getData') ->willReturn([ 'identifier' => 'test-identifier' ]); $this->cmsBlock->expects($this->once()) ->method('setData') ->with([ 'title' => 'Catalog Events Lister', 'identifier' => 'Catalog Events Lister' ]); $this->jsonFactory->expects($this->once()) ->method('create') ->willReturn($this->resultJson); } /** * @return void */ public function testExecuteWithException(): void { $this->prepareMocksForTestExecute(); $this->blockRepository->expects($this->once()) ->method('save') ->with($this->cmsBlock) ->willThrowException(new \Exception('Exception')); $this->resultJson->expects($this->once()) ->method('setData') ->with( [ 'messages' => ['[Block ID: 1] Exception'], 'error' => true ] ) ->willReturnSelf(); $this->controller->execute(); } /** * @return void */ public function testExecuteWithoutData(): void { $this->request ->method('getParam') ->withConsecutive(['isAjax'], ['items', []]) ->willReturnOnConsecutiveCalls(true, []); $this->jsonFactory->expects($this->once()) ->method('create') ->willReturn($this->resultJson); $this->resultJson->expects($this->once()) ->method('setData') ->with( [ 'messages' => ['Please correct the data sent.'], 'error' => true ] ) ->willReturnSelf(); $this->controller->execute(); } }