Download the PHP package downshiftorg/ezekiel without Composer
On this page you can find all versions of the php package downshiftorg/ezekiel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download downshiftorg/ezekiel
More information about downshiftorg/ezekiel
Files in downshiftorg/ezekiel
Package ezekiel
Short Description Alternative syntax wrapper and added convenience methods for using phpspec/prophecy within PHPUnit
License MIT
Informations about the package ezekiel
Ezekiel
Ezekiel gives you an alternate wrapper, some syntactic sugar, and added convenience methods for working with the phpspec/prophecy library from within PHPUnit.
For making simple stubs and mocks with Prophecy, Ezekiel reduces a lot of the boilerplate code and gives you a terse, but fairly expressive syntax for creating test double objects.
A simple example
Say you wanted to a quick and dirty test dummy. Using Prophecy directly, you'd need to do something like this:
With Ezekiel, you would just write this:
Short syntax
For really simple stub objects, when you don't care about argument matching or returning different values on different invocations, you can use Ezekiel's short syntax:
Special return values
Stubs/mocks can return the first argument passed by using the special string ~firstArg
Stubs/mocks can return the arbitrary arguments passed by using the special string ~arg=X
Stubs/mocks can return themselves by using the special string ~self
Stub/mocks can also return references to test-case public properties using the coffee script-ish @
syntax. This is really useful if you want to setup a generic stub or mock in a test case setUp()
method and modify individual components of what the test double returns for different test methods.
Stub/mocks can also pass arguments through callable function names using the skinny arrow ->
Stub/mocks can also return a joined version of all arguments passed by using the special string ~joinArgs
or ~joinArgs|DELIMETER
. This can be useful in very simple situations for verifying method invocations when it's not worth it to use the longer syntax or $this->calls()
Stubs/mocks can also return the result of invoking an anonymous function which is bound to the test-case's $this
and receives the invocation arguments as arguments.
You can also combine the @prop
syntax with anonymous functions to dynamically return the result of invoking a public property which is a anonymous function:
Longer syntax
Ezekiel can also accept a longer syntax that allows you to match arguments and return different values using 'with'
and 'return'
keywords.
Normally, the 'with'
property must be an array, and correponds to the arguments that are being matched. If you don't care to match a certain argument, pass in the string *
as a wildcard:
You can also pass in the non-array string value '*'
to match any combination of arguments. This is useful for specifying default return values.
Creating mocks
By default, Ezekiel creates test stubs, not mocks. That means that we're specifying the behavior of the test double objects within the system under tests, but not directly testing how these objects are used. If we want to create true mock objects that contain expections, Ezekiel can help with that too. To create a mock object that contains expectation, just use the longer syntax explained above, but use 'expect'
in place of 'with'
.
You can also specify how many times a mock invocation is expected using the 'times'
keyword. If you do not set the 'times'
property, Ezekiel will treat it as though you expected an invocation of that method with those arguments at least once.
Ezekiel also allows you to do simple argument wildcarding by using the special string '*'
.
You can make multiple expectations for a single method:
If you want to expect that a method should never be called, you can use a form of the short-syntax and the special string ~neverCalled
to indicate that:
Inspecting invocations
Prophecy keeps a record of your test double method invocations, so you can do cool stuff like:
Ezekiel gives you an alternative method for inspecting recorded invocations that can be helpful in making very specific test spy assertions without writing crazy prediction matchers and callbacks.
You can also pass the special string ~last
for the third paramater $invocationIndex
as a shortcut to returning the last invocation of the requested method. This is useful if you're re-using a cached stub and want to inspect the most recent invocation of a method.
Note: to use Ezekiel::calls()
you need to reset the recorded invocations in your teardown method like this: $this->recordedCalls = [];
The Ezekiel trait provides a tearDown
method that does this for you, but if you define your own in your test class, you'll need to do it yourself.
Transforming class names
If you want to make shortcuts to namespaced class names or otherwise transform the class names used by Ezekiel, just overwrite the transformClass
method of the Ezekiel trait.
Caching
Ezekiel also caches stubbed objects and will returned a cached value if you request a stub with the exact same specifications. This can give you a noticable speed increase since prophecy doesn't have to dynamically recreate the stubbed class multiple times. It also allows you to not worry about performance when setting up identical stubbed objects in different test methods.
Installation
Pull the package in through Composer.
Then, just use the Ezekiel trait in a TestCase class that extends PHPUnit_Framework_TestCase
.
If you want to have Ezekiel automatically available in all of your test cases, add the trait to a child class of PHPUnit_Framework_TestCase
that all of your test case classes extend, like so: