Я пишу коды для выполнения модульного тестирования в Symfony 4.2, на самом деле я использую пакет аутентификации JWT для генерации токена.Я хочу проверить, успешно ли генерируется токен.Пожалуйста, посмотрите на это и предложите мне, если есть какие-то улучшения.
Коды в логине COntroller
<?php
namespace App\Controller;
use App\Entity\Organization;
use Doctrine\Common\Annotations\AnnotationReader;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class LoginController extends AbstractController
{
protected $serializer;
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
$encoders = array(new XmlEncoder(), new JsonEncoder());
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$normalizer->setCircularReferenceLimit(2);
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$normalizers = array($normalizer);
$this->serializer = new Serializer($normalizers, $encoders);
}
/**
* Check the credentials are valid
* Check whether the user is verified
* Generate JWT token for authentic users and return
*
* @Route("/api/login_check", name="login_check")
*/
public function login(Request $request, JWTTokenManagerInterface $JWTTokenManager, UserProviderInterface $userProvider, UserPasswordEncoderInterface $encoder)
{
try {
$data = json_decode($request->getContent());
$user = $userProvider->loadUserByUsername($data->email);
$passwordValid = $encoder->isPasswordValid($user, $data->password);
if ($passwordValid) {
if ($user->getVerified()) {
$organization = $user->getOrganization();
$organization = $this->getDoctrine()->getRepository(Organization::class)->findOneBy(array('id' => $organization->getId()));
$token = $JWTTokenManager->create($user);
return new JsonResponse(['token' => $token, 'user' => $this->serializer->normalize($user, 'json')]);
} else {
$array = array('message' => 'User not verified');
$response = new Response(json_encode($array), 401);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
} else {
$array = array('message' => 'Bad credentials');
$response = new Response(json_encode($array), 401);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
} catch (\Exception $e) {
return new Response($e->getMessage());
}
}
}
Коды в папке Test / controller
namespace App\Tests\Controller
use App\Controller\Log;
use App\Controller\LoginController;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
class LoginTest extends TestCase{
protected $logger;
protected $controller;
/**
*
*/
public function setUp()
{
$this->logger = $this->createMock(LoggerInterface::class);
$this->controller = new LoginController(
$this->logger
);
parent::setUp();
}
public function testlogin()
{
$result = $this->controller->login('Test');
$this->assertEquals( 'TEST1548303737',$result);
}
}