Share
Embed
Les tests dans une application Symfony
Écrire des tests pour son projet est une assurance pour l'efficacité de développement et pour la chaîne de livraison du logiciel. Dans Symfony, on utilise PHPUnit pour cela, et on dispose de facilités pour écrire des tests fonctionnels.
En mettant en pratique la documentation, on se retrouve confronté à des questions comme : comment avoir une suite de tests maintenable ? dois-je faire des tests unitaires ou fonctionnels ? comment gérer les dépendances aux services tiers dans les tests ? comment je teste l'envoi de mails ? comment je dois organiser mes données de tests ?
Cette conférence est le résumé de plus de 10 ans de pratique de tests automatisés avec Symfony.
Alexandre Salomé
March 27, 2023
Transcript
150 slides
-
Notre cas pratique Hello Alice, Thank you for registering! To
confirm your registration, please click on the link below: Happy conferences, The speaker Click here to activate your account From: The App <[email protected]> Subject: Finish your registration -
Application Service Exemple User Repository Service Mailer Service User Manager
Request Response User -
Les différents types de tests 3 types de test définis
par Symfony : 1. Les test unitaires 2. Les tests d’intégration 3. Les tests applicatifs Tests applicatifs Tests d’intégration Tests unitaires -
Un test unitaire garantit le fonctionnement d’une unité logicielle (une
classe, une méthode). -
Application Service Les tests unitaires User Repository Service Mailer Service
User Manager Request Response User -
Les tests unitaires use PHPUnit\Framework\TestCase; class UserTest extends TestCase {
public function testActivate(): void { $user = new User(); $this→assertFalse($user→isActivated()); $user→activate(); $this→assertTrue($user→isActivated()); } } -
Les tests unitaires use PHPUnit\Framework\TestCase; class UserTest extends TestCase {
public function testActivate(): void { $user = new User(); $this→assertFalse($user→isActivated()); $user→activate(); $this→assertTrue($user→isActivated()); } } -
Les tests unitaires use PHPUnit\Framework\TestCase; class UserTest extends TestCase {
public function testActivate(): void { $user = new User(); $this→assertFalse($user→isActivated()); $user→activate(); $this→assertTrue($user→isActivated()); } } -
Les tests unitaires use PHPUnit\Framework\TestCase; class UserTest extends TestCase {
public function testActivate(): void { $user = new User(); $this→assertFalse($user→isActivated()); $user→activate(); $this→assertTrue($user→isActivated()); } } -
Les tests unitaires use PHPUnit\Framework\TestCase; class UserTest extends TestCase {
public function testActivate(): void public function testActivateTwice(): void public function testActivateWhenLocked(): void } -
Les tests unitaires Avantages - Rapides à exécuter - Simples
et fondamental Inconvénients - Bouchons pour les dépendances 17 -
Application Service Les tests d’intégration User Repository Service Mailer Service
User Manager Request Response User -
Application Service Les tests d’intégration User Repository Service Mailer Service
User Manager Request Response User -
use App\Entity\User; use App\Manager\UserManager; use App\Test\UserActionsTrait ; use Symfony\Bundle\FrameworkBundle \Test\KernelTestCase
; class RegistrationManagerTest extends KernelTestCase { use UserActionsTrait ; // Helper methods used below public function testRegisterExistingUser (): void { $this->createUser('alice'); $user = new User(); $user->setEmail('[email protected] '); $user->setName('Alice'); $manager = self::getContainer()->get(UserManager::class); $manager->register($user); $this->assertAlreadyRegisteredMailSent (); } } -
use App\Entity\User; use App\Manager\UserManager; use App\Test\UserActionsTrait ; use Symfony\Bundle\FrameworkBundle \Test\KernelTestCase
; class RegistrationManagerTest extends KernelTestCase { use UserActionsTrait ; // Helper methods used below public function testRegisterExistingUser (): void { $this->createUser('alice'); $user = new User(); $user->setEmail('[email protected] '); $user->setName('Alice'); $manager = self::getContainer()->get(UserManager::class); $manager->register($user); $this->assertAlreadyRegisteredMailSent (); } } -
use App\Entity\User; use App\Manager\UserManager; use App\Test\UserActionsTrait ; use Symfony\Bundle\FrameworkBundle \Test\KernelTestCase
; class RegistrationManagerTest extends KernelTestCase { use UserActionsTrait ; // Helper methods used below public function testRegisterExistingUser (): void { $this->createUser('alice'); $user = new User(); $user->setEmail('[email protected] '); $user->setName('Alice'); $manager = self::getContainer()->get(UserManager::class); $manager->register($user); $this->assertAlreadyRegisteredMailSent (); } } -
use App\Entity\User; use App\Manager\UserManager; use App\Test\UserActionsTrait ; use Symfony\Bundle\FrameworkBundle \Test\KernelTestCase
; class RegistrationManagerTest extends KernelTestCase { use UserActionsTrait ; // Helper methods used below public function testRegisterExistingUser (): void { $this->createUser('alice'); $user = new User(); $user->setEmail('[email protected] '); $user->setName('Alice'); $manager = self::getContainer()->get(UserManager::class); $manager->register($user); $this->assertAlreadyRegisteredMailSent (); } } -
use App\Entity\User; use App\Manager\UserManager; use App\Test\UserActionsTrait ; use Symfony\Bundle\FrameworkBundle \Test\KernelTestCase
; class RegistrationManagerTest extends KernelTestCase { use UserActionsTrait ; // Helper methods used below public function testRegisterExistingUser (): void { $this->createUser('alice'); $user = new User(); $user->setEmail('[email protected] '); $user->setName('Alice'); $manager = self::getContainer()->get(UserManager::class); $manager->register($user); $this->assertAlreadyRegisteredMailSent (); } } -
use App\Entity\User; use App\Manager\UserManager; use App\Test\UserActionsTrait ; use Symfony\Bundle\FrameworkBundle \Test\KernelTestCase
; class RegistrationManagerTest extends KernelTestCase { use UserActionsTrait ; // Helper methods used below public function testRegisterExistingUser (): void { $this->createUser('alice'); $user = new User(); $user->setEmail('[email protected] '); $user->setName('Alice'); $manager = self::getContainer()->get(UserManager::class); $manager->register($user); $this->assertAlreadyRegisteredMailSent (); } } -
use App\Entity\User; use App\Manager\UserManager; use App\Test\UserActionsTrait ; use Symfony\Bundle\FrameworkBundle \Test\KernelTestCase
; class RegistrationManagerTest extends KernelTestCase { use UserActionsTrait ; // Helper methods used below public function testRegisterExistingUser (): void { $this->createUser('alice'); $user = new User(); $user->setEmail('[email protected] '); $user->setName('Alice'); $manager = self::getContainer()->get(UserManager::class); $manager->register($user); $this->assertAlreadyRegisteredMailSent (); } } -
Les tests d’intégration Avantages - Permet de tester n’importe quel
service - Conditions réelles du service Inconvénients - Couplage du service non visible 29 -
Les tests applicatifs testent le bon fonctionnement des différentes couches
de l’application combinées ensemble. -
Application Service Les tests applicatifs User Repository Service Mailer Service
User Manager Request Response User -
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class RegisterControllerTest extends WebTestCase { public function testValid():
void { $client = self::createClient(); $crawler = $client→request('GET', '/register'); $form = $crawler→filter('main form')→form([ 'register[name]' => 'Test User', 'register[email]' => '[email protected]', ]); $client→submit($form); $this→assertResponseStatusCodeSame(302); $this→assertResponseRedirects('/register/confirmation'); } } -
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class RegisterControllerTest extends WebTestCase { public function testValid():
void { $client = self::createClient(); $crawler = $client→request('GET', '/register'); $form = $crawler→filter('main form')→form([ 'register[name]' => 'Test User', 'register[email]' => '[email protected]', ]); $client→submit($form); $this→assertResponseStatusCodeSame(302); $this→assertResponseRedirects('/register/confirmation'); } } -
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class RegisterControllerTest extends WebTestCase { public function testValid():
void { $client = self::createClient(); $crawler = $client→request('GET', '/register'); $form = $crawler→filter('main form')→form([ 'register[name]' => 'Test User', 'register[email]' => '[email protected]', ]); $client→submit($form); $this→assertResponseStatusCodeSame(302); $this→assertResponseRedirects('/register/confirmation'); } } -
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class RegisterControllerTest extends WebTestCase { public function testValid():
void { $client = self::createClient(); $crawler = $client→request('GET', '/register'); $form = $crawler→filter('main form')→form([ 'register[name]' => 'Test User', 'register[email]' => '[email protected]', ]); $client→submit($form); $this→assertResponseStatusCodeSame(302); $this→assertResponseRedirects('/register/confirmation'); } } -
PHPUnit 9.5.28 by Sebastian Bergmann and contributors. Testing App\Tests\Manager\User\RegistrationManagerTest F
1 / 1 (100%) Time: 00:00.413, Memory: 32.00 MB There was 1 failure: 1) App\Tests\Manager\User\RegistrationManagerTest::testRegister Failed asserting that Symfony\Component\HttpFoundation\RedirectResponse Object ... ( # ... 'targetUrl' => '/register/actual' ) is redirected and has header "Location" with value "/register/expected". -
PHPUnit 9.5.28 by Sebastian Bergmann and contributors. Testing App\Tests\Manager\User\RegistrationManagerTest F
1 / 1 (100%) Time: 00:00.413, Memory: 32.00 MB There was 1 failure: 1) App\Tests\Manager\User\RegistrationManagerTest::testRegister Failed asserting that Symfony\Component\DomCrawler\Crawler Object ... ( ) matches selector "h1" and the text "register.title" of the node matching selector "body" contains "Register". -
Les tests applicatifs Avantages - Tests de l’extérieur de l’application
- Utilitaires de WebTestCase Inconvénients - Tests gourmands en ressources 41 -
Installer PHPUnit Utilisez le pack de test de Symfony :
composer require --dev symfony/test-pack Cela installe PHPUnit ainsi que les outils que nous allons voir. -
Tester l’envoi d’emails public function testRegister(): void { // Fill
and submit the form $crawler = $this→client→request('GET', '/register'); $form = $crawler→filter('form')→form([ 'register[name]' => 'Alice', 'register[email]' => '[email protected]', ]); $this→client→submit($form); $this→assertResponseIsSuccessful(); } -
Tester l’envoi d’emails Hello Alice, Thank you for registering! To
confirm your registration, please click on the link below: Happy conferences, The speaker Click here to activate your account From: The App <[email protected]> Subject: Finish your registration -
// Verify that the registration mail has been sent $this→assertQueuedEmailCount(1);
// Get the email $email = $this→getMailerMessage(); // Assertions on the mail $this→assertEmailHeaderSame( $email, 'Subject', 'Finish your registration' ); $this→assertEmailHtmlBodyContains( $email, 'Click here to activate your account' ); -
// Verify that the registration mail has been sent $this→assertQueuedEmailCount(1);
// Get the email $email = $this→getMailerMessage(); // Assertions on the mail $this→assertEmailHeaderSame( $email, 'Subject', 'Finish your registration' ); $this→assertEmailHtmlBodyContains( $email, 'Click here to activate your account' ); -
// Verify that the registration mail has been sent $this→assertQueuedEmailCount(1);
// Get the email $email = $this→getMailerMessage(); // Assertions on the mail $this→assertEmailHeaderSame( $email, 'Subject', 'Finish your registration' ); $this→assertEmailHtmlBodyContains( $email, 'Click here to activate your account' ); -
// Verify that the registration mail has been sent $this→assertQueuedEmailCount(1);
// Get the email $email = $this→getMailerMessage(); // Assertions on the mail $this→assertEmailHeaderSame( $email, 'Subject', 'Finish your registration' ); $this→assertEmailHtmlBodyContains( $email, 'Click here to activate your account' ); -
// Verify that the registration mail has been sent $this→assertQueuedEmailCount(1);
// Get the email $email = $this→getMailerMessage(); // Assertions on the mail $this→assertEmailHeaderSame( $email, 'Subject', 'Finish your registration' ); $this→assertEmailHtmlBodyContains( $email, 'Click here to activate your account' ); -
Tester l’envoi d’emails Hello Alice, Thank you for registering! To
confirm your registration, please click on the link below: Happy conferences, The speaker Click here to activate your account From: The App <[email protected]> Subject: Finish your registration -
public function testRegister(): void { // ... // Extract the
link $link = (new Crawler($email->getHtmlBody())) ->filter('a:contains("Click here to activate your account")' ) ; $href = $link->attr('href'); // Browse the link $response = $this->client->request('GET', $href); $this->assertResponseIsSuccessful (); } -
public function testRegister(): void { // ... // Extract the
link $link = (new Crawler($email->getHtmlBody())) ->filter('a:contains("Click here to activate your account")' ) ; $href = $link->attr('href'); // Browse the link $response = $this->client->request('GET', $href); $this->assertResponseIsSuccessful (); } -
public function testRegister(): void { // ... // Extract the
link $link = (new Crawler($email->getHtmlBody())) ->filter('a:contains("Click here to activate your account")' ) ; $href = $link->attr('href'); // Browse the link $response = $this->client->request('GET', $href); $this->assertResponseIsSuccessful (); } -
public function testRegister(): void { // ... // Extract the
link $link = (new Crawler($email->getHtmlBody())) ->filter('a:contains("Click here to activate your account")' ) ; $href = $link->attr('href'); // Browse the link $response = $this->client->request('GET', $href); $this->assertResponseIsSuccessful (); } -
public function testRegister(): void { // ... // Extract the
link $link = (new Crawler($email->getHtmlBody())) ->filter('a:contains("Click here to activate your account")' ) ; $href = $link->attr('href'); // Browse the link $response = $this->client->request('GET', $href); $this->assertResponseIsSuccessful (); } -
Tests Messenger Supposons que notre UserManager a la méthode suivante
: public function register(User $user): void { $this→repository→save($user); $this→sendRegistrationMail($user); $this→messageBus→dispatch( new RegistrationMessage($user→getId()), ); } -
Tests Messenger Supposons que notre UserManager a la méthode suivante
: public function register(User $user): void { $this→repository→save($user); $this→sendRegistrationMail($user); $this→messageBus→dispatch( new RegistrationMessage($user→getId()), ); } -
Tests Messenger # app/config/messenger.yaml framework: messenger: transports: async: '%env(MESSENGER_TRANSPORT_DSN)%' when@test:
framework: messenger: transports: async: 'in-memory://' -
Tests Messenger On peut vérifier l’envoi d’un événement via le
transport “in-memory”: $transport = self::getContainer()->get('messenger.transport.async' ); $this->assertCount(1, $transport->getSent()); $message = $transport->getSent()[0]->getMessage(); $this->assertInstanceOf (RegistrationMessage ::class, $message); -
Tests Messenger On peut vérifier l’envoi d’un événement via le
transport “in-memory”: $transport = self::getContainer()->get('messenger.transport.async' ); $this->assertCount(1, $transport->getSent()); $message = $transport->getSent()[0]->getMessage(); $this->assertInstanceOf (RegistrationMessage ::class, $message); -
Tests Messenger On peut vérifier l’envoi d’un événement via le
transport “in-memory”: $transport = self::getContainer()->get('messenger.transport.async' ); $this->assertCount(1, $transport->getSent()); $message = $transport->getSent()[0]->getMessage(); $this->assertInstanceOf (RegistrationMessage ::class, $message); -
Tests Messenger On peut vérifier l’envoi d’un événement via le
transport “in-memory”: $transport = self::getContainer()->get('messenger.transport.async' ); $this->assertCount(1, $transport->getSent()); $message = $transport->getSent()[0]->getMessage(); $this->assertInstanceOf (RegistrationMessage ::class, $message); -
Clock mocking Supposons qu’on veuille faire le test suivant: public
function testIsActivationKeyValid (): void { $user = new User(); $key = $user->getActivationKey (); $this->assertTrue($user->isActivationKeyValid ($key)); sleep(2 * 24 * 60 * 60); // 2 days $this->assertFalse($user->isActivationKeyValid ($key)); } -
Clock mocking Supposons qu’on veuille faire le test suivant: public
function testIsActivationKeyValid (): void { $user = new User(); $key = $user->getActivationKey (); $this->assertTrue($user->isActivationKeyValid ($key)); sleep(2 * 24 * 60 * 60); // 2 days $this->assertFalse($user->isActivationKeyValid ($key)); } -
Clock mocking Supposons qu’on veuille faire le test suivant: public
function testIsActivationKeyValid (): void { $user = new User(); $key = $user->getActivationKey (); $this->assertTrue($user->isActivationKeyValid ($key)); sleep(2 * 24 * 60 * 60); // 2 days $this->assertFalse($user->isActivationKeyValid ($key)); } -
Clock mocking Supposons qu’on veuille faire le test suivant: public
function testIsActivationKeyValid (): void { $user = new User(); $key = $user->getActivationKey (); $this->assertTrue($user->isActivationKeyValid ($key)); sleep(2 * 24 * 60 * 60); // 2 days $this->assertFalse($user->isActivationKeyValid ($key)); } -
Clock mocking Supposons qu’on veuille faire le test suivant: public
function testIsActivationKeyValid (): void { $user = new User(); $key = $user->getActivationKey (); $this->assertTrue($user->isActivationKeyValid ($key)); sleep(2 * 24 * 60 * 60); // 2 days $this->assertFalse($user->isActivationKeyValid ($key)); } -
Clock mocking Grâce au test-pack de Symfony, nous n’avons qu’à
mettre une annotation: /** * @group time-sensitive */ public function testIsActivationKeyValid (): void { $user = new User(); $key = $user->getActivationKey (); $this->assertTrue($user->isActivationKeyValid ($key)); sleep(2 * 24 * 60 * 60); // 2 days $this->assertFalse($user->isActivationKeyValid ($key)); } -
Cette annotation va déclarer des bouchons pour time(), sleep(), usleep()
et date() dans les namespaces de la classe testée : - App\Entity\ - App\Tests\Entity\ Ainsi les appels de fonction dans User & UserTest utiliseront les déclarations de ce namespace. Clock mocking -
Clock mocking Mais… Il faut utiliser les fonctions : time(),
sleep(), usleep() et date() Par exemple, pour DateTime et DateTimeImmutable: \DateTimeImmutable ::createFromFormat('U', time()); \DateTimeImmutable ::createFromFormat('U', time()) ->add(new \DateInterval('P2D')) ; -
Bonjour SetPasswordType Un FormType de Symfony qui s’occupe du changement
de mot de passe : $form = $this->createForm(SetPasswordType ::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $userManager->saveUser($user); } -
Bonjour SetPasswordType Un FormType de Symfony qui s’occupe du changement
de mot de passe: ->addEventListener (FormEvents::POST_SUBMIT, function (PostSubmitEvent $e) { if (!$e->getForm()->isValid()) { Return; } $user = $e->getData(); $password = $e->getForm()->get('password')->getData(); $hashed = $this->userPasswordHasher ->hashPassword($user, $password); $user->setPassword($hashed); }) -
use Symfony\Component\Form\Test\TypeTestCase; class SetPasswordTypeTest extends TypeTestCase { public function testValid():
void { $user = new User(); $user→setPassword('foo'); $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertEquals('fooHashed', $user→getPassword()); } -
use Symfony\Component\Form\Test\TypeTestCase; class SetPasswordTypeTest extends TypeTestCase { public function testValid():
void { $user = new User(); $user→setPassword('fooHashed'); $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertEquals('barHashed', $user→getPassword()); } -
use Symfony\Component\Form\Test\TypeTestCase; class SetPasswordTypeTest extends TypeTestCase { public function testValid():
void { $user = new User(); $user→setPassword('fooHashed'); $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertEquals('barHashed', $user→getPassword()); } -
use Symfony\Component\Form\Test\TypeTestCase; class SetPasswordTypeTest extends TypeTestCase { public function testValid():
void { $user = new User(); $user→setPassword('fooHashed'); $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertEquals('barHashed', $user→getPassword()); } -
use Symfony\Component\Form\Test\TypeTestCase; class SetPasswordTypeTest extends TypeTestCase { public function testValid():
void { $user = new User(); $user→setPassword('fooHashed'); $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertEquals('barHashed', $user→getPassword()); } -
class SetPasswordTypeTest extends TypeTestCase { private UserPasswordHasherInterface|MockObject $passwordHasher; protected function
getExtensions(): array { $this→passwordHasher = $this→createMock(UserPasswordHasherInterface::class) ; $type = new SetPasswordType($this→passwordHasher); return [ new PreloadedExtension([ $type ], []), ]; } -
class SetPasswordTypeTest extends TypeTestCase { private UserPasswordHasherInterface|MockObject $passwordHasher; protected function
getExtensions(): array { $this→passwordHasher = $this→createMock(UserPasswordHasherInterface::class) ; $type = new SetPasswordType($this→passwordHasher); return [ new PreloadedExtension([ $type ], []), ]; } -
class SetPasswordTypeTest extends TypeTestCase { private UserPasswordHasherInterface|MockObject $passwordHasher; protected function
getExtensions(): array { $this→passwordHasher = $this→createMock(UserPasswordHasherInterface::class) ; $type = new SetPasswordType($this→passwordHasher); return [ new PreloadedExtension([ $type ], []), ]; } -
class SetPasswordTypeTest extends TypeTestCase { private UserPasswordHasherInterface|MockObject $passwordHasher; protected function
getExtensions(): array { $this→passwordHasher = $this→createMock(UserPasswordHasherInterface::class) ; $type = new SetPasswordType($this→passwordHasher); return [ new PreloadedExtension([ $type ], []), ]; } -
On ajoute finalement les attentes sur le bouchon dans le
test : public function testValid(): void { $user = new User(); $this→passwordHasher →expects($this→once()) →method('hashPassword') →with($user, 'bar') →willReturn('barHashed') ; # ... Test unitaire de formulaire -
$user = new User(); $user→setPassword('fooHashed'); $this→passwordHasher →expects($this→once()) →method('hashPassword') →with($user, 'bar')
→willReturn('barHashed') ; $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertTrue($form→isSynchronized()); $this→assertEquals('barHashed', $user→getPassword()); -
$user = new User(); $user→setPassword('fooHashed'); $this→passwordHasher →expects($this→once()) →method('hashPassword') →with($user, 'bar')
→willReturn('barHashed') ; $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertTrue($form→isSynchronized()); $this→assertEquals('barHashed', $user→getPassword()); -
$user = new User(); $user→setPassword('fooHashed'); $this→passwordHasher →expects($this→once()) →method('hashPassword') →with($user, 'bar')
→willReturn('barHashed') ; $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertTrue($form→isSynchronized()); $this→assertEquals('barHashed', $user→getPassword()); -
$user = new User(); $user→setPassword('fooHashed'); $this→passwordHasher →expects($this→once()) →method('hashPassword') →with($user, 'bar')
→willReturn('barHashed') ; $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertTrue($form→isSynchronized()); $this→assertEquals('barHashed', $user→getPassword()); -
$user = new User(); $user→setPassword('fooHashed'); $this→passwordHasher →expects($this→once()) →method('hashPassword') →with($user, 'bar')
→willReturn('barHashed') ; $form = $this→factory→create(SetPasswordType::class, $user); $form→submit(['password' => [ 'first' => 'bar', 'second' => 'bar', ]]); $this→assertTrue($form→isSynchronized()); $this→assertEquals('barHashed', $user→getPassword()); -
Bonjour UserActivateCommand On imagine une commande bin/console user:activate [email protected]. -
[ERROR] No user found with email "[email protected]". - [INFO] The user with email "[email protected]" is already activated. - [OK] The user with email "[email protected]" has been activated. -
class ActivateCommandTest extends KernelTestCase { public function testAlreadyActivated(): void {
// Prepare the data $this→user→activate(); $this→manager→saveUser($this→user); $app = new Application(self::bootKernel()); $command = $app→find('user:activate'); $tester = new CommandTester($command); $tester→execute(['email' => '[email protected]']); $tester→assertCommandIsSuccessful(); $this→assertStringContainsString( 'already activated', $tester→getDisplay(), ); } -
class ActivateCommandTest extends KernelTestCase { public function testAlreadyActivated(): void {
// Prepare the data $this→user→activate(); $this→manager→saveUser($this→user); $app = new Application(self::bootKernel()); $command = $app→find('user:activate'); $tester = new CommandTester($command); $tester→execute(['email' => '[email protected]']); $tester→assertCommandIsSuccessful(); $this→assertStringContainsString( 'already activated', $tester→getDisplay(), ); } -
class ActivateCommandTest extends KernelTestCase { public function testAlreadyActivated(): void {
// Prepare the data $this→user→activate(); $this→manager→saveUser($this→user); $app = new Application(self::bootKernel()); $command = $app→find('user:activate'); $tester = new CommandTester($command); $tester→execute(['email' => '[email protected]']); $tester→assertCommandIsSuccessful(); $this→assertStringContainsString( 'already activated', $tester→getDisplay(), ); } -
class ActivateCommandTest extends KernelTestCase { public function testAlreadyActivated(): void {
// Prepare the data $this→user→activate(); $this→manager→saveUser($this→user); $app = new Application(self::bootKernel()); $command = $app→find('user:activate'); $tester = new CommandTester($command); $tester→execute(['email' => '[email protected]']); $tester→assertCommandIsSuccessful(); $this→assertStringContainsString( 'already activated', $tester→getDisplay(), ); } -
class ActivateCommandTest extends KernelTestCase { public function testAlreadyActivated(): void {
// Prepare the data $this→user→activate(); $this→manager→saveUser($this→user); $app = new Application(self::bootKernel()); $command = $app→find('user:activate'); $tester = new CommandTester($command); $tester→execute(['email' => '[email protected]']); $tester→assertCommandIsSuccessful(); $this→assertStringContainsString( 'already activated', $tester→getDisplay(), ); } -
class ActivateCommandTest extends KernelTestCase { public function testAlreadyActivated(): void {
// Prepare the data $this→user→activate(); $this→manager→saveUser($this→user); $app = new Application(self::bootKernel()); $command = $app→find('user:activate'); $tester = new CommandTester($command); $tester→execute(['email' => '[email protected]']); $tester→assertCommandIsSuccessful(); $this→assertStringContainsString( 'already activated', $tester→getDisplay(), ); } -
import { Application } from '@hotwired/stimulus'; import { clearDOM, mountDOM
} from '@symfony/stimulus-testing'; import HelloController from '../controllers/hello_controller.js'; const startStimulus = () => { const application = Application.start(); application.register('hello', HelloController); }; describe('HelloController', () => { let container; beforeEach(() => { startStimulus(); container = mountDOM('<div data-controller="hello"></div>'); }); afterEach(() => { clearDOM(); }); // Test here }); -
import { Application } from '@hotwired/stimulus'; import { clearDOM, mountDOM
} from '@symfony/stimulus-testing'; import HelloController from '../controllers/hello_controller.js'; const startStimulus = () => { const application = Application.start(); application.register('hello', HelloController); }; describe('HelloController', () => { let container; beforeEach(() => { startStimulus(); container = mountDOM('<div data-controller="hello"></div>'); }); afterEach(() => { clearDOM(); }); // Test here }); -
describe('HelloController', () => { // ... it('connect', async () =>
{ expect(container.textContent).toContain("Hello world"); }); }); -
Un CRM vers lequel nous synchronisons les utilisateurs par un
appel HTTP. class SalesPower { public function __construct( private readonly HttpClientInterface $client, ) { } public function createUser(User $user): void { $this→client→request('PUT', '/users', ...); } } Bonjour SalesPower -
use Symfony\Component\HttpClient\MockHttpClient; $mock = new MockHttpClient(new MockResponse('OK')); $crm = new
SalesPower($mock); $crm→createUser($alice); Méthode simple -
$count = 0; $mock = new MockHttpClient( function (string $method,
string $path) use (&$count) { $this→assertSame(0, $count++); $this→assertSame('PUT', $method); $this→assertSame('/users', $path); return new MockResponse('OK'); } ); $crm = new SalesPower($mock); $crm→createUser($alice); $this→assertSame(1, $count, 'Effective call to the HTTP client'); Méthode avancée -
$count = 0; $mock = new MockHttpClient( function (string $method,
string $path) use (&$count) { $this→assertSame(0, $count++); $this→assertSame('PUT', $method); $this→assertSame('/users', $path); return new MockResponse('OK'); } ); $crm = new SalesPower($mock); $crm→createUser($alice); $this→assertSame(1, $count, 'Effective call to the HTTP client'); Méthode avancée -
$count = 0; $mock = new MockHttpClient( function (string $method,
string $path) use (&$count) { $this→assertSame(0, $count++); $this→assertSame('PUT', $method); $this→assertSame('/users', $path); return new MockResponse('OK'); } ); $crm = new SalesPower($mock); $crm→createUser($alice); $this→assertSame(1, $count, 'Effective call to the HTTP client'); Méthode avancée -
$repository = $this→getMockBuilder(UserRepository::class) →disableOriginalConstructor() →getMock() ; $repository →expects($this→once()) →method('findOneBy') →with(['email'
=> '[email protected]']) →willReturn($alice) ; self::getContainer()→set(UserRepository::class, $repository); $manager = self::getContainer()→get(UserManager::class); $manager→recoverPassword('[email protected]'); $this→assertQueuedEmailCount(1); -
$repository = $this→getMockBuilder(UserRepository::class) →disableOriginalConstructor() →getMock() ; $repository →expects($this→once()) →method('findOneBy') →with(['email'
=> '[email protected]']) →willReturn($alice) ; self::getContainer()→set(UserRepository::class, $repository); $manager = self::getContainer()→get(UserManager::class); $manager→recoverPassword('[email protected]'); $this→assertQueuedEmailCount(1); -
$repository = $this→getMockBuilder(UserRepository::class) →disableOriginalConstructor() →getMock() ; $repository →expects($this→once()) →method('findOneBy') →with(['email'
=> '[email protected]']) →willReturn($alice) ; self::getContainer()→set(UserRepository::class, $repository); $manager = self::getContainer()→get(UserManager::class); $manager→recoverPassword('[email protected]'); $this→assertQueuedEmailCount(1); -
$repository = $this→getMockBuilder(UserRepository::class) →disableOriginalConstructor() →getMock() ; $repository →expects($this→once()) →method('findOneBy') →with(['email'
=> '[email protected]']) →willReturn($alice) ; self::getContainer()→set(UserRepository::class, $repository); $manager = self::getContainer()→get(UserManager::class); $manager→recoverPassword('[email protected]'); $this→assertQueuedEmailCount(1); -
Profiler collectors La magie des tests est rendue possible grâce
aux collectors: $this→client→enableProfiler(); $this→client→request('GET', '/'); $collectors = $this→client→getProfile()→getCollectors(); -
- Request - Time - Memory - Validator - Ajax
- Form - Exception - Logger - Events Les collectors disponibles - Router - Cache - Security - Twig - Http Client - Database - Messenger - Mailer - Config -
On vérifie qu’aucune requête HTTP n’a été générée: $collectors =
$client→getProfile()→getCollectors(); /** @var HttpClientDataCollector $httpClientCollector */ $httpClientCollector = $collectors['http_client']; $this→assertEquals(0, $httpClientCollector→getRequestCount()); Exemple avec “http_client” -
Fixtures - Utile pour le développement - Risqué pour les
tests automatisés (modification des données) -
Fixtures - Utile pour le développement - Risqué pour les
tests automatisés (modification des données) - Préférez les tests auto-suffisants en données -
Test auto-suffisant supprimant des données public function setUp(): void {
$this→client = static::createClient(); $userManager = self::getContainer()→get(UserManager::class); try { $user = $userManager→findOneByEmail('[email protected]'); $userManager→deleteUser($user); } catch (\InvalidArgumentException) { // Ignore } } -
Abstraire les services externes Exemple industrialisé : Symfony Mailer Exemple
minimaliste: une API tierce de CRM : - PUT /user quand un utilisateur est créé - DELETE /user/<ID> quand un utilisateur est supprimé -
namespace App\Crm; interface CrmInterface { /** @throws ApiException */ public
function createUser(User $user): void; /** @throws ApiException */ public function deleteUser(string $id): void; } Créez le contrat d’interface -
namespace App\Crm; class SalesPower implements CrmInterface { public function __construct(
private readonly HttpClientInterface $client, ) { } public function createUser(User $user): void { # PUT /user } public function deleteUser(string $id): void { # DELETE /user/<id> } } -
namespace App\Crm; class SalesPower implements CrmInterface { public function __construct(
private readonly HttpClientInterface $client, ) { } public function createUser(User $user): void { # PUT /user } public function deleteUser(string $id): void { # DELETE /user/<id> } } -
namespace App\Crm; class SalesPower implements CrmInterface { public function __construct(
private readonly HttpClientInterface $client, ) { } public function createUser(User $user): void { # PUT /user } public function deleteUser(string $id): void { # DELETE /user/<id> } } -
namespace App\Crm; class SalesPower implements CrmInterface { public function __construct(
private readonly HttpClientInterface $client, ) { } public function createUser(User $user): void { # PUT /user } public function deleteUser(string $id): void { # DELETE /user/<id> } } -
Test unitaire de cette classe: - Cette implémentation utilise le
client HTTP de Symfony - Le client HTTP de Symfony peut être bouchonné SalesPowerTest -
namespace App\Crm; class InMemoryCrm implements CrmInterface { public array $createdUsers
= []; public array $deletedUserIds = []; public function createUser(User $user): void { $this→createdUsers[] = $user; } public function deleteUser(string $id): void { $this→deletedUserIds[] = $id; } } -
namespace App\Crm; class InMemoryCrm implements CrmInterface { public array $createdUsers
= []; public array $deletedUserIds = []; public function createUser(User $user): void { $this→createdUsers[] = $user; } public function deleteUser(string $id): void { $this→deletedUserIds[] = $id; } } -
class RegisterTest extends WebTestCase { public function testRegister(): void {
/** @var InMemoryCrm $crm */ $crm = self::getContainer()→get(CrmInterface::class); # ... $this→assertCount(1, $crm→createdUsers); } } -
class RegisterTest extends WebTestCase { public function testRegister(): void {
/** @var InMemoryCrm $crm */ $crm = self::getContainer()→get(CrmInterface::class); # ... $this→assertCount(1, $crm→createdUsers); } } -
Blackfire Permet de réaliser des tests de performance sur votre
application Symfony du développement à la production.