RateRegistryTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Calculation;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * Test for RateRegistry
  11. *
  12. */
  13. class RateRegistryTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Tax\Model\Calculation\RateRegistry
  17. */
  18. private $rateRegistry;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Tax\Model\Calculation\RateFactory
  21. */
  22. private $rateModelFactoryMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Tax\Model\Calculation\Rate
  25. */
  26. private $rateModelMock;
  27. const TAX_RATE_ID = 1;
  28. protected function setUp()
  29. {
  30. $objectManager = new ObjectManager($this);
  31. $this->rateModelFactoryMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\RateFactory::class)
  32. ->setMethods(['create'])
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->rateRegistry = $objectManager->getObject(
  36. \Magento\Tax\Model\Calculation\RateRegistry::class,
  37. ['taxModelRateFactory' => $this->rateModelFactoryMock]
  38. );
  39. $this->rateModelMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\Rate::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. }
  43. public function testRegisterTaxRate()
  44. {
  45. $this->rateModelMock->expects($this->any())
  46. ->method('getId')
  47. ->will($this->returnValue(self::TAX_RATE_ID));
  48. $this->rateRegistry->registerTaxRate($this->rateModelMock);
  49. $this->assertEquals($this->rateModelMock, $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID));
  50. }
  51. public function testRetrieveTaxRate()
  52. {
  53. $this->rateModelMock->expects($this->once())
  54. ->method('load')
  55. ->with(self::TAX_RATE_ID)
  56. ->will($this->returnValue($this->rateModelMock));
  57. $this->rateModelMock->expects($this->any())
  58. ->method('getId')
  59. ->will($this->returnValue(self::TAX_RATE_ID));
  60. $this->rateModelFactoryMock->expects($this->once())
  61. ->method('create')
  62. ->will($this->returnValue($this->rateModelMock));
  63. $actual = $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
  64. $this->assertEquals($this->rateModelMock, $actual);
  65. $actualCached = $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
  66. $this->assertSame($actual, $actualCached);
  67. }
  68. /**
  69. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  70. */
  71. public function testRetrieveException()
  72. {
  73. $this->rateModelMock->expects($this->once())
  74. ->method('load')
  75. ->with(self::TAX_RATE_ID)
  76. ->will($this->returnValue($this->rateModelMock));
  77. $this->rateModelMock->expects($this->any())
  78. ->method('getId')
  79. ->will($this->returnValue(null));
  80. $this->rateModelFactoryMock->expects($this->once())
  81. ->method('create')
  82. ->will($this->returnValue($this->rateModelMock));
  83. $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
  84. }
  85. public function testRemoveTaxRate()
  86. {
  87. $this->rateModelMock->expects($this->any())
  88. ->method('load')
  89. ->with(self::TAX_RATE_ID)
  90. ->will($this->returnValue($this->rateModelMock));
  91. // The second time this is called, want it to return null indicating a new object
  92. $this->rateModelMock->expects($this->any())
  93. ->method('getId')
  94. ->will($this->onConsecutiveCalls(self::TAX_RATE_ID, null));
  95. $this->rateModelFactoryMock->expects($this->any())
  96. ->method('create')
  97. ->will($this->returnValue($this->rateModelMock));
  98. $actual = $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
  99. $this->assertEquals($this->rateModelMock, $actual);
  100. // Remove the rate
  101. $this->rateRegistry->removeTaxRate(self::TAX_RATE_ID);
  102. // Verify that if the rate is retrieved again, an exception is thrown
  103. try {
  104. $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
  105. $this->fail('NoSuchEntityException was not thrown as expected');
  106. } catch (NoSuchEntityException $e) {
  107. $expectedParams = [
  108. 'fieldName' => 'taxRateId',
  109. 'fieldValue' => self::TAX_RATE_ID,
  110. ];
  111. $this->assertEquals($expectedParams, $e->getParameters());
  112. }
  113. }
  114. }