Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

PHPUnit Cheat Sheet

View 1_phpunit-api.md

PHPUnit API reference

  • version 3.6

TODO

Check those constraints:

$this->anything()
$this->contains($value)
$this->arrayHasKey($key)
$this->equalTo($value, $delta, $maxDepth)
$this->classHasAttribute($attribute)
$this->greaterThan($value)
$this->isInstanceOf($className)
$this->isType($type)
$this->matchesRegularExpression($pattern)
$this->stringContains($string, $case)
View 1_phpunit-api.md

Assertions

TOC

String

  • assertRegExp
  • assertNotRegExp
  • assertStringMatchesFormat
  • assertStringNotMatchesFormat
  • assertStringStartsWith
  • assertStringStartsNotWith
  • assertStringEndsWith
  • assertStringEndsNotWith

TOC

Array primitive

  • assertArrayHasKey
  • assertArrayNotHasKey
  • assertContains
  • assertNotContains
  • assertContainsOnly
  • assertNotContainsOnly
  • assertCount
  • assertNotCount
  • assertSameSize
  • assertNotSameSize

TOC

Array class attribute

  • assertAttributeContains
  • assertAttributeNotContains
  • assertAttributeContainsOnly
  • assertAttributeNotContainsOnly
  • assertAttributeCount
  • assertAttributeNotCount

TOC

Variable primitive

  • assertTrue
  • assertFalse
  • assertNotNull
  • assertNull
  • assertSame
  • assertNotSame
  • assertEquals
  • assertNotEquals
  • assertEmpty
  • assertNotEmpty
  • assertGreaterThan
  • assertGreaterThanOrEqual
  • assertLessThan
  • assertLessThanOrEqual
  • assertInstanceOf
  • assertNotInstanceOf
  • assertInternalType
  • assertNotInternalType

TOC

Variable class attribute

  • assertAttributeSame
  • assertAttributeNotSame
  • assertAttributeEquals
  • assertAttributeNotEquals
  • assertAttributeEmpty
  • assertAttributeNotEmpty
  • assertAttributeGreaterThan
  • assertAttributeGreaterThanOrEqual
  • assertAttributeLessThan
  • assertAttributeLessThanOrEqual
  • assertAttributeInstanceOf
  • assertAttributeNotInstanceOf
  • assertAttributeInternalType
  • assertAttributeNotInternalType

TOC

Class structure

  • assertClassHasAttribute
  • assertClassNotHasAttribute
  • assertClassHasStaticAttribute
  • assertClassNotHasStaticAttribute
  • assertObjectHasAttribute
  • assertObjectNotHasAttribute

TOC

File

  • assertFileEquals
  • assertFileNotEquals
  • assertStringEqualsFile
  • assertStringNotEqualsFile
  • assertFileExists
  • assertFileNotExists

TOC

Xml

  • assertXmlFileEqualsXmlFile
  • assertXmlFileNotEqualsXmlFile
  • assertXmlStringEqualsXmlFile
  • assertXmlStringNotEqualsXmlFile
  • assertXmlStringEqualsXmlString
  • assertXmlStringNotEqualsXmlString
  • assertEqualXMLStructure

TOC

Other

  • assertStringMatchesFormatFile
  • assertStringNotMatchesFormatFile
  • assertSelectCount
  • assertSelectRegExp
  • assertSelectEquals
  • assertTag
  • assertNotTag
  • assertThat

TOC

View 1_phpunit-api.md

Mocking

TOC

getMock()

Returns a mock object for the specified class.

  • param: ...
  • return: PHPUnit_Framework_MockObject_MockObject

PHPUnit_Framework_MockObject_MockObject getMock(string $originalClassName [, mixed $methods = array() [, array $arguments = array() [, string $mockClassName = '' [, bool $callOriginalConstructor = TRUE [, bool $callOriginalClone = TRUE [, bool $callAutoload = TRUE]]]]]])

$this->getMock(
    $originalClassName,
    $methods = array(),
    array $arguments = array(),
    $mockClassName = '',
    $callOriginalConstructor = TRUE,
    $callOriginalClone = TRUE,
    $callAutoload = TRUE
);

getMockForAbstractClass()

Returns a mock object for the specified abstract class with all abstract methods of the class mocked. Concrete methods to mock can be specified with the last parameter.

  • param: ...
  • return: PHPUnit_Framework_MockObject_MockObject

