|
| 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\Model; |
| 9 | + |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Search\Model\PopularSearchTerms; |
| 12 | +use Magento\Search\Model\ResourceModel\Query\Collection; |
| 13 | +use Magento\Store\Model\ScopeInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * @covers \Magento\Search\Model\PopularSearchTerms |
| 17 | + */ |
| 18 | +class PopularSearchTermsTest extends \PHPUnit\Framework\TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Testable Object |
| 22 | + * |
| 23 | + * @var PopularSearchTerms |
| 24 | + */ |
| 25 | + private $popularSearchTerms; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $scopeConfigMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Collection|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $queryCollectionMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * Set Up |
| 39 | + * |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + protected function setUp() |
| 43 | + { |
| 44 | + $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 45 | + $this->queryCollectionMock = $this->createMock(Collection::class); |
| 46 | + $this->popularSearchTerms = new PopularSearchTerms($this->scopeConfigMock, $this->queryCollectionMock); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Test isCacheableDataProvider method |
| 51 | + * |
| 52 | + * @dataProvider isCacheableDataProvider |
| 53 | + * |
| 54 | + * @param string $term |
| 55 | + * @param array $terms |
| 56 | + * @param $expected $terms |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testIsCacheable($term, $terms, $expected) |
| 61 | + { |
| 62 | + $storeId = 7; |
| 63 | + $pageSize = 25; |
| 64 | + |
| 65 | + $this->scopeConfigMock->expects($this->once())->method('getValue') |
| 66 | + ->with( |
| 67 | + PopularSearchTerms::XML_PATH_MAX_COUNT_CACHEABLE_SEARCH_TERMS, |
| 68 | + ScopeInterface::SCOPE_STORE, |
| 69 | + $storeId |
| 70 | + )->willReturn($pageSize); |
| 71 | + $this->queryCollectionMock->expects($this->once())->method('setPopularQueryFilter')->with($storeId) |
| 72 | + ->willReturnSelf(); |
| 73 | + $this->queryCollectionMock->expects($this->once())->method('setPageSize')->with($pageSize) |
| 74 | + ->willReturnSelf(); |
| 75 | + $this->queryCollectionMock->expects($this->once())->method('load')->willReturnSelf(); |
| 76 | + $this->queryCollectionMock->expects($this->once())->method('getColumnValues')->with('query_text') |
| 77 | + ->willReturn($terms); |
| 78 | + |
| 79 | + $actual = $this->popularSearchTerms->isCacheable($term, $storeId); |
| 80 | + self::assertEquals($expected, $actual); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return array |
| 85 | + */ |
| 86 | + public function isCacheableDataProvider() |
| 87 | + { |
| 88 | + return [ |
| 89 | + ['test01', [], false], |
| 90 | + ['test02', ['test01', 'test02'], true], |
| 91 | + ['test03', ['test01', 'test02'], false], |
| 92 | + ['test04', ['test04'], true], |
| 93 | + ]; |
| 94 | + } |
| 95 | +} |
0 commit comments