php – 测试覆盖特征方法执行

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 测试覆盖特征方法执行脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这样的情况.我有一些第三方特征(我不想测试)我有我的特性使用这个特性,在某些情况下运行第三方特质方法(在下面的例子中我总是运行它).

我有这样的代码

use Mockery;
use PHPUnIT\Framework\testCase;

class SampleTest extends TestCase
{
    /** @test */
    public function it_runs_parent_method_alternative()
    {
        $class  = Mockery::mock(B::class)->;makePartial();

        $class->shouldReceive('fooX')->once();

        $this->assertSame('bar',$class->foo());
    }

    PRotected function tearDown()
    {
        Mockery::close();
    }
}

trait X {
    function foo() {
        $this->something->complex3rdpartyStuff();
    }
}

trait Y2 {

    function foo() {
        $this->fooX();
        return 'bar';
    }
}

class B {
    use Y2,X {
        Y2::foo insteadof X;
        X::foo as fooX;
    }
}

它会工作正常,但我不希望代码组织这样.在类I的上面的代码中使用两个特征,但在代码中我想测试其实特征使用开头提到的其他特征.

但是当我有这样的代码时:

<?PHP

use Mockery;
use PHPUnit\Framework\TestCase;

class SampleTest extends TestCase
{
    /** @test */
    public function it_runs_parent_method()
    {
        $class  = Mockery::mock(A::class)->makePartial();

        $class->shouldReceive('fooX')->once();

        $this->assertSame('bar',$class->foo());
    }

    protected function tearDown()
    {
        Mockery::close();
    }
}

trait X {
    function foo() {
        $this->something->complex3rdpartyStuff();
    }
}

trait Y {
    use X {
        foo as fooX;
    }

    function foo() {
        $this->fooX();
        return 'bar';
    }
}

class A {
    use Y;
}

我越来越:

所以看来Mockery在这种情况下不再嘲笑X :: foo方法了.是否有办法使用这样组织的代码编写这样的测试?

到目前为止,无法模拟更深层次的别名方法.您可以使用本地方法代理别名方法调用,并允许模拟受保护的方法.

检查下面的代码

use Mockery;
use PHPUnit\Framework\TestCase;

class SampleTest extends TestCase
{
    /** @test */
    public function it_runs_parent_method()
    {
        $mock  = Mockery::mock(A::class)->shouldAllowMockingProtectedMethods()->makePartial();

        $mock->shouldReceive('ProxyTraitCall')->once();

        $this->assertSame('bar',$mock->foo());
    }

    protected function tearDown()
    {
        Mockery::close();
    }
}

trait X {
    function foo() {
        $this->something->complex3rdpartyStuff();
    }
}

trait Y {
    use X {
        foo as fooX;
    }

    function foo() {
        $this->proxyTraitCall();
        return 'bar';
    }

    function proxyTraitCall() {
        return $this->fooX();
    }
}

如果你自动加载特性,你可以尝试使用Mockery来overload.

/** @test */
public function it_runs_parent_method()
{
    $trait = Mockery::mock("overload:" . X::class);
    $trait->shouldReceive('foo')->once();

    $class  = Mockery::mock(A::class)->makePartial();

    $this->assertSame('bar',$class->foo());
}

Don’t test implementation details.测试它就像你使用它.

用户必须只知道使用它的公共接口,为什么测试应该有所不同?
一个内部方法调用不同的事实是实现细节和测试这打破封装.如果有一天你会在不改变类行为的情况下从trait切换到class方法,那么即使来自外部的类看起来相同,你也必须修改测试.

来自Dave Thomas和Andy Hunt的语用单元测试

脚本宝典总结

以上是脚本宝典为你收集整理的php – 测试覆盖特征方法执行全部内容,希望文章能够帮你解决php – 测试覆盖特征方法执行所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。