|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\ConfigurableProduct\Model\ResourceModel\Product; |
| 7 | + |
| 8 | +use Magento\Catalog\Model\ResourceModel\Product\Relation; |
| 9 | +use Magento\Framework\ObjectManagerInterface; |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests Catalog Product Relation resource model. |
| 17 | + * |
| 18 | + * @see Relation |
| 19 | + */ |
| 20 | +class RelationTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var ObjectManagerInterface |
| 24 | + */ |
| 25 | + private $objectManager; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Relation |
| 29 | + */ |
| 30 | + private $model; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ProductRepositoryInterface |
| 34 | + */ |
| 35 | + private $productRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var SearchCriteriaBuilder |
| 39 | + */ |
| 40 | + private $searchCriteriaBuilder; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->model = $this->objectManager->get(Relation::class); |
| 49 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 50 | + $this->searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Tests that getRelationsByChildren will return parent products entity ids of child products entity ids. |
| 55 | + * |
| 56 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php |
| 57 | + */ |
| 58 | + public function testGetRelationsByChildren(): void |
| 59 | + { |
| 60 | + // Find configurable products options |
| 61 | + $productOptionSkus = ['simple_10', 'simple_20', 'simple_30', 'simple_40']; |
| 62 | + $searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $productOptionSkus, 'in') |
| 63 | + ->create(); |
| 64 | + $productOptions = $this->productRepository->getList($searchCriteria) |
| 65 | + ->getItems(); |
| 66 | + |
| 67 | + $productOptionsIds = []; |
| 68 | + |
| 69 | + foreach ($productOptions as $productOption) { |
| 70 | + $productOptionsIds[] = $productOption->getId(); |
| 71 | + } |
| 72 | + |
| 73 | + // Find configurable products |
| 74 | + $searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', ['configurable', 'configurable_12345'], 'in') |
| 75 | + ->create(); |
| 76 | + $configurableProducts = $this->productRepository->getList($searchCriteria) |
| 77 | + ->getItems(); |
| 78 | + |
| 79 | + // Assert there are configurable products ids in result of getRelationsByChildren method. |
| 80 | + $result = $this->model->getRelationsByChildren($productOptionsIds); |
| 81 | + |
| 82 | + foreach ($configurableProducts as $configurableProduct) { |
| 83 | + $this->assertContains($configurableProduct->getId(), $result); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments