Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MethodListTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class MethodListTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $methodList;
18 
22  protected $objectManager;
23 
27  private $paymentMethodList;
28 
32  private $paymentMethodInstanceFactory;
33 
38 
39  protected function setUp()
40  {
41  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
42 
43  $this->paymentMethodList = $this->getMockBuilder(\Magento\Payment\Api\PaymentMethodListInterface::class)
44  ->disableOriginalConstructor()
45  ->getMockForAbstractClass();
46 
47  $this->paymentMethodInstanceFactory = $this->getMockBuilder(
48  \Magento\Payment\Model\Method\InstanceFactory::class
49  )->disableOriginalConstructor()->getMock();
50 
51  $this->specificationFactoryMock = $this->createMock(\Magento\Payment\Model\Checks\SpecificationFactory::class);
52  $this->methodList = $this->objectManager->getObject(
53  \Magento\Payment\Model\MethodList::class,
54  [
55  'specificationFactory' => $this->specificationFactoryMock
56  ]
57  );
58 
59  $this->objectManager->setBackwardCompatibleProperty(
60  $this->methodList,
61  'paymentMethodList',
62  $this->paymentMethodList
63  );
64  $this->objectManager->setBackwardCompatibleProperty(
65  $this->methodList,
66  'paymentMethodInstanceFactory',
67  $this->paymentMethodInstanceFactory
68  );
69  }
70 
71  public function testGetAvailableMethods()
72  {
73  $storeId = 1;
74  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
75  $quoteMock->expects($this->once())->method('getStoreId')->will($this->returnValue($storeId));
76  $quoteMock->expects($this->atLeastOnce())
77  ->method('getPayment')
78  ->will($this->returnValue($this->createMock(\Magento\Quote\Model\Quote\Payment::class)));
79 
80  $methodInstanceMock = $this->createMock(\Magento\Payment\Model\Method\AbstractMethod::class);
81  $methodInstanceMock->expects($this->once())
82  ->method('isAvailable')
83  ->willReturn(true);
84 
85  $compositeMock = $this->createMock(\Magento\Payment\Model\Checks\Composite::class);
86  $compositeMock->expects($this->atLeastOnce())
87  ->method('isApplicable')
88  ->with($methodInstanceMock, $quoteMock)
89  ->will($this->returnValue(true));
90 
91  $this->specificationFactoryMock->expects($this->atLeastOnce())
92  ->method('create')
93  ->with([
98  ])
99  ->will($this->returnValue($compositeMock));
100 
101  $methodMock = $this->getMockForAbstractClass(\Magento\Payment\Api\Data\PaymentMethodInterface::class);
102  $this->paymentMethodList->expects($this->once())
103  ->method('getActiveList')
104  ->willReturn([$methodMock]);
105  $this->paymentMethodInstanceFactory->expects($this->once())
106  ->method('create')
107  ->willReturn($methodInstanceMock);
108 
109  $methodInstanceMock->expects($this->atLeastOnce())
110  ->method('setInfoInstance')
111  ->with($this->createMock(\Magento\Quote\Model\Quote\Payment::class))
112  ->will($this->returnSelf());
113 
114  $this->assertEquals([$methodInstanceMock], $this->methodList->getAvailableMethods($quoteMock));
115  }
116 }