-
Notifications
You must be signed in to change notification settings - Fork 40
/
ApiControllerTest.php
102 lines (88 loc) · 2.94 KB
/
ApiControllerTest.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace App\Tests\Controller;
use App\Tests\Fixture\LoadAuthenticationData;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
use App\Tests\Traits\JsonControllerTest;
class ApiControllerTest extends WebTestCase
{
use JsonControllerTest;
protected string $apiVersion = 'v3';
protected KernelBrowser $kernelBrowser;
public function setUp(): void
{
parent::setUp();
$this->kernelBrowser = self::createClient();
$databaseTool = $this->kernelBrowser->getContainer()->get(DatabaseToolCollection::class)->get();
$databaseTool->loadFixtures([
LoadAuthenticationData::class
]);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->kernelBrowser);
}
public function testNoEndpoint()
{
$this->makeJsonRequest(
$this->kernelBrowser,
'GET',
"/api/{$this->apiVersion}/nothing",
null,
$this->getTokenForUser($this->kernelBrowser, 1)
);
$response = $this->kernelBrowser->getResponse();
$this->assertJsonResponse($response, Response::HTTP_NOT_FOUND);
}
public function testNoVersion()
{
$this->makeJsonRequest(
$this->kernelBrowser,
'GET',
'/api/nothing',
null,
$this->getTokenForUser($this->kernelBrowser, 1)
);
$response = $this->kernelBrowser->getResponse();
$this->assertJsonResponse($response, Response::HTTP_NOT_FOUND);
}
public function testBadVersion()
{
$this->makeJsonRequest(
$this->kernelBrowser,
'GET',
'/api/1/courses',
null,
$this->getTokenForUser($this->kernelBrowser, 1)
);
$response = $this->kernelBrowser->getResponse();
$this->assertJsonResponse($response, Response::HTTP_NOT_FOUND);
}
public function testApiInfoAuthenticated()
{
$this->kernelBrowser->request(
'GET',
'/api',
[],
[],
['HTTP_X-JWT-Authorization' => 'Token ' . $this->getTokenForUser($this->kernelBrowser, 1)],
);
$response = $this->kernelBrowser->getResponse();
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$this->assertStringContainsString('<h1>API Info</h1>', $response->getContent());
}
public function testApiInfoNotAuthenticated()
{
$this->kernelBrowser->request(
'GET',
'/api',
);
$response = $this->kernelBrowser->getResponse();
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$this->assertStringContainsString('<h1>API Info</h1>', $response->getContent());
}
}