Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddSalesRuleNameToOrderObserverTest.php
Go to the documentation of this file.
1 <?php
7 
8 class AddSalesRuleNameToOrderObserverTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
18  protected $couponMock;
19 
23  protected $ruleFactory;
24 
25  protected function setUp()
26  {
27  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
28  $this->initMocks();
29 
30  $this->model = $helper->getObject(
31  \Magento\SalesRule\Observer\AddSalesRuleNameToOrderObserver::class,
32  [
33  'ruleFactory' => $this->ruleFactory,
34  'coupon' => $this->couponMock,
35  ]
36  );
37  }
38 
39  protected function initMocks()
40  {
41  $this->couponMock = $this->createPartialMock(\Magento\SalesRule\Model\Coupon::class, [
42  '__wakeup',
43  'save',
44  'load',
45  'getId',
46  'setTimesUsed',
47  'getTimesUsed',
48  'getRuleId',
49  'loadByCode',
50  'updateCustomerCouponTimesUsed'
51  ]);
52  $this->ruleFactory = $this->createPartialMock(\Magento\SalesRule\Model\RuleFactory::class, ['create']);
53  }
54 
56  {
57  $observer = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getOrder']);
58  $order = $this->createPartialMock(
59  \Magento\Sales\Model\Order::class,
60  ['setCouponRuleName', 'getCouponCode', '__wakeup']
61  );
62 
63  $observer->expects($this->any())
64  ->method('getOrder')
65  ->will($this->returnValue($order));
66 
67  $this->couponMock->expects($this->never())
68  ->method('loadByCode');
69 
70  $this->assertEquals($this->model, $this->model->execute($observer));
71  }
72 
74  {
75  $observer = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getOrder']);
76  $order = $this->createPartialMock(
77  \Magento\Sales\Model\Order::class,
78  ['setCouponRuleName', 'getCouponCode', '__wakeup']
79  );
80  $couponCode = 'coupon code';
81 
82  $observer->expects($this->any())
83  ->method('getOrder')
84  ->will($this->returnValue($order));
85 
86  $order->expects($this->once())
87  ->method('getCouponCode')
88  ->will($this->returnValue($couponCode));
89  $this->ruleFactory->expects($this->never())
90  ->method('create');
91 
92  $this->assertEquals($this->model, $this->model->execute($observer));
93  }
94 
95  public function testAddSalesRuleNameToOrder()
96  {
97  $observer = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getOrder']);
98  $rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['load', 'getName', '__wakeup']);
99  $order = $this->createPartialMock(
100  \Magento\Sales\Model\Order::class,
101  ['setCouponRuleName', 'getCouponCode', '__wakeup']
102  );
103  $couponCode = 'coupon code';
104  $ruleId = 1;
105 
106  $observer->expects($this->any())
107  ->method('getOrder')
108  ->will($this->returnValue($order));
109 
110  $order->expects($this->once())
111  ->method('getCouponCode')
112  ->will($this->returnValue($couponCode));
113  $this->couponMock->expects($this->once())
114  ->method('getRuleId')
115  ->will($this->returnValue($ruleId));
116  $this->ruleFactory->expects($this->once())
117  ->method('create')
118  ->will($this->returnValue($rule));
119  $rule->expects($this->once())
120  ->method('load')
121  ->with($ruleId)
122  ->will($this->returnSelf());
123  $order->expects($this->once())
124  ->method('setCouponRuleName');
125 
126  $this->assertEquals($this->model, $this->model->execute($observer));
127  }
128 }
$helper
Definition: iframe.phtml:13
$order
Definition: order.php:55