symfony - Symfony PHPUnit - 注入依赖
问题描述
我想测试这个 TokenProvider
<?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
]);
}
}
这是我的测试。它还没有完成。
我想在我JWTEncoderInterface $encoder
的.UserPasswordEncoder $passwordEncoder
testGetToken()
class TokenProviderTest extends TestCase
{
/**
* @throws \Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEncodeFailureException
*/
public function testGetToken()
{
$this->markTestSkipped();
$JWTEncoder = //TODO;
$passwordEncoder = //TODO;
$tokenProvider = new TokenProvider(
$JWTEncoder,
$passwordEncoder,
new class extends UserRepository{
public function findOneBy(array $criteria, array $orderBy = null)
{
return (new User())
->setEmail('kevin@leroi.com')
->setPassword('password')
;
}
}
);
$token = $tokenProvider->getToken('kevin@leroi.com', 'password');
$this->assertEquals(true, $token);
}
}
在 TestCase 中这样做的好方法是什么?
我不想模拟这两个服务,因为我想检查我的令牌是否对 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
}
}
希望对您有所帮助:https ://symfony.com/doc/current/testing/doctrine.html#functional-testing
推荐阅读
- html - 如何只使背景轮廓不透明?
- angular - 在 mat 表中: new MatTableDataSource VS ChangeDetectorRef ,哪一个更好用于刷新源数据?
- google-apps-script - 如何在谷歌应用脚本中获取测试部署 ID?
- reactjs - 反应:使用公共目录之外的路径播放视频
- python - 为什么这段代码给出了意外的输出?
- android - 如何用moshi将房间关系变成嵌套的json?
- python - 特征提取 Inception Resnet V2 上非常慢的 K-means 轮廓分数
- java - 我无法运行我可以在 servlet 中使用 jsp 中相同的逻辑运行的东西
- javascript - 正则表达式:带有特殊字符的字符集
- javascript - 类型错误:styled_components__WEBPACK_IMPORTED_MODULE_1__.default.View 不是函数