I use PHPUnit for my unit tests (Symfony2 app).
This is my method that I want test (simplified):
public function __construct(Email $email)
{
$this->email = $email;
}
public function sendEmail()
{
// Here, more logic and condition, it's simplified
if ($this->email->getOk() === 1) {
$this->email->setSendEmail(1);
}
return $this->email;
}
And my test:
$emailMock = $this->getMock(Email::class);
$emailMock->method('getOk')->will($this->returnValue(1));
$emailMock->method('setSendEmail')->will($this->returnArgument(0));
$email = new Email($emailMock);
$emailModified = $email->sendEmail();
var_dump($emailModified->getSendEmail()); // Returns NULL
My class Email is a Doctrine Entity (the setter and getter is inside) (an example of entity)
How can I test if my mock is hydrated by my class? I want to know if my method works by looking if my object is hydrated.
Edit
I try an other method :
$object = $this->getMock(MyClass::class);
$object->method('setPosition')->will(
$this->returnCallback(
$pos = function ($arg) {
return $arg;
}
)
);
$object->method('getPosition')->will($this->returnValue($pos));
$method = new \ReflectionMethod(MyClass::class, 'testMethod');
$method->setAccessible(true);
$res = $method->invoke(new MyClass($object));
var_dump($res->getPosition()) // Return inexploitable Closure
I would like that $object->getPosition() return 1 when I execute $object->setPosition(1) from my external class MyClass() that I test.