PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/ProductTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 113 lines | 80 code | 15 blank | 18 comment | 0 complexity | 1e42abca4b17a52edb0a1e15efead1df MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Unit\Controller\Adminhtml;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. abstract class ProductTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /** @var \PHPUnit_Framework_MockObject_MockObject */
  13. protected $context;
  14. /** @var \Magento\Catalog\Controller\Product */
  15. protected $action;
  16. /** @var \Magento\Framework\View\Layout */
  17. protected $layout;
  18. /** @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $session;
  20. /** @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $request;
  22. /**
  23. * Init context object
  24. *
  25. * @param array $additionalParams
  26. * @return \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected function initContext(array $additionalParams = [])
  29. {
  30. $productActionMock = $this->getMock(\Magento\Catalog\Model\Product\Action::class, [], [], '', false);
  31. $objectManagerMock = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
  32. $objectManagerMock->expects($this->any())->method('get')->will($this->returnValue($productActionMock));
  33. $block = $this->getMockBuilder(\Magento\Framework\View\Element\AbstractBlock::class)
  34. ->disableOriginalConstructor()->getMockForAbstractClass();
  35. $this->layout = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
  36. ->setMethods(['getBlock'])->disableOriginalConstructor()
  37. ->getMock();
  38. $this->layout->expects($this->any())->method('getBlock')->will($this->returnValue($block));
  39. $eventManager = $this->getMockBuilder(\Magento\Framework\Event\Manager::class)
  40. ->setMethods(['dispatch'])->disableOriginalConstructor()->getMock();
  41. $eventManager->expects($this->any())->method('dispatch')->will($this->returnSelf());
  42. $title = $this->getMockBuilder(\Magento\Framework\App\Action\Title::class)
  43. ->setMethods(['add'])->disableOriginalConstructor()->getMock();
  44. $title->expects($this->any())->method('prepend')->withAnyParameters()->will($this->returnSelf());
  45. $requestInterfaceMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)->setMethods(
  46. ['getParam', 'getPost', 'getFullActionName', 'getPostValue']
  47. )->disableOriginalConstructor()->getMock();
  48. $responseInterfaceMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)->setMethods(
  49. ['setRedirect', 'sendResponse']
  50. )->getMock();
  51. $managerInterfaceMock = $this->getMock(\Magento\Framework\Message\ManagerInterface::class);
  52. $sessionMock = $this->getMock(
  53. \Magento\Backend\Model\Session::class,
  54. ['getProductData', 'setProductData'],
  55. [],
  56. '',
  57. false
  58. );
  59. $actionFlagMock = $this->getMock(\Magento\Framework\App\ActionFlag::class, [], [], '', false);
  60. $helperDataMock = $this->getMock(\Magento\Backend\Helper\Data::class, [], [], '', false);
  61. $this->context = $this->getMock(
  62. \Magento\Backend\App\Action\Context::class,
  63. [
  64. 'getRequest',
  65. 'getResponse',
  66. 'getObjectManager',
  67. 'getEventManager',
  68. 'getMessageManager',
  69. 'getSession',
  70. 'getActionFlag',
  71. 'getHelper',
  72. 'getTitle',
  73. 'getView',
  74. 'getResultRedirectFactory',
  75. 'getResultFactory'
  76. ],
  77. [],
  78. '',
  79. false
  80. );
  81. $this->context->expects($this->any())->method('getTitle')->will($this->returnValue($title));
  82. $this->context->expects($this->any())->method('getEventManager')->will($this->returnValue($eventManager));
  83. $this->context->expects($this->any())->method('getRequest')->will($this->returnValue($requestInterfaceMock));
  84. $this->context->expects($this->any())->method('getResponse')->will($this->returnValue($responseInterfaceMock));
  85. $this->context->expects($this->any())->method('getObjectManager')->will($this->returnValue($objectManagerMock));
  86. $this->context->expects($this->any())->method('getMessageManager')
  87. ->will($this->returnValue($managerInterfaceMock));
  88. $this->context->expects($this->any())->method('getSession')->will($this->returnValue($sessionMock));
  89. $this->context->expects($this->any())->method('getActionFlag')->will($this->returnValue($actionFlagMock));
  90. $this->context->expects($this->any())->method('getHelper')->will($this->returnValue($helperDataMock));
  91. foreach ($additionalParams as $property => $object) {
  92. $this->context->expects($this->any())->method('get' . ucfirst($property))->willReturn($object);
  93. }
  94. $this->session = $sessionMock;
  95. $this->request = $requestInterfaceMock;
  96. return $this->context;
  97. }
  98. }