|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright © Magento, Inc. All rights reserved. |
4 |
| - * See COPYING.txt for license details. |
| 3 | + * Copyright 2014 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */
|
6 | 6 | namespace Magento\Framework\Mview\View;
|
7 | 7 |
|
@@ -107,21 +107,72 @@ public function testGetVersion()
|
107 | 107 | }
|
108 | 108 |
|
109 | 109 | /**
|
110 |
| - * Test for clear() method |
111 |
| - * |
112 | 110 | * @return void
|
113 | 111 | * @throws ChangelogTableNotExistsException
|
114 |
| - * @throws \Magento\Framework\Exception\RuntimeException |
115 | 112 | */
|
116 |
| - public function testClear() |
| 113 | + public function testChangelogClearOversizeBatchSize() |
117 | 114 | {
|
118 |
| - $this->assertEquals(0, $this->model->getVersion()); |
119 |
| - //the same that a table is empty |
120 |
| - $changelogName = $this->resource->getTableName($this->model->getName()); |
121 |
| - $this->connection->insert($changelogName, ['version_id' => 1, 'entity_id' => 1]); |
122 |
| - $this->assertEquals(1, $this->model->getVersion()); |
123 |
| - $this->model->clear(1); |
124 |
| - $this->assertEquals(1, $this->model->getVersion()); //the same that a table is empty |
| 115 | + $stateVersionId = 10100; |
| 116 | + $changelog = $this->generateChangelog(20000); |
| 117 | + |
| 118 | + // #0: check if changelog generated correctly |
| 119 | + $this->assertEquals(1, $this->getChangelogFirstEntityId($changelog)); |
| 120 | + $this->assertEquals(20000, $this->getChangelogEntitiesCount($changelog)); |
| 121 | + |
| 122 | + // #1: check if changelog reduced by batch size value |
| 123 | + $this->model->clear($stateVersionId); |
| 124 | + $this->assertEquals(10001, $this->getChangelogFirstEntityId($changelog)); |
| 125 | + $this->assertEquals(10000, $this->getChangelogEntitiesCount($changelog)); |
| 126 | + |
| 127 | + // #2: check if changelog reduced to the mview state version id |
| 128 | + $this->model->clear($stateVersionId); |
| 129 | + $this->assertEquals(10100, $this->getChangelogFirstEntityId($changelog)); |
| 130 | + $this->assertEquals(9901, $this->getChangelogEntitiesCount($changelog)); |
| 131 | + |
| 132 | + // #3: check if changelog stays the same size and values on the next iteration |
| 133 | + $this->model->clear($stateVersionId); |
| 134 | + $this->assertEquals(10100, $this->getChangelogFirstEntityId($changelog)); |
| 135 | + $this->assertEquals(9901, $this->getChangelogEntitiesCount($changelog)); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @param string $changelog |
| 140 | + * @return int |
| 141 | + */ |
| 142 | + private function getChangelogFirstEntityId(string $changelog): int |
| 143 | + { |
| 144 | + return (int) $this->connection->fetchOne($this->connection->select()->from($changelog, 'version_id')); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @param string $changelog |
| 149 | + * @return int |
| 150 | + */ |
| 151 | + private function getChangelogEntitiesCount(string $changelog): int |
| 152 | + { |
| 153 | + return (int) $this->connection->fetchOne($this->connection->select()->from($changelog, 'COUNT(1)')); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @param int $entitiesCount |
| 158 | + * @return string |
| 159 | + */ |
| 160 | + private function generateChangelog(int $entitiesCount = 100): string |
| 161 | + { |
| 162 | + $data = []; |
| 163 | + $changelog = $this->resource->getTableName($this->model->getName()); |
| 164 | + |
| 165 | + for ($i = 1; $i <= $entitiesCount; $i++) { |
| 166 | + $data[$i]['version_id'] = $i; |
| 167 | + $data[$i]['entity_id'] = $i; |
| 168 | + } |
| 169 | + $this->connection->insertArray( |
| 170 | + $changelog, |
| 171 | + ['version_id', 'entity_id'], |
| 172 | + $data |
| 173 | + ); |
| 174 | + |
| 175 | + return $changelog; |
125 | 176 | }
|
126 | 177 |
|
127 | 178 | /**
|
|
0 commit comments