Skip to content

Instantly share code, notes, and snippets.

@Vinai
Created November 12, 2014 16:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Vinai/139f4054744ae69a9a8d to your computer and use it in GitHub Desktop.
Save Vinai/139f4054744ae69a9a8d to your computer and use it in GitHub Desktop.
How to mock a trait method using getMockForTrait()
class MockTraitMethodTest extends \PHPUnit_Framework_TestCase
{
public function testTraitMock()
{
// Inspect getMockForTrait signature for what the other arguments do.
$methodsToMock = ['getBar'];
$mockTrait = $this->getMockForTrait('\Example\Foo', [], '', true, true, true, $methodsToMock);
$mockTrait->expects($this->any())
->method('getBar')
->willReturn('buz');
$this->assertEquals('buz', $mockTrait->getBar());
}
}
@adamdry
Copy link

adamdry commented Nov 3, 2015

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment