InfoTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Model;
  7. use Magento\Payment\Model\Method;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class InfoTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Payment\Model\InfoInterface */
  12. protected $info;
  13. /** @var ObjectManagerHelper */
  14. protected $objectManagerHelper;
  15. /** @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $contextMock;
  17. /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $registryMock;
  19. /** @var \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $paymentHelperMock;
  21. /** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $encryptorInterfaceMock;
  23. /** @var \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $methodInstanceMock;
  25. protected function setUp()
  26. {
  27. $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
  28. $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
  29. $this->paymentHelperMock = $this->createPartialMock(\Magento\Payment\Helper\Data::class, ['getMethodInstance']);
  30. $this->encryptorInterfaceMock = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
  31. $this->methodInstanceMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  32. ->getMockForAbstractClass();
  33. $this->objectManagerHelper = new ObjectManagerHelper($this);
  34. $this->info = $this->objectManagerHelper->getObject(
  35. \Magento\Payment\Model\Info::class,
  36. [
  37. 'context' => $this->contextMock,
  38. 'registry' => $this->registryMock,
  39. 'paymentData' => $this->paymentHelperMock,
  40. 'encryptor' => $this->encryptorInterfaceMock
  41. ]
  42. );
  43. }
  44. /**
  45. * @dataProvider ccKeysDataProvider
  46. * @param string $keyCc
  47. * @param string $keyCcEnc
  48. */
  49. public function testGetDataCcNumber($keyCc, $keyCcEnc)
  50. {
  51. // no data was set
  52. $this->assertNull($this->info->getData($keyCc));
  53. // we set encrypted data
  54. $this->info->setData($keyCcEnc, $keyCcEnc);
  55. $this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($keyCcEnc)->will(
  56. $this->returnValue($keyCc)
  57. );
  58. $this->assertEquals($keyCc, $this->info->getData($keyCc));
  59. }
  60. /**
  61. * Returns array of Cc keys which needs prepare logic
  62. *
  63. * @return array
  64. */
  65. public function ccKeysDataProvider()
  66. {
  67. return [
  68. ['cc_number', 'cc_number_enc'],
  69. ['cc_cid', 'cc_cid_enc']
  70. ];
  71. }
  72. public function testGetMethodInstanceWithRealMethod()
  73. {
  74. $method = 'real_method';
  75. $this->info->setData('method', $method);
  76. $this->methodInstanceMock->expects($this->once())
  77. ->method('setInfoInstance')
  78. ->with($this->info);
  79. $this->paymentHelperMock->expects($this->once())
  80. ->method('getMethodInstance')
  81. ->with($method)
  82. ->willReturn($this->methodInstanceMock);
  83. $this->info->getMethodInstance();
  84. }
  85. public function testGetMethodInstanceWithUnrealMethod()
  86. {
  87. $method = 'unreal_method';
  88. $this->info->setData('method', $method);
  89. $this->paymentHelperMock->expects($this->at(0))
  90. ->method('getMethodInstance')
  91. ->with($method)
  92. ->willThrowException(new \UnexpectedValueException());
  93. $this->methodInstanceMock->expects($this->once())
  94. ->method('setInfoInstance')
  95. ->with($this->info);
  96. $this->paymentHelperMock->expects($this->at(1))
  97. ->method('getMethodInstance')
  98. ->with(Method\Substitution::CODE)
  99. ->willReturn($this->methodInstanceMock);
  100. $this->info->getMethodInstance();
  101. }
  102. /**
  103. * @expectedException \Magento\Framework\Exception\LocalizedException
  104. * @expectedExceptionMessage The payment method you requested is not available.
  105. */
  106. public function testGetMethodInstanceWithNoMethod()
  107. {
  108. $this->info->setData('method', false);
  109. $this->info->getMethodInstance();
  110. }
  111. public function testGetMethodInstanceRequestedMethod()
  112. {
  113. $code = 'real_method';
  114. $this->info->setData('method', $code);
  115. $this->paymentHelperMock->expects($this->once())->method('getMethodInstance')->with($code)->will(
  116. $this->returnValue($this->methodInstanceMock)
  117. );
  118. $this->methodInstanceMock->expects($this->once())->method('setInfoInstance')->with($this->info);
  119. $this->assertSame($this->methodInstanceMock, $this->info->getMethodInstance());
  120. // as the method is already stored at Info, check that it's not initialized again
  121. $this->assertSame($this->methodInstanceMock, $this->info->getMethodInstance());
  122. }
  123. public function testEncrypt()
  124. {
  125. $data = 'data';
  126. $encryptedData = 'd1a2t3a4';
  127. $this->encryptorInterfaceMock->expects($this->once())->method('encrypt')->with($data)->will(
  128. $this->returnValue($encryptedData)
  129. );
  130. $this->assertEquals($encryptedData, $this->info->encrypt($data));
  131. }
  132. public function testDecrypt()
  133. {
  134. $data = 'data';
  135. $encryptedData = 'd1a2t3a4';
  136. $this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($encryptedData)->will(
  137. $this->returnValue($data)
  138. );
  139. $this->assertEquals($data, $this->info->decrypt($encryptedData));
  140. }
  141. /**
  142. * @expectedException \Magento\Framework\Exception\LocalizedException
  143. */
  144. public function testSetAdditionalInformationException()
  145. {
  146. $this->info->setAdditionalInformation('object', new \StdClass());
  147. }
  148. /**
  149. * @dataProvider additionalInformationDataProvider
  150. * @param mixed $key
  151. * @param mixed $value
  152. */
  153. public function testSetAdditionalInformationMultipleTypes($key, $value = null)
  154. {
  155. $this->info->setAdditionalInformation($key, $value);
  156. $this->assertEquals($value ? [$key => $value] : $key, $this->info->getAdditionalInformation());
  157. }
  158. /**
  159. * Prepared data for testSetAdditionalInformationMultipleTypes
  160. *
  161. * @return array
  162. */
  163. public function additionalInformationDataProvider()
  164. {
  165. return [
  166. [['key1' => 'data1', 'key2' => 'data2'], null],
  167. ['key', 'data']
  168. ];
  169. }
  170. public function testGetAdditionalInformationByKey()
  171. {
  172. $key = 'key';
  173. $value = 'value';
  174. $this->info->setAdditionalInformation($key, $value);
  175. $this->assertEquals($value, $this->info->getAdditionalInformation($key));
  176. }
  177. public function testUnsAdditionalInformation()
  178. {
  179. // set array to additional
  180. $data = ['key1' => 'data1', 'key2' => 'data2'];
  181. $this->info->setAdditionalInformation($data);
  182. // unset by key
  183. $this->assertEquals(
  184. ['key2' => 'data2'],
  185. $this->info->unsAdditionalInformation('key1')->getAdditionalInformation()
  186. );
  187. // unset all
  188. $this->assertEmpty($this->info->unsAdditionalInformation()->getAdditionalInformation());
  189. }
  190. public function testHasAdditionalInformation()
  191. {
  192. $this->assertFalse($this->info->hasAdditionalInformation());
  193. $data = ['key1' => 'data1', 'key2' => 'data2'];
  194. $this->info->setAdditionalInformation($data);
  195. $this->assertFalse($this->info->hasAdditionalInformation('key3'));
  196. $this->assertTrue($this->info->hasAdditionalInformation('key2'));
  197. $this->assertTrue($this->info->hasAdditionalInformation());
  198. }
  199. public function testInitAdditionalInformationWithUnserialize()
  200. {
  201. $data = ['key1' => 'data1', 'key2' => 'data2'];
  202. $this->info->setData('additional_information', $data);
  203. $this->assertEquals($data, $this->info->getAdditionalInformation());
  204. }
  205. }