3
3
namespace Algolia \AlgoliaSearch \Test \Integration \Product ;
4
4
5
5
use Algolia \AlgoliaSearch \Api \Product \ReplicaManagerInterface ;
6
+ use Algolia \AlgoliaSearch \Helper \ConfigHelper ;
6
7
use Algolia \AlgoliaSearch \Helper \Entity \ProductHelper ;
7
8
use Algolia \AlgoliaSearch \Model \Indexer \Product as ProductIndexer ;
8
9
use Algolia \AlgoliaSearch \Model \IndicesConfigurator ;
@@ -83,11 +84,8 @@ public function testStandardReplicaConfig(): void
83
84
public function testVirtualReplicaConfig (): void
84
85
{
85
86
$ indexName = $ this ->getIndexName ('default_ ' );
86
- $ ogAlgoliaSettings = $ this ->algoliaHelper ->getSettings ($ indexName );
87
87
$ ogSortingState = $ this ->configHelper ->getSorting ();
88
88
89
- $ this ->assertFalse (array_key_exists ('replicas ' , $ ogAlgoliaSettings ));
90
-
91
89
$ productHelper = $ this ->objectManager ->get (ProductHelper::class);
92
90
$ sortAttr = 'color ' ;
93
91
$ sortDir = 'asc ' ;
@@ -105,14 +103,14 @@ public function testVirtualReplicaConfig(): void
105
103
];
106
104
$ this ->configHelper ->setSorting ($ sorting );
107
105
108
- $ this ->assertConfigInDb (' algoliasearch_instant/instant_sorts/sorts ' , json_encode ($ sorting ));
106
+ $ this ->assertConfigInDb (ConfigHelper:: SORTING_INDICES , json_encode ($ sorting ));
109
107
110
108
$ this ->refreshConfigFromDb ();
111
109
112
110
$ this ->assertSortingAttribute ($ sortAttr , $ sortDir );
113
111
114
112
// Cannot use config fixture because we have disabled db isolation
115
- $ this ->setConfig (' algoliasearch_instant/instant/is_instant_enabled ' , 1 );
113
+ $ this ->setConfig (ConfigHelper:: IS_INSTANT_ENABLED , 1 );
116
114
117
115
$ this ->indicesConfigurator ->saveConfigurationToAlgolia (1 );
118
116
$ this ->algoliaHelper ->waitLastTask ();
@@ -136,9 +134,37 @@ public function testVirtualReplicaConfig(): void
136
134
$ this ->assertEquals ("$ sortDir( $ sortAttr) " , array_shift ($ replicaSettings ['customRanking ' ]));
137
135
138
136
// Restore prior state (for this test only)
139
- $ this ->algoliaHelper ->setSettings ($ indexName , $ ogAlgoliaSettings );
140
137
$ this ->configHelper ->setSorting ($ ogSortingState );
141
- $ this ->setConfig ('algoliasearch_instant/instant/is_instant_enabled ' , 0 );
138
+ $ this ->setConfig (ConfigHelper::IS_INSTANT_ENABLED , 0 );
139
+ }
140
+
141
+ /**
142
+ * ConfigHelper::setSorting used WriterInterface which does not update unless DB isolation is disabled
143
+ * This provides a workaround to test using MutableScopeConfigInterface with DB isolation enabled
144
+ */
145
+ protected function mockSortUpdate (string $ sortAttr , string $ sortDir , array $ attr ): void
146
+ {
147
+ $ sorting = $ this ->configHelper ->getSorting ();
148
+ $ existing = array_filter ($ sorting , function ($ item ) use ($ sortAttr , $ sortDir ) {
149
+ return $ item ['attribute ' ] === $ sortAttr && $ item ['sort ' ] === $ sortDir ;
150
+ });
151
+
152
+
153
+ if ($ existing ) {
154
+ $ idx = array_key_first ($ existing );
155
+ $ sorting [$ idx ] = array_merge ($ existing [$ idx ], $ attr );
156
+ }
157
+ else {
158
+ $ sorting [] = array_merge (
159
+ [
160
+ 'attribute ' => $ sortAttr ,
161
+ 'sort ' => $ sortDir ,
162
+ 'sortLabel ' => $ sortAttr
163
+ ],
164
+ $ attr
165
+ );
166
+ }
167
+ $ this ->setConfig (ConfigHelper::SORTING_INDICES , json_encode ($ sorting ));
142
168
}
143
169
144
170
/**
@@ -148,38 +174,35 @@ public function testVirtualReplicaConfig(): void
148
174
public function testReplicaRebuild (): void
149
175
{
150
176
$ indexName = $ this ->getIndexName ('default_ ' );
151
- $ ogAlgoliaSettings = $ this ->algoliaHelper ->getSettings ($ indexName );
152
177
153
178
$ cmd = $ this ->objectManager ->get (\Algolia \AlgoliaSearch \Console \Command \ReplicaRebuildCommand::class);
154
179
$ this ->assertTrue (true );
155
180
}
156
181
157
182
/**
158
183
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
184
+ * @throws \ReflectionException
159
185
*/
160
186
public function testReplicaSync (): void
161
187
{
162
188
$ indexName = $ this ->getIndexName ('default_ ' );
163
- $ ogAlgoliaSettings = $ this ->algoliaHelper ->getSettings ($ indexName );
164
- $ this ->assertFalse (array_key_exists ('replicas ' , $ ogAlgoliaSettings ));
165
- $ sorting = $ this ->configHelper ->getSorting ();
189
+
190
+ $ this ->mockSortUpdate ('created_at ' , 'desc ' , ['virtualReplica ' => 1 ]);
191
+
192
+ $ sorting = $ this ->objectManager ->get (\Algolia \AlgoliaSearch \Service \Product \SortingTransformer::class)->getSortingIndices (1 , null , null , true );
166
193
167
194
$ cmd = $ this ->objectManager ->create (\Algolia \AlgoliaSearch \Console \Command \ReplicaSyncCommand::class);
168
195
169
196
$ this ->mockProperty ($ cmd , 'output ' , \Symfony \Component \Console \Output \OutputInterface::class);
170
197
171
198
$ cmd ->syncReplicas ();
172
199
173
- // $this->indicesConfigurator->saveConfigurationToAlgolia(1);
174
- // $this->algoliaHelper->waitLastTask();
175
-
176
200
$ currentSettings = $ this ->algoliaHelper ->getSettings ($ indexName );
177
201
$ this ->assertArrayHasKey ('replicas ' , $ currentSettings );
202
+ $ replicas = $ currentSettings ['replicas ' ];
178
203
179
- $ this ->assertTrue (count ($ currentSettings [ ' replicas ' ] ) >= count ($ sorting ));
204
+ $ this ->assertTrue (count ($ replicas ) >= count ($ sorting ));
180
205
181
- // reset
182
- $ this ->algoliaHelper ->setSettings ($ indexName , $ ogAlgoliaSettings );
183
206
}
184
207
185
208
0 commit comments