|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogInventory\Test\Unit\Model\Indexer\Stock; |
| 8 | + |
| 9 | +use Magento\CatalogInventory\Api\StockConfigurationInterface; |
| 10 | +use Magento\CatalogInventory\Model\Indexer\Stock\CacheCleaner; |
| 11 | +use Magento\Framework\App\ResourceConnection; |
| 12 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 13 | +use Magento\Framework\DB\Select; |
| 14 | +use Magento\Framework\Event\ManagerInterface; |
| 15 | +use Magento\Framework\Indexer\CacheContext; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 17 | +use Magento\Catalog\Model\Product; |
| 18 | + |
| 19 | +class CacheCleanerTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var CacheCleaner |
| 23 | + */ |
| 24 | + private $unit; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + private $resourceMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + private $connectionMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $eventManagerMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var CacheContext|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + private $cacheContextMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var StockConfigurationInterface|\PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + private $stockConfigurationMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Select|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $selectMock; |
| 55 | + |
| 56 | + protected function setUp() |
| 57 | + { |
| 58 | + $this->resourceMock = $this->getMockBuilder(ResourceConnection::class)->disableOriginalConstructor()->getMock(); |
| 59 | + $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)->getMock(); |
| 60 | + $this->stockConfigurationMock = $this->getMockBuilder(StockConfigurationInterface::class) |
| 61 | + ->setMethods(['getStockThresholdQty'])->getMockForAbstractClass(); |
| 62 | + $this->cacheContextMock = $this->getMockBuilder(CacheContext::class)->disableOriginalConstructor()->getMock(); |
| 63 | + $this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)->getMock(); |
| 64 | + $this->selectMock = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock(); |
| 65 | + |
| 66 | + $this->resourceMock->expects($this->any()) |
| 67 | + ->method('getConnection') |
| 68 | + ->will($this->returnValue($this->connectionMock)); |
| 69 | + |
| 70 | + $this->unit = (new ObjectManager($this))->getObject( |
| 71 | + CacheCleaner::class, |
| 72 | + [ |
| 73 | + 'resource' => $this->resourceMock, |
| 74 | + 'stockConfiguration' => $this->stockConfigurationMock, |
| 75 | + 'cacheContext' => $this->cacheContextMock, |
| 76 | + 'eventManager' => $this->eventManagerMock, |
| 77 | + ] |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param bool $stockStatusBefore |
| 83 | + * @param bool $stockStatusAfter |
| 84 | + * @param int $qtyAfter |
| 85 | + * @param bool|int $stockThresholdQty |
| 86 | + * @dataProvider cleanDataProvider |
| 87 | + */ |
| 88 | + public function testClean($stockStatusBefore, $stockStatusAfter, $qtyAfter, $stockThresholdQty) |
| 89 | + { |
| 90 | + $productId = 123; |
| 91 | + $this->selectMock->expects($this->any())->method('from')->willReturnSelf(); |
| 92 | + $this->selectMock->expects($this->any())->method('where')->willReturnSelf(); |
| 93 | + $this->connectionMock->expects($this->exactly(2))->method('select')->willReturn($this->selectMock); |
| 94 | + $this->connectionMock->expects($this->exactly(2))->method('fetchAll')->willReturnOnConsecutiveCalls( |
| 95 | + [ |
| 96 | + ['product_id' => $productId, 'stock_status' => $stockStatusBefore], |
| 97 | + ], |
| 98 | + [ |
| 99 | + ['product_id' => $productId, 'stock_status' => $stockStatusAfter, 'qty' => $qtyAfter], |
| 100 | + ] |
| 101 | + ); |
| 102 | + $this->stockConfigurationMock->expects($this->once())->method('getStockThresholdQty') |
| 103 | + ->willReturn($stockThresholdQty); |
| 104 | + $this->cacheContextMock->expects($this->once())->method('registerEntities') |
| 105 | + ->with(Product::CACHE_TAG, [$productId]); |
| 106 | + $this->eventManagerMock->expects($this->once())->method('dispatch') |
| 107 | + ->with('clean_cache_by_tags', ['object' => $this->cacheContextMock]); |
| 108 | + |
| 109 | + $this->unit->clean([], function() {}); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return array |
| 114 | + */ |
| 115 | + public function cleanDataProvider() |
| 116 | + { |
| 117 | + return [ |
| 118 | + [true, false, 1, false], |
| 119 | + [false, true, 1, false], |
| 120 | + [true, true, 1, 2], |
| 121 | + [false, false, 1, 2], |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param bool $stockStatusBefore |
| 127 | + * @param bool $stockStatusAfter |
| 128 | + * @param int $qtyAfter |
| 129 | + * @param bool|int $stockThresholdQty |
| 130 | + * @dataProvider notCleanCacheDataProvider |
| 131 | + */ |
| 132 | + public function testNotCleanCache($stockStatusBefore, $stockStatusAfter, $qtyAfter, $stockThresholdQty) |
| 133 | + { |
| 134 | + $productId = 123; |
| 135 | + $this->selectMock->expects($this->any())->method('from')->willReturnSelf(); |
| 136 | + $this->selectMock->expects($this->any())->method('where')->willReturnSelf(); |
| 137 | + $this->connectionMock->expects($this->exactly(2))->method('select')->willReturn($this->selectMock); |
| 138 | + $this->connectionMock->expects($this->exactly(2))->method('fetchAll')->willReturnOnConsecutiveCalls( |
| 139 | + [ |
| 140 | + ['product_id' => $productId, 'stock_status' => $stockStatusBefore], |
| 141 | + ], |
| 142 | + [ |
| 143 | + ['product_id' => $productId, 'stock_status' => $stockStatusAfter, 'qty' => $qtyAfter], |
| 144 | + ] |
| 145 | + ); |
| 146 | + $this->stockConfigurationMock->expects($this->once())->method('getStockThresholdQty') |
| 147 | + ->willReturn($stockThresholdQty); |
| 148 | + $this->cacheContextMock->expects($this->never())->method('registerEntities'); |
| 149 | + $this->eventManagerMock->expects($this->never())->method('dispatch'); |
| 150 | + |
| 151 | + $this->unit->clean([], function() {}); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @return array |
| 156 | + */ |
| 157 | + public function notCleanCacheDataProvider() |
| 158 | + { |
| 159 | + return [ |
| 160 | + [true, true, 1, false], |
| 161 | + [false, false, 1, false], |
| 162 | + [true, true, 3, 2], |
| 163 | + ]; |
| 164 | + } |
| 165 | +} |
0 commit comments