Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InfoTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class InfoTest extends \PHPUnit\Framework\TestCase
13 {
15  protected $info;
16 
19 
21  protected $contextMock;
22 
24  protected $registryMock;
25 
27  protected $paymentHelperMock;
28 
31 
34 
35  protected function setUp()
36  {
37  $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
38  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
39  $this->paymentHelperMock = $this->createPartialMock(\Magento\Payment\Helper\Data::class, ['getMethodInstance']);
40  $this->encryptorInterfaceMock = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
41  $this->methodInstanceMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
42  ->getMockForAbstractClass();
43 
44  $this->objectManagerHelper = new ObjectManagerHelper($this);
45  $this->info = $this->objectManagerHelper->getObject(
46  \Magento\Payment\Model\Info::class,
47  [
48  'context' => $this->contextMock,
49  'registry' => $this->registryMock,
50  'paymentData' => $this->paymentHelperMock,
51  'encryptor' => $this->encryptorInterfaceMock
52  ]
53  );
54  }
55 
61  public function testGetDataCcNumber($keyCc, $keyCcEnc)
62  {
63  // no data was set
64  $this->assertNull($this->info->getData($keyCc));
65 
66  // we set encrypted data
67  $this->info->setData($keyCcEnc, $keyCcEnc);
68  $this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($keyCcEnc)->will(
69  $this->returnValue($keyCc)
70  );
71  $this->assertEquals($keyCc, $this->info->getData($keyCc));
72  }
73 
79  public function ccKeysDataProvider()
80  {
81  return [
82  ['cc_number', 'cc_number_enc'],
83  ['cc_cid', 'cc_cid_enc']
84  ];
85  }
86 
88  {
89  $method = 'real_method';
90  $this->info->setData('method', $method);
91 
92  $this->methodInstanceMock->expects($this->once())
93  ->method('setInfoInstance')
94  ->with($this->info);
95 
96  $this->paymentHelperMock->expects($this->once())
97  ->method('getMethodInstance')
98  ->with($method)
99  ->willReturn($this->methodInstanceMock);
100 
101  $this->info->getMethodInstance();
102  }
103 
105  {
106  $method = 'unreal_method';
107  $this->info->setData('method', $method);
108 
109  $this->paymentHelperMock->expects($this->at(0))
110  ->method('getMethodInstance')
111  ->with($method)
112  ->willThrowException(new \UnexpectedValueException());
113 
114  $this->methodInstanceMock->expects($this->once())
115  ->method('setInfoInstance')
116  ->with($this->info);
117 
118  $this->paymentHelperMock->expects($this->at(1))
119  ->method('getMethodInstance')
120  ->with(Method\Substitution::CODE)
121  ->willReturn($this->methodInstanceMock);
122 
123  $this->info->getMethodInstance();
124  }
125 
131  {
132  $this->info->setData('method', false);
133  $this->info->getMethodInstance();
134  }
135 
137  {
138  $code = 'real_method';
139  $this->info->setData('method', $code);
140 
141  $this->paymentHelperMock->expects($this->once())->method('getMethodInstance')->with($code)->will(
142  $this->returnValue($this->methodInstanceMock)
143  );
144 
145  $this->methodInstanceMock->expects($this->once())->method('setInfoInstance')->with($this->info);
146  $this->assertSame($this->methodInstanceMock, $this->info->getMethodInstance());
147 
148  // as the method is already stored at Info, check that it's not initialized again
149  $this->assertSame($this->methodInstanceMock, $this->info->getMethodInstance());
150  }
151 
152  public function testEncrypt()
153  {
154  $data = 'data';
155  $encryptedData = 'd1a2t3a4';
156 
157  $this->encryptorInterfaceMock->expects($this->once())->method('encrypt')->with($data)->will(
158  $this->returnValue($encryptedData)
159  );
160  $this->assertEquals($encryptedData, $this->info->encrypt($data));
161  }
162 
163  public function testDecrypt()
164  {
165  $data = 'data';
166  $encryptedData = 'd1a2t3a4';
167 
168  $this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($encryptedData)->will(
169  $this->returnValue($data)
170  );
171  $this->assertEquals($data, $this->info->decrypt($encryptedData));
172  }
173 
178  {
179  $this->info->setAdditionalInformation('object', new \StdClass());
180  }
181 
187  public function testSetAdditionalInformationMultipleTypes($key, $value = null)
188  {
189  $this->info->setAdditionalInformation($key, $value);
190  $this->assertEquals($value ? [$key => $value] : $key, $this->info->getAdditionalInformation());
191  }
192 
199  {
200  return [
201  [['key1' => 'data1', 'key2' => 'data2'], null],
202  ['key', 'data']
203  ];
204  }
205 
207  {
208  $key = 'key';
209  $value = 'value';
210  $this->info->setAdditionalInformation($key, $value);
211  $this->assertEquals($value, $this->info->getAdditionalInformation($key));
212  }
213 
215  {
216  // set array to additional
217  $data = ['key1' => 'data1', 'key2' => 'data2'];
218  $this->info->setAdditionalInformation($data);
219 
220  // unset by key
221  $this->assertEquals(
222  ['key2' => 'data2'],
223  $this->info->unsAdditionalInformation('key1')->getAdditionalInformation()
224  );
225 
226  // unset all
227  $this->assertEmpty($this->info->unsAdditionalInformation()->getAdditionalInformation());
228  }
229 
231  {
232  $this->assertFalse($this->info->hasAdditionalInformation());
233 
234  $data = ['key1' => 'data1', 'key2' => 'data2'];
235  $this->info->setAdditionalInformation($data);
236  $this->assertFalse($this->info->hasAdditionalInformation('key3'));
237 
238  $this->assertTrue($this->info->hasAdditionalInformation('key2'));
239  $this->assertTrue($this->info->hasAdditionalInformation());
240  }
241 
243  {
244  $data = ['key1' => 'data1', 'key2' => 'data2'];
245  $this->info->setData('additional_information', $data);
246 
247  $this->assertEquals($data, $this->info->getAdditionalInformation());
248  }
249 }
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
testSetAdditionalInformationMultipleTypes($key, $value=null)
Definition: InfoTest.php:187
testGetDataCcNumber($keyCc, $keyCcEnc)
Definition: InfoTest.php:61
$code
Definition: info.phtml:12