TaxRateManagementTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. class TaxRateManagementTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Tax\Model\TaxRateManagement
  11. */
  12. protected $model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $searchCriteriaBuilderMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $filterBuilderMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $taxRuleRepositoryMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $taxRateRepositoryMock;
  29. protected function setUp()
  30. {
  31. $this->filterBuilderMock = $this->createMock(\Magento\Framework\Api\FilterBuilder::class);
  32. $this->taxRuleRepositoryMock = $this->createMock(\Magento\Tax\Api\TaxRuleRepositoryInterface::class);
  33. $this->taxRateRepositoryMock = $this->createMock(\Magento\Tax\Api\TaxRateRepositoryInterface::class);
  34. $this->searchCriteriaBuilderMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaBuilder::class);
  35. $this->model = new \Magento\Tax\Model\TaxRateManagement(
  36. $this->taxRuleRepositoryMock,
  37. $this->taxRateRepositoryMock,
  38. $this->filterBuilderMock,
  39. $this->searchCriteriaBuilderMock
  40. );
  41. }
  42. public function testGetRatesByCustomerAndProductTaxClassId()
  43. {
  44. $customerTaxClassId = 4;
  45. $productTaxClassId = 42;
  46. $rateIds = [10];
  47. $productFilterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
  48. $customerFilterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
  49. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  50. $searchResultsMock = $this->createMock(\Magento\Tax\Api\Data\TaxRuleSearchResultsInterface::class);
  51. $taxRuleMock = $this->createMock(\Magento\Tax\Api\Data\TaxRuleInterface::class);
  52. $taxRateMock = $this->createMock(\Magento\Tax\Api\Data\TaxRateInterface::class);
  53. $this->filterBuilderMock->expects($this->exactly(2))->method('setField')->withConsecutive(
  54. ['customer_tax_class_ids'],
  55. ['product_tax_class_ids']
  56. )->willReturnSelf();
  57. $this->filterBuilderMock->expects($this->exactly(2))->method('setValue')->withConsecutive(
  58. [$this->equalTo([$customerTaxClassId])],
  59. [$this->equalTo([$productTaxClassId])]
  60. )->willReturnSelf();
  61. $this->filterBuilderMock->expects($this->exactly(2))->method('create')->willReturnOnConsecutiveCalls(
  62. $customerFilterMock,
  63. $productFilterMock
  64. );
  65. $this->searchCriteriaBuilderMock->expects($this->exactly(2))->method('addFilters')->withConsecutive(
  66. [[$customerFilterMock]],
  67. [[$productFilterMock]]
  68. );
  69. $this->searchCriteriaBuilderMock->expects($this->once())->method('create')->willReturn($searchCriteriaMock);
  70. $this->taxRuleRepositoryMock->expects($this->once())->method('getList')->with($searchCriteriaMock)
  71. ->willReturn($searchResultsMock);
  72. $searchResultsMock->expects($this->once())->method('getItems')->willReturn([$taxRuleMock]);
  73. $taxRuleMock->expects($this->once())->method('getTaxRateIds')->willReturn($rateIds);
  74. $this->taxRateRepositoryMock->expects($this->once())->method('get')->with($rateIds[0])
  75. ->willReturn($taxRateMock);
  76. $this->assertEquals(
  77. [$taxRateMock],
  78. $this->model->getRatesByCustomerAndProductTaxClassId($customerTaxClassId, $productTaxClassId)
  79. );
  80. }
  81. }