0% found this document useful (0 votes)
40 views

DB Testing Phpunit

This document introduces PHPUnit for testing database interactions. It discusses using the DbUnit extension to put the database in a known state before each test, asserting the database contents match expectations. Datasets define the expected database state, and can be XML, YAML, CSV or arrays. Tests insert, update, and remove data from tables, asserting the results. The document provides an example test case class and methods for connecting to the database and getting the initial dataset. It recommends resources for learning more about testing databases with PHPUnit and DbUnit.

Uploaded by

Stefan Colotin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

DB Testing Phpunit

This document introduces PHPUnit for testing database interactions. It discusses using the DbUnit extension to put the database in a known state before each test, asserting the database contents match expectations. Datasets define the expected database state, and can be XML, YAML, CSV or arrays. Tests insert, update, and remove data from tables, asserting the results. The document provides an example test case class and methods for connecting to the database and getting the initial dataset. It recommends resources for learning more about testing databases with PHPUnit and DbUnit.

Uploaded by

Stefan Colotin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Testing your DB code with PHPUnit

Ade Slade

Why bother writing tests?


Why bother testing database interaction?

Overview

Introduction

Installation

Getting started

DataSets & DataTables

Assertions

Examples

Introduction

DbUnit extension for PHPUnit


Easy way to test code which interacts with the
database by:

Putting the database into a known state before


each test
Asserting the contents of the database are equal to
the expected contents

Internally uses PDO

PHPUnit

Test Framework

Member of the xUnit family of test frameworks

Very marvellous

> SimpleTest?

Installation

pear channel-discover pear.phpunit.de

pear install phpunit/phpunit

pear install phpunit/dbunit

Getting started

Extend PHPUnit_Extensions_Database_TestCase

Requires two methods to be implemented

getDataSet

getConnection

Test Case

CODE EXAMPLE

Flat XML DataSet

DataSets

Can be in a variety of formats:

Flat XML

XML

Yaml

CSV

PHP array based data set

Or in whatever format you like...

More fun with DataSets:

Composite

Replacement

Aggregating datasets
Replace values in the DataSet

Filter

Include and exclude tables and columns in a


DataSet

DataSets and DataTables

DataSet

Used to define known state

Set of tables

DataTable

Individual table

Retrieving table state

$this->getConnection()->createDataSet()

Optionally pass in an array of tablenames

$this->getConnection()->createQueryTable()

PHPUnit_Extensions_Database_DataSet_QueryDataSet

Assertions

Verify state of the database

Verify state of a table

assertDataSetsEqual
assertTablesEqual

Asserting table row count

getRowCount

Writing some tests

user table

User object

UserMapper to persist the User object

Schema

User insertion test

User update test

User removal test

Asserting the result of a query

Summary

Resources

PHPUnit manual

Benjamin Eberlei's tutorial

http://www.digitalsandwich.com

Sebastian Bergmann's slides on Testing PHP/MySQL


Applications with PHPUnit/DbUnit

http://www.beberlei.de/dbunit.html

Mike Lively's (author's) blog

http://www.phpunit.de/manual/current/en/database.html

On slideshare

Sebastian Bergmann's slides on Testing LAMP applications

On slideshare

Yay! Finished

Thanks for listening

Appreciate any and all feedback :)

email: [email protected]

twitter: @adeslade

web: http://adeslade.co.uk

github: https://github.com/adeslade/DBTesting

You might also like