DeleteOutdatedPriceValuesTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Catalog\Test\Unit\Cron;
  8. use Magento\Catalog\Api\Data\ProductAttributeInterface;
  9. use Magento\Catalog\Cron\DeleteOutdatedPriceValues;
  10. use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository;
  11. use Magento\Eav\Model\Entity\Attribute;
  12. use Magento\Eav\Model\Entity\Attribute\Backend\BackendInterface;
  13. use Magento\Framework\App\Config\MutableScopeConfigInterface as ScopeConfig;
  14. use Magento\Framework\App\ResourceConnection;
  15. use Magento\Framework\DB\Adapter\AdapterInterface;
  16. use Magento\Store\Model\Store;
  17. /**
  18. * @covers \Magento\Catalog\Cron\DeleteOutdatedPriceValues
  19. */
  20. class DeleteOutdatedPriceValuesTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * Testable Object
  24. *
  25. * @var DeleteOutdatedPriceValues
  26. */
  27. private $deleteOutdatedPriceValues;
  28. /**
  29. * @var AttributeRepository|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $attributeRepositoryMock;
  32. /**
  33. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $resourceConnectionMock;
  36. /**
  37. * @var ScopeConfig|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $scopeConfigMock;
  40. /**
  41. * @var Attribute|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $attributeMock;
  44. /**
  45. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $dbAdapterMock;
  48. /**
  49. * @var BackendInterface|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $attributeBackendMock;
  52. /**
  53. * Set Up
  54. *
  55. * @return void
  56. */
  57. protected function setUp()
  58. {
  59. $this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
  60. $this->attributeRepositoryMock = $this->createMock(AttributeRepository::class);
  61. $this->attributeMock = $this->createMock(Attribute::class);
  62. $this->scopeConfigMock = $this->createMock(ScopeConfig::class);
  63. $this->dbAdapterMock = $this->createMock(AdapterInterface::class);
  64. $this->attributeBackendMock = $this->createMock(BackendInterface::class);
  65. $this->deleteOutdatedPriceValues = new DeleteOutdatedPriceValues(
  66. $this->resourceConnectionMock,
  67. $this->attributeRepositoryMock,
  68. $this->scopeConfigMock
  69. );
  70. }
  71. /**
  72. * Test execute method
  73. *
  74. * @return void
  75. */
  76. public function testExecute()
  77. {
  78. $table = 'catalog_product_entity_decimal';
  79. $attributeId = 15;
  80. $conditions = ['first', 'second'];
  81. $this->scopeConfigMock
  82. ->expects($this->once())
  83. ->method('getValue')
  84. ->with(Store::XML_PATH_PRICE_SCOPE)
  85. ->willReturn(Store::XML_PATH_PRICE_SCOPE);
  86. $this->attributeRepositoryMock
  87. ->expects($this->once())
  88. ->method('get')
  89. ->with(ProductAttributeInterface::ENTITY_TYPE_CODE, ProductAttributeInterface::CODE_PRICE)
  90. ->willReturn($this->attributeMock);
  91. $this->attributeMock->expects($this->once())->method('getId')->willReturn($attributeId);
  92. $this->attributeMock->expects($this->once())->method('getBackend')->willReturn($this->attributeBackendMock);
  93. $this->attributeBackendMock->expects($this->once())->method('getTable')->willReturn($table);
  94. $this->resourceConnectionMock->expects($this->once())
  95. ->method('getConnection')
  96. ->willReturn($this->dbAdapterMock);
  97. $this->dbAdapterMock->expects($this->exactly(2))->method('quoteInto')->willReturnMap([
  98. ['attribute_id = ?', $attributeId, null, null, $conditions[0]],
  99. ['store_id != ?', Store::DEFAULT_STORE_ID, null, null, $conditions[1]],
  100. ]);
  101. $this->dbAdapterMock->expects($this->once())->method('delete')->with($table, $conditions);
  102. $this->deleteOutdatedPriceValues->execute();
  103. }
  104. /**
  105. * Test execute method
  106. * The price scope config option is not equal to global value
  107. *
  108. * @return void
  109. */
  110. public function testExecutePriceConfigIsNotSetToGlobal()
  111. {
  112. $this->scopeConfigMock
  113. ->expects($this->once())
  114. ->method('getValue')
  115. ->with(Store::XML_PATH_PRICE_SCOPE)
  116. ->willReturn(null);
  117. $this->attributeRepositoryMock
  118. ->expects($this->never())
  119. ->method('get');
  120. $this->dbAdapterMock
  121. ->expects($this->never())
  122. ->method('delete');
  123. $this->deleteOutdatedPriceValues->execute();
  124. }
  125. }