Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CatalogRulePriceTest.php
Go to the documentation of this file.
1 <?php
8 
11 
17 class CatalogRulePriceTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $object;
23 
27  protected $saleableItemMock;
28 
32  protected $dataTimeMock;
33 
37  protected $storeManagerMock;
38 
43 
47  protected $priceInfoMock;
48 
53 
58 
62  protected $coreWebsiteMock;
63 
67  protected $coreStoreMock;
68 
72  protected $calculator;
73 
77  protected $priceCurrencyMock;
78 
82  protected function setUp()
83  {
84  $this->saleableItemMock = $this->createPartialMock(
85  \Magento\Catalog\Model\Product::class,
86  ['getId', '__wakeup', 'getPriceInfo', 'hasData', 'getData']
87  );
88  $this->dataTimeMock = $this->getMockForAbstractClass(
89  \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class,
90  [],
91  '',
92  false,
93  true,
94  true,
95  []
96  );
97 
98  $this->coreStoreMock = $this->createMock(\Magento\Store\Model\Store::class);
99  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
100  $this->storeManagerMock->expects($this->any())
101  ->method('getStore')
102  ->will($this->returnValue($this->coreStoreMock));
103 
104  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
105  $this->priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo::class)
106  ->setMethods(['getAdjustments'])
107  ->disableOriginalConstructor()
108  ->getMock();
109  $this->catalogRuleResourceFactoryMock = $this->createPartialMock(
110  \Magento\CatalogRule\Model\ResourceModel\RuleFactory::class,
111  ['create']
112  );
113  $this->catalogRuleResourceMock = $this->createMock(\Magento\CatalogRule\Model\ResourceModel\Rule::class);
114 
115  $this->coreWebsiteMock = $this->createMock(\Magento\Store\Model\Website::class);
116 
117  $this->priceInfoMock->expects($this->any())
118  ->method('getAdjustments')
119  ->will($this->returnValue([]));
120  $this->saleableItemMock->expects($this->any())
121  ->method('getPriceInfo')
122  ->will($this->returnValue($this->priceInfoMock));
123 
124  $this->catalogRuleResourceFactoryMock->expects($this->any())
125  ->method('create')
126  ->will($this->returnValue($this->catalogRuleResourceMock));
127 
128  $this->calculator = $this->getMockBuilder(\Magento\Framework\Pricing\Adjustment\Calculator::class)
129  ->disableOriginalConstructor()
130  ->getMock();
131  $qty = 1;
132 
133  $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
134 
135  $this->object = new CatalogRulePrice(
136  $this->saleableItemMock,
137  $qty,
138  $this->calculator,
139  $this->priceCurrencyMock,
140  $this->dataTimeMock,
141  $this->storeManagerMock,
142  $this->customerSessionMock,
143  $this->catalogRuleResourceFactoryMock
144  );
145 
146  (new ObjectManager($this))->setBackwardCompatibleProperty(
147  $this->object,
148  'ruleResource',
149  $this->catalogRuleResourceMock
150  );
151  }
152 
156  public function testGetValue()
157  {
158  $coreStoreId = 1;
159  $coreWebsiteId = 1;
160  $productId = 1;
161  $customerGroupId = 1;
162  $dateTime = time();
163 
164  $catalogRulePrice = 55.12;
165  $convertedPrice = 45.34;
166 
167  $this->coreStoreMock->expects($this->once())
168  ->method('getId')
169  ->will($this->returnValue($coreStoreId));
170  $this->coreStoreMock->expects($this->once())
171  ->method('getWebsiteId')
172  ->will($this->returnValue($coreWebsiteId));
173  $this->dataTimeMock->expects($this->once())
174  ->method('scopeDate')
175  ->with($this->equalTo($coreStoreId))
176  ->will($this->returnValue($dateTime));
177  $this->customerSessionMock->expects($this->once())
178  ->method('getCustomerGroupId')
179  ->will($this->returnValue($customerGroupId));
180  $this->catalogRuleResourceMock->expects($this->once())
181  ->method('getRulePrice')
182  ->will($this->returnValue($catalogRulePrice));
183  $this->saleableItemMock->expects($this->any())
184  ->method('getId')
185  ->will($this->returnValue($productId));
186  $this->priceCurrencyMock->expects($this->any())
187  ->method('convertAndRound')
188  ->with($catalogRulePrice)
189  ->will($this->returnValue($convertedPrice));
190 
191  $this->assertEquals($convertedPrice, $this->object->getValue());
192  }
193 
194  public function testGetValueFromData()
195  {
196  $catalogRulePrice = 7.1;
197  $convertedPrice = 5.84;
198 
199  $this->priceCurrencyMock->expects($this->any())
200  ->method('convertAndRound')
201  ->with($catalogRulePrice)
202  ->will($this->returnValue($convertedPrice));
203 
204  $this->saleableItemMock->expects($this->once())->method('hasData')
205  ->with('catalog_rule_price')->willReturn(true);
206  $this->saleableItemMock->expects($this->once())->method('getData')
207  ->with('catalog_rule_price')->willReturn($catalogRulePrice);
208 
209  $this->assertEquals($convertedPrice, $this->object->getValue());
210  }
211 
212  public function testGetAmountNoBaseAmount()
213  {
214  $this->catalogRuleResourceMock->expects($this->once())
215  ->method('getRulePrice')
216  ->will($this->returnValue(false));
217 
218  $result = $this->object->getValue();
219  $this->assertFalse($result);
220  }
221 }
$dateTime