Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CacheCleanerTest.php
Go to the documentation of this file.
1 <?php
8 
18 
19 class CacheCleanerTest extends \PHPUnit\Framework\TestCase
20 {
24  private $unit;
25 
29  private $resourceMock;
30 
34  private $connectionMock;
35 
39  private $eventManagerMock;
40 
44  private $cacheContextMock;
45 
49  private $stockConfigurationMock;
50 
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 
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  $callback = function () {
110  };
111  $this->unit->clean([], $callback);
112  }
113 
117  public function cleanDataProvider()
118  {
119  return [
120  [true, false, 1, false],
121  [false, true, 1, false],
122  [true, true, 1, 2],
123  [false, false, 1, 2],
124  ];
125  }
126 
134  public function testNotCleanCache($stockStatusBefore, $stockStatusAfter, $qtyAfter, $stockThresholdQty)
135  {
136  $productId = 123;
137  $this->selectMock->expects($this->any())->method('from')->willReturnSelf();
138  $this->selectMock->expects($this->any())->method('where')->willReturnSelf();
139  $this->connectionMock->expects($this->exactly(2))->method('select')->willReturn($this->selectMock);
140  $this->connectionMock->expects($this->exactly(2))->method('fetchAll')->willReturnOnConsecutiveCalls(
141  [
142  ['product_id' => $productId, 'stock_status' => $stockStatusBefore],
143  ],
144  [
145  ['product_id' => $productId, 'stock_status' => $stockStatusAfter, 'qty' => $qtyAfter],
146  ]
147  );
148  $this->stockConfigurationMock->expects($this->once())->method('getStockThresholdQty')
149  ->willReturn($stockThresholdQty);
150  $this->cacheContextMock->expects($this->never())->method('registerEntities');
151  $this->eventManagerMock->expects($this->never())->method('dispatch');
152 
153  $callback = function () {
154  };
155  $this->unit->clean([], $callback);
156  }
157 
161  public function notCleanCacheDataProvider()
162  {
163  return [
164  [true, true, 1, false],
165  [false, false, 1, false],
166  [true, true, 3, 2],
167  ];
168  }
169 }
testNotCleanCache($stockStatusBefore, $stockStatusAfter, $qtyAfter, $stockThresholdQty)
testClean($stockStatusBefore, $stockStatusAfter, $qtyAfter, $stockThresholdQty)