unit-testing / mockery-helper
a simple trait for convenient implementation of mockery mocks and spies in PHPUnit tests
Installs: 68
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/unit-testing/mockery-helper
Requires
- php: >=5.4.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.4
Suggests
- mockery/mockery: Don't forget to install mockery/mockery in require-dev
This package is not auto-updated.
Last update: 2025-10-11 21:38:19 UTC
README
Simple module to help with creating mocks using mockery. You won't have to continually reference Mockery\Mockery and keep your tests a little DRY'er.
Installation
- composer require --dev unit-testing/mockery-helper:dev-master
- or in require-devblock ofcomposer.json, add"unit-testing/mockery-helper": "dev-master"and then runcomposer update
- don't forget to require-devmockery/mockery
Usage
- In your phpunit test, use UnitTesting\MockeryHelper\MockeryTrait.
- in your tearDown()method call$this->closeMocks()
- When you want to mock something, use $this->mock()as an alias forMockery::mock()
- You can also use $this->spy()as an alias forMockery::spy()
- Use the result of one of the above function calls as you normally would for your assertions.
- mockerymethod simply forwards any call to the Mockery static. The first argument is the method, and subsequent arguments are params for the Mockery method.
Example
<?php
use UnitTesting\MockeryHelper\MockeryTrait;
class SomeTest extends \PHPUnit_Framework_Testcase {
	use MockeryTrait;
	protected function tearDown()
	{
		$this->cleanUpSomeStuffBeforeEachTest();
		$this->closeMocks();
	}
	function testSomeMethodWithAMock()
	{
		$mock = $this->mock('Child');
		$parent = new ParentThatDependsChild($mock);
		$mock->shouldReceive('doSomething')->once()->with($this->mockery('type', 'string'))->andReturn('result');
		$result = $parent->someMethodWhichCallsDoSomethingOfChild('foo');
		$this->assertEquals('result', $result);
	}
}