mock objects in phpunit
TRANSCRIPT
* www.ExigenServices.com
PHPUNIT: MOCK OBJECTS
• objects supplies non-deterministic results
• has states that are difficult to create or reproduce
• slow
• does not yet exist
• would have to include information and methods exclusively
for testing purposes
Difficulties when writing Unit Tests
* www.ExigenServices.com
PHPUNIT: MOCK OBJECTS
• getMockBuilder($classname)
• setMethods($methods)
• setConstructorArgs($args)
• setMockClassName($name)
• disableOriginalConstructor()
• disableOriginalClone()
• disableAutoload()
• getMock()
Chaining: getMockBuilder
* www.ExigenServices.com
PHPUNIT: MOCK OBJECTS
• getMock()
• getMockBuilder()
• getMockClass()
• getMockForAbstractClass()
• getMockFromWsdl()
Ways to get mock
* www.ExigenServices.com
PHPUNIT: MOCK OBJECTS
• any()
• once()
• never()
• atLeastOnce()
• exactly($count)
• at($index)
Asserting the number and order of method calls
* www.ExigenServices.com
PHPUNIT: MOCK OBJECTS
• returnValue($value)
• returnArgument($index)
• returnSelf()
• returnValueMap($map)
• returnCallback($callback)
• onConsecutiveCalls()
• throwException($exception)
Various behaviors of stubs
* www.ExigenServices.com
PHPUNIT: MOCK OBJECTS
Usable constraints for checking arguments
• attribute()
• anything()
• arrayHasKey()
• contains()
• equalTo()
• attributeEqualTo()
• fileExists()
• greaterThan()
• greaterThanOrEqual()
• classHasAttribute()
• classHasStaticAttribute()
• hasAttribute()
• identicalTo()
• isFalse()
• isInstanceOf()
• isNull()
• isTrue()
• isType()
• lessThan()
• lessThanOrEqual()
• matchesRegularExpression()
• stringContains()
* www.ExigenServices.com
PHPUNIT: MOCK OBJECTS
Logical combination of constraints
• logicalAnd()
• logicalOr()
• logicalNot()
• logicalXor()