123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\SalesRule\Test\Unit\Model\ResourceModel;
- use Magento\SalesRule\Model\ResourceModel\SaveHandler;
- use Magento\SalesRule\Model\ResourceModel\Rule;
- use Magento\Framework\EntityManager\MetadataPool;
- use Magento\SalesRule\Api\Data\RuleInterface;
- /**
- * Class SaveHandlerTest
- */
- class SaveHandlerTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var SaveHandler
- */
- protected $model;
- /**
- * @var Rule|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $ruleResource;
- /**
- * @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $metadataPool;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /**
- * Setup the test
- */
- protected function setUp()
- {
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $className = \Magento\SalesRule\Model\ResourceModel\Rule::class;
- $this->ruleResource = $this->createMock($className);
- $className = \Magento\Framework\EntityManager\MetadataPool::class;
- $this->metadataPool = $this->createMock($className);
- $this->model = $this->objectManager->getObject(
- \Magento\SalesRule\Model\ResourceModel\SaveHandler::class,
- [
- 'ruleResource' => $this->ruleResource,
- 'metadataPool' => $this->metadataPool,
- ]
- );
- }
- /**
- * test Execute
- */
- public function testExecuteNoData()
- {
- $entityData = [
- 'row_id' => 2,
- 'rule_id' => 1
- ];
- $className = \Magento\Framework\EntityManager\EntityMetadata::class;
- $metadata = $this->createMock($className);
- $metadata->expects($this->once())
- ->method('getLinkField')
- ->willReturn('rule_id');
- $this->metadataPool->expects($this->once())
- ->method('getMetadata')
- ->willReturn($metadata);
- $result = $this->model->execute(RuleInterface::class, $entityData);
- $this->assertEquals($entityData, $result);
- }
- public function testExecute()
- {
- $customers = [1, 2];
- $websites = [3, 4, 5];
- $entityData = [
- 'row_id' => 2,
- 'rule_id' => 1,
- 'website_ids' => $websites,
- 'customer_group_ids' => $customers
- ];
- $className = \Magento\Framework\EntityManager\EntityMetadata::class;
- $metadata = $this->createMock($className);
- $metadata->expects($this->once())
- ->method('getLinkField')
- ->willReturn('rule_id');
- $this->metadataPool->expects($this->once())
- ->method('getMetadata')
- ->willReturn($metadata);
- $this->ruleResource->expects($this->exactly(2))
- ->method('bindRuleToEntity');
- $result = $this->model->execute(RuleInterface::class, $entityData);
- $this->assertEquals($entityData, $result);
- }
- public function testExecuteWithString()
- {
- $customers = '1,2';
- $websites = '3,4,5';
- $entityData = [
- 'row_id' => 1,
- 'rule_id' => 1,
- 'website_ids' => $websites,
- 'customer_group_ids' => $customers
- ];
- $className = \Magento\Framework\EntityManager\EntityMetadata::class;
- $metadata = $this->createMock($className);
- $metadata->expects($this->once())
- ->method('getLinkField')
- ->willReturn('rule_id');
- $this->metadataPool->expects($this->once())
- ->method('getMetadata')
- ->willReturn($metadata);
- $this->ruleResource->expects($this->any())
- ->method('bindRuleToEntity')
- ->withConsecutive([1, [3, 4, 5]], [1, [1, 2]]);
- $result = $this->model->execute(RuleInterface::class, $entityData);
- $this->assertEquals($entityData, $result);
- }
- }
|