O'Reilly logo

PHPUnit Essentials by Zdenek Machek

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Anatomy of a PHPUnit test

Now, let's have a closer look at what the test structure looks like. Let's start with a simple test case, which will show the basic PHPUnit test structure. The following code snippet is a very basic example testing two PHP functions used for sorting arrays: asort() is used to sort an array and maintaining the indexes, and ksort() is used to sort the array by key. To start with, we have an array of vegetables, where name is the key and price is the value:

<?php class SecondTest extends PHPUnit_Framework_TestCase { public function testAsort() { $vegetablesArray = array('carrot' => 1, 'broccoli' => 2.99, 'garlic' => 3.98, 'swede' => 1.75); $sortedArray = array('carrot' => 1, 'swede' => 1.75, 'broccoli' => 2.99, 'garlic' => ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required