Tải bản đầy đủ - 0 (trang)
Appendix E. Using PEAR and PHPUnit

Appendix E. Using PEAR and PHPUnit

Tải bản đầy đủ - 0trang


Download at Boykma.Com

Now navigate to C:\Program Files\EasyPHP 3.0\php\, then run the batch file

go-pear.bat by typing the following (and then pressing Return):


During installation, accept the defaults by pressing Return whenever you’re asked to

do something. Figure E-1 shows the installation process.

Figure E-1. Installing PEAR on Windows

Next you need to install PEAR’s database helper package, called MDB2, by typing in

the following (see Figure E-2):

pear install MDB2

Figure E-2. Installing the PEAR MDB2 package

474 | Appendix E: Using PEAR and PHPUnit


Download at Boykma.Com

To finish your installation, install the MDB2 add-on driver that understands how to

interact with MySQL. To do this, type in the following (see Figure E-3):

pear install -f MDB2_Driver_mysql

Figure E-3. Installing the PEAR MySQL MDB2 driver

If you receive an error message at any point from a pop-up window that says “Invalid

configuration directive” while installing PEAR or MDB2, you should be able to safely

ignore it by clicking the OK button.

Finally, as there appears to be a problem with file and path locations in EasyPHP 3.0,

you need to type the following command to copy the PEAR files to a location where

they can be found by PHP:

xcopy /E pear\*.* includes

Mac OS

Most Macs come supplied with a version of PEAR, but often it’s out of date. The safest

bet is to ensure that you have the latest version by visiting http://pear.php.net/go-pear

in your browser and then using Save As to save the file that loads into your browser as

go-pear.php in your MAMP htdocs folder. Once saved, ensure that you have MAMP

running and enter the following into your browser’s address bar:


Now all you have to do is click on the Next >> button to see the main installation screen

(see Figure E-4). Ensure that the MDB2 checkbox is checked and then edit the Installation prefix field to read:


Installation | 475


Download at Boykma.Com

Figure E-4. Installing Pear and MDB2

Finally, scroll to the bottom of the page and click the Install button. You can now sit

back and watch the installation as it progresses.

Once the installation completes, you will need to add the PEAR installation path to

your include path. To do this, open up the file /Applications/MAMP/conf/php5/

php.ini in a text or program editor and locate the line that reads:

include_path = ".:/Applications/MAMP/bin/PHP5/lip/php"

Now change the string after include_path = to read:


Once you have done this, pull up the MAMP control panel and stop and restart the

servers. If prompted, you may also have to enter your Mac password, too.

The last part of the installation involves downloading and adding the MySQL driver to

PEAR. To do this, call up the Terminal and type the following. The output on the

Terminal will look like Figure E-5.

/Applications/MAMP/bin/pear install MDB2_Driver_mysql

476 | Appendix E: Using PEAR and PHPUnit


Download at Boykma.Com

Figure E-5. Installing the PEAR MySQL MDB2 driver


If you installed the XAMPP package in Chapter 2, you already have PEAR installed.

However, you will need to install the MDB2 database access package and the MySQL

driver for it. To do this, you should need to issue only the following two commands:

pear install MDB2

pear install MDB2_Driver_mysql

Creating a Connect Instance

With all of PEAR, the MDB2 package, and the MySQL driver installed, you can start

to take advantage of these new additions. But to do so, you need to understand what

MDB2 is providing you with: a layer of abstraction.

In other words, MDB2 knows everything about accessing any major brand of database

program you may have installed. You simply use a common set of commands and tell

MDB2 which database to access. This means you can migrate to another SQL database

such as PostgreSQL and will only have to install the new MDB2 driver and change a

single line of code in your PHP file to be up and running again.

You connect to a MySQL database using MDB2 with code such as the following, where

$db_username and the other $db_ variables have already been read in from the

login.php file:

Creating a Connect Instance | 477


Download at Boykma.Com

require_once 'MDB2.php';

$dsn = "$db_username:$db_password@$db_hostname/$db_database";

$mdb2 = MDB2::connect("mysql://$dsn");

The require_once line loads MDB2. In the next line, the variable $dsn stands for data

source name and is an identifier for the database. It comprises

username:password@hostname/database. The variable $mdb2 is an object returned by

calling the connect method within the MDB2 class. Recall that as mentioned in Chapter 5, the double colon (::) token indicates a class to be used on the left and a method

to call from that class to the right.

The full string passed to the connect method is as follows:


The mysql:// at the head of the string identifies the MDB2 driver to use and hence the

type of database to access. If, for example, you were using a PostgreSQL database you

