PageRenderTime 19ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-payment/Test/Unit/Model/InfoTest.php

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