|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CatalogSearch\Model\Indexer; |
| 7 | + |
| 8 | +use Magento\Catalog\Model\Product; |
| 9 | +use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection; |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | + |
| 12 | +/** |
| 13 | + * @magentoDbIsolation disabled |
| 14 | + * @magentoDataFixture Magento/CatalogSearch/_files/indexer_fulltext.php |
| 15 | + */ |
| 16 | +class SwitcherUsedInFulltextTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var \Magento\Framework\Indexer\IndexerInterface |
| 20 | + */ |
| 21 | + protected $indexer; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\CatalogSearch\Model\ResourceModel\Engine |
| 25 | + */ |
| 26 | + protected $engine; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext |
| 30 | + */ |
| 31 | + protected $resourceFulltext; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Magento\CatalogSearch\Model\Fulltext |
| 35 | + */ |
| 36 | + protected $fulltext; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Magento\Search\Model\QueryFactory |
| 40 | + */ |
| 41 | + protected $queryFactory; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Product |
| 45 | + */ |
| 46 | + protected $productApple; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var Product |
| 50 | + */ |
| 51 | + protected $productBanana; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var Product |
| 55 | + */ |
| 56 | + protected $productOrange; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var Product |
| 60 | + */ |
| 61 | + protected $productPapaya; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var Product |
| 65 | + */ |
| 66 | + protected $productCherry; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var \Magento\Framework\Search\Request\Dimension |
| 70 | + */ |
| 71 | + protected $dimension; |
| 72 | + |
| 73 | + protected function setUp() |
| 74 | + { |
| 75 | + /** @var \Magento\Framework\Indexer\IndexerInterface indexer */ |
| 76 | + $this->indexer = Bootstrap::getObjectManager()->create( |
| 77 | + \Magento\Indexer\Model\Indexer::class |
| 78 | + ); |
| 79 | + $this->indexer->load('catalogsearch_fulltext'); |
| 80 | + |
| 81 | + $objectManager = Bootstrap::getObjectManager(); |
| 82 | + $this->engine = $objectManager->get( |
| 83 | + \Magento\CatalogSearch\Model\ResourceModel\Engine::class |
| 84 | + ); |
| 85 | + |
| 86 | + $this->resourceFulltext = Bootstrap::getObjectManager()->get( |
| 87 | + \Magento\CatalogSearch\Model\ResourceModel\Fulltext::class |
| 88 | + ); |
| 89 | + |
| 90 | + $this->queryFactory = Bootstrap::getObjectManager()->get( |
| 91 | + \Magento\Search\Model\QueryFactory::class |
| 92 | + ); |
| 93 | + |
| 94 | + $this->dimension = Bootstrap::getObjectManager()->create( |
| 95 | + \Magento\Framework\Search\Request\Dimension::class, |
| 96 | + ['name' => 'scope', 'value' => '1'] |
| 97 | + ); |
| 98 | + |
| 99 | + $this->productApple = $this->getProductBySku('fulltext-1'); |
| 100 | + $this->productBanana = $this->getProductBySku('fulltext-2'); |
| 101 | + $this->productOrange = $this->getProductBySku('fulltext-3'); |
| 102 | + $this->productPapaya = $this->getProductBySku('fulltext-4'); |
| 103 | + $this->productCherry = $this->getProductBySku('fulltext-5'); |
| 104 | + } |
| 105 | + |
| 106 | + public function testReindexAll() |
| 107 | + { |
| 108 | + $this->indexer->reindexAll(); |
| 109 | + |
| 110 | + $products = $this->search('Apple'); |
| 111 | + $this->assertCount(1, $products); |
| 112 | + $this->assertEquals($this->productApple->getId(), $products[0]->getId()); |
| 113 | + |
| 114 | + $products = $this->search('Simple Product'); |
| 115 | + $this->assertCount(5, $products); |
| 116 | + /** @var \Magento\CatalogSearch\Model\Indexer\IndexSwitcherMock $indexSwitcher */ |
| 117 | + $indexSwitcher = Bootstrap::getObjectManager()->get( |
| 118 | + \Magento\CatalogSearch\Model\Indexer\IndexSwitcherMock::class |
| 119 | + ); |
| 120 | + $this->assertTrue($indexSwitcher->isSwitched()); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Search the text and return result collection |
| 125 | + * |
| 126 | + * @param string $text |
| 127 | + * @return Product[] |
| 128 | + */ |
| 129 | + protected function search($text) |
| 130 | + { |
| 131 | + $this->resourceFulltext->resetSearchResults(); |
| 132 | + $query = $this->queryFactory->get(); |
| 133 | + $query->unsetData(); |
| 134 | + $query->setQueryText($text); |
| 135 | + $query->saveIncrementalPopularity(); |
| 136 | + $products = []; |
| 137 | + $collection = Bootstrap::getObjectManager()->create( |
| 138 | + Collection::class, |
| 139 | + [ |
| 140 | + 'searchRequestName' => 'quick_search_container' |
| 141 | + ] |
| 142 | + ); |
| 143 | + $collection->addSearchFilter($text); |
| 144 | + foreach ($collection as $product) { |
| 145 | + $products[] = $product; |
| 146 | + } |
| 147 | + return $products; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Return product by SKU |
| 152 | + * |
| 153 | + * @param string $sku |
| 154 | + * @return Product |
| 155 | + */ |
| 156 | + protected function getProductBySku($sku) |
| 157 | + { |
| 158 | + /** @var Product $product */ |
| 159 | + $product = Bootstrap::getObjectManager()->get( |
| 160 | + \Magento\Catalog\Model\Product::class |
| 161 | + ); |
| 162 | + return $product->loadByAttribute('sku', $sku); |
| 163 | + } |
| 164 | +} |
0 commit comments