Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddBillingAgreementToSessionObserverTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class AddBillingAgreementToSessionObserverTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $_model;
19 
23  protected $_observer;
24 
28  protected $_event;
29 
33  protected $_agreementFactory;
34 
38  protected $_checkoutSession;
39 
40  protected function setUp()
41  {
42  $this->_event = new \Magento\Framework\DataObject();
43 
44  $this->_observer = new \Magento\Framework\Event\Observer();
45  $this->_observer->setEvent($this->_event);
46 
47  $this->_agreementFactory = $this->createPartialMock(
48  \Magento\Paypal\Model\Billing\AgreementFactory::class,
49  ['create']
50  );
51  $this->_checkoutSession = $this->createMock(\Magento\Checkout\Model\Session::class);
52  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
53  $this->_model = $objectManagerHelper->getObject(
54  \Magento\Paypal\Observer\AddBillingAgreementToSessionObserver::class,
55  [
56  'agreementFactory' => $this->_agreementFactory,
57  'checkoutSession' => $this->_checkoutSession,
58  ]
59  );
60  }
61 
63  {
64  $payment = $this->createMock(\Magento\Sales\Model\Order\Payment::class);
65  $payment->expects(
66  $this->once()
67  )->method(
68  '__call'
69  )->with(
70  'getBillingAgreementData'
71  )->will(
72  $this->returnValue(null)
73  );
74  $this->_event->setPayment($payment);
75  $this->_agreementFactory->expects($this->never())->method('create');
76  $this->_checkoutSession->expects($this->once())->method('__call')->with('unsLastBillingAgreementReferenceId');
77  $this->_model->execute($this->_observer);
78  }
79 
84  public function testAddBillingAgreementToSession($isValid)
85  {
86  $agreement = $this->createMock(\Magento\Paypal\Model\Billing\Agreement::class);
87  $agreement->expects($this->once())->method('isValid')->will($this->returnValue($isValid));
88  $comment = $this->getMockForAbstractClass(
89  \Magento\Framework\Model\AbstractModel::class,
90  [],
91  '',
92  false,
93  true,
94  true,
95  ['__wakeup']
96  );
97  $order = $this->createMock(\Magento\Sales\Model\Order::class);
98  $order->expects(
99  $this->once()
100  )->method(
101  'addStatusHistoryComment'
102  )->with(
103  $isValid ? __(
104  'Created billing agreement #%1.',
105  'agreement reference id'
106  ) : __(
107  'We can\'t create a billing agreement for this order.'
108  )
109  )->will(
110  $this->returnValue($comment)
111  );
112  if ($isValid) {
113  $agreement->expects(
114  $this->any()
115  )->method(
116  '__call'
117  )->with(
118  'getReferenceId'
119  )->will(
120  $this->returnValue('agreement reference id')
121  );
122  $agreement->expects($this->once())->method('addOrderRelation')->with($order);
123  $order->expects(new MethodInvokedAtIndex(0))->method('addRelatedObject')->with($agreement);
124  $this->_checkoutSession->expects(
125  $this->once()
126  )->method(
127  '__call'
128  )->with(
129  'setLastBillingAgreementReferenceId',
130  ['agreement reference id']
131  );
132  } else {
133  $this->_checkoutSession->expects(
134  $this->once()
135  )->method(
136  '__call'
137  )->with(
138  'unsLastBillingAgreementReferenceId'
139  );
140  $agreement->expects($this->never())->method('__call');
141  }
142  $order->expects(new MethodInvokedAtIndex($isValid ? 1 : 0))->method('addRelatedObject')->with($comment);
143 
144  $payment = $this->createMock(\Magento\Sales\Model\Order\Payment::class);
145  $payment->expects(
146  $this->once()
147  )->method(
148  '__call'
149  )->with(
150  'getBillingAgreementData'
151  )->will(
152  $this->returnValue('not empty')
153  );
154  $payment->expects($this->once())->method('getOrder')->will($this->returnValue($order));
155  $agreement->expects(
156  $this->once()
157  )->method(
158  'importOrderPayment'
159  )->with(
160  $payment
161  )->will(
162  $this->returnValue($agreement)
163  );
164  $this->_event->setPayment($payment);
165  $this->_agreementFactory->expects($this->once())->method('create')->will($this->returnValue($agreement));
166  $this->_model->execute($this->_observer);
167  }
168 
173  {
174  return [[true], [false]];
175  }
176 }
$order
Definition: order.php:55
__()
Definition: __.php:13
$payment
Definition: order.php:17