Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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
Symfony PHPUnit-注入依赖项_Symfony_Phpunit_Symfony4_Lexikjwtauthbundle - Fatal编程技术网

Symfony PHPUnit-注入依赖项

Symfony PHPUnit-注入依赖项,symfony,phpunit,symfony4,lexikjwtauthbundle,Symfony,Phpunit,Symfony4,Lexikjwtauthbundle,我想测试这个令牌提供者 <?php declare(strict_types=1); namespace App\Services\Provider; use App\Repository\UserRepository; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInte

我想测试这个令牌提供者

<?php

declare(strict_types=1);

namespace App\Services\Provider;

use App\Repository\UserRepository;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;

/**
 * Class TokenProvider
 * @package App\Services\Provider
 */
class TokenProvider
{
    /** @var JWTEncoderInterface */
    private $JWTEncoder;
    /** @var UserPasswordEncoderInterface */
    private $passwordEncoder;
    /** @var UserRepository */
    private $userRepository;

    /**
     * TokenProvider constructor.
     *
     * @param JWTEncoderInterface          $JWTEncoder
     * @param UserPasswordEncoderInterface $passwordEncoder
     * @param UserRepository               $userRepository
     */
    public function __construct(JWTEncoderInterface $JWTEncoder, UserPasswordEncoderInterface $passwordEncoder, UserRepository $userRepository)
    {
        $this->JWTEncoder = $JWTEncoder;
        $this->passwordEncoder = $passwordEncoder;
        $this->userRepository = $userRepository;
    }

    /**
     * @param string $email
     * @param string $password
     *
     * @return string
     * @throws \Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEncodeFailureException
     */
    public function getToken(string $email, string $password): string
    {
        $user = $this->userRepository->findOneBy([
            'email' => $email,
        ]);

        if (!$user) {
            throw new NotFoundHttpException('User Not Found');
        }

        $isValid = $this->passwordEncoder->isPasswordValid($user, $password);
        if (!$isValid) {
            throw new BadCredentialsException();
        }

        return $this->JWTEncoder->encode([
            'email' => $user->getEmail(),
            'exp' => time() + 3600 // 1 hour expiration
        ]);
    }
}
在测试用例中实现这一点的好方法是什么


我不想模拟这两个服务,因为我想用LexikJWTAuthenticationBundle检查我的令牌是否有效

我建议您扩展KernelTestCase和函数setUp(),并获得如下依赖关系:

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class TokenProviderTest extends KernelTestCase
{
private $jWTEncoder

protected function setUp()
{
   self::bootKernel();
   $this->jWTEncoder = self::$container->get('App\Services\TokenProvider');
}

public function testGetToken()
{
  //Your code
}
}

希望这对您有所帮助:

当我不断收到以下弃用警告时,我发现了这个答案:

  1x: Since symfony/twig-bundle 5.2: Accessing the "twig" service directly from the container is deprecated, use dependency injection instead.
    1x in ExtensionTest::testIconsExtension from App\Tests\Templates\Icons
我用以下方法解决了这个问题:

static::bootKernel();
$container = self::$kernel->getContainer()->get('test.service_container');
$twig      = $container->get('twig');
如中所述

static::bootKernel();
$container = self::$kernel->getContainer()->get('test.service_container');
$twig      = $container->get('twig');