MethodListTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Model;
  7. use Magento\Payment\Model\MethodList;
  8. use Magento\Payment\Model\Method\AbstractMethod;
  9. class MethodListTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var MethodList
  13. */
  14. protected $methodList;
  15. /**
  16. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  17. */
  18. protected $objectManager;
  19. /**
  20. * @var \Magento\Payment\Api\PaymentMethodListInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $paymentMethodList;
  23. /**
  24. * @var \Magento\Payment\Model\Method\InstanceFactory|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $paymentMethodInstanceFactory;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $specificationFactoryMock;
  31. protected function setUp()
  32. {
  33. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  34. $this->paymentMethodList = $this->getMockBuilder(\Magento\Payment\Api\PaymentMethodListInterface::class)
  35. ->disableOriginalConstructor()
  36. ->getMockForAbstractClass();
  37. $this->paymentMethodInstanceFactory = $this->getMockBuilder(
  38. \Magento\Payment\Model\Method\InstanceFactory::class
  39. )->disableOriginalConstructor()->getMock();
  40. $this->specificationFactoryMock = $this->createMock(\Magento\Payment\Model\Checks\SpecificationFactory::class);
  41. $this->methodList = $this->objectManager->getObject(
  42. \Magento\Payment\Model\MethodList::class,
  43. [
  44. 'specificationFactory' => $this->specificationFactoryMock
  45. ]
  46. );
  47. $this->objectManager->setBackwardCompatibleProperty(
  48. $this->methodList,
  49. 'paymentMethodList',
  50. $this->paymentMethodList
  51. );
  52. $this->objectManager->setBackwardCompatibleProperty(
  53. $this->methodList,
  54. 'paymentMethodInstanceFactory',
  55. $this->paymentMethodInstanceFactory
  56. );
  57. }
  58. public function testGetAvailableMethods()
  59. {
  60. $storeId = 1;
  61. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  62. $quoteMock->expects($this->once())->method('getStoreId')->will($this->returnValue($storeId));
  63. $quoteMock->expects($this->atLeastOnce())
  64. ->method('getPayment')
  65. ->will($this->returnValue($this->createMock(\Magento\Quote\Model\Quote\Payment::class)));
  66. $methodInstanceMock = $this->createMock(\Magento\Payment\Model\Method\AbstractMethod::class);
  67. $methodInstanceMock->expects($this->once())
  68. ->method('isAvailable')
  69. ->willReturn(true);
  70. $compositeMock = $this->createMock(\Magento\Payment\Model\Checks\Composite::class);
  71. $compositeMock->expects($this->atLeastOnce())
  72. ->method('isApplicable')
  73. ->with($methodInstanceMock, $quoteMock)
  74. ->will($this->returnValue(true));
  75. $this->specificationFactoryMock->expects($this->atLeastOnce())
  76. ->method('create')
  77. ->with([
  78. AbstractMethod::CHECK_USE_CHECKOUT,
  79. AbstractMethod::CHECK_USE_FOR_COUNTRY,
  80. AbstractMethod::CHECK_USE_FOR_CURRENCY,
  81. AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX
  82. ])
  83. ->will($this->returnValue($compositeMock));
  84. $methodMock = $this->getMockForAbstractClass(\Magento\Payment\Api\Data\PaymentMethodInterface::class);
  85. $this->paymentMethodList->expects($this->once())
  86. ->method('getActiveList')
  87. ->willReturn([$methodMock]);
  88. $this->paymentMethodInstanceFactory->expects($this->once())
  89. ->method('create')
  90. ->willReturn($methodInstanceMock);
  91. $methodInstanceMock->expects($this->atLeastOnce())
  92. ->method('setInfoInstance')
  93. ->with($this->createMock(\Magento\Quote\Model\Quote\Payment::class))
  94. ->will($this->returnSelf());
  95. $this->assertEquals([$methodInstanceMock], $this->methodList->getAvailableMethods($quoteMock));
  96. }
  97. }