Codecoverage phpunit测试问题

Codecoverage phpunit测试问题,php,phpunit,code-coverage,Php,Phpunit,Code Coverage,我正在运行phpunit版本9.2,我想知道为什么phpunit覆盖范围中没有包括我的方法 这是我的班级: class Methods extends Template { const DISABLED_PAYMENT_METHODS_1 = 'free'; const DISABLED_PAYMENT_METHODS_2 = 'adyen_cc'; const DISABLED_PAYMENT_METHODS_3 = 'adyen_oneclick'; pr

我正在运行phpunit版本9.2,我想知道为什么phpunit覆盖范围中没有包括我的方法

这是我的班级:

class Methods extends Template
{

    const DISABLED_PAYMENT_METHODS_1 = 'free';
    const DISABLED_PAYMENT_METHODS_2 = 'adyen_cc';
    const DISABLED_PAYMENT_METHODS_3 = 'adyen_oneclick';

    protected $paymentMethodList;

    protected $storeManager;

    protected $logger;

    public function __construct(
        Context $context,
        PaymentMethodList $paymentMethodList,
        StoreManagerInterface $storeManager,
        LoggerInterface $logger,
        array $data = []
    ) {
        $this->paymentMethodList = $paymentMethodList;
        $this->storeManager = $storeManager;
        $this->logger = $logger;
        parent::__construct($context, $data);
    }

    public function getPaymentMethods()
    {
        try {
            $storeId = $this->storeManager->getStore()->getId();
            $paymentList = $this->paymentMethodList->getActiveList($storeId);
            $resultPayments = [];

            foreach ($paymentList as $payment) {
                if ($payment->getCode() !== self::DISABLED_PAYMENT_METHODS_1 &&
                    $payment->getCode() !== self::DISABLED_PAYMENT_METHODS_2 &&
                    $payment->getCode() !== self::DISABLED_PAYMENT_METHODS_3
                ) {
                    $resultPayments[] = $payment;
                }
            }

            return $resultPayments;
        } catch (Exception $e) {
            $this->logger->error($e->getMessage());
            return false;
        }
    }
}
这是我的测试课:

class MethodsTest extends TestCase
{

    private $model;


    private function getSimpleMock($originalClassName)
    {
        return $this->getMockBuilder($originalClassName)
            ->disableOriginalConstructor()
            ->getMock();
    }

    public function setUp() : void
    {
        $context = $this->getSimpleMock(Context::class);
        $paymentMethodList = $this->getSimpleMock(PaymentMethodList::class);
        $storeManager = $this->getSimpleMock(StoreManagerInterface::class);
        $logger = $this->getSimpleMock(LoggerInterface::class);

        $this->model = new Methods(
            $context,
            $paymentMethodList,
            $storeManager,
            $logger,
            []
        );
    }

    public function testGetPaymentMethods()
    {
        $stub = $this->createMock(Methods::class);
        $stub->method('getPaymentMethods')
            ->willReturn([]);
        try {
            $stub->getPaymentMethods();
            $this->fail("Expected exception!");
        } catch (\Exception $error) {
            $this->assertEquals("Expected exception!", $error->getMessage());
        }
    }
}
当我运行命令以获取覆盖率时。我得到:

我真的很好奇为什么我的测试没有被涵盖,或者至少是例外部分?你能分享一下你的想法吗?为什么?我能做些什么来解决这个问题?现在我得到了29%的保险,我希望得到至少60%的保险


感谢您

在这一行
$stub=$this->createMock(Methods::class)
您正在创建
方法的模拟
类,因此没有实际测试真正的类


您需要使用在
setUp()
方法中创建的对象,并对传入的依赖项设置模拟返回(可能会将其中一些转换为类属性)。

您应该测试真实的类,例如:

public function testGetPaymentMethods()
{
    // define a payment
    $paymentFreeCode = $this->createMock(Payment::class);
    $paymentFreeCode->method('getcode')
        ->willReturn("free");

    // define a payment
    $payment = $this->createMock(Payment::class);
    $payment->method('getcode')
        ->willReturn("invalid-code");

    $paymentList = [
        $paymentFreeCode,
        $payment,
    ];

    // define a store
    $store = $this->createMock(Store::class);
    $store->method('getId')
        ->willReturn("my-store-id");

    // return store from the store manager
    $this->storeManager->method('getStore')
        ->willReturn(myStore);

    // return the payment list
    $this->paymentMethodList->method('getActiveList')->with("my-store-id")
        ->willReturn($paymentList);

    // call the real class instrumented with mocks
    $paymentMethods = $this->model->getPaymentMethods();

    $this->assertIsArray($paymentMethods);
    $this->assertCount($paymentMethods, 1);
}

您的测试方法为该方法创建了一个mock,因此运行的是mock方法。好的,但是有什么线索可以在不使用mock的情况下执行吗?这也是关于依赖注入hi@Dezza,thnx来更新你的帖子,但是如果你能给我一个链接或者一个例子,我会非常感激