|
| 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 | +} |
0 commit comments