|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\KI\CoreBundle; |
|
4
|
|
|
|
|
5
|
|
|
use Liip\FunctionalTestBundle\Test\WebTestCase as LiipWebTestCase; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
|
|
8
|
|
|
abstract class WebTestCase extends LiipWebTestCase |
|
9
|
|
|
{ |
|
10
|
|
|
protected $container; |
|
11
|
|
|
protected $client; |
|
12
|
|
|
protected $authorizationHeaderPrefix = 'Bearer'; |
|
13
|
|
|
protected $queryParameterName = 'bearer'; |
|
14
|
|
|
|
|
15
|
|
|
// Crée un client autentifié |
|
16
|
|
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::__construct(); |
|
19
|
|
|
|
|
20
|
|
|
// On ne se logge qu'une fois pour tous les tests |
|
21
|
|
|
$path = __DIR__.'/../../../var/cache/token'; |
|
22
|
|
|
if (!file_exists($path)) { |
|
23
|
|
|
$client = static::createClient(); |
|
24
|
|
|
$client->request( |
|
25
|
|
|
'POST', |
|
26
|
|
|
$this->getUrl('login'), |
|
27
|
|
|
['username' => 'trancara', 'password' => 'password'] |
|
28
|
|
|
); |
|
29
|
|
|
$response = $client->getResponse(); |
|
30
|
|
|
$data = json_decode($response->getContent(), true); |
|
31
|
|
|
$this->assertArrayHasKey('token', $data); |
|
32
|
|
|
file_put_contents($path, $data['token']); |
|
33
|
|
|
|
|
34
|
|
|
// On charge aussi les fixtures |
|
35
|
|
|
self::runCommand('doctrine:fixtures:load --purge-with-truncate --append'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$client = static::createClient(); |
|
39
|
|
|
$client->enableProfiler(); |
|
40
|
|
|
$client->setServerParameter('HTTP_Authorization', $this->authorizationHeaderPrefix.' '.file_get_contents($path)); |
|
41
|
|
|
$this->client = $client; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function assertJsonResponse(Response $response, $statusCode = 200, $checkValidJson = false, $contentType = 'application/json') |
|
45
|
|
|
{ |
|
46
|
|
|
$this->assertEquals( |
|
47
|
|
|
$statusCode, $response->getStatusCode(), |
|
48
|
|
|
$response->getContent() |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
// On se fout de ce qui est retourné si rien n'est retourné |
|
52
|
|
|
if ($statusCode != 204 && $checkValidJson) { |
|
53
|
|
|
$this->assertTrue( |
|
54
|
|
|
$response->headers->contains('Content-Type', $contentType), |
|
55
|
|
|
$response->headers |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($checkValidJson) { |
|
60
|
|
|
$decode = json_decode($response->getContent(), true); |
|
61
|
|
|
$this->assertTrue( |
|
62
|
|
|
($decode !== null && $decode !== false), |
|
63
|
|
|
'is response valid json: ['.$response->getContent().']' |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function connect($username, $password) |
|
69
|
|
|
{ |
|
70
|
|
|
$client = static::createClient(); |
|
71
|
|
|
$client->request('POST', $this->getUrl('login'), ['username' => $username, 'password' => $password]); |
|
72
|
|
|
$data = json_decode($client->getResponse()->getContent(), true); |
|
73
|
|
|
$client->setServerParameter('HTTP_Authorization', sprintf('%s %s', $this->authorizationHeaderPrefix, $data['token'])); |
|
74
|
|
|
$this->client = $client; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Checke tout un tableau de routes pour aller plus vite |
|
78
|
|
|
public function checkRoutes($routes) |
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($routes as $route) { |
|
81
|
|
|
echo "\n".$route[1].' '.$route[2]; |
|
82
|
|
|
if (isset($route[3])) |
|
83
|
|
|
$this->client->request($route[1], $route[2], $route[3]); |
|
84
|
|
|
else |
|
85
|
|
|
$this->client->request($route[1], $route[2]); |
|
86
|
|
|
$this->assertJsonResponse($this->client->getResponse(), $route[0]); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|