would replace the head with pgsql://. The possible database types supported (as long

as you install the drivers) are fbsql, ibase, mssql, mysql, mysqli, oci8, pgsql, querysim,

and sqlite.

To check whether the program successfully connected to the database, you can issue

a call to the PEAR isError method, like this:

if (PEAR::isError($mdb2))

die("Unable to connect to MySQL: " . $mdb2->getMessage());

Here the $mdb2 object is passed to the isError method, which returns TRUE if there is an

error. In that case the die function is called, and an error message is issued before calling

the getMessage method from within the $mdb2 object to output the last message, describing the error encountered.


Once you have an MDB2 object in $mdb2, you can use it to query the database. Instead

of calling the mysql_query function, call the query method of the $mdb2 object as follows

(assuming that the variable $query has already been assigned a query string):

$result = $mdb2->query($query);

Fetching a Row

The variable $result, returned by the query method, is another object. To fetch a row

from the database, just call the object’s fetchRow method like this:

$row = $result->fetchRow();

You can also determine the number of rows in $result using the numRows method like


$rows = $result->numRows();

478 | Appendix E: Using PEAR and PHPUnit


Download at Boykma.Com

Closing a Connection

To close an MDB2 database connection, call the disconnect method of the $mdb2 object:


Rewriting Example 10-8 to Use PEAR

Hopefully you now have the hang of this new object-oriented approach to accessing

MySQL. So let’s look at how the sqltest.php program in Example 10-8 can be rewritten

using PEAR’s MDB2 package (see Example E-1, sqltest_mdb2.php).

Example E-1. Inserting and deleting using MDB2: sqltest_mdb2.php

require_once 'login.php';

require_once 'MDB2.php';

$dsn = "mysql://$db_username:$db_password@$db_hostname/$db_database";

$options = array('debug' => 2);

$mdb2 = MDB2::connect($dsn,$options);

if (PEAR::isError($mdb2))

die("Unable to connect to MySQL: " . $mdb2->getMessage());

