cyruscollier / phpspec-php-mock
Adds the PHP Mock function mocking library as a phpspec Collaborator
Requires
- php-mock/php-mock-prophecy: 0.0.2
- phpspec/phpspec: ^6.1.1
This package is auto-updated.
Last update: 2024-12-24 21:24:25 UTC
README
Adds the PHP Mock function mocking library as a phpspec Collaborator
This phpspec extension allows you to mock non-deterministic PHP core functions (time()
, rand()
, etc.), or mock functions from other libraries or frameworks that have side effects from dependencies like a database, filesystem or HTTP request.
By using the specially named parameter $functions
in any example method, phpspec-php-mock will turn that parameter into a special FunctionCollaborator
that wraps the php-mock-prophecy library's PHPProphet
. This allows you to mock return values for any function as you normally would for an ObjectProphecy
.
Changelog
v2.1.2 - Fixed tests related to previous version, updated phpspec to 6.x
v2.1.1 - Interface return type fix
v2.1 - Added support for defining multiple namespaces for function prophecies
v2.0 - Updated for phpspec 4.x, added spec and doc for usage with Throw Matcher
v1.0 - Initial build for phpspec 2.x
Installation
Add this to your composer.json:
{
"require-dev": {
"cyruscollier/phpspec-php-mock": "dev-master"
}
}
Then add this to your phpspec.yml:
extensions:
PhpSpec\PhpMock\Extension\PhpMockExtension: ~
Example
A PHP class that uses a non-deterministic function:
class Time { function getCurrentTime() { return time(); } }
The spec for that class that mocks the time()
function:
use PhpSpec\ObjectBehavior; class TimeSpec extends ObjectBehavior { function it_is_initializable() { $this->shouldHaveType('Time'); } function it_gets_the_current_time($functions) { $functions->time()->willReturn(123); $this->getCurrentTime()->shouldReturn(123); } }
Examples that test Exceptions require an extra line to reveal the function prophecy manually, since the Throw Matcher executes the Subject method differently than the other matchers :
use PhpSpec\ObjectBehavior; class TimeSpec extends ObjectBehavior { function it_is_initializable() { $this->shouldHaveType('Time'); } function it_gets_the_current_time($functions) { $functions->time()->willReturn(123); $functions->reveal(); $this->shouldThrow('\Exception')->during('getCurrentTime', [123]); } }