Skip to content

Commit d69f507

Browse files
committed
added tests
1 parent a90a7b6 commit d69f507

File tree

132 files changed

+15309
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+15309
-8
lines changed

phpstan-baseline.neon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Unreachable statement \\- code above always terminates\\.$#"
5+
count: 1
6+
path: src/Factory/EntityFactory.php
7+
8+
-
9+
message: "#^Variable \\$propertyValue might not be defined\\.$#"
10+
count: 1
11+
path: src/Factory/EntityFactory.php

tests/SpameriTests/Elastic/AbstractTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ protected function setUp(): void
1616
$config->setTempDirectory(\TEMP_DIR);
1717
$config->addConfig(__DIR__ . '/Data/Config/Common.neon');
1818

19+
// Load Docker-specific config if running in Docker container
20+
if (\getenv('ELASTICSEARCH_HOST') !== false) {
21+
$config->addConfig(__DIR__ . '/Data/Config/Docker.neon');
22+
}
23+
1924
$this->config = $config;
2025
$this->container = $this->config->createContainer();
2126
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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();
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 CreateIndexTest extends \SpameriTests\Elastic\AbstractTestCase
11+
{
12+
13+
private const INDEX = 'spameri_command_create_test';
14+
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
$this->cleanupIndex();
20+
}
21+
22+
23+
private function cleanupIndex(): void
24+
{
25+
/** @var \Spameri\Elastic\ClientProvider $clientProvider */
26+
$clientProvider = $this->container->getByType(\Spameri\Elastic\ClientProvider::class);
27+
28+
try {
29+
// Delete by pattern (index name + timestamp variations)
30+
$clientProvider->client()->indices()->delete(['index' => self::INDEX . '*']);
31+
} catch (\Throwable $e) {
32+
// Ignore - index may not exist
33+
}
34+
}
35+
36+
37+
public function testCreateIndexSuccessfully(): void
38+
{
39+
/** @var \Spameri\Elastic\Commands\CreateIndex $command */
40+
$command = $this->container->getByType(\Spameri\Elastic\Commands\CreateIndex::class);
41+
42+
$input = new \Symfony\Component\Console\Input\ArrayInput([
43+
'indexName' => self::INDEX,
44+
]);
45+
$output = new \Symfony\Component\Console\Output\BufferedOutput();
46+
47+
$result = $command->run($input, $output);
48+
49+
\Tester\Assert::same(0, $result);
50+
\Tester\Assert::contains('Index ' . self::INDEX . ' created.', $output->fetch());
51+
52+
// Verify index exists
53+
/** @var \Spameri\Elastic\Model\Indices\Exists $exists */
54+
$exists = $this->container->getByType(\Spameri\Elastic\Model\Indices\Exists::class);
55+
\Tester\Assert::true($exists->execute(self::INDEX));
56+
}
57+
58+
59+
public function testCreateExistingIndexShowsError(): void
60+
{
61+
// First create the index via command
62+
/** @var \Spameri\Elastic\Commands\CreateIndex $command */
63+
$command = $this->container->getByType(\Spameri\Elastic\Commands\CreateIndex::class);
64+
65+
$input = new \Symfony\Component\Console\Input\ArrayInput([
66+
'indexName' => self::INDEX,
67+
]);
68+
$output = new \Symfony\Component\Console\Output\BufferedOutput();
69+
$command->run($input, $output);
70+
71+
// Now try via command again - should show error about existing
72+
$output = new \Symfony\Component\Console\Output\BufferedOutput();
73+
74+
$result = $command->run($input, $output);
75+
76+
\Tester\Assert::same(0, $result);
77+
// Should contain error message about existing index
78+
$outputContent = $output->fetch();
79+
\Tester\Assert::true(
80+
\str_contains($outputContent, 'already exists') || \str_contains($outputContent, 'You are trying to create'),
81+
'Output should contain error about existing index: ' . $outputContent,
82+
);
83+
}
84+
85+
86+
public function testCreateWithForceDeletesExisting(): void
87+
{
88+
// First create the index via command
89+
/** @var \Spameri\Elastic\Commands\CreateIndex $command */
90+
$command = $this->container->getByType(\Spameri\Elastic\Commands\CreateIndex::class);
91+
92+
$input = new \Symfony\Component\Console\Input\ArrayInput([
93+
'indexName' => self::INDEX,
94+
]);
95+
$output = new \Symfony\Component\Console\Output\BufferedOutput();
96+
$command->run($input, $output);
97+
98+
// Verify index was created
99+
/** @var \Spameri\Elastic\Model\Indices\Exists $exists */
100+
$exists = $this->container->getByType(\Spameri\Elastic\Model\Indices\Exists::class);
101+
\Tester\Assert::true($exists->execute(self::INDEX));
102+
103+
// Now force create via command - should delete and recreate
104+
$input = new \Symfony\Component\Console\Input\ArrayInput([
105+
'indexName' => self::INDEX,
106+
'--force' => true,
107+
]);
108+
$output = new \Symfony\Component\Console\Output\BufferedOutput();
109+
110+
$result = $command->run($input, $output);
111+
112+
\Tester\Assert::same(0, $result);
113+
$outputContent = $output->fetch();
114+
\Tester\Assert::contains('Index ' . self::INDEX . ' deleted.', $outputContent);
115+
\Tester\Assert::contains('Index ' . self::INDEX . ' created.', $outputContent);
116+
117+
// Verify index still exists
118+
\Tester\Assert::true($exists->execute(self::INDEX));
119+
}
120+
121+
122+
protected function tearDown(): void
123+
{
124+
$this->cleanupIndex();
125+
}
126+
127+
}
128+
129+
(new CreateIndexTest())->run();

0 commit comments

Comments
 (0)