- 21 test files covering controllers, services, entities, enums, messages - CI: add test job with Xdebug coverage (clover + text) - SonarQube: configure coverage report path and test sources Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
103 lines
3.5 KiB
PHP
103 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
use App\Message\MeilisearchMessage;
|
|
use App\Service\MeilisearchService;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\Messenger\Envelope;
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
|
|
|
class MeilisearchServiceTest extends TestCase
|
|
{
|
|
private HttpClientInterface $httpClient;
|
|
private MessageBusInterface $bus;
|
|
private MeilisearchService $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->httpClient = $this->createMock(HttpClientInterface::class);
|
|
$this->bus = $this->createMock(MessageBusInterface::class);
|
|
$this->service = new MeilisearchService(
|
|
$this->httpClient,
|
|
$this->bus,
|
|
'http://meilisearch:7700',
|
|
'test-key',
|
|
);
|
|
}
|
|
|
|
public function testIndexExistsReturnsTrue(): void
|
|
{
|
|
$response = $this->createMock(ResponseInterface::class);
|
|
$response->method('getStatusCode')->willReturn(200);
|
|
$this->httpClient->method('request')->willReturn($response);
|
|
|
|
self::assertTrue($this->service->indexExists('events'));
|
|
}
|
|
|
|
public function testIndexExistsReturnsFalseOnException(): void
|
|
{
|
|
$this->httpClient->method('request')->willThrowException(new \RuntimeException('fail'));
|
|
|
|
self::assertFalse($this->service->indexExists('events'));
|
|
}
|
|
|
|
public function testCreateIndexDispatchesMessage(): void
|
|
{
|
|
$this->bus->expects(self::once())
|
|
->method('dispatch')
|
|
->with(self::callback(fn (MeilisearchMessage $m) => 'createIndex' === $m->action && 'events' === $m->index))
|
|
->willReturn(new Envelope(new \stdClass()));
|
|
|
|
$this->service->createIndex('events');
|
|
}
|
|
|
|
public function testDeleteIndexDispatchesMessage(): void
|
|
{
|
|
$this->bus->expects(self::once())
|
|
->method('dispatch')
|
|
->with(self::callback(fn (MeilisearchMessage $m) => 'deleteIndex' === $m->action))
|
|
->willReturn(new Envelope(new \stdClass()));
|
|
|
|
$this->service->deleteIndex('events');
|
|
}
|
|
|
|
public function testAddDocumentsDispatchesMessage(): void
|
|
{
|
|
$docs = [['id' => 1, 'title' => 'Test']];
|
|
$this->bus->expects(self::once())
|
|
->method('dispatch')
|
|
->with(self::callback(fn (MeilisearchMessage $m) => 'addDocuments' === $m->action && $m->payload['documents'] === $docs))
|
|
->willReturn(new Envelope(new \stdClass()));
|
|
|
|
$this->service->addDocuments('events', $docs);
|
|
}
|
|
|
|
public function testSearchMakesPostRequest(): void
|
|
{
|
|
$response = $this->createMock(ResponseInterface::class);
|
|
$response->method('getStatusCode')->willReturn(200);
|
|
$response->method('toArray')->willReturn(['hits' => []]);
|
|
$this->httpClient->method('request')
|
|
->with('POST', self::stringContains('/indexes/events/search'), self::anything())
|
|
->willReturn($response);
|
|
|
|
$result = $this->service->search('events', 'test');
|
|
|
|
self::assertArrayHasKey('hits', $result);
|
|
}
|
|
|
|
public function testRequestReturnsEmptyArrayOn204(): void
|
|
{
|
|
$response = $this->createMock(ResponseInterface::class);
|
|
$response->method('getStatusCode')->willReturn(204);
|
|
$this->httpClient->method('request')->willReturn($response);
|
|
|
|
$result = $this->service->request('DELETE', '/indexes/events');
|
|
|
|
self::assertSame([], $result);
|
|
}
|
|
}
|