1. Installing PHPUnit
Requirements
PHP Archive (PHAR)
Windows
Verifying PHPUnit PHAR Releases
Composer
Optional packages
2. Writing Tests for PHPUnit
Test Dependencies
Data Providers
Testing Exceptions
Testing PHP Errors
Testing Output
Error output
Edge cases
3. The Command-Line Test Runner
Command-Line Options
4. Fixtures
More setUp() than tearDown()
Variations
Sharing Fixture
Global State
5. Organizing Tests
Composing a Test Suite Using the Filesystem
Composing a Test Suite Using XML Configuration
6. Risky Tests
Useless Tests
Unintentionally Covered Code
Output During Test Execution
Test Execution Timeout
Global State Manipulation
7. Incomplete and Skipped Tests
Incomplete Tests
Skipping Tests
Skipping Tests using @requires
8. Database Testing
Supported Vendors for Database Testing
Difficulties in Database Testing
The four stages of a database test
1. Clean-Up Database
2. Set up fixture
3–5. Run Test, Verify outcome and Teardown
Configuration of a PHPUnit Database TestCase
Implementing getConnection()
Implementing getDataSet()
What about the Database Schema (DDL)?
Tip: Use your own Abstract Database TestCase
Understanding DataSets and DataTables
Available Implementations
Beware of Foreign Keys
Implementing your own DataSets/DataTables
The Connection API
Database Assertions API
Asserting the Row-Count of a Table
Asserting the State of a Table
Asserting the Result of a Query
Asserting the State of Multiple Tables
Frequently Asked Questions
Will PHPUnit (re-)create the database schema for each test?
Am I required to use PDO in my application for the Database Extension to work?
What can I do, when I get a Too much Connections Error?
How to handle NULL with Flat XML / CSV Datasets?
9. Test Doubles
Stubs
Mock Objects
Prophecy
Mocking Traits and Abstract Classes
Stubbing and Mocking Web Services
Mocking the Filesystem
10. Testing Practices
During Development
During Debugging
11. Code Coverage Analysis
Software Metrics for Code Coverage
Whitelisting Files
Ignoring Code Blocks
Specifying Covered Methods
Edge Cases
12. Other Uses for Tests
Agile Documentation
Cross-Team Tests
13. Logging
Test Results (XML)
Test Results (TAP)
Test Results (JSON)
Code Coverage (XML)
Code Coverage (TEXT)
14. Extending PHPUnit
Subclass PHPUnit_Framework_TestCase
Write custom assertions
Implement PHPUnit_Framework_TestListener
Subclass PHPUnit_Extensions_TestDecorator
Implement PHPUnit_Framework_Test
A. Assertions
assertArrayHasKey()
assertClassHasAttribute()
assertArraySubset()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertContainsOnlyInstancesOf()
assertCount()
assertEmpty()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertInfinite()
assertInstanceOf()
assertInternalType()
assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonFile()
assertJsonStringEqualsJsonString()
assertLessThan()
assertLessThanOrEqual()
assertNan()
assertNull()
assertObjectHasAttribute()
assertRegExp()
assertStringMatchesFormat()
assertStringMatchesFormatFile()
assertSame()
assertStringEndsWith()
assertStringEqualsFile()
assertStringStartsWith()
assertThat()
assertTrue()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
B. Annotations
@author
@after
@afterClass
@backupGlobals
@backupStaticAttributes
@before
@beforeClass
@codeCoverageIgnore*
@covers
@coversDefaultClass
@coversNothing
@dataProvider
@depends
@expectedException
@expectedExceptionCode
@expectedExceptionMessage
@expectedExceptionMessageRegExp
@group
@large
@medium
@preserveGlobalState
@requires
@runTestsInSeparateProcesses
@runInSeparateProcess
@small
@test
@testdox
@ticket
@uses
C. The XML Configuration File
PHPUnit
Test Suites
Groups
Whitelisting Files for Code Coverage
Logging
Test Listeners
Setting PHP INI settings, Constants and Global Variables
Configuring Browsers for Selenium RC
D. Index
E. Bibliography
F. Copyright


