The document discusses test-driven development (TDD) and provides an overview of the TDD process and benefits. It describes how TDD encourages writing decoupled, well-designed code and helps find bugs through automated testing. While TDD may initially take longer, it improves code quality and maintainability. The document also covers challenges of testing database interactions and provides examples of different approaches like mocking databases, using DBUnit, or testing with Codeception.
3. 10 Write one test
20 Run tests (they fail)
30 Write code to make test pass
40 Run tests again (they pass)
50 Refactor (code AND tests)
60 Run test (they still pass)
70 Goto 10
42. Recently Used List Class
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
43. Recently Used List Class
A recently-used-list is initially empty.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
44. Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
45. Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
46. Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
47. Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
58. Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
67. Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
70. Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
99. Mock Example
function testInsertDatabase()
{
$insertArray = array(
'name' = 'Miles',
);
// create a mock database
$mock = $this-getMockBuilder('Zend_Db_Adapter_Pdo_Mysql', array('insert'))
-disableOriginalConstructor()
-getMock();
// mock out the insert method
$mock
-expects($this-once())
-method('insert')
-with($insertArray);
// pass the database to the container
$container = new DIContainer():
$container-set('database', $mock);
// run the test
$user = new User();
$user-setName('Miles');
$user-save();
}
Not great because
• Requires Dependency Injection
• Can be fragile
• Complicated
But good because
• Super fast
• Not tied to database structure
100. DBUnit Example
public function testCanCreateNewUser()
{
$user = new User();
$user-setName('Miles');
$user-save();
$queryTable = $this-getConnection()-createTableQuery(
'users', 'SELECT * from users'
);
$expectedTable = $this-createFlatXmlDataSet(users-expected.xml)
-getTable('users');
$this-assertTableEquals($expectedTable, $queryTable);
}
Not great because
• Slow
• Can be fragile
• Tied to database structure
• Requires xml setup of expected results
But good because
• Tests are easy to write and read
• It’s obvious whats being tested
101. Codeception Example
?php
function testSavingUser()
{
$user = new User();
$user-setName('Miles');
$user-save();
$this-codeGuy-seeInDatabase('users',array('name' = 'Miles'));
}
?
Not great because
• Slow
• Tied to database structure
But good because
• Less fragile than DBUnit (no xml to maintain)
• Tests are easy to write and read
• It’s obvious whats being tested
103. When testing the DB...
Give each developer their own test database (preferably on
their own VM)
104. When testing the DB...
Give each developer their own test database (preferably on
their own VM)
DB tests are slow - move them outside the main suite or use
memory tables
105. When testing the DB...
Give each developer their own test database (preferably on
their own VM)
DB tests are slow - move them outside the main suite or use
memory tables
Use a tool like “Migrations” to keep databases in sync