Tests unitaires avec lexik_jwt_authentication.
Bonjour tous le monde,
En deux mots j’essaie de faire des tests unitaires sous symfony 4.1 (website-skeleton).
Je n'arrive pas à testé le login et récupérer le token d'autehtification.
J'ai suivi la doc du bundle qui est bien succincte.
Pour info, avec le logiciel postman
ou Curl
Code:
1 2
|
curl -X POST -H "Content-Type: application/json" http://localhost/api/login_check -d '{"_username":"username","_password":"password"}' |
je récupère bien mon token.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
<?php
namespace tests\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SecurityControllerTest extends WebTestCase
{
/**
* @var Symfony\Bundle\FrameworkBundle\Client
*/
private $client = null;
public function setUp()
{
parent::setUp();
$this->client = static::createClient();
}
/**
* Test the login.
*
* @see SecurityController::login()
*/
public function testLoginPass()
{
// Make the request
$this->client->request(
'POST',
'/api/login_check',
array(
'_username' => 'username',
'_password' => 'password',
)
);
// Check the content body
$data = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('token', $data);
// Check the statut code
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
// Check if the response is successful
$this->assertTrue($this->client->getResponse()->isSuccessful());
}
} |
Lorsque je lance mon test unitaire, j'ai un retour "Bad credentials".
Si une âme charitable à une idée, merci.