|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Search\Model; |
| 7 | + |
| 8 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 9 | +use Magento\CatalogSearch\Model\ResourceModel\EngineInterface; |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | + |
| 12 | +/** |
| 13 | + * Search test. |
| 14 | + * |
| 15 | + * @magentoDbIsolation disabled |
| 16 | + * @magentoAppIsolation enabled |
| 17 | + */ |
| 18 | +class SearchTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var \Magento\Framework\Search\AdapterInterface |
| 22 | + */ |
| 23 | + private $adapter; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\Framework\ObjectManagerInterface |
| 27 | + */ |
| 28 | + protected $objectManager; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $searchEngine = EngineResolver::CATALOG_SEARCH_MYSQL_ENGINE; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ProductRepositoryInterface |
| 37 | + */ |
| 38 | + protected $productRepository; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Framework\Search\Request\Builder |
| 42 | + */ |
| 43 | + private $requestBuilder; |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritdoc |
| 47 | + */ |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 51 | + |
| 52 | + /** @var \Magento\Framework\Search\Request\Config $config */ |
| 53 | + $config = $this->objectManager->create(\Magento\Framework\Search\Request\Config::class); |
| 54 | + |
| 55 | + $this->requestBuilder = $this->objectManager->create( |
| 56 | + \Magento\Framework\Search\Request\Builder::class, |
| 57 | + ['config' => $config] |
| 58 | + ); |
| 59 | + |
| 60 | + $this->adapter = $this->createAdapter(); |
| 61 | + $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns search adapter instance. |
| 66 | + * |
| 67 | + * @return \Magento\Framework\Search\AdapterInterface |
| 68 | + */ |
| 69 | + protected function createAdapter() |
| 70 | + { |
| 71 | + return $this->objectManager->create(\Magento\Framework\Search\Adapter\Mysql\Adapter::class); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Make sure that correct engine is set. |
| 76 | + * |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + protected function assertPreConditions() |
| 80 | + { |
| 81 | + $currentEngine = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class) |
| 82 | + ->getValue(EngineInterface::CONFIG_ENGINE_PATH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); |
| 83 | + $this->assertEquals($this->searchEngine, $currentEngine); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Execute search query. |
| 88 | + * |
| 89 | + * @return \Magento\Framework\Search\Response\QueryResponse |
| 90 | + */ |
| 91 | + private function executeQuery() |
| 92 | + { |
| 93 | + /** @var \Magento\Framework\Search\RequestInterface $queryRequest */ |
| 94 | + $queryRequest = $this->requestBuilder->create(); |
| 95 | + $queryResponse = $this->adapter->query($queryRequest); |
| 96 | + |
| 97 | + return $queryResponse; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns document ids from query response. |
| 102 | + * |
| 103 | + * @param \Magento\Framework\Search\Response\QueryResponse $queryResponse |
| 104 | + * @return array |
| 105 | + */ |
| 106 | + protected function getProductIds(\Magento\Framework\Search\Response\QueryResponse $queryResponse) |
| 107 | + { |
| 108 | + $actualIds = []; |
| 109 | + foreach ($queryResponse as $document) { |
| 110 | + /** @var \Magento\Framework\Api\Search\Document $document */ |
| 111 | + $actualIds[] = $document->getId(); |
| 112 | + } |
| 113 | + |
| 114 | + return $actualIds; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Search grouped product. |
| 119 | + * |
| 120 | + * @magentoDataFixture Magento/Framework/Search/_files/grouped_product.php |
| 121 | + * @magentoConfigFixture current_store catalog/search/engine mysql |
| 122 | + * |
| 123 | + * @return void |
| 124 | + */ |
| 125 | + public function testSearchGroupedProduct() |
| 126 | + { |
| 127 | + $this->requestBuilder->bind('search_term', 'Grouped Product'); |
| 128 | + $this->requestBuilder->setRequestName('quick_search_container'); |
| 129 | + |
| 130 | + $queryResponse = $this->executeQuery(); |
| 131 | + $result = $this->getProductIds($queryResponse); |
| 132 | + |
| 133 | + self::assertCount(3, $result); |
| 134 | + |
| 135 | + $groupedProduct = $this->productRepository->get('grouped-product'); |
| 136 | + self::assertContains($groupedProduct->getId(), $result, 'Grouped product not found by name.'); |
| 137 | + } |
| 138 | +} |
0 commit comments