Skip to content

Commit 1bf6151

Browse files
committed
MAGE-1050: code updated as per code review suggestions.
1 parent 6781b57 commit 1bf6151

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

Test/Integration/Search/SearchTest.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Algolia\AlgoliaSearch\Helper\Data;
66
use Algolia\AlgoliaSearch\Model\Indexer\Product;
77
use Algolia\AlgoliaSearch\Test\Integration\TestCase;
8+
use Magento\Framework\Exception\NoSuchEntityException;
89

910
class SearchTest extends TestCase
1011
{
@@ -27,15 +28,68 @@ public function setUp(): void
2728

2829
public function testSearch()
2930
{
30-
list($results, $totalHits, $facetsFromAlgolia) = $this->helper->getSearchResult('', 1);
31-
$this->assertNotEmpty($results);
31+
$query = 'bag';
32+
$results = $this->search($query);
33+
$result = $this->getFirstResult($results);
34+
// Search returns result
35+
$this->assertNotEmpty($result, "Query didn't bring result");
36+
37+
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
38+
$product->load($result['entity_id']);
39+
// Result exists in DB
40+
$this->assertNotEmpty($product->getName(), "Query result item couldn't find in the DB");
41+
// Query word exists title
42+
$this->assertStringContainsString($query, strtolower($product->getName()), "Query word doesn't exist in product name");
43+
}
44+
45+
public function testSearchBySku()
46+
{
47+
$sku = "24-MB01";
48+
$results = $this->search($sku);
49+
$result = $this->getFirstResult($results);
50+
// Search by SKU returns result
51+
$this->assertNotEmpty($result, "SKU search didn't bring result");
52+
53+
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
54+
$product->load($result['entity_id']);
55+
// Result exists in DB
56+
$this->assertNotEmpty($product->getSku(), "SKU search result item couldn't find in the DB");
57+
// Query word exists title
58+
$this->assertEquals($sku, $product->getSku(), "Query SKU doesn't match with product SKU");
3259
}
3360

3461
public function testCategorySearch()
3562
{
36-
list($results, $totalHits, $facetsFromAlgolia) = $this->helper->getSearchResult('', 1, [
63+
// Get products by categoryId
64+
$results = $this->search('', 1, [
3765
'facetFilters' => ['categoryIds:' . $this->assertValues->expectedCategory]
3866
]);
39-
$this->assertNotEmpty($results);
67+
$result = $this->getFirstResult($results);
68+
// Category filter returns result
69+
$this->assertNotEmpty($result, "Category filter didn't return result");
70+
}
71+
72+
/**
73+
* @param array $results
74+
* @return array
75+
*/
76+
protected function getFirstResult(array $results): array
77+
{
78+
list($results, $totalHits, $facetsFromAlgolia) = $results;
79+
return array_shift($results);
80+
}
81+
82+
/**
83+
* @param string $query
84+
* @param int $storeId
85+
* @return array
86+
*/
87+
protected function search(string $query = '', int $storeId = 1, array $params = []): array
88+
{
89+
try {
90+
return $this->helper->getSearchResult($query, $storeId, $params);
91+
} catch (NoSuchEntityException $e) {
92+
return [];
93+
}
4094
}
4195
}

0 commit comments

Comments
 (0)