/**
  * Executes the controller action and asserts non exception logic
  */
 public function testExecuteLocalizedException()
 {
     $phrase = new \Magento\Framework\Phrase('some error');
     $objectManager = new ObjectManager($this);
     $this->vault->expects($this->once())->method('processNonce')->willThrowException(new LocalizedException($phrase));
     $this->resultJson->expects($this->once())->method('setData')->with(['success' => false, 'error_message' => 'some error']);
     $this->resultFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
     $this->messageManager->expects($this->once())->method('addError')->with($phrase);
     $notification = $objectManager->getObject('Magento\\Braintree\\Controller\\Creditcard\\AjaxSave', ['request' => $this->request, 'resultFactory' => $this->resultFactory, 'vault' => $this->vault, 'messageManager' => $this->messageManager]);
     $this->assertSame($this->resultJson, $notification->execute());
 }
Exemple #2
0
 /**
  * Executes the controller action and asserts non exception logic
  */
 public function testExecuteInternalLocalizedException()
 {
     $phrase = new \Magento\Framework\Phrase('Something went wrong while processing.');
     $objectManager = new ObjectManager($this);
     $this->vault->expects($this->once())->method('generatePaymentMethodToken')->willThrowException(new LocalizedException($phrase));
     $this->resultJson->expects($this->once())->method('setData')->with(['success' => false, 'error_message' => 'Something went wrong while processing.']);
     $this->resultFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
     $this->messageManager->expects($this->once())->method('addError')->with($phrase);
     $this->request->expects($this->any())->method('getParam')->willReturn(true);
     /** @var \Magento\Braintree\Controller\Creditcard\Generate $controller */
     $controller = $objectManager->getObject('Magento\\Braintree\\Controller\\Creditcard\\Generate', ['request' => $this->request, 'resultFactory' => $this->resultFactory, 'vault' => $this->vault, 'messageManager' => $this->messageManager]);
     $this->assertSame($this->resultJson, $controller->executeInternal());
 }
 protected function setupAuthorizeRequest(array $configData, $vault, $registry, $existingCustomer, array $paymentInfo, array $expectedRequestAttributes, $paymentObject, $storeId, $amount)
 {
     //setup general payment and order information
     $transactionRequest = $this->setupPaymentObject($paymentObject, $storeId);
     //setup config options
     foreach ($configData as $methodName => $value) {
         $this->configMock->expects($this->any())->method($methodName)->willReturn($value);
     }
     //setup payment info instance
     $this->infoInstanceMock->expects($this->any())->method('getAdditionalInformation')->willReturnMap($paymentInfo);
     $this->model->setInfoInstance($this->infoInstanceMock);
     $expectedRequestAttribs = array_merge($transactionRequest, $expectedRequestAttributes);
     $expectedRequestAttribs['amount'] = $amount;
     if ($existingCustomer !== null) {
         $this->vaultMock->expects($this->once())->method('exists')->with(self::CUSTOMER_ID)->willReturn($existingCustomer);
         if ($existingCustomer) {
             unset($expectedRequestAttribs['customer']);
         }
     }
     if (!empty($vault['canSaveCard'])) {
         $this->vaultMock->expects($this->once())->method('canSaveCard')->with(self::AUTH_CC_LAST_4)->willReturn($vault['canSaveCard']);
     }
     if (array_key_exists('registry', $registry)) {
         $this->registryMock->expects($this->once())->method('registry')->with(PaymentMethod::REGISTER_NAME)->willReturn($registry['registry']);
     }
     if (array_key_exists('register', $registry)) {
         $this->registryMock->expects($this->once())->method('register')->with(PaymentMethod::REGISTER_NAME, true);
     }
     return $expectedRequestAttribs;
 }
 /**
  * @param bool $isActive
  * @param bool $isExistingCustomer
  * @param bool $deleteCustomer
  * @dataProvider deleteBraintreeCustomerDataProvider
  */
 public function testDeleteBraintreeCustomer($isActive, $isExistingCustomer, $deleteCustomer)
 {
     $braintreeCustoemrId = 'braintreeCustomerId';
     $customerId = '10002';
     $customerEmail = '*****@*****.**';
     $this->configMock->expects($this->once())->method('isActive')->willReturn($isActive);
     $customer = new \Magento\Framework\DataObject(['id' => $customerId, 'email' => $customerEmail]);
     $this->helperMock->expects($this->any())->method('generateCustomerId')->with($customerId, $customerEmail)->willReturn($braintreeCustoemrId);
     $observer = new \Magento\Framework\Event\Observer(['event' => new \Magento\Framework\DataObject(['customer' => $customer])]);
     $this->vaultMock->expects($this->any())->method('exists')->with($braintreeCustoemrId)->willReturn($isExistingCustomer);
     if ($deleteCustomer) {
         $this->vaultMock->expects($this->once())->method('deleteCustomer')->with($braintreeCustoemrId);
     } else {
         $this->vaultMock->expects($this->never())->method('deleteCustomer');
     }
     $this->assertEquals($this->deleteBraintreeCustomerObserver, $this->deleteBraintreeCustomerObserver->execute($observer));
 }
 /**
  * Executes the controller action and asserts failed deletion
  */
 public function testExecuteSaveFail()
 {
     $objectManager = new ObjectManagerHelper($this);
     $phrase = new \Magento\Framework\Phrase('There was error deleting the credit card');
     $this->vault->expects($this->once())->method('deleteCard')->willReturn(false);
     $this->request->expects($this->any())->method('getParam')->willReturn('token');
     $this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($this->resultRedirect);
     /**
      * @var \Magento\Framework\Message\ManagerInterface $messageManager
      */
     $messageManager = $this->getMockBuilder('\\Magento\\Framework\\Message\\ManagerInterface')->getMock();
     $messageManager->expects($this->any())->method('addError')->with($phrase);
     $notification = $objectManager->getObject('Magento\\Braintree\\Controller\\Creditcard\\DeleteConfirm', ['request' => $this->request, 'resultRedirectFactory' => $this->resultRedirectFactory, 'vault' => $this->vault, 'messageManager' => $messageManager]);
     $this->assertSame($this->resultRedirect, $notification->execute());
 }
 /**
  * Executes the controller action and asserts with redirects for non existing logic
  */
 public function testExecuteNonExistingTokenRedirect()
 {
     $objectManager = new ObjectManagerHelper($this);
     $phrase = new \Magento\Framework\Phrase('Credit card does not exist');
     $this->vault->expects($this->once())->method('storedCard')->willReturn(false);
     $this->request->expects($this->any())->method('getParam')->willReturn('token');
     $this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($this->resultRedirect);
     $this->resultPageFactory->expects($this->never())->method('create')->willReturn($this->resultPage);
     $this->resultRedirect->expects($this->once())->method('setPath')->willReturnSelf();
     /**
      * @var \Magento\Framework\Message\ManagerInterface $messageManager
      */
     $messageManager = $this->getMockBuilder('\\Magento\\Framework\\Message\\ManagerInterface')->getMock();
     $messageManager->expects($this->once())->method('addError')->with($phrase);
     $notification = $objectManager->getObject('Magento\\Braintree\\Controller\\Creditcard\\Edit', ['request' => $this->request, 'resultPageFactory' => $this->resultPageFactory, 'resultRedirectFactory' => $this->resultRedirectFactory, 'vault' => $this->vault, 'messageManager' => $messageManager]);
     $this->assertSame($this->resultRedirect, $notification->execute());
 }
 /**
  * @dataProvider getConfigDataProvider
  */
 public function testGetConfig($configData, $vaultData, $tokenNonceMap, $expectedResult)
 {
     foreach ($configData as $key => $value) {
         $this->configMock->expects($this->any())->method($key)->willReturn($value);
     }
     foreach ($vaultData as $key => $value) {
         $this->vaultMock->expects($this->any())->method($key)->willReturn($value);
     }
     $this->vaultMock->expects($this->any())->method('generatePaymentMethodToken')->willReturnMap($tokenNonceMap);
     $cardTypeMap = [['Visa', 'VI'], ['Master Card', 'MA'], ['American Express', 'AE'], ['Discover Card', 'DI']];
     $this->helperMock->expects($this->any())->method('getCcTypeCodeByName')->willReturnMap($cardTypeMap);
     $this->helperMock->expects($this->once())->method('getTodayMonth')->willReturn(self::TODAY_MONTH);
     $this->helperMock->expects($this->once())->method('getTodayYear')->willReturn(self::TODAY_YEAR);
     $this->helperMock->expects($this->once())->method('getCcAvailableCardTypes')->willReturn($this->availableCardTypes);
     $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->willReturn(true);
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('braintree/creditcard/generate')->willReturn(self::PAYMENT_NONCE_GENERATION_URL);
     $this->assertEquals($expectedResult, $this->model->getConfig());
 }
 /**
  * @dataProvider getConfigDataProvider
  */
 public function testGetConfig($configData, $vaultData, $tokenNonceMap, $expectedResult)
 {
     foreach ($configData as $key => $value) {
         $this->configMock->expects($this->any())->method($key)->willReturn($value);
     }
     foreach ($vaultData as $key => $value) {
         $this->vaultMock->expects($this->any())->method($key)->willReturn($value);
     }
     $this->vaultMock->expects($this->any())->method('generatePaymentMethodToken')->willReturnMap($tokenNonceMap);
     $quoteMock = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->getMock();
     $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn(new \Magento\Framework\Object(['country_id' => 'US']));
     $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
     $cardTypeMap = [['Visa', 'VI'], ['Master Card', 'MA'], ['American Express', 'AE'], ['Discover Card', 'DI']];
     $this->helperMock->expects($this->any())->method('getCcTypeCodeByName')->willReturnMap($cardTypeMap);
     $this->helperMock->expects($this->once())->method('getTodayMonth')->willReturn(self::TODAY_MONTH);
     $this->helperMock->expects($this->once())->method('getTodayYear')->willReturn(self::TODAY_YEAR);
     $this->helperMock->expects($this->once())->method('getCcAvailableCardTypes')->willReturn($this->availableCardTypes);
     $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->willReturn(true);
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('braintree/creditcard/generate')->willReturn(self::PAYMENT_NONCE_GENERATION_URL);
     $this->assertEquals($expectedResult, $this->model->getConfig());
 }
 public function testGetCurrentCustomerStoredCards()
 {
     $cards = ['card'];
     $this->vaultMock->expects($this->once())->method('currentCustomerStoredCards')->willReturn($cards);
     $this->assertEquals($cards, $this->block->getCurrentCustomerStoredCards());
 }