PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-advanced-pricing-import-export/Test/Unit/Model/Import/AdvancedPricing/Validator/TierPriceTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 388 lines | 309 code | 31 blank | 48 comment | 0 complexity | 4a1e01bfdcebb371e8aa28473f378c1d MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdvancedPricingImportExport\Test\Unit\Model\Import\AdvancedPricing\Validator;
  7. use \Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing as AdvancedPricing;
  8. /**
  9. * @SuppressWarnings(PHPMD)
  10. */
  11. class TierPriceTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var \Magento\Customer\Api\GroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $groupRepository;
  17. /**
  18. * @var \Magento\Framework\Api\SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $searchCriteriaBuilder;
  21. /**
  22. * @var \Magento\CatalogImportExport\Model\Import\Product\StoreResolver|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $storeResolver;
  25. /**
  26. * @var AdvancedPricing\Validator\TierPrice|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $tierPrice;
  29. public function setUp()
  30. {
  31. $this->groupRepository = $this->getMockBuilder('\Magento\Customer\Api\GroupRepositoryInterface')
  32. ->disableOriginalConstructor()
  33. ->setMethods(['getList'])
  34. ->getMockForAbstractClass();
  35. $this->searchCriteriaBuilder = $this->getMock(
  36. '\Magento\Framework\Api\SearchCriteriaBuilder',
  37. [],
  38. [],
  39. '',
  40. false
  41. );
  42. $this->storeResolver = $this->getMock(
  43. '\Magento\CatalogImportExport\Model\Import\Product\StoreResolver',
  44. [],
  45. [],
  46. '',
  47. false
  48. );
  49. $this->tierPrice = $this->getMock(
  50. '\Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing\Validator\TierPrice',
  51. ['isValidValueAndLength', 'hasEmptyColumns', '_addMessages'],
  52. [
  53. $this->groupRepository,
  54. $this->searchCriteriaBuilder,
  55. $this->storeResolver,
  56. ],
  57. ''
  58. );
  59. }
  60. public function testInitInternalCalls()
  61. {
  62. $searchCriteria = $this->getMock('Magento\Framework\Api\SearchCriteria', [], [], '', false);
  63. $this->searchCriteriaBuilder->expects($this->any())->method('create')->willReturn($searchCriteria);
  64. $groupSearchResult = $this->getMockForAbstractClass(
  65. '\Magento\Customer\Api\Data\GroupSearchResultsInterface',
  66. [],
  67. '',
  68. false
  69. );
  70. $this->groupRepository
  71. ->expects($this->any())
  72. ->method('getList')
  73. ->with($searchCriteria)
  74. ->willReturn($groupSearchResult);
  75. $groupTest = $this->getMockBuilder('\Magento\Customer\Api\Data\GroupInterface')
  76. ->disableOriginalConstructor()
  77. ->setMethods(['getCode', 'getId'])
  78. ->getMockForAbstractClass();
  79. $groupTest->expects($this->once())->method('getCode');
  80. $groupTest->expects($this->any())->method('getId');
  81. $groups = [$groupTest];
  82. $groupSearchResult->expects($this->any())->method('getItems')->willReturn($groups);
  83. $this->tierPrice->init(null);
  84. }
  85. public function testInitAddToCustomerGroups()
  86. {
  87. $searchCriteria = $this->getMock('Magento\Framework\Api\SearchCriteria', [], [], '', false);
  88. $this->searchCriteriaBuilder->expects($this->any())->method('create')->willReturn($searchCriteria);
  89. $groupSearchResult = $this->getMockForAbstractClass(
  90. '\Magento\Customer\Api\Data\GroupSearchResultsInterface',
  91. [],
  92. '',
  93. false
  94. );
  95. $this->groupRepository
  96. ->expects($this->any())
  97. ->method('getList')
  98. ->with($searchCriteria)
  99. ->willReturn($groupSearchResult);
  100. $groupTest = $this->getMockBuilder('\Magento\Customer\Api\Data\GroupInterface')
  101. ->disableOriginalConstructor()
  102. ->setMethods(['getCode', 'getId'])
  103. ->getMockForAbstractClass();
  104. $expectedCode = 'code';
  105. $expectedId = 'id';
  106. $expectedCustomerGroups = [
  107. $expectedCode => $expectedId,
  108. ];
  109. $groupTest->expects($this->once())->method('getCode')->willReturn($expectedCode);
  110. $groupTest->expects($this->any())->method('getId')->willReturn($expectedId);
  111. $groups = [$groupTest];
  112. $groupSearchResult->expects($this->any())->method('getItems')->willReturn($groups);
  113. $this->tierPrice->init(null);
  114. $this->assertEquals($expectedCustomerGroups, $this->getPropertyValue($this->tierPrice, 'customerGroups'));
  115. }
  116. public function testIsValidResultTrue()
  117. {
  118. $this->tierPrice->expects($this->once())->method('isValidValueAndLength')->willReturn(false);
  119. $this->setPropertyValue($this->tierPrice, 'customerGroups', true);
  120. $result = $this->tierPrice->isValid([]);
  121. $this->assertTrue($result);
  122. }
  123. /**
  124. * @dataProvider isValidAddMessagesCallDataProvider
  125. *
  126. * @param array $value
  127. * @param bool $hasEmptyColumns
  128. * @param array $customerGroups
  129. * @param array $expectedMessages
  130. */
  131. public function testIsValidAddMessagesCall($value, $hasEmptyColumns, $customerGroups, $expectedMessages)
  132. {
  133. $priceContextMock = $this->getMock(
  134. '\Magento\CatalogImportExport\Model\Import\Product',
  135. [],
  136. [
  137. '\Magento\Framework\Json\Helper\Data',
  138. '\Magento\ImportExport\Helper\Data',
  139. '\Magento\ImportExport\Model\ResourceModel\Import\Data',
  140. '\Magento\Eav\Model\Config',
  141. '\Magento\Framework\App\ResourceConnection',
  142. '\Magento\ImportExport\Model\ResourceModel\Helper',
  143. '\Magento\Framework\Stdlib\StringUtils',
  144. 'ProcessingErrorAggregatorInterface',
  145. ],
  146. '',
  147. false
  148. );
  149. $this->tierPrice->expects($this->once())->method('isValidValueAndLength')->willReturn(true);
  150. $this->tierPrice->expects($this->any())->method('hasEmptyColumns')->willReturn($hasEmptyColumns);
  151. $this->setPropertyValue($this->tierPrice, 'customerGroups', $customerGroups);
  152. $searchCriteria = $this->getMock('Magento\Framework\Api\SearchCriteria', [], [], '', false);
  153. $this->searchCriteriaBuilder->expects($this->any())->method('create')->willReturn($searchCriteria);
  154. $groupSearchResult = $this->getMockForAbstractClass(
  155. '\Magento\Customer\Api\Data\GroupSearchResultsInterface',
  156. [],
  157. '',
  158. false
  159. );
  160. $this->groupRepository
  161. ->expects($this->any())
  162. ->method('getList')
  163. ->with($searchCriteria)
  164. ->willReturn($groupSearchResult);
  165. $groupTest = $this->getMockBuilder('\Magento\Customer\Api\Data\GroupInterface')
  166. ->disableOriginalConstructor()
  167. ->setMethods(['getCode', 'getId'])
  168. ->getMockForAbstractClass();
  169. $groupTest->expects($this->once())->method('getCode');
  170. $groupTest->expects($this->any())->method('getId');
  171. $groups = [$groupTest];
  172. $groupSearchResult->expects($this->any())->method('getItems')->willReturn($groups);
  173. $this->tierPrice->init($priceContextMock);
  174. $this->tierPrice->isValid($value);
  175. }
  176. public function isValidResultFalseDataProvider()
  177. {
  178. return [
  179. // First if condition cases.
  180. [
  181. '$value' => [
  182. AdvancedPricing::COL_TIER_PRICE_WEBSITE => null,
  183. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'value',
  184. AdvancedPricing::COL_TIER_PRICE_QTY => 1000,
  185. AdvancedPricing::COL_TIER_PRICE => 1000,
  186. ],
  187. '$hasEmptyColumns' => null,
  188. '$customerGroups' => [
  189. 'value' => 'value'
  190. ],
  191. ],
  192. [
  193. '$value' => [
  194. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  195. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => null,
  196. AdvancedPricing::COL_TIER_PRICE_QTY => 1000,
  197. AdvancedPricing::COL_TIER_PRICE => 1000,
  198. ],
  199. '$hasEmptyColumns' => null,
  200. '$customerGroups' => [
  201. 'value' => 'value'
  202. ],
  203. ],
  204. [
  205. '$value' => [
  206. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  207. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'value',
  208. AdvancedPricing::COL_TIER_PRICE_QTY => null,
  209. AdvancedPricing::COL_TIER_PRICE => 1000,
  210. ],
  211. '$hasEmptyColumns' => null,
  212. '$customerGroups' => [
  213. 'value' => 'value'
  214. ],
  215. ],
  216. [
  217. '$value' => [
  218. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  219. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'value',
  220. AdvancedPricing::COL_TIER_PRICE_QTY => 1000,
  221. AdvancedPricing::COL_TIER_PRICE => null,
  222. ],
  223. '$hasEmptyColumns' => null,
  224. '$customerGroups' => [
  225. 'value' => 'value'
  226. ],
  227. ],
  228. [
  229. '$value' => [
  230. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  231. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'value',
  232. AdvancedPricing::COL_TIER_PRICE_QTY => 1000,
  233. AdvancedPricing::COL_TIER_PRICE => 1000,
  234. ],
  235. '$hasEmptyColumns' => true,
  236. '$customerGroups' => [
  237. 'value' => 'value'
  238. ],
  239. ],
  240. // Second if condition cases.
  241. [
  242. '$value' => [
  243. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  244. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'not ALL GROUPS',
  245. AdvancedPricing::COL_TIER_PRICE_QTY => 1000,
  246. AdvancedPricing::COL_TIER_PRICE => 1000,
  247. ],
  248. '$hasEmptyColumns' => null,
  249. '$customerGroups' => [
  250. 'value' => 'value'
  251. ],
  252. ],
  253. // Third if condition cases.
  254. [
  255. '$value' => [
  256. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  257. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'value',
  258. AdvancedPricing::COL_TIER_PRICE_QTY => -1000,
  259. AdvancedPricing::COL_TIER_PRICE => 1000,
  260. ],
  261. '$hasEmptyColumns' => null,
  262. '$customerGroups' => [
  263. 'value' => 'value'
  264. ],
  265. ],
  266. [
  267. '$value' => [
  268. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  269. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'value',
  270. AdvancedPricing::COL_TIER_PRICE_QTY => 1000,
  271. AdvancedPricing::COL_TIER_PRICE => -1000,
  272. ],
  273. '$hasEmptyColumns' => null,
  274. '$customerGroups' => [
  275. 'value' => 'value'
  276. ],
  277. ],
  278. ];
  279. }
  280. public function isValidAddMessagesCallDataProvider()
  281. {
  282. return [
  283. // First if condition cases.
  284. [
  285. '$value' => [
  286. AdvancedPricing::COL_TIER_PRICE_WEBSITE => null,
  287. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'value',
  288. AdvancedPricing::COL_TIER_PRICE_QTY => 1000,
  289. AdvancedPricing::COL_TIER_PRICE => 1000,
  290. ],
  291. '$hasEmptyColumns' => null,
  292. '$customerGroups' => [
  293. 'value' => 'value'
  294. ],
  295. '$expectedMessages' => [AdvancedPricing\Validator::ERROR_TIER_DATA_INCOMPLETE],
  296. ],
  297. // Second if condition cases.
  298. [
  299. '$value' => [
  300. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  301. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'not ALL GROUPS',
  302. AdvancedPricing::COL_TIER_PRICE_QTY => 1000,
  303. AdvancedPricing::COL_TIER_PRICE => 1000,
  304. ],
  305. '$hasEmptyColumns' => null,
  306. '$customerGroups' => [
  307. 'value' => 'value'
  308. ],
  309. '$expectedMessages' => [AdvancedPricing\Validator::ERROR_INVALID_TIER_PRICE_GROUP],
  310. ],
  311. // Third if condition cases.
  312. [
  313. '$value' => [
  314. AdvancedPricing::COL_TIER_PRICE_WEBSITE => 'value',
  315. AdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'value',
  316. AdvancedPricing::COL_TIER_PRICE_QTY => -1000,
  317. AdvancedPricing::COL_TIER_PRICE => 1000,
  318. ],
  319. '$hasEmptyColumns' => null,
  320. '$customerGroups' => [
  321. 'value' => 'value'
  322. ],
  323. '$expectedMessages' => [AdvancedPricing\Validator::ERROR_INVALID_TIER_PRICE_QTY],
  324. ],
  325. ];
  326. }
  327. /**
  328. * Get any object property value.
  329. *
  330. * @param object $object
  331. * @param string $property
  332. * @return mixed
  333. */
  334. protected function getPropertyValue($object, $property)
  335. {
  336. $reflection = new \ReflectionClass(get_class($object));
  337. $reflectionProperty = $reflection->getProperty($property);
  338. $reflectionProperty->setAccessible(true);
  339. return $reflectionProperty->getValue($object);
  340. }
  341. /**
  342. * Set object property value.
  343. *
  344. * @param object $object
  345. * @param string $property
  346. * @param mixed $value
  347. * @return object
  348. */
  349. protected function setPropertyValue(&$object, $property, $value)
  350. {
  351. $reflection = new \ReflectionClass(get_class($object));
  352. $reflectionProperty = $reflection->getProperty($property);
  353. $reflectionProperty->setAccessible(true);
  354. $reflectionProperty->setValue($object, $value);
  355. return $object;
  356. }
  357. }