Skip to content

Instantly share code, notes, and snippets.

@primayudantra
Forked from loonies/1_phpunit-api.md
Created January 28, 2019 05:34
Show Gist options
  • Save primayudantra/179f1e86199002fe5e105af38fd45a63 to your computer and use it in GitHub Desktop.
Save primayudantra/179f1e86199002fe5e105af38fd45a63 to your computer and use it in GitHub Desktop.
PHPUnit Cheat Sheet

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)

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

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();

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 $count times.

  • 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 ...])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment