TotalsReaderTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model\Quote;
  7. class TotalsReaderTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Quote\Model\Quote\TotalsReader
  11. */
  12. protected $model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $totalFactoryMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $collectionListMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $totalMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $quoteMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $collectorMock;
  33. protected function setUp()
  34. {
  35. $this->totalFactoryMock =
  36. $this->createPartialMock(\Magento\Quote\Model\Quote\Address\TotalFactory::class, ['create']);
  37. $this->collectionListMock = $this->createMock(\Magento\Quote\Model\Quote\TotalsCollectorList::class);
  38. $this->totalMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData']);
  39. $this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  40. $this->collectorMock =
  41. $this->createMock(\Magento\Quote\Model\Quote\Address\Total\AbstractTotal::class);
  42. $this->model = new \Magento\Quote\Model\Quote\TotalsReader(
  43. $this->totalFactoryMock,
  44. $this->collectionListMock
  45. );
  46. }
  47. public function testFetch()
  48. {
  49. $total = [];
  50. $storeId = 1;
  51. $testedTotalMock =
  52. $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData', 'getCode']);
  53. $expected = ['my_total_type' => $testedTotalMock];
  54. $data = ['code' => 'my_total_type'];
  55. $this->totalMock->expects($this->once())->method('setData')->with([])->willReturnSelf();
  56. $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  57. $this->totalFactoryMock
  58. ->expects($this->at(0))
  59. ->method('create')
  60. ->willReturn($this->totalMock);
  61. $this->totalFactoryMock->expects($this->at(1))->method('create')->willReturn($testedTotalMock);
  62. $this->collectionListMock
  63. ->expects($this->once())
  64. ->method('getCollectors')
  65. ->with($storeId)->willReturn([$this->collectorMock]);
  66. $this->collectorMock
  67. ->expects($this->once())
  68. ->method('fetch')
  69. ->with($this->quoteMock, $this->totalMock)
  70. ->willReturn($data);
  71. $testedTotalMock->expects($this->once())->method('setData')->with($data)->willReturnSelf();
  72. $testedTotalMock->expects($this->any())->method('getCode')->willReturn('my_total_type');
  73. $this->assertEquals($expected, $this->model->fetch($this->quoteMock, $total));
  74. }
  75. public function testFetchWithEmptyData()
  76. {
  77. $total = [];
  78. $storeId = 1;
  79. $this->totalMock->expects($this->once())->method('setData')->with([])->willReturnSelf();
  80. $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  81. $this->totalFactoryMock
  82. ->expects($this->once())
  83. ->method('create')
  84. ->willReturn($this->totalMock);
  85. $this->collectionListMock
  86. ->expects($this->once())
  87. ->method('getCollectors')
  88. ->with($storeId)->willReturn([$this->collectorMock]);
  89. $this->collectorMock
  90. ->expects($this->once())
  91. ->method('fetch')
  92. ->with($this->quoteMock, $this->totalMock)
  93. ->willReturn([]);
  94. $this->assertEquals([], $this->model->fetch($this->quoteMock, $total));
  95. }
  96. public function testFetchSeveralCollectors()
  97. {
  98. $total = [];
  99. $storeId = 1;
  100. $firstTotalMock =
  101. $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData', 'getCode']);
  102. $secondTotalMock =
  103. $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData', 'getCode']);
  104. $expected = ['first_total_type' => $firstTotalMock, 'second_total_type' => $secondTotalMock];
  105. $data = [['code' => 'first_total_type'], ['code' => 'second_total_type']];
  106. $this->totalMock->expects($this->once())->method('setData')->with([])->willReturnSelf();
  107. $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  108. $this->totalFactoryMock
  109. ->expects($this->at(0))
  110. ->method('create')
  111. ->willReturn($this->totalMock);
  112. $this->totalFactoryMock->expects($this->at(1))->method('create')->willReturn($firstTotalMock);
  113. $this->totalFactoryMock->expects($this->at(2))->method('create')->willReturn($secondTotalMock);
  114. $this->collectionListMock
  115. ->expects($this->once())
  116. ->method('getCollectors')
  117. ->with($storeId)->willReturn([$this->collectorMock]);
  118. $this->collectorMock
  119. ->expects($this->once())
  120. ->method('fetch')
  121. ->with($this->quoteMock, $this->totalMock)
  122. ->willReturn($data);
  123. $firstTotalMock->expects($this->once())->method('setData')->with($data[0])->willReturnSelf();
  124. $secondTotalMock->expects($this->once())->method('setData')->with($data[1])->willReturnSelf();
  125. $firstTotalMock->expects($this->any())->method('getCode')->willReturn('first_total_type');
  126. $secondTotalMock->expects($this->any())->method('getCode')->willReturn('second_total_type');
  127. $this->assertEquals($expected, $this->model->fetch($this->quoteMock, $total));
  128. }
  129. public function testConvert()
  130. {
  131. $total = [];
  132. $storeId = 1;
  133. $testedTotalMock =
  134. $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, ['setData', 'getCode']);
  135. $expected = ['my_total_type' => $testedTotalMock];
  136. $this->totalMock->expects($this->once())->method('setData')->with([])->willReturnSelf();
  137. $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
  138. $this->totalFactoryMock
  139. ->expects($this->once())
  140. ->method('create')
  141. ->willReturn($this->totalMock);
  142. $this->collectionListMock
  143. ->expects($this->once())
  144. ->method('getCollectors')
  145. ->with($storeId)->willReturn([$this->collectorMock]);
  146. $this->collectorMock
  147. ->expects($this->once())
  148. ->method('fetch')
  149. ->with($this->quoteMock, $this->totalMock)
  150. ->willReturn($testedTotalMock);
  151. $testedTotalMock->expects($this->never())->method('setData');
  152. $testedTotalMock->expects($this->any())->method('getCode')->willReturn('my_total_type');
  153. $this->assertEquals($expected, $this->model->fetch($this->quoteMock, $total));
  154. }
  155. }