FormTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Block\Express;
  7. use Magento\Customer\Helper\Session\CurrentCustomer;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Paypal\Block\Express\Form;
  10. use Magento\Paypal\Helper\Data;
  11. use Magento\Paypal\Model\Config;
  12. use Magento\Paypal\Model\Express\Checkout;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class FormTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var Data|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $_paypalData;
  22. /**
  23. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $_paypalConfig;
  26. /**
  27. * @var CurrentCustomer|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $currentCustomer;
  30. /**
  31. * @var Form
  32. */
  33. protected $_model;
  34. protected function setUp()
  35. {
  36. $this->_paypalData = $this->createMock(\Magento\Paypal\Helper\Data::class);
  37. $this->_paypalConfig = $this->createMock(\Magento\Paypal\Model\Config::class);
  38. $this->_paypalConfig->expects($this->once())
  39. ->method('setMethod')
  40. ->will($this->returnSelf());
  41. $paypalConfigFactory = $this->createPartialMock(\Magento\Paypal\Model\ConfigFactory::class, ['create']);
  42. $paypalConfigFactory->expects($this->once())
  43. ->method('create')
  44. ->will($this->returnValue($this->_paypalConfig));
  45. $mark = $this->createMock(\Magento\Framework\View\Element\Template::class);
  46. $mark->expects($this->once())
  47. ->method('setTemplate')
  48. ->will($this->returnSelf());
  49. $mark->expects($this->any())
  50. ->method('__call')
  51. ->will($this->returnSelf());
  52. $layout = $this->getMockForAbstractClass(
  53. \Magento\Framework\View\LayoutInterface::class
  54. );
  55. $layout->expects($this->once())
  56. ->method('createBlock')
  57. ->with(\Magento\Framework\View\Element\Template::class)
  58. ->will($this->returnValue($mark));
  59. $this->currentCustomer = $this
  60. ->getMockBuilder(\Magento\Customer\Helper\Session\CurrentCustomer::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $localeResolver = $this->createMock(\Magento\Framework\Locale\ResolverInterface::class);
  64. $helper = new ObjectManager($this);
  65. $this->_model = $helper->getObject(
  66. \Magento\Paypal\Block\Express\Form::class,
  67. [
  68. 'paypalData' => $this->_paypalData,
  69. 'paypalConfigFactory' => $paypalConfigFactory,
  70. 'currentCustomer' => $this->currentCustomer,
  71. 'layout' => $layout,
  72. 'localeResolver' => $localeResolver
  73. ]
  74. );
  75. }
  76. /**
  77. * @param bool $ask
  78. * @param string|null $expected
  79. * @dataProvider getBillingAgreementCodeDataProvider
  80. */
  81. public function testGetBillingAgreementCode($ask, $expected)
  82. {
  83. $this->currentCustomer->expects($this->once())
  84. ->method('getCustomerId')
  85. ->will($this->returnValue('customer id'));
  86. $this->_paypalData->expects($this->once())
  87. ->method('shouldAskToCreateBillingAgreement')
  88. ->with($this->identicalTo($this->_paypalConfig), 'customer id')
  89. ->will($this->returnValue($ask));
  90. $this->assertEquals(
  91. $expected,
  92. $this->_model->getBillingAgreementCode()
  93. );
  94. }
  95. /**
  96. * @return array
  97. */
  98. public function getBillingAgreementCodeDataProvider()
  99. {
  100. return [
  101. [true, Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT],
  102. [false, null]
  103. ];
  104. }
  105. }