60 lines
2.0 KiB
PHP
60 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Services\Elasticsearch\Client;
|
|
use System\Classes\Config;
|
|
|
|
class ElasticsearchClientTest extends TestCase
|
|
{
|
|
private Client $client;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->client = new Client(Config::get('elasticsearch', 'default') ?? []);
|
|
|
|
if (!$this->client->ping()) {
|
|
$this->markTestSkipped('Elasticsearch недоступен (контейнер не поднят) — см. docker-dev/docker-compose.yml');
|
|
}
|
|
|
|
$this->client->deleteIndex('bicycle_test_index');
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
// ping() ещё раз — если setUp() уже пропустил тест через markTestSkipped(),
|
|
// PHPUnit всё равно вызывает tearDown(), и deleteIndex() упадёт на реальном подключении.
|
|
if (isset($this->client) && $this->client->ping()) {
|
|
$this->client->deleteIndex('bicycle_test_index');
|
|
}
|
|
}
|
|
|
|
public function testIndexAndGetDocument(): void
|
|
{
|
|
$this->client->index('bicycle_test_index', ['title' => 'Bicycle'], 'doc1');
|
|
$document = $this->client->get('bicycle_test_index', 'doc1');
|
|
|
|
$this->assertSame('Bicycle', $document['_source']['title']);
|
|
}
|
|
|
|
public function testGetMissingDocumentReturnsNull(): void
|
|
{
|
|
$this->assertNull($this->client->get('bicycle_test_index', 'nonexistent'));
|
|
}
|
|
|
|
public function testDeleteDocument(): void
|
|
{
|
|
$this->client->index('bicycle_test_index', ['title' => 'Bicycle'], 'doc1');
|
|
$this->assertTrue($this->client->delete('bicycle_test_index', 'doc1'));
|
|
$this->assertNull($this->client->get('bicycle_test_index', 'doc1'));
|
|
}
|
|
|
|
public function testExists(): void
|
|
{
|
|
$this->client->index('bicycle_test_index', ['title' => 'Bicycle'], 'doc1');
|
|
$this->assertTrue($this->client->exists('bicycle_test_index', 'doc1'));
|
|
$this->assertFalse($this->client->exists('bicycle_test_index', 'nonexistent'));
|
|
}
|
|
}
|