Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SpecificationPluginTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Paypal\Model\Billing\AgreementFactory as BillingAgreementFactory;
16 
17 class SpecificationPluginTest extends \PHPUnit\Framework\TestCase
18 {
22  private $plugin;
23 
27  private $objectManagerHelper;
28 
32  private $billingAgreementFactoryMock;
33 
37  private $specificationMock;
38 
42  private $paymentMethodMock;
43 
47  private $quoteMock;
48 
52  private $billingAgreementCollectionMock;
53 
57  private $billingAgreementMock;
58 
59  protected function setUp()
60  {
61  $this->billingAgreementFactoryMock = $this->getMockBuilder(BillingAgreementFactory::class)
62  ->disableOriginalConstructor()
63  ->setMethods(['create'])
64  ->getMock();
65  $this->specificationMock = $this->getMockBuilder(SpecificationInterface::class)
66  ->getMockForAbstractClass();
67  $this->paymentMethodMock = $this->getMockBuilder(MethodInterface::class)
68  ->getMockForAbstractClass();
69  $this->quoteMock = $this->getMockBuilder(Quote::class)
70  ->disableOriginalConstructor()
71  ->setMethods(['getCustomerId'])
72  ->getMock();
73  $this->billingAgreementCollectionMock = $this->getMockBuilder(BillingAgreementCollection::class)
74  ->disableOriginalConstructor()
75  ->getMock();
76  $this->billingAgreementMock = $this->getMockBuilder(BillingAgreement::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79 
80  $this->objectManagerHelper = new ObjectManagerHelper($this);
81  $this->plugin = $this->objectManagerHelper->getObject(
82  SpecificationPlugin::class,
83  [
84  'agreementFactory' => $this->billingAgreementFactoryMock
85  ]
86  );
87  }
88 
90  {
91  $this->setExpectations('any', 'any');
92 
93  $this->assertFalse(
94  $this->plugin->afterIsApplicable(
95  $this->specificationMock,
96  false,
97  $this->paymentMethodMock,
98  $this->quoteMock
99  )
100  );
101  }
102 
104  {
105  $this->setExpectations('not_agreement', 'any');
106 
107  $this->assertTrue(
108  $this->plugin->afterIsApplicable(
109  $this->specificationMock,
110  true,
111  $this->paymentMethodMock,
112  $this->quoteMock
113  )
114  );
115  }
116 
118  {
119  $this->setExpectations('paypal_billing_agreement', null);
120 
121  $this->assertFalse(
122  $this->plugin->afterIsApplicable(
123  $this->specificationMock,
124  true,
125  $this->paymentMethodMock,
126  $this->quoteMock
127  )
128  );
129  }
130 
136  public function testAfterIsApplicable($count)
137  {
138  $this->setExpectations('paypal_billing_agreement', 1);
139 
140  $this->billingAgreementFactoryMock->expects(static::once())
141  ->method('create')
142  ->willReturn($this->billingAgreementMock);
143  $this->billingAgreementMock->expects(static::once())
144  ->method('getAvailableCustomerBillingAgreements')
145  ->with(1)
146  ->willReturn($this->billingAgreementCollectionMock);
147  $this->billingAgreementCollectionMock->expects(static::once())
148  ->method('count')
149  ->willReturn($count);
150 
151  $this->assertEquals(
152  $count > 0,
153  $this->plugin->afterIsApplicable($this->specificationMock, true, $this->paymentMethodMock, $this->quoteMock)
154  );
155  }
156 
161  {
162  return [[0], [1], [2]];
163  }
164 
172  private function setExpectations($paymentMethodCode, $customerId)
173  {
174  $this->paymentMethodMock->expects(static::any())
175  ->method('getCode')
176  ->willReturn($paymentMethodCode);
177  $this->quoteMock->expects(static::any())
178  ->method('getCustomerId')
179  ->willReturn($customerId);
180  }
181 }
$count
Definition: recent.phtml:13