Appendix D. Index

Symbols

$backupGlobalsBlacklist, Global State
$backupStaticAttributesBlacklist, Global State
@author, Command-Line Options, @author
@backupGlobals, Global State, @backupGlobals
@backupStaticAttributes, Global State, @backupStaticAttributes
@codeCoverageIgnore, Ignoring Code Blocks, @codeCoverageIgnore*
@codeCoverageIgnoreEnd, Ignoring Code Blocks, @codeCoverageIgnore*
@codeCoverageIgnoreStart, Ignoring Code Blocks, @codeCoverageIgnore*
@covers, Specifying Covered Methods, @covers
@coversDefaultClass, @coversDefaultClass
@coversNothing, Specifying Covered Methods, @coversNothing
@dataProvider, Data Providers, @dataProvider
@depends, Test Dependencies, Data Providers, @depends
@expectedException, Testing Exceptions, @expectedException
@expectedExceptionCode, @expectedExceptionCode
@expectedExceptionMessage, @expectedExceptionMessage
@expectedExceptionMessageRegExp, @expectedExceptionMessageRegExp
@group, Command-Line Options, @group
@large, @large
@medium, @medium
@preserveGlobalState, @preserveGlobalState
@requires, @requires
@runInSeparateProcess, @runInSeparateProcess
@runTestsInSeparateProcesses, @runTestsInSeparateProcesses
@small, @small
@test, Writing Tests for PHPUnit, @test
@testdox, @testdox
@ticket, @ticket
@uses, @uses

A

