Files
crm_ecosplay/tests/MessageHandler/AnalyticsMessageHandlerTest.php
Serreau Jovann a4eb9f6e2d fix: supprimer toutes les PHPUnit notices (40 → 0) et deprecations (9 → 0)
Probleme: PHPUnit 13 genere des notices quand createMock() est utilise
sans expects(), et des deprecations pour \$this->any() et ->with()
sans expects().

Corrections:
- tests/Service/AppLoggerServiceTest.php: suppression du setUp() partage,
  chaque test cree ses propres stubs/mocks selon ses besoins
  (bus createMock avec expects dans les tests log, createStub dans verify)
- tests/EventSubscriber/CsrfProtectionSubscriberTest.php: csrfTokenManager
  change de createMock a createStub (aucun expects utilise)
- tests/EventSubscriber/MessengerFailureSubscriberTest.php: em et mailer
  changes de createMock a createStub (aucun expects utilise)
- tests/EventListener/AdminLogListenerTest.php: testLogThrowsDoesNotBlock
  cree son propre stub local au lieu d'utiliser le mock du setUp,
  attribut #[AllowMockObjectsWithoutExpectations] ajoute pour le mock
  du setUp qui reste instancie mais non utilise dans ce test
- tests/Controller/SmallControllersTest.php: mocks sans expects remplaces
  par createStub via script automatise
- tests/Controller/MainControllersTest.php: idem
- tests/Controller/Admin/ClientsControllerTest.php: idem
- tests/MessageHandler/AnalyticsMessageHandlerTest.php: idem
- tests/EventListener/ExceptionListenerTest.php: idem

Resultat: 262 tests, 454 assertions, 0 failures, 0 deprecations, 0 notices

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 23:53:03 +02:00

128 lines
4.2 KiB
PHP

<?php
namespace App\Tests\MessageHandler;
use App\Entity\AnalyticsEvent;
use App\Entity\AnalyticsUniqId;
use App\Entity\User;
use App\Message\AnalyticsMessage;
use App\MessageHandler\AnalyticsMessageHandler;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\TestCase;
class AnalyticsMessageHandlerTest extends TestCase
{
private EntityManagerInterface $em;
private AnalyticsMessageHandler $handler;
protected function setUp(): void
{
$this->em = $this->createMock(EntityManagerInterface::class);
$this->handler = new AnalyticsMessageHandler($this->em);
}
public function testInvokeVisitorNotFound(): void
{
$repository = $this->createStub(EntityRepository::class);
$repository->method('findOneBy')->willReturn(null);
$this->em->method('getRepository')->willReturn($repository);
$this->em->expects($this->never())->method('flush');
$message = new AnalyticsMessage('uid', 'action');
($this->handler)($message);
}
public function testInvokeSetUserSuccess(): void
{
$visitor = new AnalyticsUniqId();
$user = new User();
$visitorRepo = $this->createStub(EntityRepository::class);
$visitorRepo->method('findOneBy')->willReturn($visitor);
$userRepo = $this->createStub(EntityRepository::class);
$userRepo->method('find')->willReturn($user);
$this->em->method('getRepository')->willReturnMap([
[AnalyticsUniqId::class, $visitorRepo],
[User::class, $userRepo],
]);
$this->em->expects($this->once())->method('flush');
$message = new AnalyticsMessage('uid', 'set_user', ['userId' => 42]);
($this->handler)($message);
$this->assertSame($user, $visitor->getUser());
}
public function testInvokeSetUserNoUserId(): void
{
$visitor = new AnalyticsUniqId();
$visitorRepo = $this->createStub(EntityRepository::class);
$visitorRepo->method('findOneBy')->willReturn($visitor);
$this->em->method('getRepository')->willReturn($visitorRepo);
$this->em->expects($this->never())->method('flush');
$message = new AnalyticsMessage('uid', 'set_user', []);
($this->handler)($message);
}
public function testInvokeSetUserNotFound(): void
{
$visitor = new AnalyticsUniqId();
$visitorRepo = $this->createStub(EntityRepository::class);
$visitorRepo->method('findOneBy')->willReturn($visitor);
$userRepo = $this->createStub(EntityRepository::class);
$userRepo->method('find')->willReturn(null);
$this->em->method('getRepository')->willReturnMap([
[AnalyticsUniqId::class, $visitorRepo],
[User::class, $userRepo],
]);
$this->em->expects($this->never())->method('flush');
$message = new AnalyticsMessage('uid', 'set_user', ['userId' => 999]);
($this->handler)($message);
}
public function testInvokePageView(): void
{
$visitor = new AnalyticsUniqId();
$visitorRepo = $this->createStub(EntityRepository::class);
$visitorRepo->method('findOneBy')->willReturn($visitor);
$this->em->method('getRepository')->willReturn($visitorRepo);
$this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(AnalyticsEvent::class));
$this->em->expects($this->once())->method('flush');
$payload = [
'url' => '/test',
'title' => 'Test Title',
'referrer' => 'http://google.com'
];
$message = new AnalyticsMessage('uid', 'page_view', $payload);
($this->handler)($message);
}
public function testInvokePageViewEmptyPayload(): void
{
$visitor = new AnalyticsUniqId();
$visitorRepo = $this->createStub(EntityRepository::class);
$visitorRepo->method('findOneBy')->willReturn($visitor);
$this->em->method('getRepository')->willReturn($visitorRepo);
$this->em->expects($this->once())->method('persist');
$this->em->expects($this->once())->method('flush');
$message = new AnalyticsMessage('uid', 'page_view');
($this->handler)($message);
}
}