|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
|
7
|
|
|
|
|
8
|
|
|
class UploadControllerTest extends WebTestCase |
|
9
|
|
|
{ |
|
10
|
|
|
private string $uploadsDir; |
|
11
|
|
|
private KernelBrowser $client; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* setUp |
|
15
|
|
|
* |
|
16
|
|
|
* @return void |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function setUp(): void |
|
19
|
|
|
{ |
|
20
|
|
|
parent::setUp(); |
|
21
|
|
|
|
|
22
|
|
|
// Use static::createClient() to initialize the client and boot the kernel |
|
23
|
|
|
$this->client = static::createClient(); |
|
24
|
|
|
|
|
25
|
|
|
// Access the container via the client |
|
26
|
|
|
$kernel = $this->client->getKernel(); |
|
27
|
|
|
|
|
28
|
|
|
// Get the project directory parameter |
|
29
|
|
|
$projectDir = $kernel->getContainer()->getParameter('kernel.project_dir'); |
|
30
|
|
|
|
|
31
|
|
|
if (!is_string($projectDir)) { |
|
32
|
|
|
throw new \RuntimeException('Could not get kernel.project_dir parameter.'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->uploadsDir = $projectDir . '/var/uploads'; |
|
36
|
|
|
|
|
37
|
|
|
if (!is_dir($this->uploadsDir)) { |
|
38
|
|
|
mkdir($this->uploadsDir, 0777, true); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Create a dummy file for testing |
|
42
|
|
|
file_put_contents($this->uploadsDir . '/testfile.txt', 'Test content'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* tearDown |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function tearDown(): void |
|
51
|
|
|
{ |
|
52
|
|
|
// Remove test file |
|
53
|
|
|
$testFile = $this->uploadsDir . '/testfile.txt'; |
|
54
|
|
|
if (file_exists($testFile)) { |
|
55
|
|
|
unlink($testFile); |
|
56
|
|
|
} |
|
57
|
|
|
parent::tearDown(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* testServeUploadFileExists |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testServeUploadFileExists(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$this->client->request('GET', '/uploads/testfile.txt'); |
|
68
|
|
|
|
|
69
|
|
|
$response = $this->client->getResponse(); |
|
70
|
|
|
|
|
71
|
|
|
// Assert status code first |
|
72
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
73
|
|
|
|
|
74
|
|
|
// Get Content-Type header directly |
|
75
|
|
|
$contentType = $response->headers->get('Content-Type'); |
|
76
|
|
|
|
|
77
|
|
|
// Assert that Content-Type is present and contains 'text/plain' |
|
78
|
|
|
$this->assertIsString($contentType); |
|
79
|
|
|
$this->assertStringContainsString('text/plain', $contentType); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* testServeUploadFileNotFound |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testServeUploadFileNotFound(): void |
|
88
|
|
|
{ |
|
89
|
|
|
|
|
90
|
|
|
$this->client->request('GET', '/uploads/nonexistent.txt'); |
|
91
|
|
|
|
|
92
|
|
|
$response = $this->client->getResponse(); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
|
95
|
|
|
|
|
96
|
|
|
$content = $response->getContent(); |
|
97
|
|
|
// Assert content is a string before asserting substring |
|
98
|
|
|
$this->assertIsString($content); |
|
99
|
|
|
$this->assertStringContainsString('Not Found', $content); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|