PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-configurable-product/Test/Unit/Controller/Adminhtml/Product/Builder/PluginTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 314 lines | 261 code | 15 blank | 38 comment | 0 complexity | 5552c0746f0acc4927bc7674aed8cde5 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Test\Unit\Controller\Adminhtml\Product\Builder;
  7. class PluginTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @var \Magento\ConfigurableProduct\Controller\Adminhtml\Product\Builder\Plugin
  11. */
  12. protected $plugin;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $productFactoryMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $configurableTypeMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $requestMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $productMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $attributeMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $configurableMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $frontendAttrMock;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $subjectMock;
  45. /**
  46. * @var \Closure
  47. */
  48. protected $closureMock;
  49. protected function setUp()
  50. {
  51. $this->productFactoryMock = $this->getMock(
  52. 'Magento\Catalog\Model\ProductFactory',
  53. ['create'],
  54. [],
  55. '',
  56. false
  57. );
  58. $this->configurableTypeMock = $this->getMock(
  59. 'Magento\ConfigurableProduct\Model\Product\Type\Configurable',
  60. [],
  61. [],
  62. '',
  63. false
  64. );
  65. $this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
  66. $methods = ['setTypeId', 'getAttributes', 'addData', 'setWebsiteIds', '__wakeup'];
  67. $this->productMock = $this->getMock('Magento\Catalog\Model\Product', $methods, [], '', false);
  68. $product = $this->productMock;
  69. $attributeMethods = [
  70. 'getId',
  71. 'getFrontend',
  72. 'getAttributeCode',
  73. '__wakeup',
  74. 'setIsRequired',
  75. 'getIsUnique',
  76. ];
  77. $this->attributeMock = $this->getMock(
  78. 'Magento\Catalog\Model\ResourceModel\Eav\Attribute',
  79. $attributeMethods,
  80. [],
  81. '',
  82. false
  83. );
  84. $configMethods = [
  85. 'setStoreId',
  86. 'getTypeInstance',
  87. 'getIdFieldName',
  88. 'getData',
  89. 'getWebsiteIds',
  90. '__wakeup',
  91. 'load',
  92. 'setTypeId',
  93. 'getSetAttributes',
  94. ];
  95. $this->configurableMock = $this->getMock(
  96. 'Magento\ConfigurableProduct\Model\Product\Type\Configurable',
  97. $configMethods,
  98. [],
  99. '',
  100. false
  101. );
  102. $this->frontendAttrMock = $this->getMock(
  103. 'Magento\Quote\Model\ResourceModel\Quote\Address\Attribute\Frontend',
  104. [],
  105. [],
  106. '',
  107. false
  108. );
  109. $this->subjectMock = $this->getMock(
  110. 'Magento\Catalog\Controller\Adminhtml\Product\Builder',
  111. [],
  112. [],
  113. '',
  114. false
  115. );
  116. $this->closureMock = function () use ($product) {
  117. return $product;
  118. };
  119. $this->plugin = new \Magento\ConfigurableProduct\Controller\Adminhtml\Product\Builder\Plugin(
  120. $this->productFactoryMock,
  121. $this->configurableTypeMock
  122. );
  123. }
  124. /**
  125. * @return void
  126. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  127. */
  128. public function testAroundBuild()
  129. {
  130. $this->requestMock->expects($this->once())->method('has')->with('attributes')->will($this->returnValue(true));
  131. $valueMap = [
  132. ['attributes', null, ['attributes']],
  133. ['popup', null, true],
  134. ['required', null, '1,2'],
  135. ['product', null, 'product'],
  136. ['id', false, false],
  137. ['type', null, 'store_type'],
  138. ];
  139. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($valueMap));
  140. $this->productMock->expects(
  141. $this->once()
  142. )->method(
  143. 'setTypeId'
  144. )->with(
  145. \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE
  146. )->will(
  147. $this->returnSelf()
  148. );
  149. $this->configurableTypeMock->expects(
  150. $this->once()
  151. )->method(
  152. 'setUsedProductAttributeIds'
  153. )->with(
  154. ['attributes']
  155. )->will(
  156. $this->returnSelf()
  157. );
  158. $this->productMock->expects(
  159. $this->once()
  160. )->method(
  161. 'getAttributes'
  162. )->will(
  163. $this->returnValue([$this->attributeMock])
  164. );
  165. $this->attributeMock->expects($this->once())->method('getId')->will($this->returnValue(1));
  166. $this->attributeMock->expects($this->once())->method('setIsRequired')->with(1)->will($this->returnSelf());
  167. $this->productFactoryMock->expects(
  168. $this->once()
  169. )->method(
  170. 'create'
  171. )->will(
  172. $this->returnValue($this->configurableMock)
  173. );
  174. $this->configurableMock->expects($this->once())->method('setStoreId')->with(0)->will($this->returnSelf());
  175. $this->configurableMock->expects($this->once())->method('load')->with('product')->will($this->returnSelf());
  176. $this->configurableMock->expects(
  177. $this->once()
  178. )->method(
  179. 'setTypeId'
  180. )->with(
  181. 'store_type'
  182. )->will(
  183. $this->returnSelf()
  184. );
  185. $this->configurableMock->expects($this->once())->method('getTypeInstance')->will($this->returnSelf());
  186. $this->configurableMock->expects(
  187. $this->once()
  188. )->method(
  189. 'getSetAttributes'
  190. )->with(
  191. $this->configurableMock
  192. )->will(
  193. $this->returnValue([$this->attributeMock])
  194. );
  195. $this->configurableMock->expects(
  196. $this->once()
  197. )->method(
  198. 'getIdFieldName'
  199. )->will(
  200. $this->returnValue('fieldName')
  201. );
  202. $this->attributeMock->expects($this->once())->method('getIsUnique')->will($this->returnValue(false));
  203. $this->attributeMock->expects(
  204. $this->once()
  205. )->method(
  206. 'getFrontend'
  207. )->will(
  208. $this->returnValue($this->frontendAttrMock)
  209. );
  210. $this->frontendAttrMock->expects($this->once())->method('getInputType');
  211. $attributeCode = 'attribute_code';
  212. $this->attributeMock->expects(
  213. $this->any()
  214. )->method(
  215. 'getAttributeCode'
  216. )->will(
  217. $this->returnValue($attributeCode)
  218. );
  219. $this->configurableMock->expects(
  220. $this->once()
  221. )->method(
  222. 'getData'
  223. )->with(
  224. $attributeCode
  225. )->will(
  226. $this->returnValue('attribute_data')
  227. );
  228. $this->productMock->expects(
  229. $this->once()
  230. )->method(
  231. 'addData'
  232. )->with(
  233. [$attributeCode => 'attribute_data']
  234. )->will(
  235. $this->returnSelf()
  236. );
  237. $this->configurableMock->expects(
  238. $this->once()
  239. )->method(
  240. 'getWebsiteIds'
  241. )->will(
  242. $this->returnValue('website_id')
  243. );
  244. $this->productMock->expects(
  245. $this->once()
  246. )->method(
  247. 'setWebsiteIds'
  248. )->with(
  249. 'website_id'
  250. )->will(
  251. $this->returnSelf()
  252. );
  253. $this->assertEquals(
  254. $this->productMock,
  255. $this->plugin->aroundBuild($this->subjectMock, $this->closureMock, $this->requestMock)
  256. );
  257. }
  258. public function testAroundBuildWhenProductNotHaveAttributeAndRequiredParameters()
  259. {
  260. $valueMap = [
  261. ['attributes', null, null],
  262. ['popup', null, false],
  263. ['product', null, 'product'],
  264. ['id', false, false],
  265. ];
  266. $this->requestMock->expects($this->once())->method('has')->with('attributes')->will($this->returnValue(true));
  267. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($valueMap));
  268. $this->productMock->expects(
  269. $this->once()
  270. )->method(
  271. 'setTypeId'
  272. )->with(
  273. \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
  274. );
  275. $this->productMock->expects($this->never())->method('getAttributes');
  276. $this->productFactoryMock->expects($this->never())->method('create');
  277. $this->configurableMock->expects($this->never())->method('getTypeInstance');
  278. $this->attributeMock->expects($this->never())->method('getAttributeCode');
  279. $this->assertEquals(
  280. $this->productMock,
  281. $this->plugin->aroundBuild($this->subjectMock, $this->closureMock, $this->requestMock)
  282. );
  283. }
  284. public function testAroundBuildWhenAttributesAreEmpty()
  285. {
  286. $valueMap = [['popup', null, false], ['product', null, 'product'], ['id', false, false]];
  287. $this->requestMock->expects($this->once())->method('has')->with('attributes')->will($this->returnValue(false));
  288. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($valueMap));
  289. $this->productMock->expects($this->never())->method('setTypeId');
  290. $this->productMock->expects($this->never())->method('getAttributes');
  291. $this->productFactoryMock->expects($this->never())->method('create');
  292. $this->configurableMock->expects($this->never())->method('getTypeInstance');
  293. $this->attributeMock->expects($this->never())->method('getAttributeCode');
  294. $this->assertEquals(
  295. $this->productMock,
  296. $this->plugin->aroundBuild($this->subjectMock, $this->closureMock, $this->requestMock)
  297. );
  298. }
  299. }