|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Search\Test\Unit\Model\ResourceModel\Query; |
| 9 | + |
| 10 | +use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; |
| 11 | +use Magento\Framework\Data\Collection\EntityFactoryInterface; |
| 12 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 13 | +use Magento\Framework\DB\Select; |
| 14 | +use Magento\Framework\Event\ManagerInterface; |
| 15 | +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; |
| 16 | +use Magento\Search\Model\ResourceModel\Query\Collection; |
| 17 | +use Magento\Framework\DB\Helper; |
| 18 | +use Magento\Store\Model\StoreManagerInterface; |
| 19 | +use PHPUnit\Framework\MockObject\Exception; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +class CollectionTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var StoreManagerInterface|MockObject |
| 28 | + */ |
| 29 | + private StoreManagerInterface $_storeManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var Helper|MockObject |
| 33 | + */ |
| 34 | + private Helper $_resourceHelper; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var EntityFactoryInterface |
| 38 | + */ |
| 39 | + private EntityFactoryInterface $entityFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var LoggerInterface |
| 43 | + */ |
| 44 | + private LoggerInterface $logger; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var FetchStrategyInterface |
| 48 | + */ |
| 49 | + private FetchStrategyInterface $fetchStrategy; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var ManagerInterface |
| 53 | + */ |
| 54 | + private ManagerInterface $eventManager; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var AdapterInterface |
| 58 | + */ |
| 59 | + private AdapterInterface $connection; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var AbstractDb |
| 63 | + */ |
| 64 | + private AbstractDb $resource; |
| 65 | + |
| 66 | + /** |
| 67 | + * @inheritDoc |
| 68 | + * @throws Exception |
| 69 | + */ |
| 70 | + protected function setUp(): void |
| 71 | + { |
| 72 | + $this->_storeManager = $this->createMock(StoreManagerInterface::class); |
| 73 | + $this->_resourceHelper = $this->createMock(Helper::class); |
| 74 | + $this->entityFactory = $this->createMock(EntityFactoryInterface::class); |
| 75 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 76 | + $this->fetchStrategy = $this->createMock(FetchStrategyInterface::class); |
| 77 | + $this->eventManager = $this->createMock(ManagerInterface::class); |
| 78 | + $this->connection = $this->createMock(AdapterInterface::class); |
| 79 | + $this->resource = $this->createMock(AbstractDb::class); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function testIsTopSearchResult(): void |
| 86 | + { |
| 87 | + $term = 'test'; |
| 88 | + $storeId = 1; |
| 89 | + $maxCountCacheableSearchTerms = 10; |
| 90 | + |
| 91 | + $this->resource->expects($this->once())->method('getConnection')->willReturn($this->connection); |
| 92 | + $select = $this->getMockBuilder(Select::class) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->getMock(); |
| 95 | + $select->expects($this->exactly(7))->method('reset')->willReturnSelf(); |
| 96 | + $select->expects($this->exactly(3))->method('from')->willReturnSelf(); |
| 97 | + $select->expects($this->exactly(3))->method('where')->willReturnSelf(); |
| 98 | + $select->expects($this->once())->method('order')->with(['main_table.popularity desc'])->willReturnSelf(); |
| 99 | + $select->expects($this->once())->method('limit')->with($maxCountCacheableSearchTerms)->willReturnSelf(); |
| 100 | + $select->expects($this->once())->method('assemble')->willReturn( |
| 101 | + "SELECT COUNT(*) FROM (SELECT DISTINCT `main_table`.`query_text` FROM `search_query` AS `main_table`" . |
| 102 | + " WHERE (main_table.store_id IN (1)) AND (main_table.num_results > 0) " . |
| 103 | + " ORDER BY `main_table`.`popularity` desc LIMIT {$maxCountCacheableSearchTerms}) AS `result` |
| 104 | + WHERE (result.query_text = '{$term}')" |
| 105 | + ); |
| 106 | + $select->expects($this->never())->method('distinct'); |
| 107 | + $this->connection->expects($this->any())->method('select')->willReturn($select); |
| 108 | + |
| 109 | + $collection = new Collection( |
| 110 | + $this->entityFactory, |
| 111 | + $this->logger, |
| 112 | + $this->fetchStrategy, |
| 113 | + $this->eventManager, |
| 114 | + $this->_storeManager, |
| 115 | + $this->_resourceHelper, |
| 116 | + $this->connection, |
| 117 | + $this->resource |
| 118 | + ); |
| 119 | + $collection->isTopSearchResult($term, $storeId, $maxCountCacheableSearchTerms); |
| 120 | + } |
| 121 | +} |
0 commit comments