SpecificationPluginTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Model\Method\Checks;
  7. use Magento\Paypal\Model\Method\Checks\SpecificationPlugin;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\Paypal\Model\Billing\AgreementFactory as BillingAgreementFactory;
  10. use Magento\Payment\Model\Checks\SpecificationInterface;
  11. use Magento\Payment\Model\MethodInterface;
  12. use Magento\Quote\Model\Quote;
  13. use Magento\Paypal\Model\ResourceModel\Billing\Agreement\Collection as BillingAgreementCollection;
  14. use Magento\Paypal\Model\Billing\Agreement as BillingAgreement;
  15. class SpecificationPluginTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var SpecificationPlugin
  19. */
  20. private $plugin;
  21. /**
  22. * @var ObjectManagerHelper
  23. */
  24. private $objectManagerHelper;
  25. /**
  26. * @var BillingAgreementFactory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $billingAgreementFactoryMock;
  29. /**
  30. * @var SpecificationInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $specificationMock;
  33. /**
  34. * @var MethodInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $paymentMethodMock;
  37. /**
  38. * @var Quote|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $quoteMock;
  41. /**
  42. * @var BillingAgreementCollection|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $billingAgreementCollectionMock;
  45. /**
  46. * @var BillingAgreement|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $billingAgreementMock;
  49. protected function setUp()
  50. {
  51. $this->billingAgreementFactoryMock = $this->getMockBuilder(BillingAgreementFactory::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods(['create'])
  54. ->getMock();
  55. $this->specificationMock = $this->getMockBuilder(SpecificationInterface::class)
  56. ->getMockForAbstractClass();
  57. $this->paymentMethodMock = $this->getMockBuilder(MethodInterface::class)
  58. ->getMockForAbstractClass();
  59. $this->quoteMock = $this->getMockBuilder(Quote::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(['getCustomerId'])
  62. ->getMock();
  63. $this->billingAgreementCollectionMock = $this->getMockBuilder(BillingAgreementCollection::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->billingAgreementMock = $this->getMockBuilder(BillingAgreement::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->objectManagerHelper = new ObjectManagerHelper($this);
  70. $this->plugin = $this->objectManagerHelper->getObject(
  71. SpecificationPlugin::class,
  72. [
  73. 'agreementFactory' => $this->billingAgreementFactoryMock
  74. ]
  75. );
  76. }
  77. public function testAfterIsApplicableNotOriginallyApplicable()
  78. {
  79. $this->setExpectations('any', 'any');
  80. $this->assertFalse(
  81. $this->plugin->afterIsApplicable(
  82. $this->specificationMock,
  83. false,
  84. $this->paymentMethodMock,
  85. $this->quoteMock
  86. )
  87. );
  88. }
  89. public function testAfterIsApplicableNotAgreement()
  90. {
  91. $this->setExpectations('not_agreement', 'any');
  92. $this->assertTrue(
  93. $this->plugin->afterIsApplicable(
  94. $this->specificationMock,
  95. true,
  96. $this->paymentMethodMock,
  97. $this->quoteMock
  98. )
  99. );
  100. }
  101. public function testAfterIsApplicableNoCustomerId()
  102. {
  103. $this->setExpectations('paypal_billing_agreement', null);
  104. $this->assertFalse(
  105. $this->plugin->afterIsApplicable(
  106. $this->specificationMock,
  107. true,
  108. $this->paymentMethodMock,
  109. $this->quoteMock
  110. )
  111. );
  112. }
  113. /**
  114. * @param int $count
  115. *
  116. * @dataProvider afterIsApplicableDataProvider
  117. */
  118. public function testAfterIsApplicable($count)
  119. {
  120. $this->setExpectations('paypal_billing_agreement', 1);
  121. $this->billingAgreementFactoryMock->expects(static::once())
  122. ->method('create')
  123. ->willReturn($this->billingAgreementMock);
  124. $this->billingAgreementMock->expects(static::once())
  125. ->method('getAvailableCustomerBillingAgreements')
  126. ->with(1)
  127. ->willReturn($this->billingAgreementCollectionMock);
  128. $this->billingAgreementCollectionMock->expects(static::once())
  129. ->method('count')
  130. ->willReturn($count);
  131. $this->assertEquals(
  132. $count > 0,
  133. $this->plugin->afterIsApplicable($this->specificationMock, true, $this->paymentMethodMock, $this->quoteMock)
  134. );
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function afterIsApplicableDataProvider()
  140. {
  141. return [[0], [1], [2]];
  142. }
  143. /**
  144. * Set expectations
  145. *
  146. * @param mixed $paymentMethodCode
  147. * @param mixed $customerId
  148. * @return void
  149. */
  150. private function setExpectations($paymentMethodCode, $customerId)
  151. {
  152. $this->paymentMethodMock->expects(static::any())
  153. ->method('getCode')
  154. ->willReturn($paymentMethodCode);
  155. $this->quoteMock->expects(static::any())
  156. ->method('getCustomerId')
  157. ->willReturn($customerId);
  158. }
  159. }