Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentTokenAssignerTest.php
Go to the documentation of this file.
1 <?php
7 
21 
26 class PaymentTokenAssignerTest extends \PHPUnit\Framework\TestCase
27 {
31  private $paymentTokenManagement;
32 
36  private $observer;
37 
38  public function setUp()
39  {
40  $this->paymentTokenManagement = $this->createMock(PaymentTokenManagementInterface::class);
41  $this->observer = new PaymentTokenAssigner($this->paymentTokenManagement);
42  }
43 
44  public function testExecuteNoPublicHash()
45  {
46  $dataObject = new DataObject();
47  $observer = $this->getPreparedObserverWithMap(
48  [
50  ]
51  );
52 
53  $this->paymentTokenManagement->expects(static::never())
54  ->method('getByPublicHash');
55  $this->observer->execute($observer);
56  }
57 
59  {
60  $dataObject = new DataObject(
61  [
63  PaymentTokenInterface::PUBLIC_HASH => 'public_hash_value'
64  ]
65  ]
66  );
67  $paymentModel = $this->createMock(InfoInterface::class);
68 
69  $observer = $this->getPreparedObserverWithMap(
70  [
73  ]
74  );
75 
76  $this->paymentTokenManagement->expects(static::never())
77  ->method('getByPublicHash');
78  $this->observer->execute($observer);
79  }
80 
81  public function testExecuteNoPaymentToken()
82  {
83  $customerId = 1;
84  $publicHash = 'public_hash_value';
85  $dataObject = new DataObject(
86  [
89  ]
90  ]
91  );
92 
93  $paymentModel = $this->getMockBuilder(Payment::class)
94  ->disableOriginalConstructor()
95  ->getMock();
96  $quote = $this->createMock(CartInterface::class);
97  $customer = $this->createMock(CustomerInterface::class);
98 
99  $paymentModel->expects(static::once())
100  ->method('getQuote')
101  ->willReturn($quote);
102  $quote->expects(static::once())
103  ->method('getCustomer')
104  ->willReturn($customer);
105  $customer->expects(static::once())
106  ->method('getId')
107  ->willReturn($customerId);
108 
109  $this->paymentTokenManagement->expects(static::once())
110  ->method('getByPublicHash')
111  ->with($publicHash, $customerId)
112  ->willReturn(null);
113 
114  $observer = $this->getPreparedObserverWithMap(
115  [
118  ]
119  );
120 
121  $paymentModel->expects(static::never())
122  ->method('setAdditionalInformation');
123 
124  $this->observer->execute($observer);
125  }
126 
127  public function testExecuteSaveMetadata()
128  {
129  $customerId = 1;
130  $publicHash = 'public_hash_value';
131  $dataObject = new DataObject(
132  [
135  ]
136  ]
137  );
138 
139  $paymentModel = $this->getMockBuilder(Payment::class)
140  ->disableOriginalConstructor()
141  ->getMock();
142  $quote = $this->createMock(CartInterface::class);
143  $customer = $this->createMock(CustomerInterface::class);
144  $paymentToken = $this->createMock(PaymentTokenInterface::class);
145 
146  $paymentModel->expects(static::once())
147  ->method('getQuote')
148  ->willReturn($quote);
149  $quote->expects(static::once())
150  ->method('getCustomer')
151  ->willReturn($customer);
152  $customer->expects(static::once())
153  ->method('getId')
154  ->willReturn($customerId);
155 
156  $this->paymentTokenManagement->expects(static::once())
157  ->method('getByPublicHash')
158  ->with($publicHash, $customerId)
159  ->willReturn($paymentToken);
160 
161  $paymentModel->expects(static::once())
162  ->method('setAdditionalInformation')
163  ->with(
164  [
167  ]
168  );
169 
170  $observer = $this->getPreparedObserverWithMap(
171  [
174  ]
175  );
176 
177  $this->observer->execute($observer);
178  }
179 
184  private function getPreparedObserverWithMap(array $returnMap)
185  {
186  $observer = $this->getMockBuilder(Observer::class)
187  ->disableOriginalConstructor()
188  ->getMock();
189  $event = $this->getMockBuilder(Event::class)
190  ->disableOriginalConstructor()
191  ->getMock();
192 
193  $observer->expects(static::atLeastOnce())
194  ->method('getEvent')
195  ->willReturn($event);
196  $event->expects(static::atLeastOnce())
197  ->method('getDataByKey')
198  ->willReturnMap(
199  $returnMap
200  );
201 
202  return $observer;
203  }
204 }
$customer
Definition: customers.php:11
$quote