Download the PHP package unit-testing/class-spy without Composer
On this page you can find all versions of the php package unit-testing/class-spy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download unit-testing/class-spy
More information about unit-testing/class-spy
Files in unit-testing/class-spy
Package class-spy
Short Description a simple trait that aids in unit testing protected class methods
License MIT
Informations about the package class-spy
UnitTesting\ClassSpy
In some cases, you may want to mock a class's methods without using testing facility like https://github.com/padraic/mockery. This trait will let you do that.
Installation
composer require --dev unit-testing/class-spy:dev-master- or in
require-devblock ofcomposer.json, add"unit-testing/class-spy": "dev-master"and then runcomposer update
Usage
In your phpunit test, you'll create a stub of your class under test. In that stub definition, add use \UnitTesting\ClassSpy\WatchableTrait;. Your stub will be extended with the following methods:
trackMethodCall: For any method you want to test on the stub, you can create (or override its parent) method by callingreturn $this->trackMethodCall();from within the method.getAllMethodCalls: Returns an array of all the arguments passed to all tracked methods. The array has a list of method names as keys and an array of arguments passed to the method as the value.getMethodCalls($method, $index = null): Pass the method name to get the arguments passed to it. Optionally add an index (where 1 = the first call) to get the arguments for a specific call if the method was called more than once. If the argument was never called, or was not called less times than the index provided, it will return null.getLastMethodCall($method): Get the last set of arguments for a particular method.setMethodResult($method, $result): Fake a specific result to be passed back from your tracked method. If the$resultparameter is an instanceClosure, the Closure will be executed with the actual parameters and its result will be returned.
All of the above have their equivalents for static class methods: trackStaticMethodCall, getAllStaticMethodCalls, getStaticMethodCalls, getLastStaticMethodCall, setStaticMethodResult.