.. ================================================== .. FOR YOUR INFORMATION .. -------------------------------------------------- .. -*- coding: utf-8 -*- with BOM. ===================================== PHPUnit ===================================== PHPUnit est une librairie permettant de réaliser des tests de votre code PHP. (même principe que vous avez vu en C#) Pour installation, aller directement sur le site de `PHPUnit `_ **Exemple** Lors de l'exercice des commandes, dans la classe Order, nous allons tester la méthode public computeVAT(). .. code-block:: php /** * calcule de la TVA * * @return float */ public function computeVAT() { $sumTVA = $this->sum() * 0.077; return $this->special_round($sumTVA, 0.05); } .. code-block:: php use PHPUnit\Framework\TestCase; require __DIR__ . "/../src/Product.php"; require __DIR__ . "/../src/Customer.php"; require __DIR__ . "/../src/Order.php"; class OrderTest extends TestCase { /** * test sur le calcul de la TVA */ public function testComputeTVAWithOneProduct() { $product = new Product('Un produit', 'Une description', 40.0); $customer = new Customer(); $order = new Order($customer, $product); $this->assertSame(3.1, $order->computeVAT()); } } L'utilisation de PHPUnit se fait via une ligne de commande. Il suffit de lancer les tests. .. code-block:: bash # Pour lancer un test sur une classe spécifique ./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/OrderTest # Rendu dans le cas où le test ne passe passe PHPUnit 8.0.0 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 250 ms, Memory: 4.00 MB There was 1 failure: 1) OrderTest::testComputeTVAWithOneProduct Failed asserting that 3.1 is identical to 1.1. C:\UwAmp\www\133CommandeAvecTest\tests\OrderTest.php:22 FAILURES! Tests: 1, Assertions: 1, Failures: 1. # Rendu dans le cas où le test passe PHPUnit 8.0.0 by Sebastian Bergmann and contributors. . 1 / 1 (100%) Time: 172 ms, Memory: 4.00 MB OK (1 test, 1 assertion) .. code-block:: bash # Pour lancer tous les tests du dossier ./vendor/bin/phpunit --testdox tests/ # Rendu dans le cas où le test ne passe passe PHPUnit 8.0.0 by Sebastian Bergmann and contributors. Order ✘ Compute t v a with one product │ │ Failed asserting that 3.1 is identical to 1.55. │ │ C:\UwAmp\www\133CommandeAvecTest\tests\OrderTest.php:18 │ Time: 242 ms, Memory: 4.00 MB FAILURES! Tests: 1, Assertions: 1, Failures: 1. # Rendu dans le cas où le test passe PHPUnit 8.0.0 by Sebastian Bergmann and contributors. Order ✔ Compute t v a with one product Time: 172 ms, Memory: 4.00 MB OK (1 test, 1 assertion)