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