Skip to content

Commit 92f95f0

Browse files
committed
MAGE-1281 Add unit tests
1 parent 31dfb42 commit 92f95f0

File tree

3 files changed

+204
-14
lines changed

3 files changed

+204
-14
lines changed

Service/ReplicaSettingsHandler.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ReplicaSettingsHandler
2323

2424
public function __construct(
2525
protected AlgoliaConnector $connector,
26-
protected ConfigHelper $config,
26+
protected ConfigHelper $config,
2727
) {}
2828

2929
/**
@@ -38,19 +38,23 @@ public function setSettings(
3838
{
3939
if ($this->config->shouldForwardPrimaryIndexSettingsToReplicas($indexOptions->getStoreId())) {
4040
[$forward, $noForward] = $this->splitSettings($indexSettings);
41-
$this->connector->setSettings(
42-
$indexOptions,
43-
$forward,
44-
true,
45-
false
46-
);
47-
$this->connector->setSettings(
48-
$indexOptions,
49-
$noForward,
50-
false,
51-
true,
52-
$mergeSettingsFrom
53-
);
41+
if ($forward) {
42+
$this->connector->setSettings(
43+
$indexOptions,
44+
$forward,
45+
true,
46+
false
47+
);
48+
}
49+
if ($noForward) {
50+
$this->connector->setSettings(
51+
$indexOptions,
52+
$noForward,
53+
false,
54+
true,
55+
$mergeSettingsFrom
56+
);
57+
}
5458
} else {
5559
$this->connector->setSettings(
5660
$indexOptions,
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Unit\Service;
4+
5+
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
6+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
7+
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
8+
use Algolia\AlgoliaSearch\Service\ReplicaSettingsHandler;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class ReplicaSettingsHandlerTest extends TestCase
12+
{
13+
protected ?AlgoliaConnector $connector = null;
14+
15+
protected ?ConfigHelper $config = null;
16+
17+
protected ?IndexOptionsInterface $indexOptions = null;
18+
19+
private ?ReplicaSettingsHandler $handler = null;
20+
21+
protected function setUp(): void
22+
{
23+
$this->connector = $this->createMock(AlgoliaConnector::class);
24+
$this->config = $this->createMock(ConfigHelper::class);
25+
$this->indexOptions = $this->createMock(IndexOptionsInterface::class);
26+
27+
$this->handler = new ReplicaSettingsHandlerTestable($this->connector, $this->config);
28+
}
29+
30+
public function testSetSettingsWithForwardingEnabledAndMixedSettings(): void
31+
{
32+
$storeId = 1;
33+
$settings = [
34+
'customRanking' => ['desc(price)'],
35+
'attributesToRetrieve' => ['name', 'price']
36+
];
37+
38+
$this->indexOptions->method('getStoreId')->willReturn($storeId);
39+
$this->config->method('shouldForwardPrimaryIndexSettingsToReplicas')
40+
->with($storeId)
41+
->willReturn(true);
42+
43+
$invocations = $this->exactly(2);
44+
$this->connector->expects($invocations)
45+
->method('setSettings')
46+
->willReturnCallback(
47+
function($indexOptions, $indexSettings, $forwardToReplicas, $mergeSettings, $mergeFrom) use ($invocations) {
48+
switch ($invocations->numberOfInvocations()) {
49+
case 1:
50+
$this->assertEquals(['attributesToRetrieve' => ['name', 'price']], $indexSettings);
51+
$this->assertTrue($forwardToReplicas);
52+
$this->assertFalse($mergeSettings);
53+
break;
54+
case 2:
55+
$this->assertEquals(['customRanking' => ['desc(price)']], $indexSettings);
56+
$this->assertFalse($forwardToReplicas);
57+
$this->assertTrue($mergeSettings);
58+
break;
59+
}
60+
});
61+
62+
$this->handler->setSettings($this->indexOptions, $settings);
63+
}
64+
65+
public function testSetSettingsWithForwardingEnabledOnlyExcludedSettings(): void
66+
{
67+
$storeId = 1;
68+
$settings = [
69+
'ranking' => ['asc(name)'],
70+
'customRanking' => ['desc(price)']
71+
];
72+
73+
$this->indexOptions->method('getStoreId')->willReturn($storeId);
74+
$this->config->method('shouldForwardPrimaryIndexSettingsToReplicas')
75+
->willReturn(true);
76+
77+
// Only one call expected (no forwarded settings since they are sorts)
78+
$this->connector->expects($this->once())
79+
->method('setSettings')
80+
->with(
81+
$this->indexOptions,
82+
$settings,
83+
false,
84+
true,
85+
''
86+
);
87+
88+
$this->handler->setSettings($this->indexOptions, $settings);
89+
}
90+
91+
public function testSetSettingsWithForwardingEnabledOnlyForwardableSettings(): void
92+
{
93+
$storeId = 1;
94+
$settings = [
95+
'attributesToHighlight' => ['title'],
96+
'attributesToRetrieve' => ['name']
97+
];
98+
99+
$this->indexOptions->method('getStoreId')->willReturn($storeId);
100+
$this->config->method('shouldForwardPrimaryIndexSettingsToReplicas')
101+
->willReturn(true);
102+
103+
// Only one call expected (all forwarded - no excluded settings)
104+
$this->connector->expects($this->once())
105+
->method('setSettings')
106+
->with(
107+
$this->indexOptions,
108+
$settings,
109+
true,
110+
false
111+
);
112+
113+
$this->handler->setSettings($this->indexOptions, $settings);
114+
}
115+
116+
public function testSetSettingsWithForwardingDisabled(): void
117+
{
118+
$storeId = 1;
119+
$settings = [
120+
'customRanking' => ['desc(price)'],
121+
'attributesToRetrieve' => ['name', 'price']
122+
];
123+
124+
$this->indexOptions->method('getStoreId')->willReturn($storeId);
125+
$this->config->method('shouldForwardPrimaryIndexSettingsToReplicas')
126+
->willReturn(false);
127+
128+
$this->connector->expects($this->once())
129+
->method('setSettings')
130+
->with(
131+
$this->indexOptions,
132+
$settings,
133+
false,
134+
true,
135+
''
136+
);
137+
138+
$this->handler->setSettings($this->indexOptions, $settings);
139+
}
140+
141+
public function testForwardSettingsWithEmptyInput(): void
142+
{
143+
$storeId = 1;
144+
$settings = [];
145+
146+
$this->indexOptions->method('getStoreId')->willReturn($storeId);
147+
$this->config->method('shouldForwardPrimaryIndexSettingsToReplicas')
148+
->willReturn(true);
149+
150+
// Connector should not be called
151+
$this->connector->expects($this->never())->method('setSettings');
152+
153+
$this->handler->setSettings($this->indexOptions, $settings);
154+
}
155+
156+
157+
public function testSplitSettings(): void
158+
{
159+
$settings = [
160+
'customRanking' => ['desc(price)'],
161+
'ranking' => ['asc(name)'],
162+
'attributesToRetrieve' => ['name']
163+
];
164+
165+
[$forward, $noForward] = $this->handler->splitSettings($settings);
166+
167+
$this->assertEquals(['attributesToRetrieve' => ['name']], $forward);
168+
$this->assertEquals([
169+
'customRanking' => ['desc(price)'],
170+
'ranking' => ['asc(name)']
171+
], $noForward);
172+
}
173+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Unit\Service;
4+
5+
use Algolia\AlgoliaSearch\Service\ReplicaSettingsHandler;
6+
7+
class ReplicaSettingsHandlerTestable extends ReplicaSettingsHandler
8+
{
9+
public function splitSettings(...$params): array
10+
{
11+
return parent::splitSettings(...$params);
12+
}
13+
}

0 commit comments

Comments
 (0)