«

hyperf pest单元测试 使用容器

时间:2024-8-22 17:24     作者:xiang     分类:


test('addClientGroup', function () {
    \App\System\Model\SystemUser::truncate();
    $container = \Hyperf\Context\ApplicationContext::getContainer();
    $res = $container->get(ClientService::class)->addClientGroup(0,1,1);

    expect($res->client_user_id)->toEqual(1);
});

测试保护方法

 $container = \Hyperf\Context\ApplicationContext::getContainer();
    // 使用反射获取受保护的方法
    $method = new ReflectionMethod(ClientService::class, 'addClientGroup');

    // 调用受保护的方法
    $res = $method->invoke($container->get(ClientService::class), 1, 1, 2);

    expect($res->client_user_id)->toEqual(2);
    expect($res->group_id)->toEqual(1);
    expect($res->is_boss)->toEqual(0);

测试私有静态方法

test('addClientGroup2', function () {
    \App\System\Model\SystemUser::truncate();

    $container = \Hyperf\Context\ApplicationContext::getContainer();
    // 使用反射获取受保护的方法
    $method = new ReflectionMethod(ClientService::class, 'addClientGroup');
    $res = $method->invokeArgs($container->get(ClientService::class), [1, 1, 2]);
    expect($res->client_user_id)->toEqual(2);
    expect($res->group_id)->toEqual(1);
    expect($res->is_boss)->toEqual(0);
});