Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PopupTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class PopupTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $objectManager;
16 
20  protected $action;
21 
25  protected $context;
26 
30  protected $request;
31 
35  protected $factory;
36 
40  protected $registry;
41 
45  protected $resultFactoryMock;
46 
50  protected $resultLayoutMock;
51 
52  protected function setUp()
53  {
54  $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
55  $this->factory = $this->createPartialMock(\Magento\Catalog\Model\ProductFactory::class, ['create']);
56  $this->registry = $this->createMock(\Magento\Framework\Registry::class);
57  $this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->resultLayoutMock = $this->getMockBuilder(\Magento\Framework\View\Result\Layout::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->resultFactoryMock->expects($this->any())
65  ->method('create')
66  ->with(ResultFactory::TYPE_LAYOUT, [])
67  ->willReturn($this->resultLayoutMock);
68 
69  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
70  $this->context = $this->objectManager->getObject(
71  \Magento\Backend\App\Action\Context::class,
72  [
73  'request' => $this->request,
74  'resultFactory' => $this->resultFactoryMock
75  ]
76  );
77  $this->action = $this->objectManager->getObject(
78  \Magento\GroupedProduct\Controller\Adminhtml\Edit\Popup::class,
79  [
80  'context' => $this->context,
81  'factory' => $this->factory,
82  'registry' => $this->registry
83  ]
84  );
85  }
86 
87  public function testPopupActionNoProductId()
88  {
89  $storeId = 12;
90  $typeId = 4;
91  $productId = null;
92  $setId = 0;
93  $product = $this->createPartialMock(
94  \Magento\Catalog\Model\Product::class,
95  ['setStoreId', 'setTypeId', 'setData', '__wakeup']
96  );
97 
98  $this->request->expects($this->at(0))->method('getParam')->with('id')->will($this->returnValue($productId));
99  $this->factory->expects($this->once())->method('create')->will($this->returnValue($product));
100  $this->request->expects(
101  $this->at(1)
102  )->method(
103  'getParam'
104  )->with(
105  'store',
106  0
107  )->will(
108  $this->returnValue($storeId)
109  );
110 
111  $product->expects($this->once())->method('setStoreId')->with($storeId);
112  $this->request->expects($this->at(2))->method('getParam')->with('type')->will($this->returnValue($typeId));
113  $product->expects($this->once())->method('setTypeId')->with($typeId);
114  $product->expects($this->once())->method('setData')->with('_edit_mode', true);
115  $this->request->expects($this->at(3))->method('getParam')->with('set')->will($this->returnValue($setId));
116  $this->registry->expects($this->once())->method('register')->with('current_product', $product);
117 
118  $this->assertSame($this->resultLayoutMock, $this->action->execute());
119  }
120 
122  {
123  $storeId = 12;
124  $typeId = 4;
125  $setId = 0;
126  $productId = 399;
127  $product = $this->createPartialMock(
128  \Magento\Catalog\Model\Product::class,
129  ['setStoreId', 'setTypeId', 'setData', 'load', '__wakeup']
130  );
131 
132  $this->request->expects($this->at(0))->method('getParam')->with('id')->will($this->returnValue($productId));
133  $this->factory->expects($this->once())->method('create')->will($this->returnValue($product));
134  $this->request->expects(
135  $this->at(1)
136  )->method(
137  'getParam'
138  )->with(
139  'store',
140  0
141  )->will(
142  $this->returnValue($storeId)
143  );
144  $product->expects($this->once())->method('setStoreId')->with($storeId);
145  $this->request->expects($this->at(2))->method('getParam')->with('type')->will($this->returnValue($typeId));
146  $product->expects($this->never())->method('setTypeId');
147  $product->expects($this->once())->method('setData')->with('_edit_mode', true);
148  $product->expects($this->once())->method('load')->with($productId);
149  $this->request->expects($this->at(3))->method('getParam')->with('set')->will($this->returnValue($setId));
150  $this->registry->expects($this->once())->method('register')->with('current_product', $product);
151 
152  $this->assertSame($this->resultLayoutMock, $this->action->execute());
153  }
154 }