Test-driven development is the process of testing every aspect of the application during development. Either the tests are defined before development and then development is made to pass these tests, or the classes and libraries are built and then tested. Testing the application is very important, and launching an application without tests is like jumping from a 30-floor-high building without a parachute.
PHP does not provide any built-in features to test, but there are other test frameworks that can be used for this purpose. One of most widely used frameworks or libraries is PHPUnit. It is a very powerful tool and provides lots of features. Now, let's have a look at it.
The installation of PHPUnit is easy. Just download it and place it in your project root so that it can be accessed from the command line.
PHPUnit installation and basic details, including features and examples, can be found at https://phpunit.de/.
Let's have a simple example. We have a Book class, as follows:
class Book
{
public $title;
public function __construct($title)
{
$this->title = $title;
}
public function getBook()
{
return $this->title;
}
}This is an example of a simple class that initializes the title property when the class is instantiated. When the getBook method is called, it returns the title of the book.
Now, we want to make a test in which we will check whether the getBook method returns PHP 7 as a title. So, perform the following steps to create the test:
tests directory at your project's root. Create a BookTest.php file in the tests directory.BookTest.php file:include (__DIR__.'/../Book.php');
class BookTest extends PHPUnit_Framework_TestCase
{
public function testBookClass()
{
$expected = 'PHP 7';
$book = new Book('PHP 7');
$actual = $book->getBook();
$this->assertEquals($expected, $book);
}
}BookTest, which extends the PHPUnit_Framework_TestCase class. We can name our test class whatever we want. However, the name should be easily recognizable so that we know this is written for the class that needs to be tested.testBookClass. We are also free to select whatever name we want to give to this method, but it should start with the word test. If not, PHPUnit will not execute the method and will issue a warning—in our case, for the preceding test class—that no tests were found.In the testBookClass method, we created an object of the Book class and passed PHP 7 as our title. Then, we fetched the title using the getBook method of the Book class. The important part is the last line of the testBookClass method, which performs the assertion and checks whether the data returned from getBook is the desired data or not.
php phpunit.phar tests/BookTest.php
When the command is executed, we will have an output similar to the following screenshot:

Our test is executed successfully as it met the criteria defined in our test.
PHP to the Book class, as shown in the following code:public function testBookClass()
{
$book = new Book('PHP');
$title = $book->getBook();
$this->assertEquals('PHP 7', $book);
}Book class returns PHP, so it does not pass our test. After executing this test, we will have a failure, as shown in the following screenshot:
As seen in the preceding screenshot, we expected PHP 7, and we got an actual result of PHP 7. The – sign shows the expected value, and the + sign shows the actual value.
In the previous topic, we discussed how we can perform tests on our libraries. We only discussed a simple basic test. PHPUnit is not limited to these simple tests, but covering PHPUnit completely is out of the scope of this book. A very nice book on PHPUnit is PHPUnit Essentials, published by Packt Publishing.