You are on page 1of 1

PHPUNIT CHEAT SHEET

VERSION 0.1 BASED ON PHPUNIT 3.6

ASSERTIONS MATCHERS for EXPECTS RETURNS for WILL method


Arrays and Traversable Objects onConsecutiveCalls
method
Basics assertArrayHasKey returnArgument
any
assertEmpty assertArrayNotHasKey returnCallback
at
assertEquals assertContains returnValue
atLeastOnce
assertFalse assertContainsOnly throwException
exactly
assertGreaterThan assertCount
never
assertGreaterThanOrEqual assertNotContains
once “getMock()” method
assertInternalType assertNotContainsOnly
/**
assertLessThan assertNotCount
CONSTRAINTS for WITH * @return PHPUnit_Framework_
assertLessThanOrEqual * MockObject_MockObject
assertNotEmpty Strings method */
assertNotEquals assertNotRegExp
Commutation public function getMock(
assertNotInternalType assertRegExp
$originalClassName,
assertNotNull assertSelectEquals logicalAnd $methods = array(),
assertNotSame assertSelectCount logicalNot array $arguments = array(),
assertNull assertSelectRegExp logicalOr $mockClassName = '',
assertSame assertStringEndsNotWith logicalXor $callOriginalConstructor = TRUE,
assertTrue assertStringEndsWith
Basics $callOriginalClone = TRUE,
assertStringEqualsFile
$callAutoload = TRUE
Objects assertStringMatchesFormat anything
)
assertInstanceOf assertStringMatchesFormatFile arrayHasKey
assertObjectHasAttribute assertStringNotEqualsFile contains
assertObjectNotHasAttribute assertStringNotMatchesFormat equalTo Template methods
assertNotInstanceOf assertStringNotMatchesFormatFile greaterThan pub static fn setUpBeforeClass()
assertStringStartsNotWith greaterThanOrEqual pro fn setUp()
Classes assertStringStartsWith identicalTo pro fn assertPreConditions()
assertClassHasAttribute isFalse pro fn assertPostConditions()
assertClassHasStaticAttribute XML and HTML isNull pro fn tearDown()
assertClassNotHasAttribute assertEqualXMLStructure isTrue pub static fn tearDownAfterClass()
assertClassNotHasStaticAttribute assertNotTag lessThan pro fn onNotSuccessfulTest()
assertTag lessThanOrEqual
Classes and Objects assertXmlFileEqualsXmlFile Utilities
assertAttributeContains assertXmlFileNotEqualsXmlFile Classes & Objects $this->fail()
assertAttributeContainsOnly assertXmlStringEqualsXmlFile attribute $this->markTestIncomplete()
assertAttributeEmpty assertXmlStringEqualsXmlString attributeEqualTo $this->markTestSkipped()
assertAttributeEquals assertXmlStringNotEqualsXmlFile classHasAttribute $this->expectOutputString()
assertAttributeGreaterThan assertXmlStringNotEqualsXmlString classHasStaticAttribute $this->setExpectedException()
assertAttributeGreaterThanOrEqual hasAttribute
assertAttributeInstanceOf Files isInstanceOf Annotations
assertAttributeInternalType assertFileEquals isType /**
assertAttributeLessThan assertFileExists
String * @expectedException <exceptionName>
assertAttributeLessThanOrEqual assertFileNotEquals
* @dataProvider <methodName>
assertAttributeNotContains assertFileNotExists matchesRegularExpression * @depends <methodName>
assertAttributeNotContainsOnly stringContains
Others */
assertAttributeNotEmpty stringEndsWith
assertAttributeNotEquals assertThat stringStartsWith <exceptionName> can be:
assertAttributeNotInstanceOf PHPUnit_Framework_Error
assertAttributeNotInternalType Others
PHPUnit_Framework_Warning
assertAttributeNotSame fileExists
assertAttributeSame

Example with a Mock object Testing Exceptions


<?php /**
require_once 'SomeClass.php'; * @expectedException MyException
*/
class StubTest extends PHPUnit_Framework_TestCase public function testThrowsAnException()
{ {
public function testStub() $stub = $this->getMock(‘stdClass’);
{ $stub->expects( $this->any() )
// Create a stub for the SomeClass class ->method(‘push’)
$stub = $this->getMock('SomeClass', array(‘doSomething’) ); ->will( $this->throwException( new MyException ) );
$stub->push(42);
// Configure the stub }
$stub->expects( $this->once() )
->method( 'doSomething' )
->with( $this->equalTo('bar') )
->will( $this->returnValue('foo') );

// Calling $stub->doSomething() will now return 'foo'


$this->assertEquals('foo', $stub->doSomething('bar'));
}
}

“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)

You might also like