如何使用phpUnit在Symfony 4中进行身份验证功能测试,以访问url?
我试图遵循以下文档:如何使用phpUnit在Symfony 4中进行身份验证功能测试,以访问url?,php,authentication,phpunit,symfony4,Php,Authentication,Phpunit,Symfony4,我试图遵循以下文档: 我想测试一个视图,它只有经过身份验证才能访问。 我的问题是我不能让它工作。它不断返回301重定向到登录页面。我将向您展示前面文档中的代码: class DefaultControllerTest extends WebTestCase { private $client = null; public function setUp() { $this->client = static::createClient(); } public functi
class DefaultControllerTest extends WebTestCase {
private $client = null;
public function setUp()
{
$this->client = static::createClient();
}
public function testSecuredHello()
{
$this->logIn();
$crawler = $this->client->request('GET', 'panel/');
self::assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
self::assertSame('Mi Panel', $crawler->filter('h1')->text());
}
private function logIn()
{
$session = self::$container->get('session');
// somehow fetch the user (e.g. using the user repository) *MOCKUP
$user = new User();
$user->setId(1);
$user->setEmail('sefhi@gmail.com');
$user->setPassword('qwerty');
$user->setRoles($user->getRoles());
$user->setName('Sefhi');
$user->setActive(true);
$user->setFlgClientBlocked(true);
$user->setDocumentOnline('asdsadasd');
$user->setIdAtenea(123213);
$user->setPhone('600000000');
$user->setIdTypeDocument(1);
$firewallName = 'main';
// if you don't define multiple connected firewalls, the context defaults to the firewall name
// See https://symfony.com/doc/current/reference/configuration/security.html#firewall-context
$firewallContext = 'main';
// you may need to use a different token class depending on your application.
// for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
$session->set('_security_' . $firewallContext, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}}
我向您展示了security.yml中的配置
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
app_user_provider:
id: App\Security\UserProvider
encoders:
App\Security\User:
algorithm: argon2i
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
json_login:
check_path: login_ajax
remember_me: true
form_login:
provider: app_user_provider
csrf_token_generator: security.csrf.token_manager
login_path: /login
logout:
path: app_logout
target: main
remember_me:
secret: '%kernel.secret%'
lifetime: 604800 # 1 week in seconds
path: /
user_providers: [app_user_provider]
guard:
authenticators:
- App\Security\LoginFormAuthenticator
- App\Security\TokenAuthenticator
entry_point: App\Security\LoginFormAuthenticator
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
#- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
#- { path: ^/main, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/panel, roles: ROLE_USER }
- { path: ^/propuesta, roles: ROLE_USER }
- { path: ^/cliente, roles: ROLE_USER }
- { path: ^/cita, roles: ROLE_USER }
爬虫程序总是向我返回301,因此身份验证不起作用。我不知道该如何解决这个问题。我不认为这是路由问题,因为如果是路由问题,它将返回404