9c311cb58b
Auto Tag Develop / tag (push) Successful in 11s
## Probleme (ERP-98) Suite PHPUnit flaky ~1 run sur 2 -> hook pre-commit qui plante, recours au `--no-verify` sur des commits sains. ## Cause racine Une seule cause commune : l'horloge `CLOCK_REALTIME` du conteneur n'est pas monotone sous WSL2/Docker (saut arriere sous charge), alors que le code et les tests supposaient une horloge stable. - **401 « Invalid JWT Token »** : lexik validait `iat`/`nbf`/`exp` avec `clock_skew: 0` (`LooseValidAt(.., PT0S)` cote lcobucci). Un recul d'horloge apres `/login_check` rend le token « dans le futur » -> rejet. - **Horodatages « meme seconde »** (`1780402904 > 1780402904`) : colonnes `TIMESTAMP(0)` + `sleep(1)` reel. L'ecart floor-seconde n'est nul que si l'horloge recule. ## Correctifs | Fichier | Modif | |---|---| | `config/packages/lexik_jwt_authentication.yaml` | `clock_skew: 15` -> tolere la derive (benefice prod aussi) | | `TimestampableBlamableSubscriber` | injection `ClockInterface` (prod inchange via NativeClock) | | `CategoryTimestampableBlamableTest` | `ClockSensitiveTrait` + MockClock fige/avance, suppression des `sleep(1)` | | `TimestampableBlamableSubscriberTest` | MockClock injecte dans les 4 instanciations | **Subtilite** : `mockTime()` cree un MockClock en UTC ; les colonnes `TIMESTAMP WITHOUT TIME ZONE` round-trippent via le fuseau PHP (Europe/Paris) -> decalage 2h. Le mock est seede dans le fuseau par defaut (comme le NativeClock prod). ## Verifications - `make test` : **464 tests verts**, 0 echec / 0 erreur - Test timestamp cible : **5/5 deterministe** (et plus rapide, sleeps reels supprimes) - `make php-cs-fixer-allow-risky` : 0 fichier a corriger - Deprecations/notices PHPUnit preexistantes (hors perimetre) Pas de migration, pas de changement front, RBAC intact. --------- Co-authored-by: admin malio <malio@yuno.malio.fr> Co-authored-by: Matthieu <contact@malio.fr> Reviewed-on: #47 Co-authored-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-committed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
166 lines
5.8 KiB
PHP
166 lines
5.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Shared\Infrastructure\Doctrine;
|
|
|
|
use App\Shared\Domain\Contract\BlamableInterface;
|
|
use App\Shared\Domain\Contract\TimestampableInterface;
|
|
use App\Shared\Domain\Trait\TimestampableBlamableTrait;
|
|
use App\Shared\Infrastructure\Doctrine\TimestampableBlamableSubscriber;
|
|
use DateTimeImmutable;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\Event\PrePersistEventArgs;
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\Clock\MockClock;
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
|
|
|
/**
|
|
* Tests unitaires du TimestampableBlamableSubscriber.
|
|
*
|
|
* On exerce directement prePersist / preUpdate avec un EntityManager et une
|
|
* Security stubbes — aucun boot de kernel, aucun acces BDD. Les entites de test
|
|
* sont des fixtures internes (cf. bas de fichier).
|
|
*
|
|
* @internal
|
|
*/
|
|
final class TimestampableBlamableSubscriberTest extends TestCase
|
|
{
|
|
public function testPrePersistWithUser(): void
|
|
{
|
|
$user = $this->createStub(UserInterface::class);
|
|
$subscriber = new TimestampableBlamableSubscriber($this->securityReturning($user), new MockClock());
|
|
$entity = new FullAuditableFixture();
|
|
|
|
$subscriber->prePersist($this->prePersistArgs($entity));
|
|
|
|
// Les 4 colonnes sont remplies : dates posees, blame = user courant.
|
|
self::assertInstanceOf(DateTimeImmutable::class, $entity->getCreatedAt());
|
|
self::assertInstanceOf(DateTimeImmutable::class, $entity->getUpdatedAt());
|
|
self::assertSame($entity->getCreatedAt(), $entity->getUpdatedAt());
|
|
self::assertSame($user, $entity->getCreatedBy());
|
|
self::assertSame($user, $entity->getUpdatedBy());
|
|
}
|
|
|
|
public function testPrePersistWithoutUser(): void
|
|
{
|
|
$subscriber = new TimestampableBlamableSubscriber($this->securityReturning(null), new MockClock());
|
|
$entity = new FullAuditableFixture();
|
|
|
|
$subscriber->prePersist($this->prePersistArgs($entity));
|
|
|
|
// Hors contexte HTTP (CLI / cron) : dates remplies, blame laisse a null.
|
|
self::assertInstanceOf(DateTimeImmutable::class, $entity->getCreatedAt());
|
|
self::assertInstanceOf(DateTimeImmutable::class, $entity->getUpdatedAt());
|
|
self::assertNull($entity->getCreatedBy());
|
|
self::assertNull($entity->getUpdatedBy());
|
|
}
|
|
|
|
public function testPreUpdate(): void
|
|
{
|
|
$user = $this->createStub(UserInterface::class);
|
|
// Horloge figee 1s apres le createdAt simule : updatedAt doit avancer
|
|
// de facon deterministe, sans dependre de l'heure reelle.
|
|
$subscriber = new TimestampableBlamableSubscriber(
|
|
$this->securityReturning($user),
|
|
new MockClock(new DateTimeImmutable('2020-01-01 10:00:01')),
|
|
);
|
|
|
|
// On simule une entite deja persistee : createdAt fige dans le passe,
|
|
// createdBy positionne par une creation anterieure.
|
|
$createdAt = new DateTimeImmutable('2020-01-01 10:00:00');
|
|
$entity = new FullAuditableFixture();
|
|
$entity->setCreatedAt($createdAt);
|
|
$entity->setUpdatedAt($createdAt);
|
|
|
|
$subscriber->preUpdate($this->preUpdateArgs($entity));
|
|
|
|
// updatedAt avance, createdAt reste fige, updatedBy = user courant.
|
|
self::assertSame($createdAt, $entity->getCreatedAt());
|
|
self::assertGreaterThan($createdAt, $entity->getUpdatedAt());
|
|
self::assertSame($user, $entity->getUpdatedBy());
|
|
}
|
|
|
|
public function testPartialEntityTimestampableOnly(): void
|
|
{
|
|
$user = $this->createStub(UserInterface::class);
|
|
$subscriber = new TimestampableBlamableSubscriber($this->securityReturning($user), new MockClock());
|
|
$entity = new TimestampableOnlyFixture();
|
|
|
|
// Entite Timestampable mais NON Blamable : seules les dates sont posees,
|
|
// aucun appel de blame (et aucune erreur).
|
|
$subscriber->prePersist($this->prePersistArgs($entity));
|
|
|
|
self::assertInstanceOf(DateTimeImmutable::class, $entity->getCreatedAt());
|
|
self::assertInstanceOf(DateTimeImmutable::class, $entity->getUpdatedAt());
|
|
}
|
|
|
|
/**
|
|
* Security stubbee renvoyant l'utilisateur fourni (ou null).
|
|
*/
|
|
private function securityReturning(?UserInterface $user): Security
|
|
{
|
|
$security = $this->createStub(Security::class);
|
|
$security->method('getUser')->willReturn($user);
|
|
|
|
return $security;
|
|
}
|
|
|
|
private function prePersistArgs(object $entity): PrePersistEventArgs
|
|
{
|
|
return new PrePersistEventArgs($entity, $this->createStub(EntityManagerInterface::class));
|
|
}
|
|
|
|
private function preUpdateArgs(object $entity): PreUpdateEventArgs
|
|
{
|
|
$changeSet = [];
|
|
|
|
return new PreUpdateEventArgs($entity, $this->createStub(EntityManagerInterface::class), $changeSet);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fixture interne : entite metier complete (Timestampable + Blamable) via le
|
|
* Trait reel teste.
|
|
*
|
|
* @internal
|
|
*/
|
|
final class FullAuditableFixture implements TimestampableInterface, BlamableInterface
|
|
{
|
|
use TimestampableBlamableTrait;
|
|
}
|
|
|
|
/**
|
|
* Fixture interne : entite Timestampable seule (sans Blamable), pour verifier
|
|
* la dissociation des deux contrats par le Subscriber.
|
|
*
|
|
* @internal
|
|
*/
|
|
final class TimestampableOnlyFixture implements TimestampableInterface
|
|
{
|
|
private ?DateTimeImmutable $createdAt = null;
|
|
private ?DateTimeImmutable $updatedAt = null;
|
|
|
|
public function getCreatedAt(): ?DateTimeImmutable
|
|
{
|
|
return $this->createdAt;
|
|
}
|
|
|
|
public function setCreatedAt(DateTimeImmutable $createdAt): void
|
|
{
|
|
$this->createdAt = $createdAt;
|
|
}
|
|
|
|
public function getUpdatedAt(): ?DateTimeImmutable
|
|
{
|
|
return $this->updatedAt;
|
|
}
|
|
|
|
public function setUpdatedAt(DateTimeImmutable $updatedAt): void
|
|
{
|
|
$this->updatedAt = $updatedAt;
|
|
}
|
|
}
|