Skip to content

Commit 85a6c71

Browse files
committed
- Fixed Unit tests
- Refactored PaymentToken to use data array instead of own variable.
1 parent 7f4dbe0 commit 85a6c71

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

app/code/Magento/Vault/Api/Data/PaymentTokenFactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ interface PaymentTokenFactoryInterface
2424
* @param $type string
2525
* @return PaymentTokenInterface
2626
*/
27-
public function create($type);
27+
public function create($type = null);
2828
}

app/code/Magento/Vault/Model/PaymentToken.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,6 @@ class PaymentToken extends AbstractModel implements PaymentTokenInterface
2020
*/
2121
protected $_eventPrefix = 'vault_payment_token';
2222

23-
/**
24-
* @param string $type
25-
* @param \Magento\Framework\Model\Context $context
26-
* @param \Magento\Framework\Registry $registry
27-
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
28-
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
29-
* @param array $data
30-
*/
31-
public function __construct(
32-
$type,
33-
\Magento\Framework\Model\Context $context,
34-
\Magento\Framework\Registry $registry,
35-
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
36-
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
37-
array $data = []
38-
) {
39-
$this->setType($type);
40-
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
41-
}
42-
4323
/**
4424
* @inheritdoc
4525
*/

app/code/Magento/Vault/Model/PaymentTokenFactory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
namespace Magento\Vault\Model;
88

9+
use Magento\Framework\ObjectManagerInterface;
910
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
11+
use Magento\Vault\Api\Data\PaymentTokenInterface;
1012

1113
/**
1214
* PaymentTokenFactory class
@@ -35,12 +37,12 @@ public function __construct(ObjectManagerInterface $objectManager, $tokenTypes =
3537
* @param $type string
3638
* @return PaymentTokenInterface
3739
*/
38-
public function create($type)
40+
public function create($type = null)
3941
{
40-
if (!in_array($type, $this->tokenTypes, true)) {
42+
if ($type !== null && !in_array($type, $this->tokenTypes, true)) {
4143
throw new \LogicException('There is no such payment token type: ' . $type);
4244
}
4345

44-
return $this->objectManager->create(PaymentTokenInterface::class, $type);
46+
return $this->objectManager->create(PaymentTokenInterface::class, ['data' => [PaymentTokenInterface::TYPE => $type]]);
4547
}
4648
}

app/code/Magento/Vault/Test/Unit/Model/AccountPaymentTokenFactoryTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\ObjectManagerInterface;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1010
use Magento\Vault\Api\Data\PaymentTokenInterface;
11+
use Magento\Vault\Model\PaymentTokenFactory;
1112
use Magento\Vault\Model\AccountPaymentTokenFactory;
1213
use Magento\Vault\Model\PaymentToken;
1314
use PHPUnit_Framework_MockObject_MockObject as MockObject;
@@ -36,10 +37,16 @@ protected function setUp()
3637
{
3738
$objectManager = new ObjectManager($this);
3839

39-
$this->paymentToken = $objectManager->getObject(PaymentToken::class);
40+
$tokenTypes = array(
41+
'account' => \Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT,
42+
'credit_card' => \Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD
43+
);
4044

45+
$this->paymentToken = $objectManager->getObject(PaymentToken::class);
4146
$this->objectManager = $this->getMock(ObjectManagerInterface::class);
42-
$this->factory = new AccountPaymentTokenFactory($this->objectManager);
47+
48+
$this->paymentTokenFactory = new PaymentTokenFactory($this->objectManager, $tokenTypes);
49+
$this->factory = new AccountPaymentTokenFactory($this->objectManager, $this->paymentTokenFactory);
4350
}
4451

4552
/**
@@ -51,6 +58,8 @@ public function testCreate()
5158
->method('create')
5259
->willReturn($this->paymentToken);
5360

61+
$this->paymentToken->setType(\Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT);
62+
5463
/** @var PaymentTokenInterface $paymentToken */
5564
$paymentToken = $this->factory->create();
5665
static::assertInstanceOf(PaymentTokenInterface::class, $paymentToken);

app/code/Magento/Vault/Test/Unit/Model/CreditCardTokenFactoryTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\ObjectManagerInterface;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1010
use Magento\Vault\Api\Data\PaymentTokenInterface;
11+
use Magento\Vault\Model\PaymentTokenFactory;
1112
use Magento\Vault\Model\CreditCardTokenFactory;
1213
use Magento\Vault\Model\PaymentToken;
1314
use PHPUnit_Framework_MockObject_MockObject as MockObject;
@@ -36,10 +37,16 @@ protected function setUp()
3637
{
3738
$objectManager = new ObjectManager($this);
3839

39-
$this->paymentToken = $objectManager->getObject(PaymentToken::class);
40+
$tokenTypes = array(
41+
'account' => \Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT,
42+
'credit_card' => \Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD
43+
);
4044

45+
$this->paymentToken = $objectManager->getObject(PaymentToken::class);
4146
$this->objectManager = $this->getMock(ObjectManagerInterface::class);
42-
$this->factory = new CreditCardTokenFactory($this->objectManager);
47+
48+
$this->paymentTokenFactory = new PaymentTokenFactory($this->objectManager, $tokenTypes);
49+
$this->factory = new CreditCardTokenFactory($this->objectManager, $this->paymentTokenFactory);
4350
}
4451

4552
/**
@@ -51,6 +58,8 @@ public function testCreate()
5158
->method('create')
5259
->willReturn($this->paymentToken);
5360

61+
$this->paymentToken->setType(\Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD);
62+
5463
/** @var PaymentTokenInterface $paymentToken */
5564
$paymentToken = $this->factory->create();
5665
static::assertInstanceOf(PaymentTokenInterface::class, $paymentToken);

0 commit comments

Comments
 (0)