PHPUnit Documentation
Main Page
Attributes
backupGlobals
backupStaticProperties
beStrictAboutChangesToGlobalState
beStrictAboutCoverageMetadata
beStrictAboutOutputDuringTests
beStrictAboutTestsThatDoNotTestAnything
bootstrap
cacheDirectory
cacheResult
colors
columns
controlGarbageCollector
defaultTestSuite
defaultTimeLimit
disableCodeCoverageIgnore
displayDetailsOnIncompleteTests
displayDetailsOnPhpunitDeprecations
displayDetailsOnSkippedTests
displayDetailsOnTestsThatTriggerDeprecations
displayDetailsOnTestsThatTriggerErrors
displayDetailsOnTestsThatTriggerNotices
displayDetailsOnTestsThatTriggerWarnings
enforceTimeLimit
executionOrder
extensionsDirectory
failOnDeprecation
failOnEmptyTestSuite
failOnIncomplete
failOnNotice
failOnPhpunitDeprecation
failOnRisky
failOnSkipped
failOnWarning
ignoreDeprecatedCodeUnits
includeUncoveredFiles
numberOfTestsBeforeGarbageCollection
pathCoverage
processIsolation
registerMockObjectsFromTestArgumentsRecursively
requireCoverageMetadata
resolveDependencies
reverseDefectList
shortenArraysForExportThreshold
stderr
stopOnDefect
stopOnDeprecation
stopOnError
stopOnFailure
stopOnIncomplete
stopOnNotice
stopOnRisky
stopOnSkipped
stopOnWarning
testdox
testdoxSummary
timeoutForLargeTests
timeoutForMediumTests
timeoutForSmallTests
Elements
<coverage>
<extensions>
<groups>
<logging>
<php>
<phpunit>
<source>
<testsuites>
Guides
1.Assertions
1.Installation
10.Extending PHPUnit
2.Attributes
2.Writing Tests for PHPUnit
3.Events
3.The Command-Line Test Runner
4.Organizing Tests
4.The XML Configuration File
5.Copyright
5.Fixtures
6.Test Doubles
7.Code Coverage
8.Risky Tests
9.Error Handling
PHPUnit Manual
Methods
assertArrayHasKey()
assertArrayIsEqualToArrayIgnoringListOfKeys()
assertArrayIsEqualToArrayOnlyConsideringListOfKeys()
assertArrayIsIdenticalToArrayIgnoringListOfKeys()
assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys()
assertContains()
assertContainsOnly()
assertContainsOnlyArray()
assertContainsOnlyBool()
assertContainsOnlyCallable()
assertContainsOnlyClosedResource()
assertContainsOnlyFloat()
assertContainsOnlyInstancesOf()
assertContainsOnlyInt()
assertContainsOnlyIterable()
assertContainsOnlyNull()
assertContainsOnlyNumeric()
assertContainsOnlyObject()
assertContainsOnlyResource()
assertContainsOnlyScalar()
assertContainsOnlyString()
assertCount()
assertDirectoryExists()
assertDirectoryIsReadable()
assertDirectoryIsWritable()
assertEmpty()
assertEquals()
assertEqualsCanonicalizing()
assertEqualsIgnoringCase()
assertEqualsWithDelta()
assertFalse()
assertFileEquals()
assertFileExists()
assertFileIsReadable()
assertFileIsWritable()
assertFileMatchesFormat()
assertFileMatchesFormatFile()
assertGreaterThan()
assertGreaterThanOrEqual()
assertInfinite()
assertInstanceOf()
assertIsArray()
assertIsBool()
assertIsCallable()
assertIsFloat()
assertIsInt()
assertIsIterable()
assertIsList()
assertIsNumeric()
assertIsObject()
assertIsReadable()
assertIsResource()
assertIsScalar()
assertIsString()
assertIsWritable()
assertJson()
assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonFile()
assertJsonStringEqualsJsonString()
assertLessThan()
assertLessThanOrEqual()
assertMatchesRegularExpression()
assertNan()
assertNull()
assertObjectEquals()
assertObjectHasProperty()
assertSame()
assertSameSize()
assertStringContainsString()
assertStringContainsStringIgnoringCase()
assertStringEndsWith()
assertStringEqualsFile()
assertStringEqualsStringIgnoringLineEndings()
assertStringMatchesFormat()
assertStringMatchesFormatFile()
assertStringStartsWith()
assertThat()
assertTrue()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
disableAutoReturnValueGeneration()
disableOriginalClone()
disableOriginalConstructor()
getMock()
onlyMethods()
setConstructorArgs()
Samples
Example 1.10: Usage of assertEquals() with objects
Example 1.11: Usage of assertEquals() with arrays
Example 1.12: Usage of assertEqualsCanonicalizing()
Example 1.13: Usage of assertEqualsIgnoringCase()
Example 1.14: Usage of assertEqualsWithDelta()
Example 1.15: Usage of assertObjectEquals()
Example 1.16: Email value object with equals() method
Example 1.17: Usage of assertFileEquals()
Example 1.18: Usage of assertArrayIsEqualToArrayOnlyConsideringListOfKeys()
Example 1.19: Usage of assertArrayIsEqualToArrayIgnoringListOfKeys()
Example 1.1: Usage of assertTrue()
Example 1.20: Usage of assertArrayHasKey()
Example 1.21: Usage of assertContains()
Example 1.22: Usage of assertContainsOnly()
Example 1.23: Usage of assertContainsOnlyArray()
Example 1.24: Usage of assertContainsOnlyBool()
Example 1.25: Usage of assertContainsOnlyCallable()
Example 1.26: Usage of assertContainsOnlyFloat()
Example 1.27: Usage of assertContainsOnlyInt()
Example 1.28: Usage of assertContainsOnlyIterable()
Example 1.29: Usage of assertContainsOnlyNull()
Example 1.2: Usage of assertFalse()
Example 1.30: Usage of assertContainsOnlyNumeric()
Example 1.31: Usage of assertContainsOnlyObject()
Example 1.32: Usage of assertContainsOnlyResource()
Example 1.33: Usage of assertContainsOnlyClosedResource()
Example 1.34: Usage of assertContainsOnlyScalar()
Example 1.35: Usage of assertContainsOnlyString()
Example 1.36: Usage of assertContainsOnlyInstancesOf()
Example 1.37: Usage of assertObjectHasProperty()
Example 1.38: Usage of assertCount()
Example 1.39: Usage of assertSameSize()
Example 1.3: Usage of assertSame()
Example 1.40: Usage of assertEmpty()
Example 1.41: Usage of assertGreaterThan()
Example 1.42: Usage of assertGreaterThanOrEqual()
Example 1.43: Usage of assertLessThan()
Example 1.44: Usage of assertLessThanOrEqual()
Example 1.45: Usage of assertInstanceOf()
Example 1.46: Usage of assertIsArray()
Example 1.47: Usage of assertIsList()
Example 1.48: Usage of assertIsBool()
Example 1.49: Usage of assertIsCallable()
Example 1.4: Usage of assertSame() with objects
Example 1.50: Usage of assertIsFloat()
Example 1.51: Usage of assertIsInt()
Example 1.52: Usage of assertIsIterable()
Example 1.53: Usage of assertIsNumeric()
Example 1.54: Usage of assertIsObject()
Example 1.55: Usage of assertIsResource()
Example 1.56: Usage of assertIsScalar()
Example 1.57: Usage of assertIsString()
Example 1.58: Usage of assertNull()
Example 1.59: Usage of assertStringStartsWith()
Example 1.5: Usage of assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys()
Example 1.60: Usage of assertStringEndsWith()
Example 1.61: Usage of assertStringContainsString()
Example 1.62: Usage of assertStringContainsStringIgnoringCase()
Example 1.63: Usage of assertMatchesRegularExpression()
Example 1.64: Usage of assertStringMatchesFormat()
Example 1.65: Usage of assertStringMatchesFormatFile()
Example 1.66: Usage of assertStringEqualsFile()
Example 1.67: Usage of assertJson()
Example 1.68: Usage of assertJsonFileEqualsJsonFile()
Example 1.69: Usage of assertJsonStringEqualsJsonFile()
Example 1.6: Usage of assertArrayIsIdenticalToArrayIgnoringListOfKeys()
Example 1.70: Usage of assertJsonStringEqualsJsonString()
Example 1.71: Usage of assertXmlFileEqualsXmlFile()
Example 1.72: Usage of assertXmlStringEqualsXmlFile()
Example 1.73: Usage of assertXmlStringEqualsXmlString()
Example 1.74: Usage of assertDirectoryExists()
Example 1.75: Usage of assertDirectoryIsReadable()
Example 1.76: Usage of assertDirectoryIsWritable()
Example 1.77: Usage of assertFileExists()
Example 1.78: Usage of assertFileIsReadable()
Example 1.79: Usage of assertFileIsWritable()
Example 1.7: Usage of assertEquals()
Example 1.80: Usage of assertIsReadable()
Example 1.81: Usage of assertIsWritable()
Example 1.82: Usage of assertInfinite()
Example 1.83: Usage of assertNan()
Example 1.84: Usage of assertThat()
Example 1.8: Usage of assertEquals() with DateTimeImmutable objects
Example 1.9: Usage of assertEquals() with DOMDocument objects
Example 10.10: An example manifest.xml
Example 10.11: An XML configuration registering an ExampleExtension with parameters, loaded from an extensions directory
Example 10.12: An XML configuration registering an ExampleExtension with parameters
Example 10.13: Output of “phpunit –no-output –log-events-text php://stdout” command
Example 10.14: Output of “phpunit –no-output –log-events-verbose-text php://stdout” command
Example 10.1: A concrete test case using a default assertion
Example 10.2: A concrete test case using a domain-specific assertion
Example 10.3: An abstract test case with a domain-specific assertion
Example 10.4: A concrete test case extending an abstract test case with a domain-specific assertion
Example 10.5: A trait with a domain-specific assertion
Example 10.6: A concrete test case using a trait with a domain-specific assertion
Example 10.7: An example extension registering an ExampleSubscriber and an ExampleTracer
Example 10.8: An ExampleSubscriber printing a message when PHPUnit emits the ExecutionFinished event
Example 10.9: An ExampleTracer receiving all events
Example 2.10: Skipping a test using attributes
Example 2.11: Using the Depends attribute to express dependencies
Example 2.12: Leveraging the dependencies between tests
Example 2.13: Output generated when an array comparison fails
Example 2.14: Output when an array comparison of a long array fails
Example 2.15: Edge case in the diff generation when using weak comparison
Example 2.16: Using the Test attribute
Example 2.17: Using the TestDox attribute
Example 2.18: Using the TestDox attribute together with data providers
Example 2.19: Using the TestWith attribute
Example 2.1: A class named Greeter (declared in src/Greeter.php)
Example 2.20: Using the TestWithJson attribute
Example 2.2: A test class named GreeterTest (declared in tests/GreeterTest.php)
Example 2.3: Using the expectException() method
Example 2.4: Using a data provider that returns an array of arrays
Example 2.5: Using an external data provider that returns an array of arrays
Example 2.6: Using a data provider with named data sets
Example 2.7: Testing the output of a function or method
Example 2.8: Marking a test as incomplete
Example 2.9: Skipping a test
Example 4.1: Composing a Test Suite Using XML Configuration
Example 5.1: Example of a test class that uses setUp() and tearDown()
Example 5.2: Example of an abstract test case class with a setUp() method
Example 5.3: Example of a concrete test case class that extends an abstract test case class with a setUp() method
Example 5.4: Sharing fixture between the tests of a test suite
Example 6.10: Using willReturn() to stub a method call to return a list of values in the specified order
Example 6.11: Using willThrowException() to stub a method call to throw an exception
Example 6.12: Using willReturnArgument() to stub a method call to return one of the arguments
Example 6.13: Using willReturnCallback() to stub a method call to return a value from a callback
Example 6.14: Using willReturnSelf() to stub a method call to return a reference to the stub object
Example 6.15: Using willReturnMap() to stub a method call to return the value from a map
Example 6.16: Interface that declares a get-hooked property
Example 6.17: Test that uses a test stub of an interface with a get-hooked property
Example 6.18: An interface named X
Example 6.19: An interface named Y
Example 6.1: An interface named X
Example 6.20: A class named Z
Example 6.21: Using createMockForIntersectionOfInterfaces() to create a mock object for an intersection type
Example 6.22: Using createConfiguredMock() to create a mock object and configure return values
Example 6.23: Subject class that is part of the System under Test (SUT)
Example 6.24: Observer interface that is part of the System under Test (SUT)
Example 6.25: Testing that a method gets called once and with a specified argument
Example 6.26: Using the Mock Builder API to configure how the test double class is generated
Example 6.27: Interface that declares a set-hooked property
Example 6.28: Test that uses a mock object of an interface with a set-hooked property
Example 6.2: An interface named Y
Example 6.3: A class named Z
Example 6.4: Using createStubForIntersectionOfInterfaces() to create a test stub for an intersection type
Example 6.5: Using createConfiguredStub() to create a test stub and configure return values
Example 6.6: The class we want to test
Example 6.7: The dependency we want to stub
Example 6.8: Stubbing a method call to return a fixed value
Example 6.9: A method with a return type declaration
Example 7.1: Test class that specifies which class it wants to cover
Example 7.2: A test that specifies that it does not want to contribute to code coverage
Example 7.3: Using the @codeCoverageIgnore, @codeCoverageIgnoreStart, and @codeCoverageIgnoreEnd annotations
Example 9.1: tests/FirstPartyClassTest.php
Example 9.2: src/FirstPartyClass.php
Example 9.3: vendor/ThirdPartyClass.php
Example 9.4: phpunit.xml
Example 9.5: phpunit.xml
Example 9.6: Usage of expectUserDeprecationMessage()
Sections
Appendix
Application
Asserting Return Values
Boolean
Cardinality
Code Coverage
Command-Line Options
Composing a Test Suite Using XML Configuration
Composing a Test Suite Using the Filesystem
Constraints
Data Provider
Data Providers
DisableReturnValueGenerationForTestDoubles
Disabling PHPUnit’s error handler
DoesNotPerformAssertions
Enhancing concrete test cases
Equality
Expecting Exceptions
Extending the Test Runner
Extracting abstract test cases
Extracting traits
Failure Output
Filesystem
Global State
Global State Manipulation
Identity
IgnoreDeprecations
Ignoring Code Blocks
Ignoring issue suppression
Ignoring previously reported issues
Including Files
Incomplete Tests
Installing PHPUnit
Iterable
JSON
Limiting issues to “your code”
Math
Mock Objects
MockBuilder API
Objects
Outcome and Issues
Output During Test Execution
PHP on the Command-Line
Sharing Fixture
Skipping Tests
Skipping Tests
Software Metrics for Code Coverage
Static vs. Non-Static Usage of Assertion Methods
Strings
Targeting Units of Code
Template Methods
Test
Test
Test Dependencies
Test Dependencies
Test Execution Timeout
Test Groups
Test Isolation
Test Stubs
TestDox
TestRunner
TestSuite
Testing Output
Types
Unintentionally Covered Code
Useless Tests
WithoutErrorHandler
Wrapping the Test Runner