|
| 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\Search\Test\Unit\Plugin\Model; |
| 9 | + |
| 10 | +use Magento\Search\Model\QueryResult; |
| 11 | +use Magento\Search\Model\ResourceModel\Query\Collection; |
| 12 | +use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory; |
| 13 | +use Magento\Search\Plugin\Model\SearchSuggestionResultsCount; |
| 14 | +use Magento\Store\Api\Data\StoreInterface; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\Framework\Data\Collection\AbstractDb; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class SearchSuggestionResultsCountTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var SearchSuggestionResultsCount |
| 23 | + */ |
| 24 | + private SearchSuggestionResultsCount $model; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var QueryCollectionFactory |
| 28 | + */ |
| 29 | + private QueryCollectionFactory $queryCollectionFactoryMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var StoreManagerInterface |
| 33 | + */ |
| 34 | + private StoreManagerInterface $storeManagerMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var QueryResult |
| 38 | + */ |
| 39 | + private QueryResult $queryResultMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Collection |
| 43 | + */ |
| 44 | + private Collection $queryCollectionMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var StoreInterface |
| 48 | + */ |
| 49 | + private StoreInterface $storeInterfaceMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var AbstractDb |
| 53 | + */ |
| 54 | + private AbstractDb $abstractDbMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + protected function setUp(): void |
| 60 | + { |
| 61 | + $this->queryCollectionFactoryMock = $this->getMockBuilder(QueryCollectionFactory::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->onlyMethods(['create']) |
| 64 | + ->getMock(); |
| 65 | + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->getMock(); |
| 68 | + $this->queryResultMock = $this->getMockBuilder(QueryResult::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + $this->queryCollectionMock = $this->getMockBuilder(Collection::class) |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->getMock(); |
| 74 | + $this->storeInterfaceMock = $this->getMockBuilder(StoreInterface::class) |
| 75 | + ->disableOriginalConstructor() |
| 76 | + ->onlyMethods(['getId']) |
| 77 | + ->getMockForAbstractClass(); |
| 78 | + $this->abstractDbMock = $this->getMockBuilder(AbstractDb::class) |
| 79 | + ->disableOriginalConstructor() |
| 80 | + ->onlyMethods(['getData']) |
| 81 | + ->getMockForAbstractClass(); |
| 82 | + $this->model = new SearchSuggestionResultsCount( |
| 83 | + $this->queryCollectionFactoryMock, |
| 84 | + $this->storeManagerMock |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return void |
| 90 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 91 | + */ |
| 92 | + public function testAfterGetResultsCount() |
| 93 | + { |
| 94 | + $expectedArr = ['num_results' => 5]; |
| 95 | + $this->queryCollectionFactoryMock->expects($this->once()) |
| 96 | + ->method('create') |
| 97 | + ->willReturn($this->queryCollectionMock); |
| 98 | + $this->storeManagerMock->expects($this->once()) |
| 99 | + ->method('getStore') |
| 100 | + ->willReturn($this->storeInterfaceMock); |
| 101 | + $this->storeInterfaceMock->expects($this->once()) |
| 102 | + ->method('getId') |
| 103 | + ->willReturn(1); |
| 104 | + $this->queryCollectionMock->expects($this->once()) |
| 105 | + ->method('setStoreId') |
| 106 | + ->willReturnSelf(); |
| 107 | + $this->queryResultMock->expects($this->once()) |
| 108 | + ->method('getQueryText') |
| 109 | + ->willReturn('mint'); |
| 110 | + $this->queryCollectionMock->expects($this->once()) |
| 111 | + ->method('setQueryFilter') |
| 112 | + ->willReturn($this->queryCollectionMock); |
| 113 | + $this->abstractDbMock->expects($this->once()) |
| 114 | + ->method('getData') |
| 115 | + ->willReturn(['num_results' => 5]); |
| 116 | + $this->queryCollectionMock->expects($this->once()) |
| 117 | + ->method('getIterator') |
| 118 | + ->willReturn(new \ArrayIterator([$this->abstractDbMock])); |
| 119 | + $this->assertEquals( |
| 120 | + $expectedArr, |
| 121 | + $this->model->afterGetResultsCount( |
| 122 | + $this->queryResultMock, |
| 123 | + 0 |
| 124 | + ) |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments