public function testUpdate() { $statusInfo = 'some status info'; $data = ['status' => 'success', 'message' => __('Database(s) successfully updated.'), 'status_info' => $statusInfo]; $this->resultJson->expects($this->once())->method('setData')->with($data)->willReturnSelf(); $this->updaterSelected->expects($this->once())->method('update')->willReturnSelf(); $statusBlock = $this->getMockBuilder('Tobai\\GeoIp2\\Block\\Adminhtml\\System\\Config\\Status')->disableOriginalConstructor()->getMock(); $layout = $this->getMock('Magento\\Framework\\View\\LayoutInterface'); $this->layoutFactory->expects($this->once())->method('create')->willReturn($layout); $layout->expects($this->once())->method('createBlock')->with('Tobai\\GeoIp2\\Block\\Adminhtml\\System\\Config\\Status')->willReturn($statusBlock); $statusBlock->expects($this->once())->method('getDbStatus')->willReturn($statusInfo); $this->logger->expects($this->never())->method('critical'); $this->assertSame($this->resultJson, $this->controllerUpdate->execute()); }
/** * Run test execute method * * @param int|bool $categoryId * @param int $storeId * @return void * * @dataProvider dataProviderExecute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute($categoryId, $storeId) { $rootCategoryId = 2; $this->requestMock->expects($this->atLeastOnce())->method('getParam')->will($this->returnValueMap([['id', false, $categoryId], ['store', null, $storeId]])); $this->requestMock->expects($this->atLeastOnce())->method('getQuery')->with('isAjax')->will($this->returnValue(false)); $this->mockInitCategoryCall(); $this->sessionMock->expects($this->once())->method('__call')->will($this->returnValue([])); if (!$categoryId) { if ($storeId) { $this->storeManagerInterfaceMock->expects($this->once())->method('getStore')->with($storeId)->will($this->returnSelf()); } else { $this->storeManagerInterfaceMock->expects($this->once())->method('getDefaultStoreView')->will($this->returnSelf()); } $this->storeManagerInterfaceMock->expects($this->once())->method('getRootCategoryId')->will($this->returnValue($rootCategoryId)); $categoryId = $rootCategoryId; } $this->requestMock->expects($this->atLeastOnce())->method('setParam')->with('id', $categoryId)->will($this->returnValue(true)); $this->categoryMock->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($categoryId)); /** * @var \Magento\Framework\View\Element\Template * |\PHPUnit_Framework_MockObject_MockObject $blockMock */ $blockMock = $this->getMock('Magento\\Framework\\View\\Element\\Template', ['setStoreId'], [], '', false); $blockMock->expects($this->once())->method('setStoreId')->with($storeId); $this->resultPageMock->expects($this->once())->method('getLayout')->will($this->returnSelf()); $this->resultPageMock->expects($this->once())->method('getBlock')->willReturn($blockMock); $this->edit->execute(); }
protected function setUp() { $this->layout = $this->getMockBuilder('Magento\\Framework\\View\\Layout')->setMethods(['addHandle', 'getUpdate', 'isLayoutDefined'])->disableOriginalConstructor()->getMock(); $this->layoutFactory = $this->getMockBuilder('Magento\\Framework\\View\\LayoutFactory')->disableOriginalConstructor()->getMock(); $this->layoutFactory->expects($this->any())->method('create')->will($this->returnValue($this->layout)); $this->layoutMerge = $this->getMockBuilder('Magento\\Framework\\View\\Model\\Layout\\Merge')->disableOriginalConstructor()->getMock(); $this->layout->expects($this->any())->method('getUpdate')->will($this->returnValue($this->layoutMerge)); $this->request = $this->getMockBuilder('Magento\\Framework\\App\\Request\\Http')->disableOriginalConstructor()->getMock(); $this->pageConfig = $this->getMockBuilder('Magento\\Framework\\View\\Page\\Config')->disableOriginalConstructor()->getMock(); $this->viewFileSystem = $this->getMockBuilder('Magento\\Framework\\View\\FileSystem')->disableOriginalConstructor()->getMock(); $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->context = $objectManagerHelper->getObject('Magento\\Framework\\View\\Element\\Template\\Context', ['layout' => $this->layout, 'request' => $this->request, 'viewFileSystem' => $this->viewFileSystem, 'pageConfig' => $this->pageConfig]); $this->translateInline = $this->getMock('Magento\\Framework\\Translate\\InlineInterface'); $this->pageConfigRenderer = $this->getMockBuilder('Magento\\Framework\\View\\Page\\Config\\Renderer')->disableOriginalConstructor()->getMock(); $pageConfigRendererFactory = $this->getMockBuilder('Magento\\Framework\\View\\Page\\Config\\RendererFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $pageConfigRendererFactory->expects($this->once())->method('create')->with(['pageConfig' => $this->pageConfig])->willReturn($this->pageConfigRenderer); $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->page = $objectManagerHelper->getObject('Magento\\Framework\\View\\Result\\Page', ['isIsolated' => true, 'layoutFactory' => $this->layoutFactory, 'context' => $this->context, 'translateInline' => $this->translateInline, 'pageConfigRendererFactory' => $pageConfigRendererFactory]); }
/** * Run test execute method * * @param int|bool $categoryId * @param int $storeId * @param int|null $parentId * @return void * * @dataProvider dataProviderExecute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute($categoryId, $storeId, $parentId) { $rootCategoryId = \Magento\Catalog\Model\Category::TREE_ROOT_ID; $products = [['any_product']]; $postData = ['general-data', 'parent' => $parentId, 'category_products' => json_encode($products)]; if (isset($storeId)) { $postData['store_id'] = $storeId; } /** * @var \Magento\Backend\Model\View\Result\Redirect * |\PHPUnit_Framework_MockObject_MockObject $resultRedirectMock */ $resultRedirectMock = $this->getMock('Magento\\Backend\\Model\\View\\Result\\Redirect', [], [], '', false); /** * @var \Magento\Framework\View\Element\Messages * |\PHPUnit_Framework_MockObject_MockObject $blockMock */ $blockMock = $this->getMock('Magento\\Framework\\View\\Element\\Messages', ['setMessages', 'getGroupedHtml'], [], '', false); /** * @var \Magento\Catalog\Model\Category * |\PHPUnit_Framework_MockObject_MockObject $categoryMock */ $categoryMock = $this->getMock('Magento\\Catalog\\Model\\Category', ['setStoreId', 'load', 'getPath', 'getResource', 'setPath', 'setParentId', 'setData', 'addData', 'setAttributeSetId', 'getDefaultAttributeSetId', 'getProductsReadonly', 'setPostedProducts', 'getId', 'validate', 'unsetData', 'save', 'toArray'], [], '', false); /** * @var \Magento\Catalog\Model\Category * |\PHPUnit_Framework_MockObject_MockObject $parentCategoryMock */ $parentCategoryMock = $this->getMock('Magento\\Catalog\\Model\\Category', ['setStoreId', 'load', 'getPath', 'setPath', 'setParentId', 'setData', 'addData', 'setAttributeSetId', 'getDefaultAttributeSetId', 'getProductsReadonly', 'setPostedProducts', 'getId'], [], '', false); /** * @var \Magento\Backend\Model\Auth\Session * |\PHPUnit_Framework_MockObject_MockObject $sessionMock */ $sessionMock = $this->getMock('Magento\\Backend\\Model\\Auth\\Session', [], [], '', false); /** * @var \Magento\Framework\Registry * |\PHPUnit_Framework_MockObject_MockObject $registryMock */ $registryMock = $this->getMock('Magento\\Framework\\Registry', ['register'], [], '', false); /** * @var \Magento\Cms\Model\Wysiwyg\Config * |\PHPUnit_Framework_MockObject_MockObject $wysiwygConfigMock */ $wysiwygConfigMock = $this->getMock('Magento\\Cms\\Model\\Wysiwyg\\Config', ['setStoreId'], [], '', false); /** * @var \Magento\Store\Model\StoreManagerInterface * |\PHPUnit_Framework_MockObject_MockObject $storeManagerMock */ $storeManagerMock = $this->getMockForAbstractClass('Magento\\Store\\Model\\StoreManagerInterface', [], '', false, true, true, ['getStore', 'getRootCategoryId']); /** * @var \Magento\Framework\View\Layout * |\PHPUnit_Framework_MockObject_MockObject $layoutMock */ $layoutMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\Layout', [], '', false, true, true, ['getMessagesBlock']); /** * @var \Magento\Framework\Controller\Result\Json * |\PHPUnit_Framework_MockObject_MockObject $resultJsonMock */ $resultJsonMock = $this->getMock('Magento\\Cms\\Model\\Wysiwyg\\Config', ['setData'], [], '', false); /** * @var \Magento\Framework\Message\Collection * |\PHPUnit_Framework_MockObject_MockObject $messagesMock */ $messagesMock = $this->getMock('Magento\\Framework\\Message\\Collection', [], [], '', false); $this->resultRedirectFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultRedirectMock)); $this->requestMock->expects($this->atLeastOnce())->method('getParam')->will($this->returnValueMap([['id', false, $categoryId], ['store', null, $storeId], ['parent', null, $parentId]])); $this->objectManagerMock->expects($this->atLeastOnce())->method('create')->will($this->returnValue($categoryMock)); $this->objectManagerMock->expects($this->atLeastOnce())->method('get')->will($this->returnValueMap([['Magento\\Backend\\Model\\Auth\\Session', $sessionMock], ['Magento\\Framework\\Registry', $registryMock], ['Magento\\Cms\\Model\\Wysiwyg\\Config', $wysiwygConfigMock], ['Magento\\Store\\Model\\StoreManagerInterface', $storeManagerMock]])); $categoryMock->expects($this->once())->method('setStoreId')->with($storeId); $registryMock->expects($this->any())->method('register')->will($this->returnValueMap([['category', $categoryMock], ['current_category', $categoryMock]])); $wysiwygConfigMock->expects($this->once())->method('setStoreId')->with($storeId); $this->requestMock->expects($this->atLeastOnce())->method('getPost')->will($this->returnValueMap([['use_config', ['attribute']], ['use_default', ['default-attribute']], ['return_session_messages_only', true]])); $this->requestMock->expects($this->atLeastOnce())->method('getPostValue')->willReturn($postData); $categoryMock->expects($this->once())->method('addData')->with($postData); $categoryMock->expects($this->at(0))->method('getId')->will($this->returnValue($categoryId)); if (!$parentId) { if ($storeId) { $storeManagerMock->expects($this->once())->method('getStore')->with($storeId)->will($this->returnSelf()); $storeManagerMock->expects($this->once())->method('getRootCategoryId')->will($this->returnValue($rootCategoryId)); $parentId = $rootCategoryId; } } $categoryMock->expects($this->any())->method('load')->will($this->returnValue($parentCategoryMock)); $parentCategoryMock->expects($this->once())->method('getPath')->will($this->returnValue('parent_category_path')); $categoryMock->expects($this->once())->method('setPath')->with('parent_category_path'); $categoryMock->expects($this->once())->method('setParentId')->with($parentId); $categoryMock->expects($this->atLeastOnce())->method('setData')->will($this->returnValueMap([['attribute', null, true], ['default-attribute', false, true], ['use_post_data_config', ['attribute'], true]])); $categoryMock->expects($this->once())->method('getDefaultAttributeSetId')->will($this->returnValue('default-attribute')); $categoryMock->expects($this->once())->method('setAttributeSetId')->with('default-attribute'); $categoryMock->expects($this->once())->method('getProductsReadonly')->will($this->returnValue(false)); $categoryMock->expects($this->once())->method('setPostedProducts')->with($products); $this->eventManagerMock->expects($this->once())->method('dispatch')->with('catalog_category_prepare_save', ['category' => $categoryMock, 'request' => $this->requestMock]); $categoryResource = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Category', [], [], '', false); $categoryMock->expects($this->once())->method('getResource')->will($this->returnValue($categoryResource)); $categoryMock->expects($this->once())->method('validate')->will($this->returnValue(true)); $categoryMock->expects($this->once())->method('unsetData')->with('use_post_data_config'); $categoryMock->expects($this->once())->method('save'); $this->messageManagerMock->expects($this->once())->method('addSuccess')->with(__('You saved the category.')); $categoryMock->expects($this->at(1))->method('getId')->will($this->returnValue(111)); $this->layoutFactoryMock->expects($this->once())->method('create')->will($this->returnValue($layoutMock)); $layoutMock->expects($this->once())->method('getMessagesBlock')->will($this->returnValue($blockMock)); $this->messageManagerMock->expects($this->once())->method('getMessages')->with(true)->will($this->returnValue($messagesMock)); $blockMock->expects($this->once())->method('setMessages')->with($messagesMock); $blockMock->expects($this->once())->method('getGroupedHtml')->will($this->returnValue('grouped-html')); $this->resultJsonFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultJsonMock)); $categoryMock->expects($this->once())->method('toArray')->will($this->returnValue(['category-data'])); $resultJsonMock->expects($this->once())->method('setData')->with(['messages' => 'grouped-html', 'error' => false, 'category' => ['category-data']])->will($this->returnValue('result-execute')); $this->assertEquals('result-execute', $this->save->execute()); }