-
Notifications
You must be signed in to change notification settings - Fork 40
/
UploadControllerTest.php
107 lines (90 loc) · 2.87 KB
/
UploadControllerTest.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
103
104
105
106
107
<?php
declare(strict_types=1);
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use App\Tests\Traits\JsonControllerTest;
/**
* Upload controller Test.
*/
class UploadControllerTest extends WebTestCase
{
use JsonControllerTest;
protected $fakeTestFileDir;
protected $fakeTestFile;
protected $fs;
public function setUp(): void
{
parent::setUp();
$this->fs = new Filesystem();
$this->fakeTestFileDir = __DIR__ . '/FakeTestFiles';
if (!$this->fs->exists($this->fakeTestFileDir)) {
$this->fs->mkdir($this->fakeTestFileDir);
}
$this->fs->copy(__FILE__, $this->fakeTestFileDir . '/TESTFILE.txt');
$this->fakeTestFile = new UploadedFile(
$this->fakeTestFileDir . '/TESTFILE.txt',
'TESTFILE.txt',
'text/plain'
);
}
public function tearDown(): void
{
parent::tearDown();
$this->fs->remove($this->fakeTestFileDir);
unset($this->fs);
unset($this->fakeTestFile);
}
public function testUploadFile()
{
$client = static::createClient();
$this->makeJsonRequest(
$client,
'POST',
'/upload',
null,
$this->getAuthenticatedUserToken($client),
['file' => $this->fakeTestFile]
);
$response = $client->getResponse();
$this->assertJsonResponse($response, Response::HTTP_OK);
$data = json_decode($response->getContent(), true);
$this->assertSame($data['filename'], 'TESTFILE.txt');
$this->assertSame($data['fileHash'], md5_file(__FILE__));
}
public function testAnonymousUploadFileDenied()
{
$client = static::createClient();
$this->makeJsonRequest(
$client,
'POST',
'/upload',
null,
[],
['file' => $this->fakeTestFile]
);
$response = $client->getResponse();
$this->assertJsonResponse($response, Response::HTTP_UNAUTHORIZED);
}
public function testBadUpload()
{
$client = static::createClient();
$this->makeJsonRequest(
$client,
'POST',
'/upload',
null,
$this->getAuthenticatedUserToken($client),
['nofile' => $this->fakeTestFile]
);
$response = $client->getResponse();
$this->assertJsonResponse($response, Response::HTTP_BAD_REQUEST);
$data = json_decode($response->getContent(), true);
$this->assertSame(
$data['errors'],
'Unable to find file in the request. The uploaded file may have exceeded the maximum allowed size'
);
}
}