-
Notifications
You must be signed in to change notification settings - Fork 27
Mockery
This is a tutorial for using https://github.com/etsy/phpunit-extensions/tree/master/PHPUnit/Extensions/Mockery.
Mockery is a simple yet PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succint API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL). Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library, Mockery is easy to integrate with PHPUnit and can operate alongside phpunit-mock-objects without the World ending. — Read more
This extension provides a tooling to make Mockery easier to use in PHPUnit.
This new test case eliminates
- The need for a
bootstrap.php
- The need for the
Mockery\Adapter\Phpunit\TestListener
- The need for explicit static calls to
Mockery
- You can still use explicit static calls to
Mockery
, but withHamcrest
,@mockery
, and$this->getMockery()
you should never need to.
- You can still use explicit static calls to
All of this can be had while maintaining backwards compatibility with the MockObject
Framework currently employed in PHPUnit
.
You can give your protected
or public
properties in your test case an attribute of @mockery {string param}
to create a mockery mock of that specification.
<?php
class MyTest extends PHPUnit_Extensions_Mockery_TestCase {
/** @mockery Foo */
protected $foo;
}
?>
is the same as
<?php
use Mockery;
class MyTest extends PHPUnit_Framework_TestCase {
protected $foo;
protected function setUp() {
parent::setUp();
$this->foo = Mockery::mock('Foo');
}
}
?>
Another way to create a mockery mock is like so
<?php
class MyTest extends PHPUnit_Extensions_Mockery_TestCase {
protected $foo;
protected function setUp() {
parent::setUp();
$this->foo = $this->getMockery('foo');
}
}
?>
which is still the same as
<?php
class MyTest extends PHPUnit_Extensions_Mockery_TestCase {
/** @mockery Foo */
protected $foo;
}
?>
If you wish to try something more complex, like have a partial mock for Bar
that needs a Foo
for construction, you could do the following.
<?php
class MyTest extends PHPUnit_Extensions_Mockery_TestCase {
/** @mockery Foo */
protected $foo;
protected $bar;
protected function setUp() {
parent::setUp();
$this->bar = $this->getMockery(new Bar($this->foo));
}
}
?>
This test case does not override $this->getMock()
or $this->getMockBuilder()
, so that you may still use old mocks in new tests, or you could even switch your old tests over to this test case with out changing anything other than the extends
clause.
Through some naughty implementation details, the expectations on mockery mocks are included in the assertion count for the test, so now you can run tests using mockery that will pass the --strict
standards.