|
5 | 5 | use Algolia\AlgoliaSearch\Helper\Data;
|
6 | 6 | use Algolia\AlgoliaSearch\Model\Indexer\Product;
|
7 | 7 | use Algolia\AlgoliaSearch\Test\Integration\TestCase;
|
| 8 | +use Magento\Framework\Exception\NoSuchEntityException; |
8 | 9 |
|
9 | 10 | class SearchTest extends TestCase
|
10 | 11 | {
|
11 |
| - public function testSearch() |
| 12 | + const BAGS_CATEGORY_ID = 4; |
| 13 | + |
| 14 | + /** @var Product */ |
| 15 | + protected $productIndexer; |
| 16 | + |
| 17 | + /** @var Data */ |
| 18 | + protected $helper; |
| 19 | + |
| 20 | + public function setUp(): void |
12 | 21 | {
|
13 |
| - /** @var Product $indexer */ |
14 |
| - $indexer = $this->getObjectManager()->create(Product::class); |
15 |
| - $indexer->executeFull(); |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + $this->productIndexer = $this->objectManager->get(Product::class); |
| 25 | + $this->helper = $this->objectManager->create(Data::class); |
16 | 26 |
|
| 27 | + $this->productIndexer->executeFull(); |
17 | 28 | $this->algoliaHelper->waitLastTask();
|
| 29 | + } |
| 30 | + |
| 31 | + public function testSearch() |
| 32 | + { |
| 33 | + $query = 'bag'; |
| 34 | + $results = $this->search($query); |
| 35 | + $result = $this->getFirstResult($results); |
| 36 | + // Search returns result |
| 37 | + $this->assertNotEmpty($result, "Query didn't bring result"); |
| 38 | + |
| 39 | + $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class); |
| 40 | + $product->load($result['entity_id']); |
| 41 | + // Result exists in DB |
| 42 | + $this->assertNotEmpty($product->getName(), "Query result item couldn't find in the DB"); |
| 43 | + // Query word exists title |
| 44 | + $this->assertStringContainsString($query, strtolower($product->getName()), "Query word doesn't exist in product name"); |
| 45 | + } |
18 | 46 |
|
19 |
| - /** @var Data $helper */ |
20 |
| - $helper = $this->getObjectManager()->create(Data::class); |
21 |
| - list($results, $totalHits, $facetsFromAlgolia) = $helper->getSearchResult('', 1); |
| 47 | + public function testSearchBySku() |
| 48 | + { |
| 49 | + $sku = "24-MB01"; |
| 50 | + $results = $this->search($sku); |
| 51 | + $result = $this->getFirstResult($results); |
| 52 | + // Search by SKU returns result |
| 53 | + $this->assertNotEmpty($result, "SKU search didn't bring result"); |
| 54 | + |
| 55 | + $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class); |
| 56 | + $product->load($result['entity_id']); |
| 57 | + // Result exists in DB |
| 58 | + $this->assertNotEmpty($product->getSku(), "SKU search result item couldn't find in the DB"); |
| 59 | + // Query word exists title |
| 60 | + $this->assertEquals($sku, $product->getSku(), "Query SKU doesn't match with product SKU"); |
| 61 | + } |
22 | 62 |
|
23 |
| - $this->assertNotEmpty($results); |
| 63 | + public function testCategorySearch() |
| 64 | + { |
| 65 | + // Get products by categoryId |
| 66 | + list($results, $totalHits, $facetsFromAlgolia) = $this->search('', 1, [ |
| 67 | + 'facetFilters' => ['categoryIds:' . self::BAGS_CATEGORY_ID] |
| 68 | + ]); |
| 69 | + // Category filter returns result |
| 70 | + $this->assertNotEmpty($results, "Category filter didn't return result"); |
| 71 | + |
| 72 | + $collection = $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\Collection::class); |
| 73 | + $collection |
| 74 | + ->addAttributeToSelect('*') |
| 75 | + ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) |
| 76 | + ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) |
| 77 | + ->addCategoriesFilter(["in" => self::BAGS_CATEGORY_ID]) |
| 78 | + ->setStore(1); |
| 79 | + // Products in category count matches |
| 80 | + $this->assertEquals(count($results), $collection->count(), "Indexed number of products in a category doesn't match with DB"); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param array $results |
| 85 | + * @return array |
| 86 | + */ |
| 87 | + protected function getFirstResult(array $results): array |
| 88 | + { |
| 89 | + list($results, $totalHits, $facetsFromAlgolia) = $results; |
| 90 | + return array_shift($results); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param string $query |
| 95 | + * @param int $storeId |
| 96 | + * @param array $params |
| 97 | + * @return array |
| 98 | + */ |
| 99 | + protected function search(string $query = '', int $storeId = 1, array $params = []): array |
| 100 | + { |
| 101 | + try { |
| 102 | + return $this->helper->getSearchResult($query, $storeId, $params); |
| 103 | + } catch (NoSuchEntityException $e) { |
| 104 | + return []; |
| 105 | + } |
24 | 106 | }
|
25 | 107 | }
|
0 commit comments