In a piece of legacy code I was tasked to test a static function in a trait like that:
namespace App\Model\SomeLogic;
trait WhyDecidedToUseTrait
{
public static function aMethodThatDoesSomeFancyStuff()
{
//Method Logic
}
}
And from this piece of documentation using the getMockForTrait
method. But in my case making a dummy object in order to test a static function where object instants are useless to begin with has no value.
Also testing the method in objects that use this trait seems pretty much time consuming, also tdoing a larger scale refactoring is time consuming as well.
So how I can test the trait in order to gradually refactor any class that uses it?
Just make a Dummy class using this trait:
namespace Tests\YourTeasts;
use PHPUnit\Framework\TestCase;
use App\Model\SomeLogic\WhyDecidedToUseTrait;
class Dummy
{
use WhyDecidedToUseTrait;
}
class StoreExtraAttributesTraitTest extends TestCase
{
public function setTheStaticMethod()
{
Dummy::aMethodThatDoesSomeFancyStuff();
//Assertions are done here
}
}
Hence you can test the method, but in case os coverage tests I have no idea whenther is shown or not.