Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TotalsReaderTest.php
Go to the documentation of this file.
1 <?php
8 
9 class TotalsReaderTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $model;
15 
19  protected $totalFactoryMock;
20 
25 
29  protected $totalMock;
30 
34  protected $quoteMock;
35 
39  protected $collectorMock;
40 
41  protected function setUp()
42  {
43  $this->totalFactoryMock =
44  $this->createPartialMock(\Magento\Quote\Model\Quote\Address\TotalFactory::class, ['create']);
45  $this->collectionListMock = $this->createMock(\Magento\Quote\Model\Quote\TotalsCollectorList::class);
46  $this->totalMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData']);
47  $this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
48  $this->collectorMock =
49  $this->createMock(\Magento\Quote\Model\Quote\Address\Total\AbstractTotal::class);
50  $this->model = new \Magento\Quote\Model\Quote\TotalsReader(
51  $this->totalFactoryMock,
52  $this->collectionListMock
53  );
54  }
55 
56  public function testFetch()
57  {
58  $total = [];
59  $storeId = 1;
60  $testedTotalMock =
61  $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData', 'getCode']);
62  $expected = ['my_total_type' => $testedTotalMock];
63  $data = ['code' => 'my_total_type'];
64  $this->totalMock->expects($this->once())->method('setData')->with([])->willReturnSelf();
65  $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
66  $this->totalFactoryMock
67  ->expects($this->at(0))
68  ->method('create')
69  ->willReturn($this->totalMock);
70  $this->totalFactoryMock->expects($this->at(1))->method('create')->willReturn($testedTotalMock);
71  $this->collectionListMock
72  ->expects($this->once())
73  ->method('getCollectors')
74  ->with($storeId)->willReturn([$this->collectorMock]);
75  $this->collectorMock
76  ->expects($this->once())
77  ->method('fetch')
78  ->with($this->quoteMock, $this->totalMock)
79  ->willReturn($data);
80  $testedTotalMock->expects($this->once())->method('setData')->with($data)->willReturnSelf();
81  $testedTotalMock->expects($this->any())->method('getCode')->willReturn('my_total_type');
82  $this->assertEquals($expected, $this->model->fetch($this->quoteMock, $total));
83  }
84 
85  public function testFetchWithEmptyData()
86  {
87  $total = [];
88  $storeId = 1;
89  $this->totalMock->expects($this->once())->method('setData')->with([])->willReturnSelf();
90  $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
91  $this->totalFactoryMock
92  ->expects($this->once())
93  ->method('create')
94  ->willReturn($this->totalMock);
95  $this->collectionListMock
96  ->expects($this->once())
97  ->method('getCollectors')
98  ->with($storeId)->willReturn([$this->collectorMock]);
99  $this->collectorMock
100  ->expects($this->once())
101  ->method('fetch')
102  ->with($this->quoteMock, $this->totalMock)
103  ->willReturn([]);
104  $this->assertEquals([], $this->model->fetch($this->quoteMock, $total));
105  }
106 
107  public function testFetchSeveralCollectors()
108  {
109  $total = [];
110  $storeId = 1;
111  $firstTotalMock =
112  $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData', 'getCode']);
113  $secondTotalMock =
114  $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData', 'getCode']);
115  $expected = ['first_total_type' => $firstTotalMock, 'second_total_type' => $secondTotalMock];
116  $data = [['code' => 'first_total_type'], ['code' => 'second_total_type']];
117  $this->totalMock->expects($this->once())->method('setData')->with([])->willReturnSelf();
118  $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
119  $this->totalFactoryMock
120  ->expects($this->at(0))
121  ->method('create')
122  ->willReturn($this->totalMock);
123  $this->totalFactoryMock->expects($this->at(1))->method('create')->willReturn($firstTotalMock);
124  $this->totalFactoryMock->expects($this->at(2))->method('create')->willReturn($secondTotalMock);
125  $this->collectionListMock
126  ->expects($this->once())
127  ->method('getCollectors')
128  ->with($storeId)->willReturn([$this->collectorMock]);
129  $this->collectorMock
130  ->expects($this->once())
131  ->method('fetch')
132  ->with($this->quoteMock, $this->totalMock)
133  ->willReturn($data);
134  $firstTotalMock->expects($this->once())->method('setData')->with($data[0])->willReturnSelf();
135  $secondTotalMock->expects($this->once())->method('setData')->with($data[1])->willReturnSelf();
136  $firstTotalMock->expects($this->any())->method('getCode')->willReturn('first_total_type');
137  $secondTotalMock->expects($this->any())->method('getCode')->willReturn('second_total_type');
138  $this->assertEquals($expected, $this->model->fetch($this->quoteMock, $total));
139  }
140 
141  public function testConvert()
142  {
143  $total = [];
144  $storeId = 1;
145  $testedTotalMock =
146  $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData', 'getCode']);
147  $expected = ['my_total_type' => $testedTotalMock];
148  $this->totalMock->expects($this->once())->method('setData')->with([])->willReturnSelf();
149  $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
150  $this->totalFactoryMock
151  ->expects($this->once())
152  ->method('create')
153  ->willReturn($this->totalMock);
154  $this->collectionListMock
155  ->expects($this->once())
156  ->method('getCollectors')
157  ->with($storeId)->willReturn([$this->collectorMock]);
158  $this->collectorMock
159  ->expects($this->once())
160  ->method('fetch')
161  ->with($this->quoteMock, $this->totalMock)
162  ->willReturn($testedTotalMock);
163  $testedTotalMock->expects($this->never())->method('setData');
164  $testedTotalMock->expects($this->any())->method('getCode')->willReturn('my_total_type');
165  $this->assertEquals($expected, $this->model->fetch($this->quoteMock, $total));
166  }
167 }