When you mock a class with PHPUnit it extends the class and overrides the methods that you are mocking with a replacement. So in your case, it would look like this:
$sut = $this->getMockBuilder('SomeClass')
->setMethods(['traitFunction'])
->getMock();
$sut->expects($this->once())
->method('traitFunction');
$sut->handle();
Though doing this is not a good idea, IMO. You are specifying how your class should do something rather than what it is supposed to do. If the method that your trait is providing is complicated enough to warrant its own class that you feel that you need to mock it. Make it a stand-alone class and use the strategy pattern to pass in the behavior.
Your class would like this:
Class SomeClass {
private $handler;
public function __construct(ClassFormerlyATrait $handler) {
$this->handler = $handler;
}
public function handler() {
$this->handler->traitFunction();
}
}
Class ClassFormerlyATrait {
public function traitFunction() {
// Does whatever the trait did.
}
}
This makes your design a little more complicated but you are now able to adapt your class to changes. For example, you need traitFunction
to do something slightly different under certain circumstances. You would extend the ClassFormerlyATrait with the new functionality and pass it in to SomeClass instead without worrying about breaking any existing functionality.