{"id":431,"date":"2021-03-13T22:23:00","date_gmt":"2021-03-13T22:23:00","guid":{"rendered":"https:\/\/rishikantsri.in\/blog\/?p=431"},"modified":"2023-10-16T10:55:16","modified_gmt":"2023-10-16T10:55:16","slug":"php-unit-testing","status":"publish","type":"post","link":"https:\/\/rishikantsri.in\/blog\/php-unit-testing\/","title":{"rendered":"PHP Unit Testing(updated)"},"content":{"rendered":"\n<p>Unit testing is a fundamental practice in software development to ensure that individual units of code, typically functions or methods, perform as expected. This comprehensive guide will explore PHP unit testing in detail, covering syntax, examples, tools, pros and cons, and demonstrating unit tests for CRUD (Create, Read, Update, Delete) operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Unit Testing in PHP<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is Unit Testing?<\/h3>\n\n\n\n<p>Unit testing is the practice of testing individual units or components of your code to ensure that they work as expected. In PHP, these units are usually functions or methods. The primary purpose of unit testing is to validate that each unit of the software performs as designed. When you write a unit test, you&#8217;re specifying how a particular part of your code should behave and checking if it does.<\/p>\n\n\n\n<p>Unit testing is essential for the following reasons:<\/p>\n\n\n\n<ol>\n<li><strong>Early Detection of Bugs<\/strong>: Unit tests help you find and fix bugs early in the development process.<\/li>\n\n\n\n<li><strong>Documentation<\/strong>: Tests serve as documentation, explaining how your code is supposed to work.<\/li>\n\n\n\n<li><strong>Refactoring Safety<\/strong>: Unit tests make it safer to refactor or modify your code since you can quickly verify that it still works correctly after changes.<\/li>\n\n\n\n<li><strong>Isolation of Issues<\/strong>: If a unit test fails, it&#8217;s easier to pinpoint the problem because the scope of testing is limited to a specific function or method.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Unit Testing Syntax<\/h3>\n\n\n\n<p>In PHP, unit tests are typically written using a testing framework. One of the most popular PHP testing frameworks is PHPUnit. Here&#8217;s the basic syntax for writing unit tests with PHPUnit:<\/p>\n\n\n\n<ol>\n<li><strong>Create a Test Class<\/strong>: Create a PHP class that extends <code>PHPUnit\\Framework\\TestCase<\/code>. This class will contain your test methods.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.887496948242188px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   class MyTest extends PHPUnit\\Framework\\TestCase {\n       \/\/ Test methods will go here\n   }\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   <\/span><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> MyTest <\/span><span style=\"color: #F97583\">extends<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">PHPUnit<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Framework<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #B392F0\">TestCase {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">       <\/span><span style=\"color: #6B737C\">\/\/ Test methods will go here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ol start=\"2\">\n<li><strong>Write Test Methods<\/strong>: Write test methods within the test class. Each test method&#8217;s name should start with &#8220;test&#8221; to indicate that it&#8217;s a test. Use various PHPUnit assertion methods to check if your code behaves as expected.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.887496948242188px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   public function testAddition() {\n       $result = add(2, 3);\n       $this-&gt;assertEquals(5, $result);\n   }\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testAddition() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">       $result <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> add<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">2<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F8F8F8\">3<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">       <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">5<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $result<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ol start=\"3\">\n<li><strong>Run the Tests<\/strong>: Use the PHPUnit command-line tool to run your tests. It will discover and execute all test methods in your test class.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   vendor\/bin\/phpunit MyTest.php<\/code><\/pre>\n\n\n\n<ol start=\"4\">\n<li><strong>Interpret the Results<\/strong>: PHPUnit will report the results of your tests. It will indicate which tests passed and which ones failed.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">PHPUnit Example: Basic Math Operations<\/h3>\n\n\n\n<p>Let&#8217;s create a simple PHP class for performing basic math operations and write unit tests for it using PHPUnit.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.800000190734863px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class MathOperations {\n    public function add($a, $b) {\n        return $a + $b;\n    }\n\n    public function subtract($a, $b) {\n        return $a - $b;\n    }\n\n    public function multiply($a, $b) {\n        return $a * $b;\n    }\n\n    public function divide($a, $b) {\n        if ($b === 0) {\n            throw new InvalidArgumentException(&quot;Division by zero is not allowed.&quot;);\n        }\n        return $a \/ $b;\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> MathOperations {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> add($a<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $b) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> $a <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #B392F0\"> $b;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> subtract($a<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $b) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> $a <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #B392F0\"> $b;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> multiply($a<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $b) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> $a <\/span><span style=\"color: #F97583\">*<\/span><span style=\"color: #B392F0\"> $b;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> divide($a<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $b) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> ($b <\/span><span style=\"color: #F97583\">===<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F8F8F8\">0<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            <\/span><span style=\"color: #F97583\">throw<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">InvalidArgumentException<\/span><span style=\"color: #B392F0\">(<\/span><span style=\"color: #FFAB70\">&quot;Division by zero is not allowed.&quot;<\/span><span style=\"color: #B392F0\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> $a <\/span><span style=\"color: #F97583\">\/<\/span><span style=\"color: #B392F0\"> $b;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Now, let&#8217;s write unit tests for this class using PHPUnit:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.800000190734863px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"use PHPUnit\\Framework\\TestCase;\n\nclass MathOperationsTest extends TestCase {\n    public function testAddition() {\n        $math = new MathOperations();\n        $result = $math-&gt;add(2, 3);\n        $this-&gt;assertEquals(5, $result);\n    }\n\n    public function testSubtraction() {\n        $math = new MathOperations();\n        $result = $math-&gt;subtract(5, 3);\n        $this-&gt;assertEquals(2, $result);\n    }\n\n    public function testMultiplication() {\n        $math = new MathOperations();\n        $result = $math-&gt;multiply(4, 3);\n        $this-&gt;assertEquals(12, $result);\n    }\n\n    public function testDivision() {\n        $math = new MathOperations();\n        $result = $math-&gt;divide(10, 2);\n        $this-&gt;assertEquals(5, $result);\n    }\n\n    public function testDivisionByZero() {\n        $this-&gt;expectException(InvalidArgumentException::class);\n        $math = new MathOperations();\n        $math-&gt;divide(5, 0);\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">PHPUnit<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Framework<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">TestCase<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> MathOperationsTest <\/span><span style=\"color: #F97583\">extends<\/span><span style=\"color: #B392F0\"> TestCase {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testAddition() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $math <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">MathOperations<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $result <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $math<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">add<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">2<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F8F8F8\">3<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">5<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $result<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testSubtraction() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $math <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">MathOperations<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $result <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $math<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">subtract<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">5<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F8F8F8\">3<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">2<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $result<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testMultiplication() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $math <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">MathOperations<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $result <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $math<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">multiply<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">4<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F8F8F8\">3<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">12<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $result<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testDivision() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $math <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">MathOperations<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $result <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $math<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">divide<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">10<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F8F8F8\">2<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">5<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $result<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testDivisionByZero() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">expectException<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #79B8FF\">InvalidArgumentException<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $math <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">MathOperations<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $math<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">divide<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">5<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F8F8F8\">0<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">PHPUnit Assertions<\/h3>\n\n\n\n<p>PHPUnit provides a variety of assertion methods to check different conditions in your tests. Here are some commonly used assertions:<\/p>\n\n\n\n<ul>\n<li><code>assertEquals($expected, $actual)<\/code>: Verifies that <code>$expected<\/code> is equal to <code>$actual<\/code>.<\/li>\n\n\n\n<li><code>assertNotEquals($expected, $actual)<\/code>: Verifies that <code>$expected<\/code> is not equal to <code>$actual<\/code>.<\/li>\n\n\n\n<li><code>assertTrue($condition)<\/code>: Verifies that the condition is <code>true<\/code>.<\/li>\n\n\n\n<li><code>assertFalse($condition)<\/code>: Verifies that the condition is <code>false<\/code>.<\/li>\n\n\n\n<li><code>assertNull($value)<\/code>: Verifies that the value is <code>null<\/code>.<\/li>\n\n\n\n<li><code>assertNotNull($value)<\/code>: Verifies that the value is not <code>null<\/code>.<\/li>\n\n\n\n<li><code>assertArrayHasKey($key, $array)<\/code>: Verifies that an array contains a specific key.<\/li>\n\n\n\n<li><code>assertContains($needle, $haystack)<\/code>: Verifies that a value is in an array or string.<\/li>\n\n\n\n<li><code>assertInstanceOf($expected, $actual)<\/code>: Verifies that <code>$actual<\/code> is an instance of the expected class or interface.<\/li>\n\n\n\n<li><code>expectException($exception)<\/code>: Verifies that an exception is thrown.<\/li>\n\n\n\n<li><code>assertCount($count, $array)<\/code>: Verifies that an array contains a specific number of elements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">PHPUnit Tools and Installation<\/h3>\n\n\n\n<p>To start using PHPUnit, you&#8217;ll need to install it. You can do this using Composer, a popular PHP package manager.<\/p>\n\n\n\n<ol>\n<li>If you don&#8217;t already have Composer installed, download it from <a href=\"https:\/\/getcomposer.org\/\">getcomposer.org<\/a>.<\/li>\n\n\n\n<li>Create a <code>composer.json<\/code> file in your project directory (if you don&#8217;t already have one) and add PHPUnit as a development dependency:<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.887496948242188px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   {\n       &quot;require-dev&quot;: {\n           &quot;phpunit\/phpunit&quot;: &quot;^9&quot;\n       }\n   }\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">       <\/span><span style=\"color: #FFAB70\">&quot;require-dev&quot;<\/span><span style=\"color: #B392F0\">: {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">           <\/span><span style=\"color: #FFAB70\">&quot;phpunit\/phpunit&quot;<\/span><span style=\"color: #B392F0\">: <\/span><span style=\"color: #FFAB70\">&quot;^9&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">       }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ol start=\"3\">\n<li>Run <code>composer install<\/code> to install PHPUnit.<\/li>\n\n\n\n<li>Once PHPUnit is installed, you can use the <code>vendor\/bin\/phpunit<\/code> command to run your tests.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Pros and Cons of Unit Testing<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Pros of Unit Testing<\/h4>\n\n\n\n<ol>\n<li><strong>Early Bug Detection<\/strong>: Unit tests catch bugs early in the development process, making them easier and cheaper to fix.<\/li>\n\n\n\n<li><strong>Documentation<\/strong>: Tests serve as living documentation that explains how your code should behave.<\/li>\n\n\n\n<li><strong>Refactoring Support<\/strong>: Unit tests give you the confidence to refactor or modify your code because they help you verify that it still works correctly.<\/li>\n\n\n\n<li><strong>Regression Prevention<\/strong>: Unit tests act as a safety net, preventing previously fixed bugs from re-emerging.<\/li>\n\n\n\n<li><strong>Isolation of Issues<\/strong>: If a test fails, you can easily identify the issue because the scope of testing is limited to a specific unit.<\/li>\n\n\n\n<li><strong>Increased Code Quality<\/strong>: Writing tests encourages better code design and adherence to coding standards.<\/li>\n\n\n\n<li><strong>Collaboration<\/strong>: Tests facilitate collaboration among team members, as they can understand how a specific piece of code is supposed to work.<\/li>\n\n\n\n<li><strong>Automation<\/strong>: Unit tests can be automated, allowing for quick and repeatable verification of code.<\/li>\n\n\n\n<li><strong>Support for Continuous Integration<\/strong>: Unit tests can be integrated into Continuous Integration (CI) pipelines, ensuring that tests are executed automatically on code changes.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Cons of Unit Testing<\/h4>\n\n\n\n<ol>\n<li><strong>Initial Setup Overhead<\/strong>: Setting up unit tests initially can be time-consuming, especially if the codebase lacks test coverage.<\/li>\n\n\n\n<li><strong>Maintenance Overhead<\/strong>: As the codebase evolves, tests may need to be updated to reflect code changes, resulting in maintenance overhead.<\/li>\n\n\n\n<li><strong>Not a Panacea<\/strong>: Unit tests alone do not guarantee bug-free code; other testing types, like integration and end-to-end tests, are also essential.<\/li>\n\n\n\n<li><strong>False Positives<\/strong>: Some tests may produce false positives, indicating issues that do not exist, leading to wasted time and effort.<\/li>\n\n\n\n<li><strong>Resource Consumption<\/strong>: Running a large suite of unit tests can consume significant system resources and slow down development processes.<\/li>\n\n\n\n<li><strong>Learning Curve<\/strong>: Writing effective unit tests requires skill and experience, which may have a learning curve for developers.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Unit Testing for CRUD Operations<\/h3>\n\n\n\n<p>Let&#8217;s explore how to write unit tests for CRUD operations, which involve creating, reading, updating, and deleting records in a database. In this example, we&#8217;ll create a simple class for managing a list of items.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.80000114440918px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php \nclass ItemList {\n    private $items = [];\n\n    public function addItem($item) {\n        $this-&gt;items[] = $item;\n    }\n\n    public function getItem($index) {\n        if (isset($this-&gt;items[$index])) {\n            return $this-&gt;items[$index];\n        } else {\n            return null;\n        }\n    }\n\n    public function updateItem($index, $newItem) {\n        if (isset($this-&gt;items[$index])) {\n            $this-&gt;items[$index] = $newItem;\n        }\n    }\n\n    public function deleteItem($index) {\n        if (isset($this-&gt;items[$index])) {\n            array_splice($this-&gt;items, $index, 1);\n        }\n    }\n}\n\n?&gt;\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> ItemList {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">private<\/span><span style=\"color: #B392F0\"> $items <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> [];<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> addItem($item) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">items[] <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $item;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> getItem($index) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> (isset<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">items[$index]<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">items[$index];<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        } <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #B392F0\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">null<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> updateItem($index<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $newItem) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> (isset<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">items[$index]<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">items[$index] <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $newItem;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> deleteItem($index) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> (isset<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">items[$index]<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            array_splice<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">items<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $index<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F8F8F8\">1<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">?&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Now, let&#8217;s write unit tests for this class using PHPUnit:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.800000190734863px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php \nrequire_once 'MyItemList.php';\nuse PHPUnit\\Framework\\TestCase;\n\n\n\nclass MyTest extends TestCase {\n    public function testAddItem() : void {\n        $list = new ItemList();\n        $list-&gt;addItem('Item 1');\n        $this-&gt;assertEquals('Item 1', $list-&gt;getItem(0));\n    }\n\n    public function testGetItem() {\n        $list = new ItemList();\n        $list-&gt;addItem('Item 1');\n        $this-&gt;assertEquals('Item 1', $list-&gt;getItem(0));\n    }\n\n    public function testUpdateItem() {\n        $list = new ItemList();\n        $list-&gt;addItem('Item 1');\n        $list-&gt;updateItem(0, 'Item 2');\n        $this-&gt;assertEquals('Item 2', $list-&gt;getItem(0));\n    }\n\n    public function testDeleteItem() {\n        $list = new ItemList();\n        $list-&gt;addItem('Item 1');\n        $list-&gt;addItem('Item 2');\n        $list-&gt;deleteItem(0);\n        $this-&gt;assertEquals('Item 2', $list-&gt;getItem(0));\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">require_once<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&#39;MyItemList.php&#39;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">PHPUnit<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Framework<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">TestCase<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> MyTest <\/span><span style=\"color: #F97583\">extends<\/span><span style=\"color: #B392F0\"> TestCase {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testAddItem() <\/span><span style=\"color: #F97583\">:<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">void<\/span><span style=\"color: #B392F0\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">ItemList<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">addItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 1&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 1&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">getItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">0<\/span><span style=\"color: #BBBBBB\">))<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testGetItem() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">ItemList<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">addItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 1&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 1&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">getItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">0<\/span><span style=\"color: #BBBBBB\">))<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testUpdateItem() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">ItemList<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">addItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 1&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">updateItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">0<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&#39;Item 2&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 2&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">getItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">0<\/span><span style=\"color: #BBBBBB\">))<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> testDeleteItem() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">new<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">ItemList<\/span><span style=\"color: #B392F0\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">addItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 1&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">addItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 2&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">deleteItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">0<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertEquals<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;Item 2&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $list<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">getItem<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #F8F8F8\">0<\/span><span style=\"color: #BBBBBB\">))<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this example, we wrote unit tests to validate the functionality of our <code>ItemList<\/code> class, which provides CRUD operations for managing a list of items. We test adding, retrieving, updating, and deleting items. If any of these tests fail, it&#8217;s a sign that something is wrong with our CRUD operations.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"348\" src=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/10\/image-33-1024x348.png\" alt=\"\" class=\"wp-image-442\" srcset=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/10\/image-33-1024x348.png 1024w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/10\/image-33-300x102.png 300w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/10\/image-33-768x261.png 768w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/10\/image-33.png 1093w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Unit testing is an essential practice in PHP development for ensuring the quality and reliability of your code. It helps catch bugs early, serves as documentation, and supports code refactoring. PHPUnit is a powerful testing framework for PHP, providing a variety of assertion methods and tools for writing and running unit tests.<\/p>\n\n\n\n<p>When it comes to unit testing CRUD operations, it&#8217;s important to write tests that cover adding, retrieving, updating, and deleting data. By using PHPUnit and adhering to best practices, you can confidently develop and maintain high-quality PHP code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing is a fundamental practice in software development to ensure that individual units of code, typically functions or methods, perform as expected. This comprehensive guide will explore PHP unit testing in detail, covering syntax, examples, tools, pros and cons, and demonstrating unit tests for CRUD (Create, Read, Update, Delete) operations. Unit Testing in PHP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,52],"tags":[4,53],"_links":{"self":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/431"}],"collection":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/comments?post=431"}],"version-history":[{"count":4,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":443,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/431\/revisions\/443"}],"wp:attachment":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}