Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TaxRuleRegistryTest.php
Go to the documentation of this file.
1 <?php
8 
11 
16 class TaxRuleRegistryTest extends \PHPUnit\Framework\TestCase
17 {
21  private $taxRuleRegistry;
22 
26  private $taxRuleModelFactoryMock;
27 
31  private $taxRuleModelMock;
32 
33  const TAX_RULE_ID = 1;
34 
35  protected function setUp()
36  {
37  $objectManager = new ObjectManager($this);
38  $this->taxRuleModelFactoryMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\RuleFactory::class)
39  ->setMethods(['create'])
40  ->disableOriginalConstructor()
41  ->getMock();
42  $this->taxRuleRegistry = $objectManager->getObject(
43  \Magento\Tax\Model\Calculation\TaxRuleRegistry::class,
44  ['taxRuleModelFactory' => $this->taxRuleModelFactoryMock]
45  );
46  $this->taxRuleModelMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\Rule::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  }
50 
51  public function testRemoveTaxRule()
52  {
53  $this->taxRuleModelMock->expects($this->any())
54  ->method('load')
55  ->with(self::TAX_RULE_ID)
56  ->will($this->returnValue($this->taxRuleModelMock));
57 
58  $this->taxRuleModelMock->expects($this->any())
59  ->method('getId')
60  ->will($this->onConsecutiveCalls(self::TAX_RULE_ID, null));
61 
62  $this->taxRuleModelFactoryMock->expects($this->any())
63  ->method('create')
64  ->will($this->returnValue($this->taxRuleModelMock));
65  $this->taxRuleRegistry->registerTaxRule($this->taxRuleModelMock);
66  $expected = $this->taxRuleRegistry->retrieveTaxRule(self::TAX_RULE_ID);
67  $this->assertEquals($this->taxRuleModelMock, $expected);
68 
69  // Remove the tax rule
70  $this->taxRuleRegistry->removeTaxRule(self::TAX_RULE_ID);
71 
72  // Verify that if the tax rule is retrieved again, an exception is thrown
73  try {
74  $this->taxRuleRegistry->retrieveTaxRule(self::TAX_RULE_ID);
75  $this->fail('NoSuchEntityException was not thrown as expected');
76  } catch (NoSuchEntityException $e) {
77  $expectedParams = [
78  'fieldName' => 'taxRuleId',
79  'fieldValue' => self::TAX_RULE_ID,
80  ];
81  $this->assertEquals($expectedParams, $e->getParameters());
82  }
83  }
84 }
$objectManager
Definition: bootstrap.php:17