ReadHandlerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\ReadHandler;
  8. use Magento\SalesRule\Model\ResourceModel\Rule;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\SalesRule\Api\Data\RuleInterface;
  11. /**
  12. * Class ReadHandlerTest
  13. */
  14. class ReadHandlerTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var ReadHandler
  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. ReadHandler::class,
  44. [
  45. 'ruleResource' => $this->ruleResource,
  46. 'metadataPool' => $this->metadataPool,
  47. ]
  48. );
  49. }
  50. /**
  51. * test Execute
  52. */
  53. public function testExecute()
  54. {
  55. $entityData = [
  56. 'row_id' => 2,
  57. 'rule_id' => 1
  58. ];
  59. $customers = [1, 2];
  60. $websites = [3, 4, 5];
  61. $className = \Magento\Framework\EntityManager\EntityMetadata::class;
  62. $metadata = $this->createMock($className);
  63. $metadata->expects($this->once())
  64. ->method('getLinkField')
  65. ->willReturn('rule_id');
  66. $this->metadataPool->expects($this->once())
  67. ->method('getMetadata')
  68. ->willReturn($metadata);
  69. $this->ruleResource->expects($this->once())
  70. ->method('getCustomerGroupIds')
  71. ->willReturn($customers);
  72. $this->ruleResource->expects($this->once())
  73. ->method('getWebsiteIds')
  74. ->willReturn($websites);
  75. $result = $this->model->execute(RuleInterface::class, $entityData);
  76. $expected = [
  77. 'row_id' => 2,
  78. 'rule_id' => 1,
  79. 'customer_group_ids' => [1, 2],
  80. 'website_ids' => [3, 4, 5],
  81. ];
  82. $this->assertEquals($expected, $result);
  83. }
  84. }