/**
  * @param bool $isValid
  * @dataProvider addBillingAgreementToSessionDataProvider
  */
 public function testAddBillingAgreementToSession($isValid)
 {
     $agreement = $this->getMock('Magento\\Paypal\\Model\\Billing\\Agreement', [], [], '', false);
     $agreement->expects($this->once())->method('isValid')->will($this->returnValue($isValid));
     $comment = $this->getMockForAbstractClass('Magento\\Framework\\Model\\AbstractModel', [], '', false, true, true, ['__wakeup']);
     $order = $this->getMock('Magento\\Sales\\Model\\Order', [], [], '', false);
     $order->expects($this->once())->method('addStatusHistoryComment')->with($isValid ? __('Created billing agreement #%1.', 'agreement reference id') : __('We can\'t create a billing agreement for this order.'))->will($this->returnValue($comment));
     if ($isValid) {
         $agreement->expects($this->any())->method('__call')->with('getReferenceId')->will($this->returnValue('agreement reference id'));
         $agreement->expects($this->once())->method('addOrderRelation')->with($order);
         $order->expects(new MethodInvokedAtIndex(0))->method('addRelatedObject')->with($agreement);
         $this->_checkoutSession->expects($this->once())->method('__call')->with('setLastBillingAgreementReferenceId', ['agreement reference id']);
     } else {
         $this->_checkoutSession->expects($this->once())->method('__call')->with('unsLastBillingAgreementReferenceId');
         $agreement->expects($this->never())->method('__call');
     }
     $order->expects(new MethodInvokedAtIndex($isValid ? 1 : 0))->method('addRelatedObject')->with($comment);
     $payment = $this->getMock('Magento\\Sales\\Model\\Order\\Payment', [], [], '', false);
     $payment->expects($this->once())->method('__call')->with('getBillingAgreementData')->will($this->returnValue('not empty'));
     $payment->expects($this->once())->method('getOrder')->will($this->returnValue($order));
     $agreement->expects($this->once())->method('importOrderPayment')->with($payment)->will($this->returnValue($agreement));
     $this->_event->setPayment($payment);
     $this->_agreementFactory->expects($this->once())->method('create')->will($this->returnValue($agreement));
     $this->_model->execute($this->_observer);
 }