Agile Documentation, Command-Line Options, Agile Documentation
Annotation, Writing Tests for PHPUnit, Test Dependencies, Data Providers, Testing Exceptions, Command-Line Options, Ignoring Code Blocks, Specifying Covered Methods, Annotations
anything(), assertThat()
arrayHasKey(), assertThat()
assertArrayHasKey(), assertArrayHasKey()
assertArrayNotHasKey(), assertArrayHasKey()
assertArraySubset(), assertArraySubset()
assertAttributeContains(), assertContains()
assertAttributeContainsOnly(), assertContainsOnly()
assertAttributeEmpty(), assertEmpty()
assertAttributeEquals(), assertEquals()
assertAttributeGreaterThan(), assertGreaterThan()
assertAttributeGreaterThanOrEqual(), assertGreaterThanOrEqual()
assertAttributeInstanceOf(), assertInstanceOf()
assertAttributeInternalType(), assertInternalType()
assertAttributeLessThan(), assertLessThan()
assertAttributeLessThanOrEqual(), assertLessThanOrEqual()
assertAttributeNotContains(), assertContains()
assertAttributeNotContainsOnly(), assertContainsOnly()
assertAttributeNotEmpty(), assertEmpty()
assertAttributeNotEquals(), assertEquals()
assertAttributeNotInstanceOf(), assertInstanceOf()
assertAttributeNotInternalType(), assertInternalType()
assertAttributeNotSame(), assertSame()
assertAttributeSame(), assertSame()
assertClassHasAttribute(), assertClassHasAttribute()
assertClassHasStaticAttribute(), assertClassHasStaticAttribute()
assertClassNotHasAttribute(), assertClassHasAttribute()
assertClassNotHasStaticAttribute(), assertClassHasStaticAttribute()
assertContains(), assertContains()
assertContainsOnly(), assertContainsOnly()
assertContainsOnlyInstancesOf(), assertContainsOnlyInstancesOf()
assertCount(), assertCount()
assertEmpty(), assertEmpty()
assertEquals(), assertEquals()
assertEqualXMLStructure(), assertEqualXMLStructure()
assertFalse(), assertFalse()
assertFileEquals(), assertFileEquals()
assertFileExists(), assertFileExists()
assertFileNotEquals(), assertFileEquals()
assertFileNotExists(), assertFileExists()
assertFinite(), assertInfinite()
assertGreaterThan(), assertGreaterThan()
assertGreaterThanOrEqual(), assertGreaterThanOrEqual()
assertInfinite(), assertInfinite()
assertInstanceOf(), assertInstanceOf()
assertInternalType(), assertInternalType()
assertJsonFileEqualsJsonFile(), assertJsonFileEqualsJsonFile()
assertJsonFileNotEqualsJsonFile(), assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonFile(), assertJsonStringEqualsJsonFile()
assertJsonStringEqualsJsonString(), assertJsonStringEqualsJsonString()
assertJsonStringNotEqualsJsonFile(), assertJsonStringEqualsJsonFile()
assertJsonStringNotEqualsJsonString(), assertJsonStringEqualsJsonString()
assertLessThan(), assertLessThan()
assertLessThanOrEqual(), assertLessThanOrEqual()
assertNan(), assertNan()
assertNotContains(), assertContains()
assertNotContainsOnly(), assertContainsOnly()
assertNotCount(), assertCount()
assertNotEmpty(), assertEmpty()
assertNotEquals(), assertEquals()
assertNotInstanceOf(), assertInstanceOf()
assertNotInternalType(), assertInternalType()
assertNotNull(), assertNull()
assertNotRegExp(), assertRegExp()
assertNotSame(), assertSame()
assertNull(), assertNull()
assertObjectHasAttribute(), assertObjectHasAttribute()
assertObjectNotHasAttribute(), assertObjectHasAttribute()
assertPostConditions(), Fixtures
assertPreConditions(), Fixtures
assertRegExp(), assertRegExp()
assertSame(), assertSame()
assertStringEndsNotWith(), assertStringEndsWith()
assertStringEndsWith(), assertStringEndsWith()
assertStringEqualsFile(), assertStringEqualsFile()
assertStringMatchesFormat(), assertStringMatchesFormat()
assertStringMatchesFormatFile(), assertStringMatchesFormatFile()
assertStringNotEqualsFile(), assertStringEqualsFile()
assertStringNotMatchesFormat(), assertStringMatchesFormat()
assertStringNotMatchesFormatFile(), assertStringMatchesFormatFile()
assertStringStartsNotWith(), assertStringStartsWith()
assertStringStartsWith(), assertStringStartsWith()
assertThat(), assertThat()
assertTrue(), assertTrue()
assertXmlFileEqualsXmlFile(), assertXmlFileEqualsXmlFile()
assertXmlFileNotEqualsXmlFile(), assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile(), assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString(), assertXmlStringEqualsXmlString()
assertXmlStringNotEqualsXmlFile(), assertXmlStringEqualsXmlFile()
assertXmlStringNotEqualsXmlString(), assertXmlStringEqualsXmlString()
attribute(), assertThat()
attributeEqualTo(), assertThat()
Automated Documentation, Agile Documentation

C

Change Risk Anti-Patterns (CRAP) Index, Software Metrics for Code Coverage
classHasAttribute(), assertThat()
classHasStaticAttribute(), assertThat()
Code Coverage, Command-Line Options, Code Coverage Analysis, @covers, Whitelisting Files for Code Coverage
Branch Coverage, Software Metrics for Code Coverage
Class and Trait Coverage, Software Metrics for Code Coverage
Function and Method Coverage, Software Metrics for Code Coverage
Line Coverage, Software Metrics for Code Coverage
Opcode Coverage, Software Metrics for Code Coverage
Path Coverage, Software Metrics for Code Coverage
Whitelist, Whitelisting Files
Configuration, Command-Line Options
Constant, Setting PHP INI settings, Constants and Global Variables
contains(), assertThat()
containsOnly(), assertThat()
containsOnlyInstancesOf(), assertThat()

D

Data-Driven Tests, Implement PHPUnit_Framework_Test
Defect Localization, Test Dependencies
Depended-On Component, Test Doubles
Documenting Assumptions, Agile Documentation

E

equalTo(), assertThat()
Error, The Command-Line Test Runner
Error Handler, Testing PHP Errors
Exception, Testing Exceptions
expectException(), Testing Exceptions
expectExceptionCode(), Testing Exceptions
expectExceptionMessage(), Testing Exceptions
expectExceptionMessageRegExp(), Testing Exceptions
Extreme Programming, Agile Documentation

