dqrnskni67375 2019-07-04 15:05
浏览 109
已采纳

PHPUnit - 调用Assert trait方法

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?

  • 写回答

1条回答 默认 最新

  • drpogkqqi536984960 2019-07-05 14:15
    关注

    I found a solution using Reflection, I'll post it in case someone needs it because I couldn't find anything related to my problem. Feel free to comment or add different solutions if you want.

    So the following test asserts that $serviceClass uses $traitClass and doesn't override methods declared in $traitClass except abstract ones and those which are manually added to $overriddenMethods array.

    public function testServiceUsesTrait()
    {
        $serviceClass = MyClass::class;
        $traitClass = MyTrait::class;
    
        $this->assertContains($traitClass, (new \ReflectionClass($serviceClass))->getTraitNames());
    
        $reflectedTrait = new \ReflectionClass($traitClass);
        $reflectedTraitFile = $reflectedTrait->getFileName();
    
        /**
         * If $serviceClass overrides some trait methods they
         * should be inserted into this array to avoid test failure.
         * Additional unit-tests should be written for overridden methods.
         */
        $overriddenMethods = [];
    
        foreach ($reflectedTrait->getMethods() as $traitMethod) {
            if ($traitMethod->isAbstract() || in_array($traitMethod->getName(), $overriddenMethods, true)) {
                continue;
            }
            $classMethod = new \ReflectionMethod($serviceClass, $traitMethod->getName());
            $this->assertSame($reflectedTraitFile, $classMethod->getFileName(), sprintf(
                'Trait method "%s" is overridden in class "%s" thus it must be excluded from this test.',
                $traitMethod->getName(), $serviceClass
            ));
        }
    }
    

    I also tried to compare declaring classes using $classMethod->getDeclaringClass() instead of comparing filenames but it didn't work: even if trait method is not overridden in class, getDeclaringClass() always returns the class itself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题