PriceTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\ResourceModel\Indexer;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
  9. class PriceTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Catalog\Model\Indexer\Product\Price\Processor
  13. */
  14. private $indexer;
  15. /**
  16. * @var \Magento\Catalog\Model\ProductRepository
  17. */
  18. private $productRepository;
  19. /**
  20. * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
  21. */
  22. private $productCollectionFactory;
  23. protected function setUp()
  24. {
  25. $this->indexer = Bootstrap::getObjectManager()->get(
  26. \Magento\Catalog\Model\Indexer\Product\Price\Processor::class
  27. );
  28. $this->productCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
  29. $this->productRepository = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\ProductRepository::class);
  30. }
  31. /**
  32. * Steps:
  33. * 1. Add custom tier prices to the product from fixture.
  34. * 2. Run reindexing.
  35. * 3. Load the product again and check all the prices.
  36. *
  37. * @magentoDbIsolation disabled
  38. * @magentoAppIsolation enabled
  39. * @magentoDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php
  40. */
  41. public function testReindexEntity()
  42. {
  43. $specialPrice = 7.90;
  44. $product = $this->productRepository->get('downloadable-product');
  45. $tierData = [
  46. ['website_id' => 0, 'cust_group' => 1, 'price_qty' => 11, 'price' => 8.20],
  47. ['website_id' => 0, 'cust_group' => 1, 'price_qty' => 21, 'price' => 7.55],
  48. ];
  49. $product->setData('tier_price', $tierData);
  50. $product->setData('special_price', $specialPrice);
  51. $this->productRepository->save($product);
  52. $this->indexer->reindexAll();
  53. /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
  54. $collection = $this->productCollectionFactory->create();
  55. $collection->addPriceData()->addFieldToFilter('sku', 'downloadable-product');
  56. /** @var \Magento\Catalog\Model\Product $product */
  57. $product = $collection->getFirstItem();
  58. $this->assertEquals(10, $product->getPrice(), 'Wrong downloadable product price');
  59. $this->assertEquals($specialPrice, $product->getMinimalPrice());
  60. $resultTiers = $product->getTierPrices();
  61. $this->assertTrue(is_array($resultTiers), 'Tiers not found');
  62. $this->assertEquals(count($tierData), count($resultTiers), 'Incorrect number of result tiers');
  63. for ($i = 0; $i < count($tierData); $i++) {
  64. $this->assertEquals($tierData[$i]['price_qty'], $resultTiers[$i]->getQty(), 'Wrong tier price quantity');
  65. $this->assertEquals($tierData[$i]['price'], $resultTiers[$i]->getValue(), 'Wrong tier price value');
  66. }
  67. }
  68. }