|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright © Magento, Inc. All rights reserved. |
4 |
| - * See COPYING.txt for license details. |
| 3 | + * Copyright 2013 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
|
11 | 11 | use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
|
12 | 12 | use Magento\CatalogImportExport\Model\Export\Product;
|
13 | 13 | use Magento\CatalogImportExport\Model\Export\Product\Type\Factory;
|
| 14 | +use Magento\CatalogImportExport\Model\Export\ProductFilterInterface; |
14 | 15 | use Magento\CatalogImportExport\Model\Export\RowCustomizer\Composite;
|
| 16 | +use Magento\CatalogInventory\Api\StockConfigurationInterface; |
15 | 17 | use Magento\Eav\Model\Config;
|
16 | 18 | use Magento\Eav\Model\Entity\Collection\AbstractCollection;
|
17 | 19 | use Magento\Eav\Model\Entity\Type;
|
18 | 20 | use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
|
19 | 21 | use Magento\Framework\App\ResourceConnection;
|
20 | 22 | use Magento\Framework\EntityManager\MetadataPool;
|
| 23 | +use Magento\Framework\Exception\LocalizedException; |
21 | 24 | use Magento\Framework\Logger\Monolog;
|
22 | 25 | use Magento\Framework\Stdlib\DateTime\Timezone;
|
23 | 26 | use Magento\ImportExport\Model\Export\Adapter\AbstractAdapter;
|
24 | 27 | use Magento\ImportExport\Model\Export\ConfigInterface;
|
25 | 28 | use Magento\Store\Model\Store;
|
26 | 29 | use Magento\Store\Model\StoreManager;
|
27 | 30 | use Magento\Store\Model\StoreManagerInterface;
|
| 31 | +use PHPUnit\Framework\MockObject\Exception; |
28 | 32 | use PHPUnit\Framework\MockObject\MockObject;
|
29 | 33 | use PHPUnit\Framework\TestCase;
|
30 | 34 | use Psr\Log\LoggerInterface;
|
@@ -139,6 +143,21 @@ class ProductTest extends TestCase
|
139 | 143 | */
|
140 | 144 | protected $object;
|
141 | 145 |
|
| 146 | + /** |
| 147 | + * @var ProductFilterInterface|MockObject |
| 148 | + */ |
| 149 | + private $filter; |
| 150 | + |
| 151 | + /** |
| 152 | + * @var StockConfigurationInterface|MockObject |
| 153 | + */ |
| 154 | + private $stockConfiguration; |
| 155 | + |
| 156 | + /** |
| 157 | + * @return void |
| 158 | + * @throws LocalizedException |
| 159 | + * @throws Exception |
| 160 | + */ |
142 | 161 | protected function setUp(): void
|
143 | 162 | {
|
144 | 163 | $this->localeDate = $this->createMock(Timezone::class);
|
@@ -210,6 +229,9 @@ protected function setUp(): void
|
210 | 229 | 'getContents',
|
211 | 230 | ]);
|
212 | 231 |
|
| 232 | + $this->filter = $this->createMock(ProductFilterInterface::class); |
| 233 | + $this->stockConfiguration = $this->createMock(StockConfigurationInterface::class); |
| 234 | + |
213 | 235 | $constructorMethods = [
|
214 | 236 | 'initTypeModels',
|
215 | 237 | 'initAttributes',
|
@@ -255,7 +277,10 @@ protected function setUp(): void
|
255 | 277 | $this->attributeColFactory,
|
256 | 278 | $this->typeFactory,
|
257 | 279 | $this->linkTypeProvider,
|
258 |
| - $this->rowCustomizer |
| 280 | + $this->rowCustomizer, |
| 281 | + [], |
| 282 | + $this->filter, |
| 283 | + $this->stockConfiguration |
259 | 284 | );
|
260 | 285 | $this->setPropertyValue($this->product, 'metadataPool', $this->metadataPool);
|
261 | 286 |
|
@@ -410,4 +435,118 @@ protected function setPropertyValue(&$object, $property, $value)
|
410 | 435 |
|
411 | 436 | return $object;
|
412 | 437 | }
|
| 438 | + |
| 439 | + /** |
| 440 | + * Test for getItemsPerPage and adjustItemsPerPageByAttributeOptions methods |
| 441 | + * |
| 442 | + * @return void |
| 443 | + * @throws \ReflectionException |
| 444 | + * |
| 445 | + * @dataProvider getItemsPerPageDataProvider |
| 446 | + */ |
| 447 | + public function testGetItemsPerPage($scenarios) |
| 448 | + { |
| 449 | + |
| 450 | + $reflection = new \ReflectionClass(get_class($this->object)); |
| 451 | + $method = $reflection->getMethod('getItemsPerPage'); |
| 452 | + |
| 453 | + $currentMemoryLimit = ini_get('memory_limit'); |
| 454 | + |
| 455 | + foreach ($scenarios as $scenario) { |
| 456 | + if ($currentMemoryLimit !== "-1" && $currentMemoryLimit < $scenario['memory_limit']) { |
| 457 | + $this->markTestSkipped('Memory limit is too low for this test'); |
| 458 | + } |
| 459 | + ini_set('memory_limit', $scenario['memory_limit']); |
| 460 | + $this->setPropertyValue( |
| 461 | + $this->product, |
| 462 | + '_attributeValues', |
| 463 | + ['test_attribute' => $scenario['options'] ?? []] |
| 464 | + ); |
| 465 | + $result = $method->invoke($this->product); |
| 466 | + $this->assertLessThanOrEqual( |
| 467 | + $scenario['expected_items_per_page'], |
| 468 | + $result, |
| 469 | + 'Memory limit: ' . $scenario['memory_limit'] . ' Options count: ' . count($scenario['options']) |
| 470 | + ); |
| 471 | + $this->setPropertyValue($this->product, '_itemsPerPage', null); |
| 472 | + ini_set('memory_limit', $currentMemoryLimit); |
| 473 | + } |
| 474 | + } |
| 475 | + |
| 476 | + /** |
| 477 | + * @return array[] |
| 478 | + */ |
| 479 | + public static function getItemsPerPageDataProvider(): array |
| 480 | + { |
| 481 | + $options = []; |
| 482 | + |
| 483 | + // Simulate different scenarios without attribute options |
| 484 | + $scenarios['Attribute options: ' . count($options)] = [[ |
| 485 | + [ |
| 486 | + 'memory_limit' => '4G', |
| 487 | + 'options' => $options, |
| 488 | + 'expected_items_per_page' => 5000, |
| 489 | + ], |
| 490 | + [ |
| 491 | + 'memory_limit' => '3G', |
| 492 | + 'options' => $options, |
| 493 | + 'expected_items_per_page' => 5000, |
| 494 | + ], |
| 495 | + [ |
| 496 | + 'memory_limit' => '2G', |
| 497 | + 'options' => $options, |
| 498 | + 'expected_items_per_page' => 5000, |
| 499 | + ] |
| 500 | + ]]; |
| 501 | + |
| 502 | + $options = []; |
| 503 | + for ($i = 0; $i <= 5000; $i++) { |
| 504 | + $options[] = ['label' => 'Option ' . $i, 'value' => $i]; |
| 505 | + } |
| 506 | + |
| 507 | + // Simulate different scenarios with attribute options over 5000 |
| 508 | + $scenarios['Attribute options: ' . count($options)] = [[ |
| 509 | + [ |
| 510 | + 'memory_limit' => '4G', |
| 511 | + 'options' => $options, |
| 512 | + 'expected_items_per_page' => 1800, |
| 513 | + ], |
| 514 | + [ |
| 515 | + 'memory_limit' => '3G', |
| 516 | + 'options' => $options, |
| 517 | + 'expected_items_per_page' => 1500, |
| 518 | + ], |
| 519 | + [ |
| 520 | + 'memory_limit' => '2G', |
| 521 | + 'options' => $options, |
| 522 | + 'expected_items_per_page' => 1000, |
| 523 | + ] |
| 524 | + ]]; |
| 525 | + |
| 526 | + $options = []; |
| 527 | + for ($i = 0; $i <= 2500; $i++) { |
| 528 | + $options[] = ['label' => 'Option ' . $i, 'value' => $i]; |
| 529 | + } |
| 530 | + |
| 531 | + // Simulate different scenarios with attribute options over 2500 |
| 532 | + $scenarios['Attribute options: ' . count($options)] = [[ |
| 533 | + [ |
| 534 | + 'memory_limit' => '4G', |
| 535 | + 'options' => $options, |
| 536 | + 'expected_items_per_page' => 3500, |
| 537 | + ], |
| 538 | + [ |
| 539 | + 'memory_limit' => '3G', |
| 540 | + 'options' => $options, |
| 541 | + 'expected_items_per_page' => 3000, |
| 542 | + ], |
| 543 | + [ |
| 544 | + 'memory_limit' => '2G', |
| 545 | + 'options' => $options, |
| 546 | + 'expected_items_per_page' => 2500, |
| 547 | + ] |
| 548 | + ]]; |
| 549 | + |
| 550 | + return $scenarios; |
| 551 | + } |
413 | 552 | }
|
0 commit comments