Skip to content

Commit 9a87c31

Browse files
committed
Fix collecting index names when indices not provided
1 parent 389f6ea commit 9a87c31

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

src/Command/IndexCommand.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function getIndices(): Collection
3838

3939
protected function getEntitiesFromArgs(InputInterface $input, OutputInterface $output): Collection
4040
{
41+
$indices = $this->getIndices();
4142
$indexNames = collect();
4243

4344
if ($indexList = $input->getOption('indices')) {
@@ -52,18 +53,18 @@ protected function getEntitiesFromArgs(InputInterface $input, OutputInterface $o
5253
});
5354
}
5455

55-
$config = $this->searchService->getConfiguration();
56-
57-
if ((0 === count($indexNames)) && count($config->get('indices')) > 0) {
58-
$indexNames = $this->getIndices();
59-
}
60-
61-
if (0 === count($indexNames)) {
56+
if (0 === count($indexNames) && 0 === count($indices)) {
6257
$output->writeln(
6358
'<comment>No indices specified. Please either specify indices using the cli option or YAML configuration.</comment>'
6459
);
60+
61+
return collect();
62+
}
63+
64+
if (count($indexNames) > 0) {
65+
return $indices->reject(fn (array $item) => !in_array($item['name'], $indexNames->toArray(), true));
6566
}
6667

67-
return collect($this->getIndices())->reject(fn (array $item) => !in_array($item['name'], $indexNames->toArray(), true));
68+
return $indices;
6869
}
6970
}

tests/Integration/CommandsTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,60 @@ public function testSearchClearUnknownIndex(): void
5151
$this->assertStringContainsString('Cannot clear index. Not found.', $output);
5252
}
5353

54+
public function testSearchImportAndClearWithoutIndices(): void
55+
{
56+
for ($i = 0; $i <= 5; ++$i) {
57+
$this->createPost();
58+
}
59+
60+
for ($i = 0; $i <= 5; ++$i) {
61+
$this->createTag(['id' => $i]);
62+
}
63+
64+
$importCommand = $this->application->find('meili:import');
65+
$importCommandTester = new CommandTester($importCommand);
66+
$importCommandTester->execute([]);
67+
68+
$importOutput = $importCommandTester->getDisplay();
69+
70+
$this->assertSame(<<<'EOD'
71+
Importing for index MeiliSearch\Bundle\Test\Entity\Post
72+
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Post entities into sf_phpunit__posts index
73+
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Post entities into sf_phpunit__aggregated index
74+
Settings updated.
75+
Settings updated.
76+
Importing for index MeiliSearch\Bundle\Test\Entity\Comment
77+
Importing for index MeiliSearch\Bundle\Test\Entity\Tag
78+
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Tag entities into sf_phpunit__tags index
79+
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Tag entities into sf_phpunit__aggregated index
80+
Importing for index MeiliSearch\Bundle\Test\Entity\Link
81+
Importing for index MeiliSearch\Bundle\Test\Entity\Post
82+
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Post entities into sf_phpunit__posts index
83+
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Post entities into sf_phpunit__aggregated index
84+
Importing for index MeiliSearch\Bundle\Test\Entity\Tag
85+
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Tag entities into sf_phpunit__tags index
86+
Indexed 6 / 6 MeiliSearch\Bundle\Test\Entity\Tag entities into sf_phpunit__aggregated index
87+
Done!
88+
89+
EOD, $importOutput);
90+
91+
$clearCommand = $this->application->find('meili:clear');
92+
$clearCommandTester = new CommandTester($clearCommand);
93+
$clearCommandTester->execute([]);
94+
95+
$clearOutput = $clearCommandTester->getDisplay();
96+
97+
$this->assertSame(<<<'EOD'
98+
Cleared sf_phpunit__posts index of MeiliSearch\Bundle\Test\Entity\Post
99+
Cleared sf_phpunit__comments index of MeiliSearch\Bundle\Test\Entity\Comment
100+
Cleared sf_phpunit__aggregated index of MeiliSearch\Bundle\Test\Entity\ContentAggregator
101+
Cleared sf_phpunit__tags index of MeiliSearch\Bundle\Test\Entity\Tag
102+
Cleared sf_phpunit__tags index of MeiliSearch\Bundle\Test\Entity\Link
103+
Done!
104+
105+
EOD, $clearOutput);
106+
}
107+
54108
/**
55109
* Importing 'Tag' and 'Link' into the same 'tags' index.
56110
*/

0 commit comments

Comments
 (0)