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
{
@@ -27,15 +28,68 @@ public function setUp(): void
27
28
28
29
public function testSearch ()
29
30
{
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 " );
32
59
}
33
60
34
61
public function testCategorySearch ()
35
62
{
36
- list ($ results , $ totalHits , $ facetsFromAlgolia ) = $ this ->helper ->getSearchResult ('' , 1 , [
63
+ // Get products by categoryId
64
+ $ results = $ this ->search ('' , 1 , [
37
65
'facetFilters ' => ['categoryIds: ' . $ this ->assertValues ->expectedCategory ]
38
66
]);
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
+ }
40
94
}
41
95
}
0 commit comments