|
| 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\Review\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection; |
| 11 | +use Magento\Framework\Event; |
| 12 | +use Magento\Framework\Event\Observer; |
| 13 | +use Magento\Review\Model\ResourceModel\Review\Summary; |
| 14 | +use Magento\Review\Model\ResourceModel\Review\SummaryFactory; |
| 15 | +use Magento\Review\Observer\CatalogProductListCollectionAppendSummaryFieldsObserver; |
| 16 | +use Magento\Store\Api\Data\StoreInterface; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | +use MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Test class for \Magento\Review\Observer\CatalogProductListCollectionAppendSummaryFieldsObserver |
| 23 | + */ |
| 24 | +class CatalogProductListCollectionAppendSummaryFieldsObserverTest extends TestCase |
| 25 | +{ |
| 26 | + private const STORE_ID = '1'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Event|MockObject |
| 30 | + */ |
| 31 | + private $eventMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * Testable Object |
| 35 | + * |
| 36 | + * @var CatalogProductListCollectionAppendSummaryFieldsObserver |
| 37 | + */ |
| 38 | + private $observer; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Observer|MockObject |
| 42 | + */ |
| 43 | + private $observerMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var Collection|MockObject |
| 47 | + */ |
| 48 | + private $productCollectionMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var StoreInterface|MockObject |
| 52 | + */ |
| 53 | + private $storeMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var StoreManagerInterface|MockObject |
| 57 | + */ |
| 58 | + private $storeManagerMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var Summary|MockObject |
| 62 | + */ |
| 63 | + private $sumResourceMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var SummaryFactory|MockObject |
| 67 | + */ |
| 68 | + private $sumResourceFactoryMock; |
| 69 | + |
| 70 | + /** |
| 71 | + * @inheritdoc |
| 72 | + */ |
| 73 | + protected function setUp() : void |
| 74 | + { |
| 75 | + $this->eventMock = $this->getMockBuilder(Event::class) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->setMethods(['getCollection']) |
| 78 | + ->getMock(); |
| 79 | + |
| 80 | + $this->observerMock = $this->createMock(Observer::class); |
| 81 | + |
| 82 | + $this->productCollectionMock = $this->getMockBuilder(Collection::class) |
| 83 | + ->disableOriginalConstructor() |
| 84 | + ->getMock(); |
| 85 | + |
| 86 | + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->setMethods(['getStore']) |
| 89 | + ->getMockForAbstractClass(); |
| 90 | + |
| 91 | + $this->storeMock = $this->getMockBuilder(StoreInterface::class) |
| 92 | + ->disableOriginalConstructor() |
| 93 | + ->setMethods(['getId']) |
| 94 | + ->getMockForAbstractClass(); |
| 95 | + |
| 96 | + $this->sumResourceMock = $this->createPartialMock( |
| 97 | + Summary::class, |
| 98 | + ['appendSummaryFieldsToCollection'] |
| 99 | + ); |
| 100 | + |
| 101 | + $this->sumResourceFactoryMock = $this->getMockBuilder(SummaryFactory::class) |
| 102 | + ->disableOriginalConstructor() |
| 103 | + ->setMethods(['create']) |
| 104 | + ->getMock(); |
| 105 | + |
| 106 | + $this->observer = new CatalogProductListCollectionAppendSummaryFieldsObserver( |
| 107 | + $this->sumResourceFactoryMock, |
| 108 | + $this->storeManagerMock |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Product listing test |
| 114 | + */ |
| 115 | + public function testAddSummaryFieldToProductsCollection() : void |
| 116 | + { |
| 117 | + $this->eventMock |
| 118 | + ->expects($this->once()) |
| 119 | + ->method('getCollection') |
| 120 | + ->willReturn($this->productCollectionMock); |
| 121 | + |
| 122 | + $this->observerMock |
| 123 | + ->expects($this->once()) |
| 124 | + ->method('getEvent') |
| 125 | + ->willReturn($this->eventMock); |
| 126 | + |
| 127 | + $this->storeManagerMock |
| 128 | + ->expects($this->once()) |
| 129 | + ->method('getStore') |
| 130 | + ->willReturn($this->storeMock); |
| 131 | + |
| 132 | + $this->storeMock |
| 133 | + ->expects($this->once()) |
| 134 | + ->method('getId') |
| 135 | + ->willReturn(self::STORE_ID); |
| 136 | + |
| 137 | + $this->sumResourceFactoryMock |
| 138 | + ->expects($this->once()) |
| 139 | + ->method('create') |
| 140 | + ->willReturn($this->sumResourceMock); |
| 141 | + |
| 142 | + $this->sumResourceMock |
| 143 | + ->expects($this->once()) |
| 144 | + ->method('appendSummaryFieldsToCollection') |
| 145 | + ->willReturn($this->sumResourceMock); |
| 146 | + |
| 147 | + $this->observer->execute($this->observerMock); |
| 148 | + } |
| 149 | +} |
0 commit comments