Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $this->;Symfony控制器功能测试中的getUser()返回null_Php_Symfony - Fatal编程技术网

Php $this->;Symfony控制器功能测试中的getUser()返回null

Php $this->;Symfony控制器功能测试中的getUser()返回null,php,symfony,Php,Symfony,我正在尝试测试一个使用$this->getUser()->getUsername()的控制器。它抱怨对null调用了getUsername() 这是来自测试类的客户端登录代码 protected function logInAsAdmin(Client $client): void { $session = $client->getContainer()->get('session'); $firewallName = 'main';

我正在尝试测试一个使用$this->getUser()->getUsername()的控制器。它抱怨对null调用了getUsername()

这是来自测试类的客户端登录代码

protected function logInAsAdmin(Client $client): void
    {
        $session = $client->getContainer()->get('session');

        $firewallName = 'main';
        $firewallContext = 'main';

        $roles = [
            'ROLE_USER',
            'ROLE_ADMIN',
        ];

        $token = new UsernamePasswordToken('admin', null, $firewallName, $roles);
        $session->set('_security_' . $firewallContext, serialize($token));
        $session->save();

        $cookie = new Cookie($session->getName(), $session->getId());
        $client->getCookieJar()->set($cookie);
    }
以下是控制器的功能:

public function home(EmployerRepository $employerRepository, AuthorizationCheckerInterface $authorizationChecker): Response
    {
        if ($authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
            $jobs = [];

            foreach ($employerRepository->findBy(['owner' => $this->getUser()->getUsername()]) as $employer) {
                $jobs = array_merge($jobs, $employer->getJobs()->toArray());
            }

            return $this->render('home.html.twig', ['jobs' => $jobs]);
        }

        return $this->redirectToRoute('login');
    }
有人能告诉我为什么这不起作用吗?我尝试实例化一个用户对象并将其传递到UsernamePasswordToken,但也没有成功

使用Symfony 4

测试:

/**
     * @test
     */
    public function indexPageIsRenderedWhenLoggedIn(): void
    {
        $client = static::createClient();
        $this->logInAsAdmin($client);
        $client->request('GET', '/');
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertRegExp('/Your jobs/', $client->getResponse()->getContent());
    }

尝试从logInAsAdmin函数返回$client

protected function logInAsAdmin(Client $client): Client
{
    $session = $client->getContainer()->get('session');

    $firewallName = 'main';
    $firewallContext = 'main';

    $roles = [
        'ROLE_USER',
        'ROLE_ADMIN',
    ];

    $token = new UsernamePasswordToken('admin', null, $firewallName, $roles);
    $session->set('_security_' . $firewallContext, serialize($token));
    $session->save();

    $cookie = new Cookie($session->getName(), $session->getId());
    $client->getCookieJar()->set($cookie);
    return $client;
}
在测试中使用:

 /**
 * @test
 */
public function indexPageIsRenderedWhenLoggedIn(): void
{
    $client = static::createClient();
    $client = $this->logInAsAdmin($client);
    $client->request('GET', '/');
    $this->assertEquals(200, $client->getResponse()->getStatusCode());
    $this->assertRegExp('/Your jobs/', $client->getResponse()->getContent());
}

想让大家知道我通过将
TokenStorageInterface
注入控制器,并通过控制器方法的第6行
$tokenStorage->getToken()->getUsername()

获取用户名,解决了我的问题。否,它是在哪里定义的?在中扩展AbstractController时,您可以自动访问该方法Symfony@Woeler
main
防火墙提供程序存在吗?我在See中使用了类似的测试,这很好,但您应该将其转移到某种服务中。请记住,可能没有令牌,或者如果用户未经过身份验证(当使用多个防火墙时),则令牌可能包含字符串。据我所知,在必须以实际用户的形式进行身份验证的部分代码中,会专门调用该方法。