Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductPriceTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Model;
7 
12 
20 class ProductPriceTest extends \PHPUnit\Framework\TestCase
21 {
25  protected $_model;
26 
30  private $productRepository;
31 
35  protected function setUp()
36  {
37  $this->_model = Bootstrap::getObjectManager()->create(Product::class);
38  $this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
39  }
40 
44  public function testGetPrice()
45  {
46  $this->assertEmpty($this->_model->getPrice());
47  $this->_model->setPrice(10.0);
48  $this->assertEquals(10.0, $this->_model->getPrice());
49  }
50 
54  public function testGetPriceModel()
55  {
56  $default = $this->_model->getPriceModel();
57  $this->assertInstanceOf(\Magento\Catalog\Model\Product\Type\Price::class, $default);
58  $this->assertSame($default, $this->_model->getPriceModel());
59  }
60 
64  public function testGetTierPrice()
65  {
66  $this->assertEquals([], $this->_model->getTierPrice());
67  }
68 
72  public function testGetTierPriceCount()
73  {
74  $this->assertEquals(0, $this->_model->getTierPriceCount());
75  }
76 
80  public function testGetFormatedPrice()
81  {
82  $this->assertEquals('<span class="price">$0.00</span>', $this->_model->getFormatedPrice());
83  }
84 
88  public function testSetGetFinalPrice()
89  {
90  $this->assertEquals(0, $this->_model->getFinalPrice());
91  $this->_model->setPrice(10);
92  $this->_model->setFinalPrice(10);
93  $this->assertEquals(10, $this->_model->getFinalPrice());
94  }
95 
101  public function testGetMinPrice(): void
102  {
103  $product = $this->productRepository->get('simple');
104  $collection = Bootstrap::getObjectManager()->create(Collection::class);
105  $collection->addIdFilter($product->getId());
106  $collection->addPriceData();
107  $collection->load();
109  $product = $collection->getFirstItem();
110  $this->assertEquals(323, $product->getData('min_price'));
111  }
112 
117  public function testGetMinPriceForComposite(): void
118  {
119  $confProduct = $this->productRepository->get('configurable');
120  $collection = Bootstrap::getObjectManager()->create(Collection::class);
121  $collection->addIdFilter($confProduct->getId());
122  $collection->addPriceData();
123  $collection->load();
124  $product = $collection->getFirstItem();
125  $this->assertEquals(10, $product->getData('min_price'));
126 
127  $childProduct = $this->productRepository->get('simple_10');
128  $stockRegistry = Bootstrap::getObjectManager()->get(StockRegistryInterface::class);
129  $stockItem = $stockRegistry->getStockItem($childProduct->getId());
130  $stockItem->setIsInStock(false);
131  $stockRegistry->updateStockItemBySku($childProduct->getSku(), $stockItem);
132  $collection->clear()->load();
133  $product = $collection->getFirstItem();
134  $this->assertEquals(20, $product->getData('min_price'));
135  }
136 }