Download the PHP package danrevah/shortifypunit without Composer
On this page you can find all versions of the php package danrevah/shortifypunit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download danrevah/shortifypunit
More information about danrevah/shortifypunit
Files in danrevah/shortifypunit
Package shortifypunit
Short Description PHP Mocking framework
License MIT
Homepage https://github.com/danrevah/ShortifyPunit
Informations about the package shortifypunit
ShortifyPunit
PHP Mocking Framework, inspired by Mockito library for Java
Table of contents
- Installation
- Mocking
- Stubbing
- Partial Mock
- Stubbing Method Chaining
- Verifying
- Argument Matcher
Installation
PHP Version >= 5.4 is required!
The following instructions outline installation using Composer. If you don't have Composer, you can download it from http://getcomposer.org/
Mocking Examples
Basic mocking example, if a function wasn't stubbed the return value will always be NULL
.
Stubbing
The when
function is used to stubbing methods with specific parameters, following a throws
, returns
or a callback
action.
Methods:
throws($exception)
- Throws an exceptionreturns($response)
- Returns a $responsecallback(function() { /*...*/ })
- Calling a callback
Partial Mock
partial mock is used when you need some of the methods to behave normally except from that one method you need to test. that can be done with partial mock, it keeps the logic unless you stub the method.
Stubbing Method Chaining
when
function is also used to stub chained methods, follows the same actions as the single function stubbing return
, throw
or callback
.
Verifying
Once created, mock will remember all invocations. Then you can selectively verify some interaction you are inserted in.
Methods:
atLeast($times)
- Verify called at least $timesatLeastOnce()
- Alias of atLeast(1)calledTimes($times)
- Verify called exactly $timesneverCalled()
- Alias of calledTimes(0)lessThan($times)
- Verify called less than $times
Argument Matcher
ShortifyPunit allows the use of Hamcrest PHP (https://github.com/hamcrest/hamcrest-php) matcher on any argument. Hamcrest is a library of "matching functions" that, given a value, return true if that value matches some rule.
Hamcrest matchers are included by default.
Examples:
Some common Hamcrest matchers:
- Core
anything
- always matches, useful if you don't care what the object under test is
- Logical
allOf
- matches if all matchers match, short circuits (like PHP &&)anyOf
- matches if any matchers match, short circuits (like PHP ||)not
- matches if the wrapped matcher doesn't match and vice versa
- Object
equalTo
- test object equality using the == operatoranInstanceOf
- test typenotNullValue
,nullValue
- test for null
- Number
closeTo
- test floating point values are close to a given valuegreaterThan
,greaterThanOrEqualTo
,lessThan
,lessThanOrEqualTo
- test ordering
- Text
equalToIgnoringCase
- test string equality ignoring caseequalToIgnoringWhiteSpace
- test string equality ignoring differences in runs of whitespacecontainsString
,endsWith
,startsWith
- test string matching