F

Failure, The Command-Line Test Runner
fileExists(), assertThat()
Fixture, Fixtures
Fluent Interface, Stubs

G

getMock(), Stubs
getMockBuilder(), Stubs
getMockForAbstractClass(), Mocking Traits and Abstract Classes
getMockForTrait(), Mocking Traits and Abstract Classes
getMockFromWsdl(), Stubbing and Mocking Web Services
Global Variable, Global State, Setting PHP INI settings, Constants and Global Variables
greaterThan(), assertThat()
greaterThanOrEqual(), assertThat()

H

hasAttribute(), assertThat()

I

identicalTo(), assertThat()
include_path, Command-Line Options
Incomplete Test, Incomplete Tests
isFalse(), assertThat()
isInstanceOf(), assertThat()
isNull(), assertThat()
isTrue(), assertThat()
isType(), assertThat()

L

lessThan(), assertThat()
lessThanOrEqual(), assertThat()
Logfile, Command-Line Options
Logging, Logging, Logging
logicalAnd(), assertThat()
logicalNot(), assertThat()
logicalOr(), assertThat()
logicalXor(), assertThat()

M

matchesRegularExpression(), assertThat()
method(), Stubs
Mock Object, Mock Objects

O

onConsecutiveCalls(), Stubs
onNotSuccessfulTest(), Fixtures

P

PHP Error, Testing PHP Errors
PHP Notice, Testing PHP Errors
PHP Warning, Testing PHP Errors
php.ini, Setting PHP INI settings, Constants and Global Variables
PHPUnit_Extensions_RepeatedTest, Subclass PHPUnit_Extensions_TestDecorator
PHPUnit_Extensions_TestDecorator, Subclass PHPUnit_Extensions_TestDecorator
PHPUnit_Framework_BaseTestListener, Implement PHPUnit_Framework_TestListener
PHPUnit_Framework_Error, Testing PHP Errors
PHPUnit_Framework_Error_Notice, Testing PHP Errors
PHPUnit_Framework_Error_Warning, Testing PHP Errors
PHPUnit_Framework_IncompleteTest, Incomplete Tests
PHPUnit_Framework_IncompleteTestError, Incomplete Tests
PHPUnit_Framework_Test, Implement PHPUnit_Framework_Test
PHPUnit_Framework_TestCase, Writing Tests for PHPUnit, Subclass PHPUnit_Framework_TestCase
PHPUnit_Framework_TestListener, Command-Line Options, Implement PHPUnit_Framework_TestListener, Test Listeners
PHPUnit_Runner_TestSuiteLoader, Command-Line Options
PHPUnit_Util_Printer, Command-Line Options
PHP_Invoker, @large, @medium, @small
Process Isolation, Command-Line Options

R

Refactoring, During Development
Report, Command-Line Options
returnArgument(), Stubs
returnCallback(), Stubs
returnSelf(), Stubs
returnValueMap(), Stubs

S

Selenium RC, Configuring Browsers for Selenium RC
setUp(), Fixtures
setUpBeforeClass, Sharing Fixture
setUpBeforeClass(), Fixtures
stringContains(), assertThat()
stringEndsWith(), assertThat()
stringStartsWith(), assertThat()
Stub, Stubs
Stubs, Cross-Team Tests
System Under Test, Test Doubles

T

tearDown(), Fixtures
tearDownAfterClass, Sharing Fixture
tearDownAfterClass(), Fixtures
Template Method, Fixtures
Test Dependencies, Test Dependencies
Test Double, Test Doubles
Test Groups, Command-Line Options, Groups
Test Isolation, Command-Line Options, Global State
Test Listener, Test Listeners
Test Suite, Organizing Tests, Test Suites
TestDox, Agile Documentation, @testdox
throwException(), Stubs
timeoutForLargeTests, @large
timeoutForMediumTests, @medium
timeoutForSmallTests, @small

W

Whitelist, Whitelisting Files for Code Coverage
will(), Stubs
willReturn(), Stubs