Php Unit Cheat Sheet

ADVERTISEMENT

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

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go