PHPUnit Cheat Sheet
PHPUnit Cheat Sheet
getMock() method
/** * @return PHPUnit_Framework_ * MockObject_MockObject */ public function getMock( $originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE )
Strings
assertNotRegExp assertRegExp assertSelectEquals assertSelectCount assertSelectRegExp assertStringEndsNotWith assertStringEndsWith assertStringEqualsFile assertStringMatchesFormat assertStringMatchesFormatFile assertStringNotEqualsFile assertStringNotMatchesFormat assertStringNotMatchesFormatFile assertStringStartsNotWith assertStringStartsWith
Basics
anything arrayHasKey contains equalTo greaterThan greaterThanOrEqual identicalTo isFalse isNull isTrue lessThan lessThanOrEqual
Objects
assertInstanceOf assertObjectHasAttribute assertObjectNotHasAttribute assertNotInstanceOf
Template methods
pub pro pro pro pro pub pro static fn setUpBeforeClass() fn setUp() fn assertPreConditions() fn assertPostConditions() fn tearDown() static fn tearDownAfterClass() fn onNotSuccessfulTest()
Classes
assertClassHasAttribute assertClassHasStaticAttribute assertClassNotHasAttribute assertClassNotHasStaticAttribute
Utilities
$this->fail() $this->markTestIncomplete() $this->markTestSkipped() $this->expectOutputString() $this->setExpectedException()
Files
assertFileEquals assertFileExists assertFileNotEquals assertFileNotExists
Annotations
/** * @expectedException <exceptionName> * @dataProvider <methodName> * @depends <methodName> */ <exceptionName> can be: PHPUnit_Framework_Error PHPUnit_Framework_Warning
String
matchesRegularExpression stringContains stringEndsWith stringStartsWith
Others
assertThat
Others
fileExists
Testing Exceptions
/** * @expectedException MyException */ public function testThrowsAnException() { $stub = $this->getMock(stdClass); $stub->expects( $this->any() ) ->method(push) ->will( $this->throwException( new MyException ) ); $stub->push(42); }
PHPUnit Cheat Sheet by Ian Monge (http://otroblogmas.com) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)