Unit Testing in PHP Intro

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Sure, I can provide you with an example of how to write a unit test for PHP using

the PHPUnit framework.

PHPUnit is a popular testing framework for PHP that provides a set of tools for
writing and running unit tests. It is an open-source project that is widely used by
PHP developers for unit testing their code.

First, you need to install PHPUnit. You can do this by running the following
command:

composer require --dev phpunit/phpunit

Once you have PHPUnit installed, you can create a new test class by extending the
PHPUnit\Framework\TestCase class. This class provides a set of assertion methods
that you can use to make assertions about the behavior of your code.

Here is an example of a simple test class for a function that adds two numbers:

<?php

use PHPUnit\Framework\TestCase;

class CalculatorTest extends TestCase


{
public function testAdd()
{
$calculator = new Calculator();
$result = $calculator->add(1, 2);

$this->assertEquals(3, $result);
}
}
In this example, we have a test class called CalculatorTest, which has a single
test method called testAdd. This method creates a new instance of the Calculator
class and calls the add method with the arguments 1 and 2. It then uses the
assertEquals method provided by the TestCase class to check that the result is
equal to 3.

You can run the test by running the following command:

vendor/bin/phpunit CalculatorTest.php

This will run the test and display the results in the console.

You can also assert that a method throws an exception when certain conditions are
met:

public function testDivisionByZero()


{
$this->expectException(DivisionByZeroError::class);

$calculator = new Calculator();


$calculator->divide(10, 0);
}

In this example, we're using the expectException method to assert that the divide
method of the calculator class throws a DivisionByZeroError exception when called
with the arguments 10 and 0.
You can also use DataProvider to provide multiple set of inputs and assert the
result:

/**
* @dataProvider additionProvider
*/
public function testAdd($a, $b, $expected)
{
$calculator = new Calculator();
$this->assertEquals($expected, $calculator->add($a, $b));
}

public function additionProvider()


{
return [
[0, 0, 0],
[0, 1, 1],
[1, 0, 1],
[1, 1, 2]
];
}

In this example, we have a test method called testAdd that takes three arguments:
$a, $b and $expected. The test method creates a new instance of the Calculator
class and calls the add method with the arguments $a and $b. It then uses the
assertEquals method to check that the result is equal to $expected. The
additionProvider method provides multiple set of inputs and expected output.

It's important to write testable code, keep the test methods independent and use
meaningful names for the test methods. Also,

it's important to keep in mind that unit tests should test the smallest unit of
code possible and should not rely on external resources such as databases or APIs.
This makes the tests more reliable and easier to maintain.

In addition to the tips and examples provided, it's also important to use code
coverage tools to measure how much of your code is covered by tests. This can help
you identify areas of your code that are not being tested and can help you ensure
that your tests are comprehensive. Some popular code coverage tools for PHP include
Xdebug and PHPUnit's own code coverage tool.

It's also important to have a well-defined testing strategy that covers all the
aspects of testing, from unit tests to acceptance tests. This will help ensure that
the final product is of high quality and meets the requirements.

In conclusion, unit testing is an important part of software development and can


help you catch bugs early on, improve the quality of your code, and make it easier
to maintain and modify. By following best practices such as writing testable code,
using assertion libraries, and using code coverage tools, you can write great unit
tests in PHP using the PHPUnit framework.

You might also like