|
| 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\Elasticsearch\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor; |
| 11 | +use Magento\Elasticsearch\Model\Config; |
| 12 | +use Magento\Elasticsearch\Observer\CategoryProductIndexer; |
| 13 | +use Magento\Framework\Event; |
| 14 | +use Magento\Framework\Event\Observer; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class CategoryProductIndexerTest |
| 19 | + */ |
| 20 | +class CategoryProductIndexerTest extends \PHPUnit\Framework\TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CategoryProductIndexer |
| 24 | + */ |
| 25 | + private $observer; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Config|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $configMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Processor|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $processorMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Observer|\PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + private $observerMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * Set Up method |
| 44 | + * |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + protected function setUp(): void |
| 48 | + { |
| 49 | + $this->configMock = $this->createMock(Config::class); |
| 50 | + $this->processorMock = $this->createMock(Processor::class); |
| 51 | + $this->observerMock = $this->createMock(Observer::class); |
| 52 | + |
| 53 | + $objectManager = new ObjectManagerHelper($this); |
| 54 | + $this->observer = $objectManager->getObject( |
| 55 | + CategoryProductIndexer::class, |
| 56 | + [ |
| 57 | + 'config' => $this->configMock, |
| 58 | + 'processor' => $this->processorMock, |
| 59 | + ] |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test if a category has changed products |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function testExecuteIfCategoryHasChangedProducts() |
| 69 | + { |
| 70 | + $this->getProductIdsWithEnabledElasticSearch(); |
| 71 | + $this->processorMock->expects($this->once())->method('isIndexerScheduled')->willReturn(true); |
| 72 | + $this->processorMock->expects($this->once())->method('markIndexerAsInvalid'); |
| 73 | + $this->observer->execute($this->observerMock); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test if a category has changed products and not scheduled indexer |
| 78 | + * |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function testExecuteIfCategoryHasChangedProductsAndNotScheduledIndexer(): void |
| 82 | + { |
| 83 | + $this->getProductIdsWithEnabledElasticSearch(); |
| 84 | + $this->processorMock->expects($this->once())->method('isIndexerScheduled')->willReturn(false); |
| 85 | + $this->processorMock->expects($this->never())->method('markIndexerAsInvalid'); |
| 86 | + $this->observer->execute($this->observerMock); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test if a category has none changed products |
| 91 | + * |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function testExecuteIfCategoryHasNoneChangedProducts(): void |
| 95 | + { |
| 96 | + /** @var Event|\PHPUnit_Framework_MockObject_MockObject $eventMock */ |
| 97 | + $eventMock = $this->createPartialMock(Event::class, ['getProductIds']); |
| 98 | + $this->configMock->expects($this->once())->method('isElasticsearchEnabled')->willReturn(true); |
| 99 | + |
| 100 | + $eventMock->expects($this->once())->method('getProductIds')->willReturn([]); |
| 101 | + $this->observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock); |
| 102 | + |
| 103 | + $this->processorMock->expects($this->never())->method('isIndexerScheduled'); |
| 104 | + $this->processorMock->expects($this->never())->method('markIndexerAsInvalid'); |
| 105 | + |
| 106 | + $this->observer->execute($this->observerMock); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test if ElasticSearch is disabled |
| 111 | + * |
| 112 | + * @return void |
| 113 | + */ |
| 114 | + public function testExecuteIfElasticSearchIsDisabled(): void |
| 115 | + { |
| 116 | + /** @var Event|\PHPUnit_Framework_MockObject_MockObject $eventMock */ |
| 117 | + $eventMock = $this->createPartialMock(Event::class, ['getProductIds']); |
| 118 | + $this->configMock->expects($this->once())->method('isElasticsearchEnabled')->willReturn(false); |
| 119 | + $eventMock->expects($this->never())->method('getProductIds')->willReturn([]); |
| 120 | + $this->observer->execute($this->observerMock); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Get product ids with enabled ElasticSearch |
| 125 | + * |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + private function getProductIdsWithEnabledElasticSearch(): void |
| 129 | + { |
| 130 | + /** @var Event|\PHPUnit_Framework_MockObject_MockObject $eventMock */ |
| 131 | + $eventMock = $this->createPartialMock(Event::class, ['getProductIds']); |
| 132 | + $this->configMock->expects($this->once())->method('isElasticsearchEnabled')->willReturn(true); |
| 133 | + $eventMock->expects($this->once())->method('getProductIds')->willReturn([1]); |
| 134 | + $this->observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock); |
| 135 | + } |
| 136 | +} |
0 commit comments