Skip to content

Instantly share code, notes, and snippets.

@hussani
Created February 26, 2014 21:02
Show Gist options
  • Save hussani/9238460 to your computer and use it in GitHub Desktop.
Save hussani/9238460 to your computer and use it in GitHub Desktop.
PHPUnit error on mock expecting interface
class SampleProduct
{
/* implementation */
public function setBougthBy(UserInterface $user)
{
$this->bougthBy = $user;
}
}
class SampleProductTest
{
/* other tests */
public function testSetBougthByWithValidUserShouldSuccess()
{
$stubUser = $this->getMock('SampleUser');
$product = new SampleProduct();
/**
* PHPUnit_Framework_Error
* Expected UserInterface, got Mock_MockObject
*/
$product->setBougthBy($stubUser);
}
}
<?php
class SampleUser implements UserInterface
{
/* Implementation */
}
<?php
interface UserInterface
{
/* methods */
}
@netojoaobatista
Copy link

Use the getMockBuilder method:

//...

$stubUser = $this->getMockBuilder('UserInterface')
                 ->setMethods(array('someMethod`))
                 ->getMock();

$product = new SampleProduct();

$product->setBougthBy($stubUser);
//...

;)

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