NOTE: Test file names must end with Test, and test cases must begin with test or should be anotated like /* @test /
//tests/ExampleTest.php <?php use PHPUnit\Framework\TestCase; class ExampleTest extends TestCase { public function testTwoStringsAreTheSame() { $string1 = 'Hello'; $string2 = 'Hello'; $this->assertSame($string1, $string2); } }
Efficiently managing PhpUnit testing configurations can significantly enhance the testing process. One way to streamline this is by creating a dedicated configuration file in the .xml format.
To get started, you'll want to create a phpunit.xml file in your project's root directory.
//phpunit.xml <?xml version="1.0" encoding="UTF-8"?> <phpunit colors="true"> <testsuites> <testsuite name="Tests"> <directory>tests</directory> </testsuite> </testsuites> </phpunit>
This configuration file serves as a centralized hub for all your PhpUnit settings, allowing for a more organized and efficient testing environment. Customize it to fit your project's specific needs, making testing a seamless part of your development workflow.
Now you can execute all the test cases inside test directory by
php vendor/bin/phpunit
1. Setup: In this process, you set up the initial test settings and prepare the environment.
2. Do something: In this phase the code you want to test will be executed.This stage involves calling methods or carrying out operations on the setup-phase-prepared objects.Here, you replicate the use of your code in the actual world and collect data for eventual use in assertions.
3. Assertion: In this phase, you compare the actual outcomes of the "do something" phase with the anticipated results during the assertion step. There are a variety of assertion methods that PHPUnit provides to verify that the behaviour of your code adheres to your expectations.
Note: The application is typically bootstrapped in PHPUnit prior to running any test cases. Thus, the test cases are isolated from one another.
The environment is created during the bootstrapping process by initialising objects, loading configurations, and gathering any resources the tests may require.
You can perform the necessary setup processes using PHPUnit's setUp() method, which is automatically called before each test case runs in that class. To similarly clean away any resources or carry out actions after each test case, use the tearDown() function.
Let's write a test case for a php class
Create Cart.php class in the src/ directory
//src/Cart.php <?php class Cart { public float $price; public static float $tax = 1.2; public function getNetPrice() { return $this->price * self::$tax; } }
Now let's write a test case for getNetPrice() function
Create CartTest.php class in the tests/ directory
//tests/CartTest.php <?php use PHPUnit\Framework\TestCase; class CartTest extends TestCase { protected $cart; protected function setUp(): void { parent::setUp(); require './src/Cart.php'; $this->cart = new Cart(); } public function testNetPriceIsCalculatedCorrectly() { //setup //setUp() method called automatically $this->cart->price = 10; //do something $netPrice = $this->cart->getNetPrice(); //assertion $this->assertEquals(12, $netPrice); } }
Maximizing Code Reusability with PhpUnit's setUp()
The setUp() method in PhpUnit is a powerful asset, automatically executed before each test case. It's the perfect spot for housing reusable code, ensuring consistent preparation for every test. Leverage this feature to streamline your testing process and keep your test cases concise and focused.
There are additional aspects of unit testing, such as mocking, to cover.Yet, this will be useful to developers wishing to begin test-driven development.