|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SpameriTests\Elastic\Commands; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../../../bootstrap.php'; |
| 6 | + |
| 7 | +/** |
| 8 | + * @testCase |
| 9 | + */ |
| 10 | +class AddAliasTest extends \SpameriTests\Elastic\AbstractTestCase |
| 11 | +{ |
| 12 | + |
| 13 | + private const INDEX = 'spameri_command_alias_test'; |
| 14 | + private const ALIAS = 'spameri_alias_test'; |
| 15 | + private const ALIAS_2 = 'spameri_alias_test_2'; |
| 16 | + |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + // Delete index if exists |
| 23 | + /** @var \Spameri\Elastic\Model\Indices\Delete $delete */ |
| 24 | + $delete = $this->container->getByType(\Spameri\Elastic\Model\Indices\Delete::class); |
| 25 | + |
| 26 | + try { |
| 27 | + $delete->execute(self::INDEX); |
| 28 | + } catch (\Throwable $e) { |
| 29 | + // Ignore if index doesn't exist |
| 30 | + } |
| 31 | + |
| 32 | + // Create index |
| 33 | + /** @var \Spameri\Elastic\Model\Indices\Create $create */ |
| 34 | + $create = $this->container->getByType(\Spameri\Elastic\Model\Indices\Create::class); |
| 35 | + $create->execute(self::INDEX, []); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + public function testAddAliasToIndex(): void |
| 40 | + { |
| 41 | + /** @var \Spameri\Elastic\Commands\AddAlias $command */ |
| 42 | + $command = $this->container->getByType(\Spameri\Elastic\Commands\AddAlias::class); |
| 43 | + |
| 44 | + $input = new \Symfony\Component\Console\Input\ArrayInput([ |
| 45 | + 'index' => self::INDEX, |
| 46 | + 'alias' => self::ALIAS, |
| 47 | + ]); |
| 48 | + $output = new \Symfony\Component\Console\Output\BufferedOutput(); |
| 49 | + |
| 50 | + $result = $command->run($input, $output); |
| 51 | + |
| 52 | + \Tester\Assert::same(0, $result); |
| 53 | + |
| 54 | + // Verify alias was added by checking index info |
| 55 | + /** @var \Spameri\Elastic\Model\Indices\Get $getIndex */ |
| 56 | + $getIndex = $this->container->getByType(\Spameri\Elastic\Model\Indices\Get::class); |
| 57 | + $indexInfo = $getIndex->execute(self::INDEX); |
| 58 | + |
| 59 | + \Tester\Assert::true(isset($indexInfo[self::INDEX]['aliases'][self::ALIAS])); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public function testAddMultipleAliasesToIndex(): void |
| 64 | + { |
| 65 | + /** @var \Spameri\Elastic\Commands\AddAlias $command */ |
| 66 | + $command = $this->container->getByType(\Spameri\Elastic\Commands\AddAlias::class); |
| 67 | + |
| 68 | + // Add first alias |
| 69 | + $input = new \Symfony\Component\Console\Input\ArrayInput([ |
| 70 | + 'index' => self::INDEX, |
| 71 | + 'alias' => self::ALIAS, |
| 72 | + ]); |
| 73 | + $output = new \Symfony\Component\Console\Output\BufferedOutput(); |
| 74 | + $command->run($input, $output); |
| 75 | + |
| 76 | + // Add second alias |
| 77 | + $input = new \Symfony\Component\Console\Input\ArrayInput([ |
| 78 | + 'index' => self::INDEX, |
| 79 | + 'alias' => self::ALIAS_2, |
| 80 | + ]); |
| 81 | + $output = new \Symfony\Component\Console\Output\BufferedOutput(); |
| 82 | + $result = $command->run($input, $output); |
| 83 | + |
| 84 | + \Tester\Assert::same(0, $result); |
| 85 | + |
| 86 | + // Verify both aliases exist |
| 87 | + /** @var \Spameri\Elastic\Model\Indices\Get $getIndex */ |
| 88 | + $getIndex = $this->container->getByType(\Spameri\Elastic\Model\Indices\Get::class); |
| 89 | + $indexInfo = $getIndex->execute(self::INDEX); |
| 90 | + |
| 91 | + \Tester\Assert::true(isset($indexInfo[self::INDEX]['aliases'][self::ALIAS])); |
| 92 | + \Tester\Assert::true(isset($indexInfo[self::INDEX]['aliases'][self::ALIAS_2])); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public function testAddExistingAliasShowsError(): void |
| 97 | + { |
| 98 | + /** @var \Spameri\Elastic\Commands\AddAlias $command */ |
| 99 | + $command = $this->container->getByType(\Spameri\Elastic\Commands\AddAlias::class); |
| 100 | + |
| 101 | + // Add alias first time |
| 102 | + $input = new \Symfony\Component\Console\Input\ArrayInput([ |
| 103 | + 'index' => self::INDEX, |
| 104 | + 'alias' => self::ALIAS, |
| 105 | + ]); |
| 106 | + $output = new \Symfony\Component\Console\Output\BufferedOutput(); |
| 107 | + $command->run($input, $output); |
| 108 | + |
| 109 | + // Try to add same alias again |
| 110 | + $output = new \Symfony\Component\Console\Output\BufferedOutput(); |
| 111 | + $result = $command->run($input, $output); |
| 112 | + |
| 113 | + \Tester\Assert::same(0, $result); |
| 114 | + $outputContent = $output->fetch(); |
| 115 | + // Error message contains "already existing alias" |
| 116 | + \Tester\Assert::true( |
| 117 | + \str_contains($outputContent, 'already existing alias') || \str_contains($outputContent, 'already exists'), |
| 118 | + 'Output should indicate alias exists: ' . $outputContent, |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + public function testAliasCanBeUsedToQueryIndex(): void |
| 124 | + { |
| 125 | + // Add some data to index |
| 126 | + /** @var \Spameri\Elastic\ClientProvider $clientProvider */ |
| 127 | + $clientProvider = $this->container->getByType(\Spameri\Elastic\ClientProvider::class); |
| 128 | + |
| 129 | + $clientProvider->client()->index([ |
| 130 | + 'index' => self::INDEX, |
| 131 | + 'id' => 'test_doc', |
| 132 | + 'body' => ['title' => 'Test via alias'], |
| 133 | + ]); |
| 134 | + $clientProvider->client()->indices()->refresh(['index' => self::INDEX]); |
| 135 | + |
| 136 | + // Add alias |
| 137 | + /** @var \Spameri\Elastic\Commands\AddAlias $command */ |
| 138 | + $command = $this->container->getByType(\Spameri\Elastic\Commands\AddAlias::class); |
| 139 | + |
| 140 | + $input = new \Symfony\Component\Console\Input\ArrayInput([ |
| 141 | + 'index' => self::INDEX, |
| 142 | + 'alias' => self::ALIAS, |
| 143 | + ]); |
| 144 | + $output = new \Symfony\Component\Console\Output\BufferedOutput(); |
| 145 | + $command->run($input, $output); |
| 146 | + |
| 147 | + // Query using alias |
| 148 | + $response = $clientProvider->client()->search([ |
| 149 | + 'index' => self::ALIAS, |
| 150 | + 'body' => [ |
| 151 | + 'query' => ['match_all' => new \stdClass()], |
| 152 | + ], |
| 153 | + ])->asArray(); |
| 154 | + |
| 155 | + \Tester\Assert::same(1, $response['hits']['total']['value']); |
| 156 | + \Tester\Assert::same('Test via alias', $response['hits']['hits'][0]['_source']['title']); |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + protected function tearDown(): void |
| 161 | + { |
| 162 | + /** @var \Spameri\Elastic\Model\Indices\Delete $delete */ |
| 163 | + $delete = $this->container->getByType(\Spameri\Elastic\Model\Indices\Delete::class); |
| 164 | + |
| 165 | + try { |
| 166 | + $delete->execute(self::INDEX); |
| 167 | + } catch (\Throwable $e) { |
| 168 | + // Ignore |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | +(new AddAliasTest())->run(); |
0 commit comments