Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SalesOrderAfterPlaceObserverTest.php
Go to the documentation of this file.
1 <?php
7 
8 class SalesOrderAfterPlaceObserverTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
18  protected $couponMock;
19 
23  protected $ruleFactory;
24 
29 
33  protected $couponUsage;
34 
35  protected function setUp()
36  {
37  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38  $this->initMocks();
39 
40  $this->model = $helper->getObject(
41  \Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver::class,
42  [
43  'ruleFactory' => $this->ruleFactory,
44  'ruleCustomerFactory' => $this->ruleCustomerFactory,
45  'coupon' => $this->couponMock,
46  'couponUsage' => $this->couponUsage,
47  ]
48  );
49  }
50 
51  protected function initMocks()
52  {
53  $this->couponMock = $this->createPartialMock(\Magento\SalesRule\Model\Coupon::class, [
54  '__wakeup',
55  'save',
56  'load',
57  'getId',
58  'setTimesUsed',
59  'getTimesUsed',
60  'getRuleId',
61  'loadByCode',
62  'updateCustomerCouponTimesUsed'
63  ]);
64  $this->ruleFactory = $this->createPartialMock(\Magento\SalesRule\Model\RuleFactory::class, ['create']);
65  $this->ruleCustomerFactory = $this->createPartialMock(
66  \Magento\SalesRule\Model\Rule\CustomerFactory::class,
67  ['create']
68  );
69  $this->couponUsage = $this->createMock(\Magento\SalesRule\Model\ResourceModel\Coupon\Usage::class);
70  }
71 
76  protected function initOrderFromEvent($observer)
77  {
78  $event = $this->createPartialMock(\Magento\Framework\Event::class, ['getOrder']);
79  $order = $this->createPartialMock(
80  \Magento\Sales\Model\Order::class,
81  ['getAppliedRuleIds', 'getCustomerId', 'getDiscountAmount', 'getCouponCode', '__wakeup']
82  );
83 
84  $observer->expects($this->any())
85  ->method('getEvent')
86  ->will($this->returnValue($event));
87  $event->expects($this->any())
88  ->method('getOrder')
89  ->will($this->returnValue($order));
90 
91  return $order;
92  }
93 
95  {
96  $observer = $this->createMock(\Magento\Framework\Event\Observer::class);
98 
99  $this->assertEquals($this->model, $this->model->execute($observer));
100  }
101 
103  {
104  $observer = $this->createMock(\Magento\Framework\Event\Observer::class);
106  $ruleIds = null;
107  $order->expects($this->once())
108  ->method('getAppliedRuleIds')
109  ->will($this->returnValue($ruleIds));
110 
111  $this->ruleFactory->expects($this->never())
112  ->method('create');
113  $this->assertEquals($this->model, $this->model->execute($observer));
114  }
115 
120  public function testSalesOrderAfterPlace($ruleCustomerId)
121  {
122  $observer = $this->createMock(\Magento\Framework\Event\Observer::class);
123  $rule = $this->createMock(\Magento\SalesRule\Model\Rule::class);
124  $ruleCustomer = $this->createPartialMock(\Magento\SalesRule\Model\Rule\Customer::class, [
125  'setCustomerId',
126  'loadByCustomerRule',
127  'getId',
128  'setTimesUsed',
129  'setRuleId',
130  'save',
131  '__wakeup'
132  ]);
134  $ruleId = 1;
135  $couponId = 1;
136  $customerId = 1;
137 
138  $order->expects($this->exactly(2))
139  ->method('getAppliedRuleIds')
140  ->will($this->returnValue($ruleId));
141  $order->expects($this->once())
142  ->method('getCustomerId')
143  ->will($this->returnValue($customerId));
144  $this->ruleFactory->expects($this->once())
145  ->method('create')
146  ->will($this->returnValue($rule));
147  $rule->expects($this->once())
148  ->method('getId')
149  ->will($this->returnValue($ruleId));
150  $this->ruleCustomerFactory->expects($this->once())
151  ->method('create')
152  ->will($this->returnValue($ruleCustomer));
153  $ruleCustomer->expects($this->once())
154  ->method('getId')
155  ->will($this->returnValue($ruleCustomerId));
156  $ruleCustomer->expects($this->any())
157  ->method('setCustomerId')
158  ->will($this->returnSelf());
159  $ruleCustomer->expects($this->any())
160  ->method('setRuleId')
161  ->will($this->returnSelf());
162  $this->couponMock->expects($this->any())
163  ->method('getId')
164  ->will($this->returnValue($couponId));
165 
166  $this->couponUsage->expects($this->once())
167  ->method('updateCustomerCouponTimesUsed')
168  ->with($customerId, $couponId);
169 
170  $this->assertEquals($this->model, $this->model->execute($observer));
171  }
172 
177  {
178  return [
179  'With customer rule id' => [1],
180  'Without customer rule id' => [null]
181  ];
182  }
183 }
$helper
Definition: iframe.phtml:13
$order
Definition: order.php:55