TaxRuleRegistryTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 TaxRuleRegistry
  11. *
  12. */
  13. class TaxRuleRegistryTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Tax\Model\Calculation\TaxRuleRegistry
  17. */
  18. private $taxRuleRegistry;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Tax\Model\Calculation\RuleFactory
  21. */
  22. private $taxRuleModelFactoryMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Tax\Model\Calculation\Rule
  25. */
  26. private $taxRuleModelMock;
  27. const TAX_RULE_ID = 1;
  28. protected function setUp()
  29. {
  30. $objectManager = new ObjectManager($this);
  31. $this->taxRuleModelFactoryMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\RuleFactory::class)
  32. ->setMethods(['create'])
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->taxRuleRegistry = $objectManager->getObject(
  36. \Magento\Tax\Model\Calculation\TaxRuleRegistry::class,
  37. ['taxRuleModelFactory' => $this->taxRuleModelFactoryMock]
  38. );
  39. $this->taxRuleModelMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\Rule::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. }
  43. public function testRemoveTaxRule()
  44. {
  45. $this->taxRuleModelMock->expects($this->any())
  46. ->method('load')
  47. ->with(self::TAX_RULE_ID)
  48. ->will($this->returnValue($this->taxRuleModelMock));
  49. $this->taxRuleModelMock->expects($this->any())
  50. ->method('getId')
  51. ->will($this->onConsecutiveCalls(self::TAX_RULE_ID, null));
  52. $this->taxRuleModelFactoryMock->expects($this->any())
  53. ->method('create')
  54. ->will($this->returnValue($this->taxRuleModelMock));
  55. $this->taxRuleRegistry->registerTaxRule($this->taxRuleModelMock);
  56. $expected = $this->taxRuleRegistry->retrieveTaxRule(self::TAX_RULE_ID);
  57. $this->assertEquals($this->taxRuleModelMock, $expected);
  58. // Remove the tax rule
  59. $this->taxRuleRegistry->removeTaxRule(self::TAX_RULE_ID);
  60. // Verify that if the tax rule is retrieved again, an exception is thrown
  61. try {
  62. $this->taxRuleRegistry->retrieveTaxRule(self::TAX_RULE_ID);
  63. $this->fail('NoSuchEntityException was not thrown as expected');
  64. } catch (NoSuchEntityException $e) {
  65. $expectedParams = [
  66. 'fieldName' => 'taxRuleId',
  67. 'fieldValue' => self::TAX_RULE_ID,
  68. ];
  69. $this->assertEquals($expectedParams, $e->getParameters());
  70. }
  71. }
  72. }