Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RateRegistryTest.php
Go to the documentation of this file.
1 <?php
8 
11 
16 class RateRegistryTest extends \PHPUnit\Framework\TestCase
17 {
21  private $rateRegistry;
22 
26  private $rateModelFactoryMock;
27 
31  private $rateModelMock;
32 
33  const TAX_RATE_ID = 1;
34 
35  protected function setUp()
36  {
37  $objectManager = new ObjectManager($this);
38  $this->rateModelFactoryMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\RateFactory::class)
39  ->setMethods(['create'])
40  ->disableOriginalConstructor()
41  ->getMock();
42  $this->rateRegistry = $objectManager->getObject(
43  \Magento\Tax\Model\Calculation\RateRegistry::class,
44  ['taxModelRateFactory' => $this->rateModelFactoryMock]
45  );
46  $this->rateModelMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\Rate::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  }
50 
51  public function testRegisterTaxRate()
52  {
53  $this->rateModelMock->expects($this->any())
54  ->method('getId')
55  ->will($this->returnValue(self::TAX_RATE_ID));
56  $this->rateRegistry->registerTaxRate($this->rateModelMock);
57  $this->assertEquals($this->rateModelMock, $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID));
58  }
59 
60  public function testRetrieveTaxRate()
61  {
62  $this->rateModelMock->expects($this->once())
63  ->method('load')
64  ->with(self::TAX_RATE_ID)
65  ->will($this->returnValue($this->rateModelMock));
66  $this->rateModelMock->expects($this->any())
67  ->method('getId')
68  ->will($this->returnValue(self::TAX_RATE_ID));
69  $this->rateModelFactoryMock->expects($this->once())
70  ->method('create')
71  ->will($this->returnValue($this->rateModelMock));
72 
73  $actual = $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
74  $this->assertEquals($this->rateModelMock, $actual);
75 
76  $actualCached = $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
77  $this->assertSame($actual, $actualCached);
78  }
79 
83  public function testRetrieveException()
84  {
85  $this->rateModelMock->expects($this->once())
86  ->method('load')
87  ->with(self::TAX_RATE_ID)
88  ->will($this->returnValue($this->rateModelMock));
89  $this->rateModelMock->expects($this->any())
90  ->method('getId')
91  ->will($this->returnValue(null));
92  $this->rateModelFactoryMock->expects($this->once())
93  ->method('create')
94  ->will($this->returnValue($this->rateModelMock));
95  $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
96  }
97 
98  public function testRemoveTaxRate()
99  {
100  $this->rateModelMock->expects($this->any())
101  ->method('load')
102  ->with(self::TAX_RATE_ID)
103  ->will($this->returnValue($this->rateModelMock));
104 
105  // The second time this is called, want it to return null indicating a new object
106  $this->rateModelMock->expects($this->any())
107  ->method('getId')
108  ->will($this->onConsecutiveCalls(self::TAX_RATE_ID, null));
109 
110  $this->rateModelFactoryMock->expects($this->any())
111  ->method('create')
112  ->will($this->returnValue($this->rateModelMock));
113 
114  $actual = $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
115  $this->assertEquals($this->rateModelMock, $actual);
116 
117  // Remove the rate
118  $this->rateRegistry->removeTaxRate(self::TAX_RATE_ID);
119 
120  // Verify that if the rate is retrieved again, an exception is thrown
121  try {
122  $this->rateRegistry->retrieveTaxRate(self::TAX_RATE_ID);
123  $this->fail('NoSuchEntityException was not thrown as expected');
124  } catch (NoSuchEntityException $e) {
125  $expectedParams = [
126  'fieldName' => 'taxRateId',
127  'fieldValue' => self::TAX_RATE_ID,
128  ];
129  $this->assertEquals($expectedParams, $e->getParameters());
130  }
131  }
132 }
$objectManager
Definition: bootstrap.php:17