Skip to content

Commit a88d270

Browse files
committed
fixes after refactoring
1 parent b666edd commit a88d270

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
</source>
2323
<php>
2424
<server name="DB_CONNECTION" value="testing"/>
25+
<server name="ELASTICSEARCH_QUEUE_CONNECTION" value="sync"/>
2526
</php>
2627
</phpunit>

src/Events/EventHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ function ($event, $models): void {
2929
$name,
3030
$model::class,
3131
$model->getKeyName(),
32-
$model->getKey()
32+
$model->getKey(),
33+
$model->getElasticsearchId()
3334
));
3435
});
3536
}

src/Jobs/ModelEvent.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public function __construct(
5353
private readonly string $event,
5454
private readonly string $modelClass,
5555
private readonly string $keyName,
56-
private readonly mixed $keyValue
56+
private readonly mixed $keyValue,
57+
private readonly string $elasticsearchId,
5758
) {
5859
$this->onConnection(config('elastica-bridge.connection'));
5960
}
@@ -63,10 +64,18 @@ public function handle(): void
6364
$model = $this->modelClass::query()->where($this->keyName, $this->keyValue)->first();
6465

6566
if (! $model instanceof ElasticsearchableInterface || ! $model instanceof Model) {
67+
foreach ($this->matchingIndicesForElement() as $index) {
68+
if (! $index->getElasticaIndex()->exists()) {
69+
continue;
70+
}
71+
72+
$this->ensureModelMissingFromIndex($index);
73+
}
74+
6675
return;
6776
}
6877

69-
foreach ($this->matchingIndicesForElement($model) as $index) {
78+
foreach ($this->matchingIndicesForElement() as $index) {
7079
if (! $index->getElasticaIndex()->exists()) {
7180
continue;
7281
}
@@ -79,7 +88,7 @@ public function handle(): void
7988

8089
$shouldBePresent
8190
? $this->ensureModelPresentInIndex($index, $model)
82-
: $this->ensureModelMissingFromIndex($index, $model);
91+
: $this->ensureModelMissingFromIndex($index);
8392
}
8493
}
8594

@@ -89,23 +98,22 @@ private function ensureModelPresentInIndex(IndexInterface $index, Model $model):
8998
$index->getElasticaIndex()->addDocument($model->toElasticaDocument($index));
9099
}
91100

92-
/** @param ElasticsearchableInterface&Model $model */
93-
private function ensureModelMissingFromIndex(IndexInterface $index, Model $model): void
101+
private function ensureModelMissingFromIndex(IndexInterface $index): void
94102
{
95103
try {
96-
$index->getElasticaIndex()->deleteById($model->getElasticsearchId());
104+
$index->getElasticaIndex()->deleteById($this->elasticsearchId);
97105
} catch (ClientResponseException) {
98106
}
99107
}
100108

101109
/**
102110
* @return IndexInterface[]
103111
*/
104-
public function matchingIndicesForElement(Model $model): array
112+
public function matchingIndicesForElement(): array
105113
{
106114
return array_filter(
107115
app(IndexRepository::class)->all(),
108-
fn (IndexInterface $index): bool => in_array($model::class, $index->getAllowedDocuments(), true)
116+
fn (IndexInterface $index): bool => in_array($this->modelClass, $index->getAllowedDocuments(), true)
109117
);
110118
}
111119
}

tests/Feature/EventTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function test_event_update_disabled_listener(): void
4444
LaravelElasticaBridgeFacade::disableEventListener();
4545
$product = Product::all()->random();
4646
$oldName = $product->name;
47-
$newName = \Carbon\Carbon::now()->getTimestamp();
47+
$newName = now()->getTimestamp();
4848
$product->name = $newName;
4949
$this->assertSame($oldName, $this->productIndex->getDocumentInstance($product)->get('name'));
5050
$this->assertNotSame($oldName, $newName);
@@ -59,7 +59,7 @@ public function test_event_update_enabled_listener(): void
5959
LaravelElasticaBridgeFacade::enableEventListener();
6060
$product = Product::all()->random();
6161
$oldName = $product->name;
62-
$newName = \Carbon\Carbon::now()->getTimestamp();
62+
$newName = 'doc-'.now()->getTimestamp();
6363
$product->name = $newName;
6464
$this->assertSame($oldName, $this->productIndex->getDocumentInstance($product)->get('name'));
6565
$this->assertNotSame($oldName, $newName);

tests/Unit/ClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function test_configured_client(): void
2424
{
2525
$client = $this->elasticaClient->getClient();
2626

27-
$this->assertSame('localhost', $client->getConfig('host'));
28-
$this->assertEquals(9200, $client->getConfig('port'));
27+
$this->assertSame('localhost', $client->getTransport()->getNodePool()->nextNode()->getUri()->getHost());
28+
$this->assertSame(9200, $client->getTransport()->getNodePool()->nextNode()->getUri()->getPort());
2929

3030
}
3131

0 commit comments

Comments
 (0)