|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Quote\Model\ResourceModel\Quote\Item; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\Quote\Model\Quote; |
| 15 | +use Magento\Quote\Model\ResourceModel\Quote\Item\Collection as QuoteItemCollection; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests Magento\Quote\Model\ResourceModel\Quote\Item\Collection. |
| 20 | + */ |
| 21 | +class CollectionTest extends \PHPUnit\Framework\TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ObjectManagerInterface |
| 25 | + */ |
| 26 | + private $objectManager; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ProductRepositoryInterface |
| 30 | + */ |
| 31 | + private $productRepository; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritdoc |
| 35 | + */ |
| 36 | + protected function setUp() |
| 37 | + { |
| 38 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 39 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Covers case when during quote item collection load product exists in db but not accessible. |
| 44 | + * |
| 45 | + * @magentoDataFixture Magento/Sales/_files/quote.php |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + public function testLoadCollectionWithNotAccessibleProduct() |
| 49 | + { |
| 50 | + /** @var Quote $quote */ |
| 51 | + $quote = $this->objectManager->create(Quote::class); |
| 52 | + $quote->load('test01', 'reserved_order_id'); |
| 53 | + |
| 54 | + $this->assertCount(1, $quote->getItemsCollection()); |
| 55 | + |
| 56 | + $product = $this->productRepository->get('simple'); |
| 57 | + /** @var ProductCollection $productCollection */ |
| 58 | + $productCollection = $this->objectManager->create(ProductCollection::class); |
| 59 | + $this->setPropertyValue($productCollection, '_isCollectionLoaded', true); |
| 60 | + /** @var ProductCollectionFactory|\PHPUnit_Framework_MockObject_MockObject $productCollectionFactoryMock */ |
| 61 | + $productCollectionFactoryMock = $this->createMock(ProductCollectionFactory::class); |
| 62 | + $productCollectionFactoryMock->expects($this->any())->method('create')->willReturn($productCollection); |
| 63 | + |
| 64 | + /** @var QuoteItemCollection $quoteItemCollection */ |
| 65 | + $quoteItemCollection = $this->objectManager->create( |
| 66 | + QuoteItemCollection::class, |
| 67 | + [ |
| 68 | + 'productCollectionFactory' => $productCollectionFactoryMock, |
| 69 | + ] |
| 70 | + ); |
| 71 | + |
| 72 | + $quoteItemCollection->setQuote($quote); |
| 73 | + $this->assertCount(1, $quoteItemCollection); |
| 74 | + $item = $quoteItemCollection->getItemByColumnValue('product_id', $product->getId()); |
| 75 | + |
| 76 | + $this->assertNotNull($item); |
| 77 | + $this->assertTrue($item->isDeleted()); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Set object non-public property value. |
| 82 | + * |
| 83 | + * @param object $object |
| 84 | + * @param string $propertyName |
| 85 | + * @param mixed $value |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + private function setPropertyValue($object, string $propertyName, $value) |
| 89 | + { |
| 90 | + $reflectionClass = new \ReflectionClass($object); |
| 91 | + if ($reflectionClass->hasProperty($propertyName)) { |
| 92 | + $reflectionProperty = $reflectionClass->getProperty($propertyName); |
| 93 | + $reflectionProperty->setAccessible(true); |
| 94 | + $reflectionProperty->setValue($object, $value); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments