Skip to content

Commit

Permalink
ENGCOM-7639: magento/magneto2#27570: fixed issue withsue with store i…
Browse files Browse the repository at this point in the history
…d for cash on… #27582
  • Loading branch information
slavvka authored Aug 7, 2020
2 parents 4e1d900 + 89318ba commit dab4a1a
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 67 deletions.
1 change: 1 addition & 0 deletions app/code/Magento/Sales/Model/Order/Payment/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function getMethodInstance()
$instance = $this->paymentData->getMethodInstance(Substitution::CODE);
}
$instance->setInfoInstance($this);
$instance->setStore($this->getOrder()->getStoreId());
$this->setMethodInstance($instance);
}
return $this->getData('method_instance');
Expand Down
191 changes: 140 additions & 51 deletions app/code/Magento/Sales/Test/Unit/Model/Order/Payment/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Sales\Test\Unit\Model\Order\Payment;
Expand All @@ -12,72 +13,90 @@
use Magento\Framework\Registry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Payment\Helper\Data;
use Magento\Payment\Model\Method;
use Magento\Payment\Model\Method\Substitution;
use Magento\Payment\Model\MethodInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\Payment\Info;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\Exception\LocalizedException;

/**
* Test for \Magento\Sales\Model\Order\Payment\Info.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class InfoTest extends TestCase
{
/** @var \Magento\Sales\Model\Order\Payment\Info */
protected $info;

/** @var ObjectManagerHelper */
protected $objectManagerHelper;

/** @var Context|MockObject */
protected $contextMock;
/**
* @var Info
*/
private $info;

/** @var Registry|MockObject */
protected $registryMock;
/**
* @var Data|MockObject
*/
private $paymentHelperMock;

/** @var Data|MockObject */
protected $paymentHelperMock;
/**
* @var EncryptorInterface|MockObject
*/
private $encryptorInterfaceMock;

/** @var EncryptorInterface|MockObject */
protected $encryptorInterfaceMock;
/**
* @var Data|MockObject
*/
private $methodInstanceMock;

/** @var Data|MockObject */
protected $methodInstanceMock;
/**
* @var OrderInterface|MockObject
*/
private $orderMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->contextMock = $this->createMock(Context::class);
$this->registryMock = $this->createMock(Registry::class);
$contextMock = $this->createMock(Context::class);
$registryMock = $this->createMock(Registry::class);
$this->paymentHelperMock = $this->createPartialMock(Data::class, ['getMethodInstance']);
$this->encryptorInterfaceMock = $this->getMockForAbstractClass(EncryptorInterface::class);
$this->methodInstanceMock = $this->getMockBuilder(MethodInterface::class)
->getMockForAbstractClass();
$this->methodInstanceMock = $this->getMockForAbstractClass(MethodInterface::class);
$this->orderMock = $this->createMock(OrderInterface::class);

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->info = $this->objectManagerHelper->getObject(
$objectManagerHelper = new ObjectManagerHelper($this);
$this->info = $objectManagerHelper->getObject(
Info::class,
[
'context' => $this->contextMock,
'registry' => $this->registryMock,
'context' => $contextMock,
'registry' => $registryMock,
'paymentData' => $this->paymentHelperMock,
'encryptor' => $this->encryptorInterfaceMock
]
);
$this->info->setData('order', $this->orderMock);
}

/**
* Get data cc number
*
* @dataProvider ccKeysDataProvider
* @param string $keyCc
* @param string $keyCcEnc
* @return void
*/
public function testGetDataCcNumber($keyCc, $keyCcEnc)
public function testGetDataCcNumber($keyCc, $keyCcEnc): void
{
// 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)->willReturn(
$keyCc
);
$this->encryptorInterfaceMock->expects($this->once())
->method('decrypt')
->with($keyCcEnc)
->willReturn($keyCc);

$this->assertEquals($keyCc, $this->info->getData($keyCc));
}

Expand All @@ -86,22 +105,34 @@ public function testGetDataCcNumber($keyCc, $keyCcEnc)
*
* @return array
*/
public function ccKeysDataProvider()
public function ccKeysDataProvider(): array
{
return [
['cc_number', 'cc_number_enc'],
['cc_cid', 'cc_cid_enc']
];
}

public function testGetMethodInstanceWithRealMethod()
/**
* Get method instance with real method
*
* @return void
*/
public function testGetMethodInstanceWithRealMethod(): void
{
$storeId = 2;
$method = 'real_method';
$this->info->setData('method', $method);

$this->orderMock->expects($this->once())
->method('getStoreId')
->willReturn($storeId);
$this->methodInstanceMock->expects($this->once())
->method('setInfoInstance')
->with($this->info);
$this->methodInstanceMock->expects($this->once())
->method('setStore')
->with($storeId);

$this->paymentHelperMock->expects($this->once())
->method('getMethodInstance')
Expand All @@ -111,7 +142,12 @@ public function testGetMethodInstanceWithRealMethod()
$this->info->getMethodInstance();
}

public function testGetMethodInstanceWithUnrealMethod()
/**
* Get method instance with unreal method
*
* @return void
*/
public function testGetMethodInstanceWithUnrealMethod(): void
{
$method = 'unreal_method';
$this->info->setData('method', $method);
Expand All @@ -133,15 +169,26 @@ public function testGetMethodInstanceWithUnrealMethod()
$this->info->getMethodInstance();
}

public function testGetMethodInstanceWithNoMethod()
/**
* Get method instance withot method
*
* @return void
*/
public function testGetMethodInstanceWithNoMethod(): void
{
$this->expectException('Magento\Framework\Exception\LocalizedException');
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('The payment method you requested is not available.');

$this->info->setData('method', false);
$this->info->getMethodInstance();
}

public function testGetMethodInstanceRequestedMethod()
/**
* Get method instance requested method
*
* @return void
*/
public function testGetMethodInstanceRequestedMethod(): void
{
$code = 'real_method';
$this->info->setData('method', $code);
Expand All @@ -160,40 +207,62 @@ public function testGetMethodInstanceRequestedMethod()
$this->assertSame($this->methodInstanceMock, $this->info->getMethodInstance());
}

public function testEncrypt()
/**
* Encrypt test
*
* @return void
*/
public function testEncrypt(): void
{
$data = 'data';
$encryptedData = 'd1a2t3a4';

$this->encryptorInterfaceMock->expects($this->once())->method('encrypt')->with($data)->willReturn(
$encryptedData
);
$this->encryptorInterfaceMock->expects($this->once())
->method('encrypt')
->with($data)
->willReturn($encryptedData);

$this->assertEquals($encryptedData, $this->info->encrypt($data));
}

public function testDecrypt()
/**
* Decrypt test
*
* @return void
*/
public function testDecrypt(): void
{
$data = 'data';
$encryptedData = 'd1a2t3a4';

$this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($encryptedData)->willReturn(
$data
);
$this->encryptorInterfaceMock->expects($this->once())
->method('decrypt')
->with($encryptedData)
->willReturn($data);

$this->assertEquals($data, $this->info->decrypt($encryptedData));
}

public function testSetAdditionalInformationException()
/**
* Set additional information exception
*
* @return void
*/
public function testSetAdditionalInformationException(): void
{
$this->expectException('Magento\Framework\Exception\LocalizedException');
$this->expectException(LocalizedException::class);
$this->info->setAdditionalInformation('object', new \stdClass());
}

/**
* Set additional info multiple types
*
* @dataProvider additionalInformationDataProvider
* @param mixed $key
* @param mixed $value
* @return void
*/
public function testSetAdditionalInformationMultipleTypes($key, $value = null)
public function testSetAdditionalInformationMultipleTypes($key, $value = null): void
{
$this->info->setAdditionalInformation($key, $value);
$this->assertEquals($value ? [$key => $value] : $key, $this->info->getAdditionalInformation());
Expand All @@ -204,23 +273,33 @@ public function testSetAdditionalInformationMultipleTypes($key, $value = null)
*
* @return array
*/
public function additionalInformationDataProvider()
public function additionalInformationDataProvider(): array
{
return [
[['key1' => 'data1', 'key2' => 'data2'], null],
['key', 'data']
];
}

public function testGetAdditionalInformationByKey()
/**
* Get additional info by key
*
* @return void
*/
public function testGetAdditionalInformationByKey(): void
{
$key = 'key';
$value = 'value';
$this->info->setAdditionalInformation($key, $value);
$this->assertEquals($value, $this->info->getAdditionalInformation($key));
}

public function testUnsAdditionalInformation()
/**
* Unsetter additional info
*
* @return void
*/
public function testUnsAdditionalInformation(): void
{
// set array to additional
$data = ['key1' => 'data1', 'key2' => 'data2'];
Expand All @@ -236,7 +315,12 @@ public function testUnsAdditionalInformation()
$this->assertEmpty($this->info->unsAdditionalInformation()->getAdditionalInformation());
}

public function testHasAdditionalInformation()
/**
* Has additional info
*
* @return void
*/
public function testHasAdditionalInformation(): void
{
$this->assertFalse($this->info->hasAdditionalInformation());

Expand All @@ -248,7 +332,12 @@ public function testHasAdditionalInformation()
$this->assertTrue($this->info->hasAdditionalInformation());
}

public function testInitAdditionalInformationWithUnserialize()
/**
* Init additional info with unserialize
*
* @return void
*/
public function testInitAdditionalInformationWithUnserialize(): void
{
$data = ['key1' => 'data1', 'key2' => 'data2'];
$this->info->setData('additional_information', $data);
Expand Down
Loading

0 comments on commit dab4a1a

Please sign in to comment.