|
4 | 4 |
|
5 | 5 | namespace RunAsRoot\PrometheusExporter\Test\Unit\Aggregator\Index; |
6 | 6 |
|
7 | | -use Magento\Framework\App\ResourceConnection; |
8 | | -use Magento\Framework\DB\Adapter\AdapterInterface; |
9 | | -use Magento\Framework\DB\Select; |
10 | 7 | use Magento\Framework\Indexer\IndexerInterface; |
11 | 8 | use Magento\Framework\Mview\View\ChangelogInterface; |
12 | 9 | use Magento\Framework\Mview\ViewInterface; |
13 | 10 | use Magento\Indexer\Model\Indexer\Collection; |
14 | 11 | use Magento\Indexer\Model\Indexer\CollectionFactory; |
| 12 | +use Magento\Indexer\Model\Mview\View\State; |
15 | 13 | use PHPUnit\Framework\MockObject\MockObject; |
16 | 14 | use PHPUnit\Framework\TestCase; |
17 | | -use Psr\Log\LoggerInterface; |
18 | 15 | use RunAsRoot\PrometheusExporter\Aggregator\Index\IndexerBacklogCountAggregator; |
19 | 16 | use RunAsRoot\PrometheusExporter\Service\UpdateMetricService; |
20 | 17 | use RunAsRoot\PrometheusExporter\Service\UpdateMetricServiceInterface; |
21 | 18 |
|
22 | 19 | final class IndexerBacklogCountAggregatorTest extends TestCase |
23 | 20 | { |
24 | | - private const TABLE_CHANGELOG_1 = 'table_name_cl'; |
25 | | - private const TABLE_CHANGELOG_2 = 'table_name_2_cl'; |
26 | 21 | private const VIEW_ID_1 = 'some_name'; |
27 | 22 | private const VIEW_ID_2 = 'some_name_another_name'; |
28 | | - private const TABLE_MVIEW_STATE = 'mview_state'; |
29 | 23 | private const METRIC_CODE = 'magento_indexer_backlog_count_total'; |
30 | 24 |
|
31 | 25 | private IndexerBacklogCountAggregator $sut; |
32 | | - |
33 | | - /** @var MockObject|UpdateMetricServiceInterface */ |
34 | | - private $updateMetricService; |
35 | | - |
36 | | - /** @var MockObject|CollectionFactory */ |
37 | | - private $indexerCollectionFactory; |
38 | | - |
39 | | - /** @var MockObject|LoggerInterface */ |
40 | | - private $logger; |
41 | | - |
42 | | - /** @var MockObject|Collection */ |
43 | | - private $indexerCollection; |
44 | | - |
45 | | - /** @var MockObject|ResourceConnection */ |
46 | | - private $resourceConnection; |
| 26 | + private MockObject|UpdateMetricServiceInterface $updateMetricService; |
| 27 | + private MockObject|Collection $indexerCollection; |
47 | 28 |
|
48 | 29 | protected function setUp(): void |
49 | 30 | { |
50 | 31 | $this->updateMetricService = $this->createMock(UpdateMetricService::class); |
51 | | - $this->indexerCollectionFactory = $this->createMock(CollectionFactory::class); |
| 32 | + $indexerCollectionFactory = $this->createMock(CollectionFactory::class); |
52 | 33 | $this->indexerCollection = $this->createMock(Collection::class); |
53 | | - $this->logger = $this->createMock(LoggerInterface::class); |
54 | | - $this->resourceConnection = $this->createMock(ResourceConnection::class); |
55 | 34 |
|
56 | | - $this->indexerCollectionFactory->method('create')->willReturn($this->indexerCollection); |
| 35 | + $indexerCollectionFactory->method('create')->willReturn($this->indexerCollection); |
57 | 36 |
|
58 | | - $this->sut = new IndexerBacklogCountAggregator( |
59 | | - $this->updateMetricService, |
60 | | - $this->indexerCollectionFactory, |
61 | | - $this->resourceConnection, |
62 | | - $this->logger |
63 | | - ); |
| 37 | + $this->sut = new IndexerBacklogCountAggregator($this->updateMetricService, $indexerCollectionFactory); |
64 | 38 | } |
65 | 39 |
|
66 | | - public function testAggregate(): void |
| 40 | + public function testItAggregatesIndexersChangelogCount(): void |
67 | 41 | { |
68 | | - $this->setUpIndexCollection(); |
69 | | - $this->setUpSelects(); |
70 | | - |
71 | | - $lables1 = [ |
72 | | - 'isValid' => true, |
73 | | - 'title' => 'name', |
74 | | - 'status' => 'success', |
75 | | - ]; |
76 | | - |
77 | | - $lables2 = [ |
78 | | - 'isValid' => true, |
79 | | - 'title' => 'other_name', |
80 | | - 'status' => 'failed', |
81 | | - ]; |
| 42 | + $indexer1 = $this->createMock(IndexerInterface::class); |
| 43 | + $indexer2 = $this->createMock(IndexerInterface::class); |
| 44 | + $view1 = $this->createMock(ViewInterface::class); |
| 45 | + $view2 = $this->createMock(ViewInterface::class); |
| 46 | + $changelog1 = $this->createMock(ChangelogInterface::class); |
| 47 | + $changelog2 = $this->createMock(ChangelogInterface::class); |
| 48 | + $stateMock1 = $this->createMock(State::class); |
| 49 | + $stateMock2 = $this->createMock(State::class); |
| 50 | + |
| 51 | + $testCurrentVersionId1 = '1111'; |
| 52 | + $testCurrentVersionId2 = '2222'; |
| 53 | + $testStateVersion1 = '1100'; |
| 54 | + $testStateVersion2 = '2200'; |
| 55 | + $testPendingCount1 = 11; |
| 56 | + $testPendingCount2 = 22; |
| 57 | + |
| 58 | + $indexer1->method('getTitle')->willReturn(self::VIEW_ID_1); |
| 59 | + $indexer2->method('getTitle')->willReturn(self::VIEW_ID_2); |
| 60 | + |
| 61 | + $indexer1->expects($this->once())->method('getView')->willReturn($view1); |
| 62 | + $indexer2->expects($this->once())->method('getView')->willReturn($view2); |
| 63 | + $view1->expects($this->once())->method('getChangelog')->willReturn($changelog1); |
| 64 | + $view2->expects($this->once())->method('getChangelog')->willReturn($changelog2); |
| 65 | + $view1->expects($this->once())->method('getState')->willReturn($stateMock1); |
| 66 | + $view2->expects($this->once())->method('getState')->willReturn($stateMock2); |
| 67 | + |
| 68 | + $changelog1->method('getVersion')->willReturn($testCurrentVersionId1); |
| 69 | + $changelog2->method('getVersion')->willReturn($testCurrentVersionId2); |
| 70 | + $stateMock1->method('getVersionId')->willReturn($testStateVersion1); |
| 71 | + $stateMock2->method('getVersionId')->willReturn($testStateVersion2); |
| 72 | + |
| 73 | + $changelog1->method('getList') |
| 74 | + ->with($testStateVersion1, $testCurrentVersionId1) |
| 75 | + ->willReturn(range(1, $testPendingCount1)); |
| 76 | + $changelog2->method('getList') |
| 77 | + ->with($testStateVersion2, $testCurrentVersionId2) |
| 78 | + ->willReturn(range(1, $testPendingCount2)); |
| 79 | + |
| 80 | + $this->indexerCollection->expects($this->once())->method('getItems')->willReturn([ $indexer1, $indexer2 ]); |
| 81 | + |
| 82 | + $lables1 = [ 'title' => self::VIEW_ID_1 ]; |
| 83 | + $lables2 = [ 'title' => self::VIEW_ID_2 ]; |
82 | 84 |
|
83 | 85 | $this->updateMetricService |
84 | 86 | ->expects($this->at(0)) |
85 | 87 | ->method('update') |
86 | | - ->with(self::METRIC_CODE, '320', $lables1); |
| 88 | + ->with(self::METRIC_CODE, '11', $lables1); |
87 | 89 |
|
88 | 90 | $this->updateMetricService |
89 | 91 | ->expects($this->at(1)) |
90 | 92 | ->method('update') |
91 | | - ->with(self::METRIC_CODE, '33', $lables2); |
| 93 | + ->with(self::METRIC_CODE, '22', $lables2); |
92 | 94 |
|
93 | 95 | $this->sut->aggregate(); |
94 | 96 | } |
95 | | - |
96 | | - public function testExceptionBehaviour(): void |
97 | | - { |
98 | | - $this->setUpIndexCollection(); |
99 | | - $this->setUpSelects(true); |
100 | | - |
101 | | - $this->logger |
102 | | - ->expects($this->at(0)) |
103 | | - ->method('critical') |
104 | | - ->with(...['ERROR CTNEE']); |
105 | | - |
106 | | - $this->logger |
107 | | - ->expects($this->at(1)) |
108 | | - ->method('critical') |
109 | | - ->with(...['ERROR RE']); |
110 | | - |
111 | | - $this->updateMetricService |
112 | | - ->expects($this->never()) |
113 | | - ->method('update'); |
114 | | - |
115 | | - $this->sut->aggregate(); |
116 | | - } |
117 | | - |
118 | | - private function setUpSelects(bool $throwException = false): void |
119 | | - { |
120 | | - $select1 = $this->createMock(Select::class); |
121 | | - $select2 = $this->createMock(Select::class); |
122 | | - $select3 = $this->createMock(Select::class); |
123 | | - $select4 = $this->createMock(Select::class); |
124 | | - |
125 | | - $connection = $this->createMock(AdapterInterface::class); |
126 | | - $this->resourceConnection->expects($this->once())->method('getConnection') |
127 | | - ->willReturn($connection); |
128 | | - $connection->expects($this->exactly(4)) |
129 | | - ->method('select') |
130 | | - ->will($this->onConsecutiveCalls($select1, $select2, $select3, $select4)); |
131 | | - $connection->expects($this->exactly(4)) |
132 | | - ->method('getTableName') |
133 | | - ->willReturnMap( |
134 | | - [ |
135 | | - [self::TABLE_CHANGELOG_1, self::TABLE_CHANGELOG_1], |
136 | | - [self::TABLE_CHANGELOG_2, self::TABLE_CHANGELOG_2], |
137 | | - [self::TABLE_MVIEW_STATE, self::TABLE_MVIEW_STATE] |
138 | | - ] |
139 | | - ); |
140 | | - $select1 = $this->setUpSelectChangelog($select1, self::TABLE_CHANGELOG_1); |
141 | | - $select2 = $this->setUpSelectMviewState($select2, self::VIEW_ID_1); |
142 | | - $select3 = $this->setUpSelectChangelog($select3, self::TABLE_CHANGELOG_2); |
143 | | - $select4 = $this->setUpSelectMviewState($select4, self::VIEW_ID_2); |
144 | | - |
145 | | - if (!$throwException) { |
146 | | - $connection->expects($this->exactly(4)) |
147 | | - ->method('fetchOne') |
148 | | - ->willReturnMap( |
149 | | - [ |
150 | | - [$select1, [], 550], |
151 | | - [$select2, [], 230], |
152 | | - [$select3, [], 99], |
153 | | - [$select4, [], 66] |
154 | | - ] |
155 | | - ); |
156 | | - } else { |
157 | | - $connection->expects($this->exactly(4)) |
158 | | - ->method('fetchOne') |
159 | | - ->will( |
160 | | - $this->returnCallback(function ($arg) use ($select2, $select4) { |
161 | | - if (spl_object_id($select2) === spl_object_id($arg)) { |
162 | | - throw new \Zend_Db_Exception('ERROR CTNEE'); |
163 | | - } elseif (spl_object_id($select4) === spl_object_id($arg)) { |
164 | | - throw new \Zend_Db_Exception('ERROR RE'); |
165 | | - } else { |
166 | | - return 20; |
167 | | - } |
168 | | - }) |
169 | | - ); |
170 | | - } |
171 | | - } |
172 | | - |
173 | | - private function setUpSelectMviewState(Select $select, string $viewId): Select |
174 | | - { |
175 | | - $select->expects($this->once())->method('from')->with(self::TABLE_MVIEW_STATE) |
176 | | - ->willReturn($select); |
177 | | - $select->expects($this->once())->method('reset')->with(Select::COLUMNS) |
178 | | - ->willReturn($select); |
179 | | - $select->expects($this->once())->method('where')->with('view_id = ?', $viewId) |
180 | | - ->willReturn($select); |
181 | | - $select->expects($this->once())->method('columns')->with(['version_id']) |
182 | | - ->willReturn($select); |
183 | | - |
184 | | - return $select; |
185 | | - } |
186 | | - |
187 | | - private function setUpSelectChangelog(Select $select, string $changelogTableName): Select |
188 | | - { |
189 | | - $select->expects($this->once())->method('from')->with($changelogTableName) |
190 | | - ->willReturn($select); |
191 | | - $select->expects($this->once())->method('reset')->with(Select::COLUMNS) |
192 | | - ->willReturn($select); |
193 | | - $select->expects($this->once())->method('order')->with('version_id DESC') |
194 | | - ->willReturn($select); |
195 | | - $select->expects($this->once())->method('columns')->with(['version_id']) |
196 | | - ->willReturn($select); |
197 | | - |
198 | | - return $select; |
199 | | - } |
200 | | - |
201 | | - private function setUpIndexCollection(): void |
202 | | - { |
203 | | - $indexer1 = $this->createMock(IndexerInterface::class); |
204 | | - $indexer2 = $this->createMock(IndexerInterface::class); |
205 | | - $view1 = $this->createMock(ViewInterface::class); |
206 | | - $view2 = $this->createMock(ViewInterface::class); |
207 | | - $changelog1 = $this->createMock(ChangelogInterface::class); |
208 | | - $changelog2 = $this->createMock(ChangelogInterface::class); |
209 | | - |
210 | | - $indexer1->method('isValid')->willReturn(true); |
211 | | - $indexer1->method('getTitle')->willReturn('name'); |
212 | | - $indexer1->method('getStatus')->willReturn('success'); |
213 | | - |
214 | | - $indexer2->method('isValid')->willReturn(true); |
215 | | - $indexer2->method('getTitle')->willReturn('other_name'); |
216 | | - $indexer2->method('getStatus')->willReturn('failed'); |
217 | | - |
218 | | - $indexer1 |
219 | | - ->expects($this->once()) |
220 | | - ->method('getView') |
221 | | - ->willReturn($view1); |
222 | | - |
223 | | - $indexer2 |
224 | | - ->expects($this->once()) |
225 | | - ->method('getView') |
226 | | - ->willReturn($view2); |
227 | | - |
228 | | - $view1 |
229 | | - ->expects($this->once()) |
230 | | - ->method('getChangelog') |
231 | | - ->willReturn($changelog1); |
232 | | - |
233 | | - $view2 |
234 | | - ->expects($this->once()) |
235 | | - ->method('getChangelog') |
236 | | - ->willReturn($changelog2); |
237 | | - |
238 | | - $view1 |
239 | | - ->expects($this->once()) |
240 | | - ->method('getId') |
241 | | - ->willReturn(self::VIEW_ID_1); |
242 | | - |
243 | | - $view2 |
244 | | - ->expects($this->once()) |
245 | | - ->method('getId') |
246 | | - ->willReturn(self::VIEW_ID_2); |
247 | | - |
248 | | - $changelog1 |
249 | | - ->expects($this->once()) |
250 | | - ->method('getName') |
251 | | - ->willReturn(self::TABLE_CHANGELOG_1); |
252 | | - |
253 | | - $changelog2 |
254 | | - ->expects($this->once()) |
255 | | - ->method('getName') |
256 | | - ->willReturn(self::TABLE_CHANGELOG_2); |
257 | | - |
258 | | - $this->indexerCollection |
259 | | - ->expects($this->once()) |
260 | | - ->method('getItems') |
261 | | - ->willReturn([$indexer1, $indexer2]); |
262 | | - } |
263 | 97 | } |
0 commit comments