RuleTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Test\Unit\Model\ResourceModel;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class RuleTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  14. */
  15. private $objectManager;
  16. /**
  17. * @var \Magento\SalesRule\Model\ResourceModel\Rule
  18. */
  19. protected $model;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $ruleResource;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $entityManager;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\AdapterInterface
  30. */
  31. protected $adapter;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $select;
  36. /**
  37. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $resourcesMock;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $transactionManagerMock;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $rule;
  48. /**
  49. * @var \PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $relationProcessorMock;
  52. protected function setUp()
  53. {
  54. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  55. $this->rule = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->ruleResource = $this->getMockBuilder(\Magento\SalesRule\Model\ResourceModel\Rule::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $context = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\Context::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $connectionName = 'test';
  65. $this->resourcesMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->relationProcessorMock =
  69. $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->transactionManagerMock =
  73. $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $context->expects($this->any())
  77. ->method('getResources')
  78. ->willReturn($this->resourcesMock);
  79. $this->entityManager = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityManager::class)
  80. ->setMethods(['load', 'save', 'delete'])
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->adapter = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->resourcesMock->expects($this->any())
  87. ->method('getConnection')
  88. ->willReturn($this->adapter);
  89. $this->resourcesMock->expects($this->any())
  90. ->method('getTableName')
  91. ->withAnyParameters()
  92. ->willReturnArgument(0);
  93. $context->expects($this->once())
  94. ->method('getObjectRelationProcessor')
  95. ->willReturn($this->relationProcessorMock);
  96. $context->expects($this->once())
  97. ->method('getTransactionManager')
  98. ->willReturn($this->transactionManagerMock);
  99. $this->select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $associatedEntitiesMap = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getData']);
  103. $associatedEntitiesMap->expects($this->once())
  104. ->method('getData')
  105. ->willReturn(
  106. [
  107. 'customer_group' => [
  108. 'associations_table' => 'salesrule_customer_group',
  109. 'rule_id_field' => 'rule_id',
  110. 'entity_id_field' => 'customer_group_id'
  111. ],
  112. 'website' => [
  113. 'associations_table' => 'salesrule_website',
  114. 'rule_id_field' => 'rule_id',
  115. 'entity_id_field' => 'website_id'
  116. ],
  117. ]
  118. );
  119. $this->model = $this->objectManager->getObject(
  120. \Magento\SalesRule\Model\ResourceModel\Rule::class,
  121. [
  122. 'context' => $context,
  123. 'connectionName' => $connectionName,
  124. 'entityManager' => $this->entityManager,
  125. 'associatedEntityMapInstance' => $associatedEntitiesMap
  126. ]
  127. );
  128. }
  129. /**
  130. * test load
  131. */
  132. public function testLoad()
  133. {
  134. $ruleId = 1;
  135. /** @var \Magento\Framework\Model\AbstractModel|\PHPUnit_Framework_MockObject_MockObject $abstractModel */
  136. $abstractModel = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
  137. ->disableOriginalConstructor()
  138. ->getMockForAbstractClass();
  139. $this->entityManager->expects($this->once())
  140. ->method('load')
  141. ->with($abstractModel, $ruleId);
  142. $result = $this->model->load($abstractModel, $ruleId);
  143. $this->assertSame($this->model, $result);
  144. }
  145. public function testSave()
  146. {
  147. $this->entityManager->expects($this->once())
  148. ->method('save')
  149. ->with($this->rule);
  150. $this->assertEquals($this->model->save($this->rule), $this->model);
  151. }
  152. public function testDelete()
  153. {
  154. $this->entityManager->expects($this->once())
  155. ->method('delete')
  156. ->with($this->rule);
  157. $this->assertEquals($this->model->delete($this->rule), $this->model);
  158. }
  159. /**
  160. * Check that can parse JSON string correctly.
  161. *
  162. * @param string $testString
  163. * @param array $expects
  164. * @dataProvider dataProviderForProductAttributes
  165. */
  166. public function testGetProductAttributes($testString, $expects)
  167. {
  168. $result = $this->model->getProductAttributes($testString);
  169. $this->assertEquals($expects, $result);
  170. }
  171. /**
  172. * @return array
  173. */
  174. public function dataProviderForProductAttributes()
  175. {
  176. return [
  177. [
  178. json_encode([
  179. 'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
  180. 'attribute' => 'some_attribute',
  181. ]),
  182. [
  183. 'some_attribute',
  184. ]
  185. ],
  186. [
  187. json_encode([
  188. [
  189. 'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
  190. 'attribute' => 'some_attribute',
  191. ],
  192. [
  193. 'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
  194. 'attribute' => 'some_attribute2',
  195. ],
  196. ]),
  197. [
  198. 'some_attribute',
  199. 'some_attribute2',
  200. ]
  201. ],
  202. [
  203. json_encode([
  204. 'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
  205. 'attribute' => 'some_attribute',
  206. ]),
  207. []
  208. ],
  209. [
  210. json_encode([]),
  211. []
  212. ],
  213. ];
  214. }
  215. }