CustomerTokenManagementTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Test\Unit\Model;
  7. use Magento\Customer\Model\Session;
  8. use Magento\Vault\Api\Data\PaymentTokenInterface;
  9. use Magento\Vault\Model\CustomerTokenManagement;
  10. use Magento\Vault\Model\PaymentTokenManagement;
  11. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  12. class CustomerTokenManagementTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var PaymentTokenManagement|MockObject
  16. */
  17. private $paymentTokenManagement;
  18. /**
  19. * @var Session|MockObject
  20. */
  21. private $customerSession;
  22. /**
  23. * @var CustomerTokenManagement
  24. */
  25. private $tokenManagement;
  26. protected function setUp()
  27. {
  28. $this->paymentTokenManagement = $this->getMockBuilder(PaymentTokenManagement::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->customerSession = $this->getMockBuilder(Session::class)
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->tokenManagement = new CustomerTokenManagement(
  35. $this->paymentTokenManagement,
  36. $this->customerSession
  37. );
  38. }
  39. /**
  40. * @param int|null $customerId
  41. * @param bool $isLoggedCustomer
  42. * @return void
  43. * @dataProvider getCustomerSessionTokensNegativeDataProvider
  44. */
  45. public function testGetCustomerSessionTokensNegative($customerId, bool $isLoggedCustomer)
  46. {
  47. $this->customerSession->method('getCustomerId')->willReturn($customerId);
  48. $this->customerSession->method('isLoggedIn')->willReturn($isLoggedCustomer);
  49. $this->paymentTokenManagement->expects(static::never())->method('getVisibleAvailableTokens');
  50. static::assertEquals([], $this->tokenManagement->getCustomerSessionTokens());
  51. }
  52. /**
  53. * @return array
  54. */
  55. public function getCustomerSessionTokensNegativeDataProvider()
  56. {
  57. return [
  58. 'not registered customer' => [null, false],
  59. 'not logged in customer' => [1, false],
  60. ];
  61. }
  62. public function testGetCustomerSessionTokens()
  63. {
  64. $customerId = 1;
  65. $token = $this->createMock(PaymentTokenInterface::class);
  66. $expectation = [$token];
  67. $this->customerSession->expects(static::once())
  68. ->method('getCustomerId')
  69. ->willReturn($customerId);
  70. $this->customerSession->expects(static::once())
  71. ->method('isLoggedIn')
  72. ->willReturn(true);
  73. $this->paymentTokenManagement->expects(static::once())
  74. ->method('getVisibleAvailableTokens')
  75. ->with($customerId)
  76. ->willReturn($expectation);
  77. static::assertEquals(
  78. $expectation,
  79. $this->tokenManagement->getCustomerSessionTokens()
  80. );
  81. }
  82. }