if (isset($_POST['author']) &&

isset($_POST['title']) &&

isset($_POST['category']) &&

isset($_POST['year']) &&




= get_post('author');


= get_post('title');

$category = get_post('category');


= get_post('year');


= get_post('isbn');

if (isset($_POST['delete']) && $isbn != "")


$query = "DELETE FROM classics WHERE isbn='$isbn'";

if (!$mdb2->query($query))

echo "DELETE failed: $query
" .

$mdb2->getMessage() . "





$query = "INSERT INTO classics VALUES" .

"('$author', '$title', '$category', '$year', '$isbn')";


if (!$mdb2->query($query))

echo "INSERT failed: $query
" .

$mdb2->getMessage() . "


Rewriting Example 10-8 to Use PEAR | 479


Download at Boykma.Com


echo <<<_END







$query = "SELECT * FROM classics";

$result = $mdb2->query($query);

if (!$result)

die ("Database access failed: " . $mdb2->getMessage());

$rows = $result->numRows();

for ($j = 0 ; $j < $rows ; ++$j)


$row = $result->fetchRow();

echo <<<_END

Author $row[0]

Title $row[1]

Category $row[2]

Year $row[3]

ISBN $row[4]




function get_post($var)


return mysql_real_escape_string(@$_POST[$var]);



Looking through this code, you should see that very little has been changed from the

nonobject-oriented version of the program, other than replacing the database accessing

functions with calls to methods contained within the $mdb2 object, and objects returned

from them. The differences have been highlighted in bold.

I recommend that you try modifying sqltest.php for yourself to use MDB2, as in the

example, and then save it as sqltest_mdb2.php to test it.

480 | Appendix E: Using PEAR and PHPUnit


Download at Boykma.Com

Using PEAR, you can save yourself a considerable amount of work, but if you still prefer

not to use it, at least you will now recognize PEAR packages when you see them used

in other programs and will know how to work with them.

Adding Other PEAR Packages

With PEAR properly installed on your system, you are able to install additional packages from the command line by using one of the commands in Table E-2, where

package is the name of a PEAR package, as listed at the web page http://pear.php.net/


Table E-2. Installing PEAR packages on different systems




"C:\Program Files\EasyPHP 3.0\php\pear" install package

Mac OS X

/Applications/MAMP/bin/pear install package

Linux Unix

pear install package

To determine which packages are installed, replace install package in Table E-2 with

the word list and the output should be similar to the following:














MDB2_Driver_mysql 1.4.1








Unit Testing with PHPUnit

Now that you are familiar with object-oriented programming, it’s a good idea to get a

taste of unit testing. This is a method of code testing that verifies whether individual

units of source code are working correctly.

Unit testing provides the following benefits:

• It allows for automation of the testing process.

• It reduces the difficulty of discovering errors within more complex code.

• Testing is often enhanced because attention is given to each unit.

Install PHPUnit with the PEAR installer. To do this, go to a Command or Terminal

prompt, ensure that you have Administrator or Superuser privileges, and issue the two

Unit Testing with PHPUnit | 481


Download at Boykma.Com

lines of code in Table E-3, according to your operating system. (On Windows systems,

ignore and close any pop-up “Invalid configuration directive” alerts that may appear.)

Table E-3. Commands for Installing PHPUnit on different systems


Instructions to type from an Administrator Command prompt or a Terminal window


cd \Program Files\EasyPHP 3.0\php

(4 instructions)

pear channel-discover pear.phpunit.de

pear install phpunit/PHPUnit

Xcopy /E pear\*.* includes

Mac OS X

cd /Applications/MAMP/bin

(3 instructions)

pear channel-discover pear.phpunit.de

pear install phpunit/PHPUnit


pear channel-discover pear.phpunit.de

(2 instructions)

pear install phpunit/PHPUnit

The two main commands register the PEAR channel on your system, download

PHPUnit, and install it. On Windows, you need the additional XCOPY command to

resolve a file and path bug in EasyPHP 3.0. Press the A key if prompted to “Overwrite


You are then ready to take on some powerful bug testing. But first, let’s look at how

you might perform testing without PHPUnit. So, for example, consider the case of

testing PHP’s in-built array and the function sizeof and its alias count. For a newly

created array, sizeof should return a value of 0 and then increase by 1 for every new

element added, as in Example E-2.

Example E-2. Testing array and sizeof

$names = array();

echo sizeof($names) . "

$names[] = 'Bob';

echo count($names) . "
"; // count is an alias of sizeof

As you would expect, the output from this code is:



In Example E-3, this code is expanded to support automatic interpretation by writing

the comparison of the expected and actual values, outputting “OK” if the value is correct and “Not OK” if it isn’t.

482 | Appendix E: Using PEAR and PHPUnit


Download at Boykma.Com

Example E-3. Modified Example E-2 to output OK/Not OK

$names = array();

echo sizeof($names) == 0 ? "OK
" : "Not OK

$names[] = 'Bob';

echo sizeof($names) == 1 ? "OK
" : "Not OK

Helpful as this code is, there’s an even better way to handle errors, which is to display

a message only when a value is incorrect. Example E-4 uses function assertTrue to do

this by throwing an exception.

Example E-4. Modified Example E-2 to throw an exception

$names = array();

assertTrue(sizeof($names) == 0);

$names[] = 'Bob';

assertTrue(sizeof($names) == 1);

function assertTrue($condition)


if (!$condition) throw new Exception('Assertion failed.');


Now we’ve arrived at a fully automated test, let’s look at how we would rewrite it using

PHPUnit (see Example E-5).

Example E-5. PHPUnit testing in action

require_once 'PHPUnit/Framework.php';

class ArrayTest extends PHPUnit_Framework_TestCase


public function testNewArrayIsEmpty()


$names = array();

$this->assertEquals(0, sizeof($names));



public function testArrayContainsAnElement()


$names = array();

$names[] = 'Bob';

$this->assertEquals(1, sizeof($names));


$testObject = new ArrayTest;



Unit Testing with PHPUnit | 483


Download at Boykma.Com

The first thing to notice is that PHPUnit/Framework.php has been included in order to

make the PHPUnit classes available to the program. After that, the program defines a

new class that extends the PHPUnit_Framework_TestCase class. This new class contains

two methods: one for testing a newly created array and another for testing an array

containing an element.

The rules for writing PHPUnit tests are:

The tests for a class called Class go into a class with the name ClassTest.

ClassTest usually inherits from PHPUnit_Framework_TestCase.

The tests are public methods that are named testSomethingDescriptive.

Inside the test methods, assertion methods such as assertEquals are used to assert

that an actual value matches an expected value.

And there you have it. The three lines of code at the end of Example E-5 create a new

test object called $testObject and then call each of the object’s methods in turn. All

being well, this program will display nothing, so to see the output from PHPUnit, try

changing the 0 or 1 parameters in the assertEquals calls to other values.

A comprehensive and easy-to-follow manual on PHPUnit is available at http://www

.phpunit.de. Click “Read the documentation” to view it in either HTML or PDF format.

484 | Appendix E: Using PEAR and PHPUnit

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix E. Using PEAR and PHPUnit

Tải bản đầy đủ ngay(0 tr)