SaveHandlerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. use Magento\SalesRule\Model\ResourceModel\SaveHandler;
  8. use Magento\SalesRule\Model\ResourceModel\Rule;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\SalesRule\Api\Data\RuleInterface;
  11. /**
  12. * Class SaveHandlerTest
  13. */
  14. class SaveHandlerTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var SaveHandler
  18. */
  19. protected $model;
  20. /**
  21. * @var Rule|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $ruleResource;
  24. /**
  25. * @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $metadataPool;
  28. /**
  29. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  30. */
  31. protected $objectManager;
  32. /**
  33. * Setup the test
  34. */
  35. protected function setUp()
  36. {
  37. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  38. $className = \Magento\SalesRule\Model\ResourceModel\Rule::class;
  39. $this->ruleResource = $this->createMock($className);
  40. $className = \Magento\Framework\EntityManager\MetadataPool::class;
  41. $this->metadataPool = $this->createMock($className);
  42. $this->model = $this->objectManager->getObject(
  43. \Magento\SalesRule\Model\ResourceModel\SaveHandler::class,
  44. [
  45. 'ruleResource' => $this->ruleResource,
  46. 'metadataPool' => $this->metadataPool,
  47. ]
  48. );
  49. }
  50. /**
  51. * test Execute
  52. */
  53. public function testExecuteNoData()
  54. {
  55. $entityData = [
  56. 'row_id' => 2,
  57. 'rule_id' => 1
  58. ];
  59. $className = \Magento\Framework\EntityManager\EntityMetadata::class;
  60. $metadata = $this->createMock($className);
  61. $metadata->expects($this->once())
  62. ->method('getLinkField')
  63. ->willReturn('rule_id');
  64. $this->metadataPool->expects($this->once())
  65. ->method('getMetadata')
  66. ->willReturn($metadata);
  67. $result = $this->model->execute(RuleInterface::class, $entityData);
  68. $this->assertEquals($entityData, $result);
  69. }
  70. public function testExecute()
  71. {
  72. $customers = [1, 2];
  73. $websites = [3, 4, 5];
  74. $entityData = [
  75. 'row_id' => 2,
  76. 'rule_id' => 1,
  77. 'website_ids' => $websites,
  78. 'customer_group_ids' => $customers
  79. ];
  80. $className = \Magento\Framework\EntityManager\EntityMetadata::class;
  81. $metadata = $this->createMock($className);
  82. $metadata->expects($this->once())
  83. ->method('getLinkField')
  84. ->willReturn('rule_id');
  85. $this->metadataPool->expects($this->once())
  86. ->method('getMetadata')
  87. ->willReturn($metadata);
  88. $this->ruleResource->expects($this->exactly(2))
  89. ->method('bindRuleToEntity');
  90. $result = $this->model->execute(RuleInterface::class, $entityData);
  91. $this->assertEquals($entityData, $result);
  92. }
  93. public function testExecuteWithString()
  94. {
  95. $customers = '1,2';
  96. $websites = '3,4,5';
  97. $entityData = [
  98. 'row_id' => 1,
  99. 'rule_id' => 1,
  100. 'website_ids' => $websites,
  101. 'customer_group_ids' => $customers
  102. ];
  103. $className = \Magento\Framework\EntityManager\EntityMetadata::class;
  104. $metadata = $this->createMock($className);
  105. $metadata->expects($this->once())
  106. ->method('getLinkField')
  107. ->willReturn('rule_id');
  108. $this->metadataPool->expects($this->once())
  109. ->method('getMetadata')
  110. ->willReturn($metadata);
  111. $this->ruleResource->expects($this->any())
  112. ->method('bindRuleToEntity')
  113. ->withConsecutive([1, [3, 4, 5]], [1, [1, 2]]);
  114. $result = $this->model->execute(RuleInterface::class, $entityData);
  115. $this->assertEquals($entityData, $result);
  116. }
  117. }