0% found this document useful (0 votes)
438 views

PHPUnit Cheat Sheet

This document provides a cheat sheet for PHPUnit version 3.6 that summarizes various assertions, matchers, returns, constraints, and other features for testing with PHPUnit. It includes sections on assertions for arrays, strings, files, classes, objects, and more. It also demonstrates how to create and configure mock objects using the getMock() method and test exceptions. The cheat sheet was created by Ian Monge and is available under a Creative Commons license.

Uploaded by

Oxkhar Punt Ocom
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
438 views

PHPUnit Cheat Sheet

This document provides a cheat sheet for PHPUnit version 3.6 that summarizes various assertions, matchers, returns, constraints, and other features for testing with PHPUnit. It includes sections on assertions for arrays, strings, files, classes, objects, and more. It also demonstrates how to create and configure mock objects using the getMock() method and test exceptions. The cheat sheet was created by Ian Monge and is available under a Creative Commons license.

Uploaded by

Oxkhar Punt Ocom
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHPUNIT CHEAT SHEET

VERSION 0.1 BASED ON PHPUNIT 3.6


ASSERTIONS
Basics
assertEmpty assertEquals assertFalse assertGreaterThan assertGreaterThanOrEqual assertInternalType assertLessThan assertLessThanOrEqual assertNotEmpty assertNotEquals assertNotInternalType assertNotNull assertNotSame assertNull assertSame assertTrue

Arrays and Traversable Objects


assertArrayHasKey assertArrayNotHasKey assertContains assertContainsOnly assertCount assertNotContains assertNotContainsOnly assertNotCount

MATCHERS for EXPECTS method


any at atLeastOnce exactly never once

RETURNS for WILL method


onConsecutiveCalls returnArgument returnCallback returnValue throwException

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

CONSTRAINTS for WITH method


Commutation
logicalAnd logicalNot logicalOr logicalXor

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

XML and HTML


assertEqualXMLStructure assertNotTag assertTag assertXmlFileEqualsXmlFile assertXmlFileNotEqualsXmlFile assertXmlStringEqualsXmlFile assertXmlStringEqualsXmlString assertXmlStringNotEqualsXmlFile assertXmlStringNotEqualsXmlString

Classes and Objects


assertAttributeContains assertAttributeContainsOnly assertAttributeEmpty assertAttributeEquals assertAttributeGreaterThan assertAttributeGreaterThanOrEqual assertAttributeInstanceOf assertAttributeInternalType assertAttributeLessThan assertAttributeLessThanOrEqual assertAttributeNotContains assertAttributeNotContainsOnly assertAttributeNotEmpty assertAttributeNotEquals assertAttributeNotInstanceOf assertAttributeNotInternalType assertAttributeNotSame assertAttributeSame

Classes & Objects


attribute attributeEqualTo classHasAttribute classHasStaticAttribute hasAttribute isInstanceOf isType

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

Example with a Mock object


<?php require_once 'SomeClass.php'; class StubTest extends PHPUnit_Framework_TestCase { public function testStub() { // Create a stub for the SomeClass class $stub = $this->getMock('SomeClass', array(doSomething) ); // 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')); } }

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)

You might also like