123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\SalesRule\Test\Unit\Model\Converter;
- class ToModelTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\SalesRule\Model\RuleFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $ruleFactory;
- /**
- * @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $dataObjectProcessor;
- /**
- * @var \Magento\SalesRule\Model\Converter\ToModel
- */
- protected $model;
- protected function setUp()
- {
- $this->ruleFactory = $this->getMockBuilder(\Magento\SalesRule\Model\RuleFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->dataObjectProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->model = $helper->getObject(
- \Magento\SalesRule\Model\Converter\ToModel::class,
- [
- 'ruleFactory' => $this->ruleFactory,
- 'dataObjectProcessor' => $this->dataObjectProcessor,
- ]
- );
- }
- public function testDataModelToArray()
- {
- $array = [
- 'type' => 'conditionType',
- 'value' => 'value',
- 'attribute' => 'getAttributeName',
- 'operator' => 'getOperator',
- 'aggregator' => 'getAggregatorType',
- 'conditions' => [
- [
- 'type' => null,
- 'value' => null,
- 'attribute' => null,
- 'operator' => null,
- ],
- [
- 'type' => null,
- 'value' => null,
- 'attribute' => null,
- 'operator' => null,
- ],
- ],
- ];
- /**
- * @var \Magento\SalesRule\Model\Data\Condition $dataCondition
- */
- $dataCondition = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
- ->disableOriginalConstructor()
- ->setMethods(['create', 'load', 'getConditionType', 'getValue', 'getAttributeName', 'getOperator',
- 'getAggregatorType', 'getConditions'])
- ->getMock();
- $dataCondition
- ->expects($this->atLeastOnce())
- ->method('getConditionType')
- ->willReturn('conditionType');
- $dataCondition
- ->expects($this->atLeastOnce())
- ->method('getValue')
- ->willReturn('value');
- $dataCondition
- ->expects($this->atLeastOnce())
- ->method('getAttributeName')
- ->willReturn('getAttributeName');
- $dataCondition
- ->expects($this->atLeastOnce())
- ->method('getOperator')
- ->willReturn('getOperator');
- $dataCondition
- ->expects($this->atLeastOnce())
- ->method('getAggregatorType')
- ->willReturn('getAggregatorType');
- $dataCondition1 = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
- ->disableOriginalConstructor()
- ->setMethods(['create', 'load', 'getConditionType', 'getValue', 'getAttributeName', 'getOperator',
- 'getAggregatorType', 'getConditions'])
- ->getMock();
- $dataCondition2 = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
- ->disableOriginalConstructor()
- ->setMethods(['create', 'load', 'getConditionType', 'getValue', 'getAttributeName', 'getOperator',
- 'getAggregatorType', 'getConditions'])
- ->getMock();
- $dataCondition
- ->expects($this->atLeastOnce())
- ->method('getConditions')
- ->willReturn([$dataCondition1, $dataCondition2]);
- $result = $this->model->dataModelToArray($dataCondition);
- $this->assertEquals($array, $result);
- }
- public function testToModel()
- {
- /**
- * @var \Magento\SalesRule\Model\Data\Rule $dataModel
- */
- $dataModel = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Rule::class)
- ->disableOriginalConstructor()
- ->setMethods(['create', 'load', 'getData', 'getRuleId', 'getCondition', 'getActionCondition',
- 'getStoreLabels'])
- ->getMock();
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getRuleId')
- ->willReturn(1);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getCondition')
- ->willReturn(false);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getActionCondition')
- ->willReturn(false);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getStoreLabels')
- ->willReturn([]);
- $ruleModel = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class)
- ->disableOriginalConstructor()
- ->setMethods(['create', 'load', 'getId', 'getData'])
- ->getMock();
- $ruleModel
- ->expects($this->atLeastOnce())
- ->method('load')
- ->willReturn($ruleModel);
- $ruleModel
- ->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn(1);
- $ruleModel
- ->expects($this->atLeastOnce())
- ->method('getData')
- ->willReturn(['data_1'=>1]);
- $this->dataObjectProcessor
- ->expects($this->any())
- ->method('buildOutputDataArray')
- ->willReturn(['data_2'=>2]);
- $this->ruleFactory
- ->expects($this->any())
- ->method('create')
- ->willReturn($ruleModel);
- $result = $this->model->toModel($dataModel);
- $this->assertEquals($ruleModel, $result);
- }
- /**
- * @dataProvider expectedDatesProvider
- */
- public function testFormattingDate($data)
- {
- /**
- * @var \Magento\SalesRule\Model\Data\Rule|\PHPUnit_Framework_MockObject_MockObject $dataModel
- */
- $dataModel = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Rule::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'create',
- 'load',
- 'getData',
- 'getRuleId',
- 'getCondition',
- 'getActionCondition',
- 'getStoreLabels',
- 'getFromDate',
- 'setFromDate',
- 'getToDate',
- 'setToDate',
- ]
- )
- ->getMock();
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getRuleId')
- ->willReturn(null);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getCondition')
- ->willReturn(false);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getActionCondition')
- ->willReturn(false);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getStoreLabels')
- ->willReturn([]);
- $ruleModel = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class)
- ->disableOriginalConstructor()
- ->setMethods(['create', 'load', 'getId', 'getData'])
- ->getMock();
- $ruleModel
- ->expects($this->atLeastOnce())
- ->method('getData')
- ->willReturn(['data_1'=>1]);
- $this->dataObjectProcessor
- ->expects($this->any())
- ->method('buildOutputDataArray')
- ->willReturn(['data_2'=>2]);
- $this->ruleFactory
- ->expects($this->any())
- ->method('create')
- ->willReturn($ruleModel);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getFromDate')
- ->willReturn($data['from_date']);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('getToDate')
- ->willReturn($data['to_date']);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('setFromDate')
- ->with($data['expected_from_date']);
- $dataModel
- ->expects($this->atLeastOnce())
- ->method('setToDate')
- ->with($data['expected_to_date']);
- $this->model->toModel($dataModel);
- }
- /**
- * @return array
- */
- public function expectedDatesProvider()
- {
- return [
- 'mm/dd/yyyy to yyyy-mm-dd' => [
- [
- 'from_date' => '03/24/2016',
- 'to_date' => '03/25/2016',
- 'expected_from_date' => '2016-03-24T00:00:00-0700',
- 'expected_to_date' => '2016-03-25T00:00:00-0700',
- ]
- ],
- 'yyyy-mm-dd to yyyy-mm-dd' => [
- [
- 'from_date' => '2016-03-24',
- 'to_date' => '2016-03-25',
- 'expected_from_date' => '2016-03-24T00:00:00-0700',
- 'expected_to_date' => '2016-03-25T00:00:00-0700',
- ]
- ],
- 'yymmdd to yyyy-mm-dd' => [
- [
- 'from_date' => '20160324',
- 'to_date' => '20160325',
- 'expected_from_date' => '2016-03-24T00:00:00-0700',
- 'expected_to_date' => '2016-03-25T00:00:00-0700',
- ]
- ],
- ];
- }
- }
|