Supposing I have a trait which currently has a method:
trait MyTrait
{
public function traitMethod()
{
return true;
}
}
Now let's say this trait is used by several classes but I don't want to write a unit-test for every class. Instead I want to write a single unit-test only for the trait:
public function testTraitMethod()
{
$trait = $this->getMockForTrait(MyTrait::class);
$this->assertTrue($trait->traitMethod());
}
But the problem is that a class may actually override trait's method:
class MyClass
{
use MyTrait;
public function traitMethod()
{
return false;
}
}
In this case MyClass
is doing something wrong but I'm not aware of it since I'm testing only the trait.
My idea would be write a single unit-test for every class just to check if it's using that trait and it's not overriding that method. If a class needs to override trait's method then it needs a specific unit-test as well.
Currently I'm writing unit-tests for each class that implements my trait but it's of course kind of copy-paste tests everywhere.
So is there a way to test if a class calls it's underlying trait method?