TaxRateCollectionTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Model;
  7. use \Magento\Tax\Model\TaxRateCollection;
  8. class TaxRateCollectionTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var TaxRateCollection
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $entityFactoryMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $filterBuilderMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $searchCriteriaBuilderMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $sortOrderBuilderMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $rateServiceMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $rateConverterMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $searchCriteriaMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $searchResultsMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $taxRateMock;
  50. protected function setUp()
  51. {
  52. $this->entityFactoryMock = $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class);
  53. $this->filterBuilderMock = $this->createMock(\Magento\Framework\Api\FilterBuilder::class);
  54. $this->searchCriteriaBuilderMock =
  55. $this->createMock(\Magento\Framework\Api\SearchCriteriaBuilder::class);
  56. $this->sortOrderBuilderMock = $this->createMock(\Magento\Framework\Api\SortOrderBuilder::class);
  57. $this->rateServiceMock = $this->createPartialMock(\Magento\Tax\Api\TaxRateRepositoryInterface::class, [
  58. 'save',
  59. 'get',
  60. 'deleteById',
  61. 'getList',
  62. 'delete',
  63. '__wakeup'
  64. ]);
  65. $this->rateConverterMock = $this->createMock(\Magento\Tax\Model\Calculation\Rate\Converter::class);
  66. $this->searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class);
  67. $this->searchResultsMock = $this->createMock(\Magento\Tax\Api\Data\TaxRateSearchResultsInterface::class);
  68. $this->taxRateMock = $this->createMock(\Magento\Tax\Model\Calculation\Rate::class);
  69. $this->searchCriteriaBuilderMock->expects($this->any())
  70. ->method('create')
  71. ->willReturn($this->searchCriteriaMock);
  72. $this->model = new TaxRateCollection(
  73. $this->entityFactoryMock,
  74. $this->filterBuilderMock,
  75. $this->searchCriteriaBuilderMock,
  76. $this->sortOrderBuilderMock,
  77. $this->rateServiceMock,
  78. $this->rateConverterMock
  79. );
  80. }
  81. public function testLoadData()
  82. {
  83. $this->rateServiceMock->expects($this->once())
  84. ->method('getList')
  85. ->with($this->searchCriteriaMock)
  86. ->willReturn($this->searchResultsMock);
  87. $this->searchResultsMock->expects($this->once())->method('getTotalCount')->willReturn(123);
  88. $this->searchResultsMock->expects($this->once())->method('getItems')->willReturn([$this->taxRateMock]);
  89. $this->taxRateMock->expects($this->once())->method('getId')->willReturn(33);
  90. $this->taxRateMock->expects($this->once())->method('getCode')->willReturn(44);
  91. $this->taxRateMock->expects($this->once())->method('getTaxCountryId')->willReturn('CountryId');
  92. $this->taxRateMock->expects($this->once())->method('getTaxRegionId')->willReturn(55);
  93. $this->taxRateMock->expects($this->once())->method('getRegionName')->willReturn('Region Name');
  94. $this->taxRateMock->expects($this->once())->method('getTaxPostcode')->willReturn('Post Code');
  95. $this->taxRateMock->expects($this->once())->method('getRate')->willReturn(1.85);
  96. $this->rateConverterMock->expects($this->once())
  97. ->method('createTitleArrayFromServiceObject')
  98. ->with($this->taxRateMock)
  99. ->willReturn([]);
  100. $this->taxRateMock->expects($this->once())->method('getZipTo')->willReturn(null);
  101. $this->taxRateMock->expects($this->never())->method('getZipFrom');
  102. $this->model->loadData();
  103. }
  104. public function testCreateTaxRateCollectionItem()
  105. {
  106. $this->rateServiceMock->expects($this->once())
  107. ->method('getList')
  108. ->with($this->searchCriteriaMock)
  109. ->willReturn($this->searchResultsMock);
  110. $this->searchResultsMock->expects($this->once())->method('getTotalCount')->willReturn(123);
  111. $this->searchResultsMock->expects($this->once())->method('getItems')->willReturn([$this->taxRateMock]);
  112. $this->taxRateMock->expects($this->once())->method('getId')->willReturn(33);
  113. $this->taxRateMock->expects($this->once())->method('getCode')->willReturn(44);
  114. $this->taxRateMock->expects($this->once())->method('getTaxCountryId')->willReturn('CountryId');
  115. $this->taxRateMock->expects($this->once())->method('getTaxRegionId')->willReturn(55);
  116. $this->taxRateMock->expects($this->once())->method('getRegionName')->willReturn('Region Name');
  117. $this->taxRateMock->expects($this->once())->method('getTaxPostcode')->willReturn('Post Code');
  118. $this->taxRateMock->expects($this->once())->method('getRate')->willReturn(1.85);
  119. $this->rateConverterMock->expects($this->once())
  120. ->method('createTitleArrayFromServiceObject')
  121. ->with($this->taxRateMock)
  122. ->willReturn([]);
  123. $this->taxRateMock->expects($this->exactly(2))->method('getZipTo')->willReturn(1);
  124. $this->taxRateMock->expects($this->exactly(2))->method('getZipFrom')->willReturn(200);
  125. $this->model->loadData();
  126. }
  127. }