123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Cms\Test\Unit\Controller\Block;
- use Magento\Cms\Controller\Adminhtml\Block\InlineEdit;
- class InlineEditTest extends \PHPUnit\Framework\TestCase
- {
- /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $request;
- /** @var \Magento\Cms\Model\Block|\PHPUnit_Framework_MockObject_MockObject */
- protected $cmsBlock;
- /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
- protected $context;
- /** @var \Magento\Cms\Api\BlockRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $blockRepository;
- /** @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject */
- protected $jsonFactory;
- /** @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject */
- protected $resultJson;
- /** @var InlineEdit */
- protected $controller;
- protected function setUp()
- {
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->request = $this->getMockForAbstractClass(
- \Magento\Framework\App\RequestInterface::class,
- [],
- '',
- false
- );
- $this->cmsBlock = $this->createMock(\Magento\Cms\Model\Block::class);
- $this->context = $helper->getObject(
- \Magento\Backend\App\Action\Context::class,
- [
- 'request' => $this->request
- ]
- );
- $this->blockRepository = $this->getMockForAbstractClass(
- \Magento\Cms\Api\BlockRepositoryInterface::class,
- [],
- '',
- false
- );
- $this->resultJson = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
- $this->jsonFactory = $this->createPartialMock(
- \Magento\Framework\Controller\Result\JsonFactory::class,
- ['create']
- );
- $this->controller = new InlineEdit(
- $this->context,
- $this->blockRepository,
- $this->jsonFactory
- );
- }
- public function prepareMocksForTestExecute()
- {
- $postData = [
- 1 => [
- 'title' => 'Catalog Events Lister',
- 'identifier' => 'Catalog Events Lister'
- ]
- ];
- $this->request->expects($this->at(0))
- ->method('getParam')
- ->with('isAjax')
- ->willReturn(true);
- $this->request->expects($this->at(1))
- ->method('getParam')
- ->with('items', [])
- ->willReturn($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);
- }
- public function testExecuteWithException()
- {
- $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();
- }
- public function testExecuteWithoutData()
- {
- $this->request->expects($this->at(0))
- ->method('getParam')
- ->with('isAjax')
- ->willReturn(true);
- $this->request->expects($this->at(1))
- ->method('getParam')
- ->with('items', [])
- ->willReturn([]);
- $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();
- }
- }
|