PHPUnit_Framework_MockObject_MockObject getMockForAbstractClass(string $originalClassName [, array $arguments = array() [, string $mockClassName = '' [, bool $callOriginalConstructor = TRUE [, bool $callOriginalClone = TRUE [, bool $callAutoload = TRUE [, array $mockedMethods = array()]]]]]])

$this->getMockForAbstractClass(
    $originalClassName,
    array $arguments = array(),
    $mockClassName = '',
    $callOriginalConstructor = TRUE,
    $callOriginalClone = TRUE,
    $callAutoload = TRUE,
    array $mockedMethods
);

getMockFromWsdl()

Returns a mock object based on the given WSDL file.

  • param: ...
  • return: PHPUnit_Framework_MockObject_MockObject

PHPUnit_Framework_MockObject_MockObject getMockFromWsdl(string $wsdlFile [, string $originalClassName [, string $mockClassName = '' [, array $mockedMethods = array() [, bool $callOriginalConstructor = TRUE]]]])

$this->getMockFromWsdl(
    $wsdlFile,
    $originalClassName = '',
    $mockClassName = '',
    array $methods = array(),
    $callOriginalConstructor = TRUE
);

getMockBuilder()

Returns a builder object to create mock objects using a fluent interface.

  • param: string
  • return: PHPUnit_Framework_MockObject_MockBuilder

PHPUnit_Framework_MockObject_MockBuilder getMockBuilder(string $className)

$mock = $this->getMockBuilder($className)
    ->setMockClassName($name)
    ->setConstructorArgs(array())
    ->disableOriginalConstructor()
    ->disableOriginalClone()
    ->disableAutoload()
    ->setMethods(array()|NULL)
    ->getMock();

$mock = $this->getMockBuilder($className)
    ->setMockClassName($name)
    ->setConstructorArgs(array())
    ->disableOriginalConstructor()
    ->disableOriginalClone()
    ->disableAutoload()
    ->setMethods(array()|NULL)
    ->getMockForAbstractClass();
View 1_phpunit-api.md

Setting up the expectations

Matchers

$mock->expects($this->matcher())

any()

Returns a matcher that matches when the method it is evaluated for is executed zero or more times.

  • return: PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount

PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount any()

never()

Returns a matcher that matches when the method it is evaluated for is never executed.

  • return: PHPUnit_Framework_MockObject_Matcher_InvokedCount

PHPUnit_Framework_MockObject_Matcher_InvokedCount never()

atLeastOnce()

Returns a matcher that matches when the method it is evaluated for is executed at least once.

  • return: PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce

PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce atLeastOnce()

once()

Returns a matcher that matches when the method it is evaluated for is executed exactly once.

  • return: PHPUnit_Framework_MockObject_Matcher_InvokedCount

PHPUnit_Framework_MockObject_Matcher_InvokedCount once()

exactly()

Returns a matcher that matches when the method it is evaluated for is executed exactly once.

  • param: int
  • return: PHPUnit_Framework_MockObject_Matcher_InvokedCount

PHPUnit_Framework_MockObject_Matcher_InvokedCount exactly(int $count)

at()

Returns a matcher that matches when the method it is evaluated for is invoked at the given $index.

  • param: int
  • return: PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex

PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex at(int $index)

Constraints

$mock->with($this->constraint())

equalTo()

logicalAnd()

logicalNot()

logicalOr()

logicalXor()

Returns

$mock->will($this->return())

returnValue()

Desc.

PHPUnit_Framework_MockObject_Stub_Return returnValue(mixed $value)

returnValueMap()

Desc.

PHPUnit_Framework_MockObject_Stub_ReturnValue MapreturnValueMap(array $valueMap)

returnArgument()

PHPUnit_Framework_MockObject_Stub_ReturnArgument returnArgument(int $argumentIndex)

returnCallback()

Desc.

PHPUnit_Framework_MockObject_Stub_ReturnCallback returnCallback(mixed $callback)

returnSelf()

Returns the current object.

PHPUnit_Framework_MockObject_Stub_ReturnSelf returnSelf()

throwException()

Desc.

PHPUnit_Framework_MockObject_Stub_Exception throwException(Exception $exception)

onConsecutiveCalls()

Desc.

PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls onConsecutiveCalls(mixed ... [, mixed ...])

good job :DDD

TheWass commented

FYI: "The UNIT in unit testing" link is broken. :frowning:

Owner
loonies commented

This Gist needs some serious update... Although it's nice to have PHPUnit methods grouped like here, PHPUnit got much better documentation, type hinting, and DocBlok comments in the later releases.

Update: Fixed link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.