test: ajout tests AnalyticsEvent, AnalyticsUniqId, AppLog, Attestation
tests/Entity/AnalyticsTest.php (nouveau, 8 tests): - AnalyticsUniqId: constructor (id null, createdAt, events vide), setters (uid, hash, ipHash, userAgent, deviceType), screen (width, height), optionals (language, os, browser), user (nullable, set/get) - AnalyticsEvent: constructor (id null, createdAt), setters (visitor, eventName, url, title, referrer), nullables (title, referrer null par defaut) tests/Entity/AppLogTest.php (nouveau, 6 tests): - testConstructor: method, url, route, action, user null, ip null, hmac non vide, createdAt - testConstructorWithUser: user et ip passes au constructeur - testVerifyHmacValid: meme secret retourne true - testVerifyHmacInvalid: secret different retourne false - testHmacDeterministic: hmac identique a chaque verification - testDifferentDataProducesDifferentHmac: donnees differentes produisent des hmac differents tests/Entity/AttestationTest.php (nouveau, 9 tests): - testConstructor: type, ip, email, reference non vide, hmac non vide, status='pending', createdAt - testReferencePrefix: 'access' contient ACC, 'deletion' contient DEL, 'no_data' contient ABS dans la reference - testPdfFiles: unsigned, signed, certificate get/set nullable - testSubmitterId: get/set nullable - testMarkAsSigned: status passe a 'signed' - testMarkAsSent: status passe a 'sent' - testVerifyValid: meme secret retourne true - testVerifyInvalid: secret different retourne false - testEmailTracking: null par defaut Resultat: 338 tests, 670 assertions, 0 failures Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
104
tests/Entity/AnalyticsTest.php
Normal file
104
tests/Entity/AnalyticsTest.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use App\Entity\AnalyticsEvent;
|
||||
use App\Entity\AnalyticsUniqId;
|
||||
use App\Entity\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AnalyticsTest extends TestCase
|
||||
{
|
||||
// --- AnalyticsUniqId ---
|
||||
|
||||
public function testUniqIdConstructor(): void
|
||||
{
|
||||
$v = new AnalyticsUniqId();
|
||||
$this->assertNull($v->getId());
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $v->getCreatedAt());
|
||||
$this->assertCount(0, $v->getEvents());
|
||||
}
|
||||
|
||||
public function testUniqIdSetters(): void
|
||||
{
|
||||
$v = new AnalyticsUniqId();
|
||||
$v->setUid('uid-123');
|
||||
$v->setHash('hash-abc');
|
||||
$v->setIpHash('ip-hash');
|
||||
$v->setUserAgent('Mozilla/5.0');
|
||||
$v->setDeviceType('desktop');
|
||||
|
||||
$this->assertSame('uid-123', $v->getUid());
|
||||
$this->assertSame('hash-abc', $v->getHash());
|
||||
$this->assertSame('ip-hash', $v->getIpHash());
|
||||
$this->assertSame('Mozilla/5.0', $v->getUserAgent());
|
||||
$this->assertSame('desktop', $v->getDeviceType());
|
||||
}
|
||||
|
||||
public function testUniqIdScreen(): void
|
||||
{
|
||||
$v = new AnalyticsUniqId();
|
||||
$v->setScreenWidth(1920);
|
||||
$v->setScreenHeight(1080);
|
||||
$this->assertSame(1920, $v->getScreenWidth());
|
||||
$this->assertSame(1080, $v->getScreenHeight());
|
||||
}
|
||||
|
||||
public function testUniqIdOptionals(): void
|
||||
{
|
||||
$v = new AnalyticsUniqId();
|
||||
$v->setLanguage('fr');
|
||||
$v->setOs('Linux');
|
||||
$v->setBrowser('Firefox');
|
||||
$this->assertSame('fr', $v->getLanguage());
|
||||
$this->assertSame('Linux', $v->getOs());
|
||||
$this->assertSame('Firefox', $v->getBrowser());
|
||||
}
|
||||
|
||||
public function testUniqIdUser(): void
|
||||
{
|
||||
$v = new AnalyticsUniqId();
|
||||
$this->assertNull($v->getUser());
|
||||
|
||||
$user = new User();
|
||||
$user->setEmail('u@t.com');
|
||||
$user->setFirstName('U');
|
||||
$user->setLastName('T');
|
||||
$user->setPassword('h');
|
||||
$v->setUser($user);
|
||||
$this->assertSame($user, $v->getUser());
|
||||
}
|
||||
|
||||
// --- AnalyticsEvent ---
|
||||
|
||||
public function testEventConstructor(): void
|
||||
{
|
||||
$e = new AnalyticsEvent();
|
||||
$this->assertNull($e->getId());
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $e->getCreatedAt());
|
||||
}
|
||||
|
||||
public function testEventSetters(): void
|
||||
{
|
||||
$v = new AnalyticsUniqId();
|
||||
$e = new AnalyticsEvent();
|
||||
$e->setVisitor($v);
|
||||
$e->setEventName('pageview');
|
||||
$e->setUrl('/home');
|
||||
$e->setTitle('Accueil');
|
||||
$e->setReferrer('https://google.com');
|
||||
|
||||
$this->assertSame($v, $e->getVisitor());
|
||||
$this->assertSame('pageview', $e->getEventName());
|
||||
$this->assertSame('/home', $e->getUrl());
|
||||
$this->assertSame('Accueil', $e->getTitle());
|
||||
$this->assertSame('https://google.com', $e->getReferrer());
|
||||
}
|
||||
|
||||
public function testEventNullables(): void
|
||||
{
|
||||
$e = new AnalyticsEvent();
|
||||
$this->assertNull($e->getTitle());
|
||||
$this->assertNull($e->getReferrer());
|
||||
}
|
||||
}
|
||||
65
tests/Entity/AppLogTest.php
Normal file
65
tests/Entity/AppLogTest.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use App\Entity\AppLog;
|
||||
use App\Entity\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AppLogTest extends TestCase
|
||||
{
|
||||
public function testConstructor(): void
|
||||
{
|
||||
$log = new AppLog('GET', '/admin/dashboard', 'app_admin_dashboard', 'Consultation du tableau de bord', 'secret');
|
||||
$this->assertNull($log->getId());
|
||||
$this->assertSame('GET', $log->getMethod());
|
||||
$this->assertSame('/admin/dashboard', $log->getUrl());
|
||||
$this->assertSame('app_admin_dashboard', $log->getRoute());
|
||||
$this->assertSame('Consultation du tableau de bord', $log->getAction());
|
||||
$this->assertNull($log->getUser());
|
||||
$this->assertNull($log->getIp());
|
||||
$this->assertNotEmpty($log->getHmac());
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $log->getCreatedAt());
|
||||
}
|
||||
|
||||
public function testConstructorWithUser(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->setEmail('admin@test.com');
|
||||
$user->setFirstName('Admin');
|
||||
$user->setLastName('User');
|
||||
$user->setPassword('h');
|
||||
|
||||
$log = new AppLog('POST', '/admin/clients/create', 'app_admin_clients_create', 'Creation client', 'secret', $user, '192.168.1.1');
|
||||
$this->assertSame($user, $log->getUser());
|
||||
$this->assertSame('192.168.1.1', $log->getIp());
|
||||
$this->assertSame('POST', $log->getMethod());
|
||||
}
|
||||
|
||||
public function testVerifyHmacValid(): void
|
||||
{
|
||||
$log = new AppLog('GET', '/admin', 'route', 'action', 'my-secret');
|
||||
$this->assertTrue($log->verifyHmac('my-secret'));
|
||||
}
|
||||
|
||||
public function testVerifyHmacInvalid(): void
|
||||
{
|
||||
$log = new AppLog('GET', '/admin', 'route', 'action', 'my-secret');
|
||||
$this->assertFalse($log->verifyHmac('wrong-secret'));
|
||||
}
|
||||
|
||||
public function testHmacDeterministic(): void
|
||||
{
|
||||
$log = new AppLog('GET', '/admin', 'route', 'action', 'secret');
|
||||
$hmac1 = $log->getHmac();
|
||||
$this->assertTrue($log->verifyHmac('secret'));
|
||||
$this->assertSame($hmac1, $log->getHmac());
|
||||
}
|
||||
|
||||
public function testDifferentDataProducesDifferentHmac(): void
|
||||
{
|
||||
$log1 = new AppLog('GET', '/admin/a', 'route_a', 'action_a', 'secret');
|
||||
$log2 = new AppLog('GET', '/admin/b', 'route_b', 'action_b', 'secret');
|
||||
$this->assertNotSame($log1->getHmac(), $log2->getHmac());
|
||||
}
|
||||
}
|
||||
91
tests/Entity/AttestationTest.php
Normal file
91
tests/Entity/AttestationTest.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use App\Entity\Attestation;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AttestationTest extends TestCase
|
||||
{
|
||||
public function testConstructor(): void
|
||||
{
|
||||
$a = new Attestation('access', '127.0.0.1', 'test@test.com', 'secret');
|
||||
$this->assertNull($a->getId());
|
||||
$this->assertSame('access', $a->getType());
|
||||
$this->assertSame('127.0.0.1', $a->getIp());
|
||||
$this->assertSame('test@test.com', $a->getEmail());
|
||||
$this->assertNotEmpty($a->getReference());
|
||||
$this->assertNotEmpty($a->getHmac());
|
||||
$this->assertSame('pending', $a->getStatus());
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $a->getCreatedAt());
|
||||
}
|
||||
|
||||
public function testReferencePrefix(): void
|
||||
{
|
||||
$access = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
|
||||
$this->assertStringContainsString('ACC', $access->getReference());
|
||||
|
||||
$deletion = new Attestation('deletion', '1.1.1.1', 'a@t.com', 's');
|
||||
$this->assertStringContainsString('DEL', $deletion->getReference());
|
||||
|
||||
$noData = new Attestation('no_data', '1.1.1.1', 'a@t.com', 's');
|
||||
$this->assertStringContainsString('ABS', $noData->getReference());
|
||||
}
|
||||
|
||||
public function testPdfFiles(): void
|
||||
{
|
||||
$a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
|
||||
$this->assertNull($a->getPdfFileUnsigned());
|
||||
$this->assertNull($a->getPdfFileSigned());
|
||||
$this->assertNull($a->getPdfFileCertificate());
|
||||
|
||||
$a->setPdfFileUnsigned('/path/unsigned.pdf');
|
||||
$a->setPdfFileSigned('/path/signed.pdf');
|
||||
$a->setPdfFileCertificate('/path/cert.pdf');
|
||||
|
||||
$this->assertSame('/path/unsigned.pdf', $a->getPdfFileUnsigned());
|
||||
$this->assertSame('/path/signed.pdf', $a->getPdfFileSigned());
|
||||
$this->assertSame('/path/cert.pdf', $a->getPdfFileCertificate());
|
||||
}
|
||||
|
||||
public function testSubmitterId(): void
|
||||
{
|
||||
$a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
|
||||
$this->assertNull($a->getSubmitterId());
|
||||
|
||||
$a->setSubmitterId(42);
|
||||
$this->assertSame(42, $a->getSubmitterId());
|
||||
}
|
||||
|
||||
public function testMarkAsSigned(): void
|
||||
{
|
||||
$a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
|
||||
$a->markAsSigned();
|
||||
$this->assertSame('signed', $a->getStatus());
|
||||
}
|
||||
|
||||
public function testMarkAsSent(): void
|
||||
{
|
||||
$a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
|
||||
$a->markAsSent();
|
||||
$this->assertSame('sent', $a->getStatus());
|
||||
}
|
||||
|
||||
public function testVerifyValid(): void
|
||||
{
|
||||
$a = new Attestation('access', '1.1.1.1', 'a@t.com', 'my-secret');
|
||||
$this->assertTrue($a->verify('my-secret'));
|
||||
}
|
||||
|
||||
public function testVerifyInvalid(): void
|
||||
{
|
||||
$a = new Attestation('access', '1.1.1.1', 'a@t.com', 'my-secret');
|
||||
$this->assertFalse($a->verify('wrong-secret'));
|
||||
}
|
||||
|
||||
public function testEmailTracking(): void
|
||||
{
|
||||
$a = new Attestation('access', '1.1.1.1', 'a@t.com', 's');
|
||||
$this->assertNull($a->getEmailTracking());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user