PopularSearchTermsTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Search\Test\Unit\Model;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Search\Model\PopularSearchTerms;
  10. use Magento\Search\Model\ResourceModel\Query\Collection;
  11. use Magento\Store\Model\ScopeInterface;
  12. /**
  13. * @covers \Magento\Search\Model\PopularSearchTerms
  14. */
  15. class PopularSearchTermsTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * Testable Object
  19. *
  20. * @var PopularSearchTerms
  21. */
  22. private $popularSearchTerms;
  23. /**
  24. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $scopeConfigMock;
  27. /**
  28. * @var Collection|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $queryCollectionMock;
  31. /**
  32. * Set Up
  33. *
  34. * @return void
  35. */
  36. protected function setUp()
  37. {
  38. $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
  39. $this->queryCollectionMock = $this->createMock(Collection::class);
  40. $this->popularSearchTerms = new PopularSearchTerms($this->scopeConfigMock, $this->queryCollectionMock);
  41. }
  42. /**
  43. * Test isCacheableDataProvider method
  44. *
  45. * @dataProvider isCacheableDataProvider
  46. *
  47. * @param string $term
  48. * @param array $terms
  49. * @param $expected $terms
  50. *
  51. * @return void
  52. */
  53. public function testIsCacheable($term, $terms, $expected)
  54. {
  55. $storeId = 7;
  56. $pageSize = 25;
  57. $this->scopeConfigMock->expects($this->once())->method('getValue')
  58. ->with(
  59. PopularSearchTerms::XML_PATH_MAX_COUNT_CACHEABLE_SEARCH_TERMS,
  60. ScopeInterface::SCOPE_STORE,
  61. $storeId
  62. )->willReturn($pageSize);
  63. $this->queryCollectionMock->expects($this->once())->method('setPopularQueryFilter')->with($storeId)
  64. ->willReturnSelf();
  65. $this->queryCollectionMock->expects($this->once())->method('setPageSize')->with($pageSize)
  66. ->willReturnSelf();
  67. $this->queryCollectionMock->expects($this->once())->method('load')->willReturnSelf();
  68. $this->queryCollectionMock->expects($this->once())->method('getColumnValues')->with('query_text')
  69. ->willReturn($terms);
  70. $actual = $this->popularSearchTerms->isCacheable($term, $storeId);
  71. self::assertEquals($expected, $actual);
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function isCacheableDataProvider()
  77. {
  78. return [
  79. ['test01', [], false],
  80. ['test02', ['test01', 'test02'], true],
  81. ['test03', ['test01', 'test02'], false],
  82. ['test04', ['test04'], true],
  83. ];
  84. }
  85. }