123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Payment\Test\Unit\Model;
- use Magento\Payment\Model\Method;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- class InfoTest extends \PHPUnit\Framework\TestCase
- {
- /** @var \Magento\Payment\Model\InfoInterface */
- protected $info;
- /** @var ObjectManagerHelper */
- protected $objectManagerHelper;
- /** @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject */
- protected $contextMock;
- /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */
- protected $registryMock;
- /** @var \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
- protected $paymentHelperMock;
- /** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $encryptorInterfaceMock;
- /** @var \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
- protected $methodInstanceMock;
- protected function setUp()
- {
- $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
- $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
- $this->paymentHelperMock = $this->createPartialMock(\Magento\Payment\Helper\Data::class, ['getMethodInstance']);
- $this->encryptorInterfaceMock = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
- $this->methodInstanceMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
- ->getMockForAbstractClass();
- $this->objectManagerHelper = new ObjectManagerHelper($this);
- $this->info = $this->objectManagerHelper->getObject(
- \Magento\Payment\Model\Info::class,
- [
- 'context' => $this->contextMock,
- 'registry' => $this->registryMock,
- 'paymentData' => $this->paymentHelperMock,
- 'encryptor' => $this->encryptorInterfaceMock
- ]
- );
- }
- /**
- * @dataProvider ccKeysDataProvider
- * @param string $keyCc
- * @param string $keyCcEnc
- */
- public function testGetDataCcNumber($keyCc, $keyCcEnc)
- {
- // no data was set
- $this->assertNull($this->info->getData($keyCc));
- // we set encrypted data
- $this->info->setData($keyCcEnc, $keyCcEnc);
- $this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($keyCcEnc)->will(
- $this->returnValue($keyCc)
- );
- $this->assertEquals($keyCc, $this->info->getData($keyCc));
- }
- /**
- * Returns array of Cc keys which needs prepare logic
- *
- * @return array
- */
- public function ccKeysDataProvider()
- {
- return [
- ['cc_number', 'cc_number_enc'],
- ['cc_cid', 'cc_cid_enc']
- ];
- }
- public function testGetMethodInstanceWithRealMethod()
- {
- $method = 'real_method';
- $this->info->setData('method', $method);
- $this->methodInstanceMock->expects($this->once())
- ->method('setInfoInstance')
- ->with($this->info);
- $this->paymentHelperMock->expects($this->once())
- ->method('getMethodInstance')
- ->with($method)
- ->willReturn($this->methodInstanceMock);
- $this->info->getMethodInstance();
- }
- public function testGetMethodInstanceWithUnrealMethod()
- {
- $method = 'unreal_method';
- $this->info->setData('method', $method);
- $this->paymentHelperMock->expects($this->at(0))
- ->method('getMethodInstance')
- ->with($method)
- ->willThrowException(new \UnexpectedValueException());
- $this->methodInstanceMock->expects($this->once())
- ->method('setInfoInstance')
- ->with($this->info);
- $this->paymentHelperMock->expects($this->at(1))
- ->method('getMethodInstance')
- ->with(Method\Substitution::CODE)
- ->willReturn($this->methodInstanceMock);
- $this->info->getMethodInstance();
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- * @expectedExceptionMessage The payment method you requested is not available.
- */
- public function testGetMethodInstanceWithNoMethod()
- {
- $this->info->setData('method', false);
- $this->info->getMethodInstance();
- }
- public function testGetMethodInstanceRequestedMethod()
- {
- $code = 'real_method';
- $this->info->setData('method', $code);
- $this->paymentHelperMock->expects($this->once())->method('getMethodInstance')->with($code)->will(
- $this->returnValue($this->methodInstanceMock)
- );
- $this->methodInstanceMock->expects($this->once())->method('setInfoInstance')->with($this->info);
- $this->assertSame($this->methodInstanceMock, $this->info->getMethodInstance());
- // as the method is already stored at Info, check that it's not initialized again
- $this->assertSame($this->methodInstanceMock, $this->info->getMethodInstance());
- }
- public function testEncrypt()
- {
- $data = 'data';
- $encryptedData = 'd1a2t3a4';
- $this->encryptorInterfaceMock->expects($this->once())->method('encrypt')->with($data)->will(
- $this->returnValue($encryptedData)
- );
- $this->assertEquals($encryptedData, $this->info->encrypt($data));
- }
- public function testDecrypt()
- {
- $data = 'data';
- $encryptedData = 'd1a2t3a4';
- $this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($encryptedData)->will(
- $this->returnValue($data)
- );
- $this->assertEquals($data, $this->info->decrypt($encryptedData));
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testSetAdditionalInformationException()
- {
- $this->info->setAdditionalInformation('object', new \StdClass());
- }
- /**
- * @dataProvider additionalInformationDataProvider
- * @param mixed $key
- * @param mixed $value
- */
- public function testSetAdditionalInformationMultipleTypes($key, $value = null)
- {
- $this->info->setAdditionalInformation($key, $value);
- $this->assertEquals($value ? [$key => $value] : $key, $this->info->getAdditionalInformation());
- }
- /**
- * Prepared data for testSetAdditionalInformationMultipleTypes
- *
- * @return array
- */
- public function additionalInformationDataProvider()
- {
- return [
- [['key1' => 'data1', 'key2' => 'data2'], null],
- ['key', 'data']
- ];
- }
- public function testGetAdditionalInformationByKey()
- {
- $key = 'key';
- $value = 'value';
- $this->info->setAdditionalInformation($key, $value);
- $this->assertEquals($value, $this->info->getAdditionalInformation($key));
- }
- public function testUnsAdditionalInformation()
- {
- // set array to additional
- $data = ['key1' => 'data1', 'key2' => 'data2'];
- $this->info->setAdditionalInformation($data);
- // unset by key
- $this->assertEquals(
- ['key2' => 'data2'],
- $this->info->unsAdditionalInformation('key1')->getAdditionalInformation()
- );
- // unset all
- $this->assertEmpty($this->info->unsAdditionalInformation()->getAdditionalInformation());
- }
- public function testHasAdditionalInformation()
- {
- $this->assertFalse($this->info->hasAdditionalInformation());
- $data = ['key1' => 'data1', 'key2' => 'data2'];
- $this->info->setAdditionalInformation($data);
- $this->assertFalse($this->info->hasAdditionalInformation('key3'));
- $this->assertTrue($this->info->hasAdditionalInformation('key2'));
- $this->assertTrue($this->info->hasAdditionalInformation());
- }
- public function testInitAdditionalInformationWithUnserialize()
- {
- $data = ['key1' => 'data1', 'key2' => 'data2'];
- $this->info->setData('additional_information', $data);
- $this->assertEquals($data, $this->info->getAdditionalInformation());
- }
- }
|