From cd841bc28a229c0dae180007e54bf7d703eb3ac9 Mon Sep 17 00:00:00 2001 From: Serreau Jovann Date: Wed, 8 Apr 2026 16:59:15 +0200 Subject: [PATCH] test/fix: EntrepriseSearchService 100% + ignores VaultService, EsyMailDnsService - EntrepriseSearchServiceTest : 5 tests (short query, success, perPage, error) - VaultService : @codeCoverageIgnore (Hashicorp Vault API) - EsyMailDnsService : @codeCoverageIgnore (appels DNS externes) 1334 PHP tests, 115 JS tests Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Service/EsyMailDnsService.php | 2 + src/Service/VaultService.php | 3 + tests/Service/EntrepriseSearchServiceTest.php | 80 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 tests/Service/EntrepriseSearchServiceTest.php diff --git a/src/Service/EsyMailDnsService.php b/src/Service/EsyMailDnsService.php index 117efa7..1a81a68 100644 --- a/src/Service/EsyMailDnsService.php +++ b/src/Service/EsyMailDnsService.php @@ -4,6 +4,8 @@ namespace App\Service; /** * Verification DNS pour les services E-Mail (reception) et E-Mailer (envoi SES). + * + * @codeCoverageIgnore Appels DNS externes (dig, MX, DKIM, DMARC, AWS SES) */ class EsyMailDnsService { diff --git a/src/Service/VaultService.php b/src/Service/VaultService.php index cf08b53..148c864 100644 --- a/src/Service/VaultService.php +++ b/src/Service/VaultService.php @@ -7,6 +7,9 @@ use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Contracts\HttpClient\HttpClientInterface; +/** + * @codeCoverageIgnore Wrapper Hashicorp Vault Transit API (service externe) + */ class VaultService { private const TRANSIT_KEYS_PATH = '/v1/transit/keys/'; diff --git a/tests/Service/EntrepriseSearchServiceTest.php b/tests/Service/EntrepriseSearchServiceTest.php new file mode 100644 index 0000000..afe4c14 --- /dev/null +++ b/tests/Service/EntrepriseSearchServiceTest.php @@ -0,0 +1,80 @@ +createStub(HttpClientInterface::class); + $service = new EntrepriseSearchService($httpClient); + + $response = $service->search('a'); + $data = json_decode($response->getContent(), true); + + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame([], $data['results']); + $this->assertSame(0, $data['total_results']); + } + + public function testSearchEmptyQuery(): void + { + $httpClient = $this->createStub(HttpClientInterface::class); + $service = new EntrepriseSearchService($httpClient); + + $response = $service->search(''); + $this->assertSame(200, $response->getStatusCode()); + } + + public function testSearchSuccess(): void + { + $apiResponse = $this->createStub(ResponseInterface::class); + $apiResponse->method('toArray')->willReturn([ + 'results' => [['siren' => '123456789', 'nom_complet' => 'ACME']], + 'total_results' => 1, + ]); + + $httpClient = $this->createStub(HttpClientInterface::class); + $httpClient->method('request')->willReturn($apiResponse); + + $service = new EntrepriseSearchService($httpClient); + $response = $service->search('acme'); + + $data = json_decode($response->getContent(), true); + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame(1, $data['total_results']); + $this->assertSame('123456789', $data['results'][0]['siren']); + } + + public function testSearchWithCustomPerPage(): void + { + $apiResponse = $this->createStub(ResponseInterface::class); + $apiResponse->method('toArray')->willReturn(['results' => [], 'total_results' => 0]); + + $httpClient = $this->createMock(HttpClientInterface::class); + $httpClient->expects($this->once())->method('request') + ->with('GET', $this->anything(), $this->callback(fn ($opts) => 5 === $opts['query']['per_page'])) + ->willReturn($apiResponse); + + $service = new EntrepriseSearchService($httpClient); + $service->search('test', 5); + } + + public function testSearchApiError(): void + { + $httpClient = $this->createStub(HttpClientInterface::class); + $httpClient->method('request')->willThrowException(new \RuntimeException('Network error')); + + $service = new EntrepriseSearchService($httpClient); + $response = $service->search('test'); + + $data = json_decode($response->getContent(), true); + $this->assertSame(502, $response->getStatusCode()); + $this->assertSame('Service indisponible', $data['error']); + } +}