Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit - Mocking a trait

I have a trait that is used by multiple classes, i.e

class SomeClass
{
     use TimeoutTrait;

     function handle() {
         $this->traitFunction()    // can this be mocked?
     }
}

PHP unit is capable to mock the traitFunction()?.

Thanks in advance for the help.

Greetings

like image 697
Pablo Rodriguez V. Avatar asked Feb 06 '23 21:02

Pablo Rodriguez V.


1 Answers

Traits are code containers which code is "copy-pasted" by compiler into a class you want to use it in, thus making it reusable all over the world.

Generally, there is nothing special to do with trait functions in unit tests, bec. when class is instantiated trait functions don't make any difference from functions that could be copy-pasted right they are written in trait inside your class.

So, don't hesitate to mock your trait function the same way as any other regular function defined inside the class.

like image 71
Paul T. Rawkeen Avatar answered Feb 08 '23 11:02

Paul T. Rawkeen