Skip to content

Commit a52bc65

Browse files
committed
Add missing functional tests
1 parent dd75900 commit a52bc65

File tree

4 files changed

+194
-2
lines changed

4 files changed

+194
-2
lines changed

src/Filesystem/Symfony/HttpKernel/PendingFileValueResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
4646
}
4747

4848
/** @var UploadedFile|null $attribute */
49-
$attribute = $attributes[0];
49+
$attribute = $attributes[0] ?? null;
5050

5151
$path = $attribute?->path
5252
?? $argument->getName();
@@ -93,7 +93,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
9393
\assert(!empty($attributes));
9494

9595
/** @var UploadedFile|null $attribute */
96-
$attribute = $attributes[0];
96+
$attribute = $attributes[0] ?? null;
9797

9898
$path = $attribute?->path
9999
?? $argument->getName();
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the zenstruck/filesystem package.
5+
*
6+
* (c) Kevin Bond <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Zenstruck\Tests\Filesystem\Symfony\HttpKernel;
13+
14+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
15+
use Symfony\Component\HttpFoundation\File\UploadedFile;
16+
17+
/**
18+
* @author Jakub Caban <[email protected]>
19+
*/
20+
class PendingDocumentValueResolverTest extends WebTestCase
21+
{
22+
/**
23+
* @test
24+
*/
25+
public function do_nothing_on_wrong_type(): void
26+
{
27+
$client = self::createClient();
28+
29+
$client->request(
30+
'GET',
31+
'no-injection',
32+
files: ['file' => self::uploadedFile()]
33+
);
34+
35+
self::assertSame('0', $client->getResponse()->getContent());
36+
}
37+
38+
/**
39+
* @test
40+
*/
41+
public function inject_on_typed_argument(): void
42+
{
43+
$client = self::createClient();
44+
45+
$client->request(
46+
'GET',
47+
'single-file',
48+
);
49+
50+
self::assertSame('', $client->getResponse()->getContent());
51+
52+
$client->request(
53+
'GET',
54+
'single-file',
55+
files: ['file' => self::uploadedFile()]
56+
);
57+
58+
self::assertSame("some content\n", $client->getResponse()->getContent());
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function inject_on_typed_argument_with_path(): void
65+
{
66+
$client = self::createClient();
67+
68+
$client->request(
69+
'GET',
70+
'single-file-with-path',
71+
files: ['data' => ['file' => self::uploadedFile()]]
72+
);
73+
74+
self::assertSame("some content\n", $client->getResponse()->getContent());
75+
}
76+
77+
/**
78+
* @test
79+
*/
80+
public function inject_array_on_argument_with_attribute(): void
81+
{
82+
$client = self::createClient();
83+
84+
$client->request(
85+
'GET',
86+
'multiple-files'
87+
);
88+
89+
self::assertSame('0', $client->getResponse()->getContent());
90+
91+
$client->request(
92+
'GET',
93+
'multiple-files',
94+
files: ['files' => self::uploadedFile()]
95+
);
96+
97+
self::assertSame('1', $client->getResponse()->getContent());
98+
}
99+
100+
/**
101+
* @test
102+
*/
103+
public function inject_array_on_argument_with_attribute_and_path(): void
104+
{
105+
$client = self::createClient();
106+
107+
$client->request(
108+
'GET',
109+
'multiple-files-with-path'
110+
);
111+
112+
self::assertSame('0', $client->getResponse()->getContent());
113+
114+
$client->request(
115+
'GET',
116+
'multiple-files-with-path',
117+
files: ['data' => ['files' => self::uploadedFile()]]
118+
);
119+
120+
self::assertSame('1', $client->getResponse()->getContent());
121+
}
122+
123+
private static function uploadedFile(): UploadedFile
124+
{
125+
return new UploadedFile(
126+
__DIR__.'/../../../Fixtures/files/textfile.txt',
127+
'test.txt',
128+
test: true
129+
);
130+
}
131+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the zenstruck/filesystem package.
5+
*
6+
* (c) Kevin Bond <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Zenstruck\Tests\Fixtures\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Routing\Annotation\Route;
16+
use Zenstruck\Filesystem\Attribute\UploadedFile;
17+
use Zenstruck\Filesystem\Node\File;
18+
use Zenstruck\Filesystem\Node\File\PendingFile;
19+
20+
/**
21+
* @author Jakub Caban <[email protected]>
22+
*/
23+
class ArgumentResolverController
24+
{
25+
#[Route('/multiple-files', name: 'multiple-files')]
26+
public function multipleFiles(
27+
#[UploadedFile]
28+
array $files
29+
): Response {
30+
return new Response((string) \count($files));
31+
}
32+
33+
#[Route('/multiple-files-with-path', name: 'multiple-files-with-path')]
34+
public function multipleFilesWithPath(
35+
#[UploadedFile('data[files]')]
36+
array $files
37+
): Response {
38+
return new Response((string) \count($files));
39+
}
40+
41+
#[Route('/no-injection', name: 'no-injection')]
42+
public function noInjection(array $file = []): Response
43+
{
44+
return new Response((string) \count($file));
45+
}
46+
47+
#[Route('/single-file', name: 'single-file')]
48+
public function singleFile(?PendingFile $file): Response
49+
{
50+
return new Response($file?->contents() ?? '');
51+
}
52+
53+
#[Route('/single-file-with-path', name: 'single-file-with-path')]
54+
public function singleFileWithPath(
55+
#[UploadedFile('data[file]')]
56+
?PendingFile $file
57+
): Response {
58+
return new Response($file?->contents() ?? '');
59+
}
60+
}

tests/Fixtures/TestKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,6 @@ protected function configureRoutes(RoutingConfigurator $routes): void
184184
$routes->add('private_public', '/private/{path}')
185185
->requirements(['path' => '.+'])
186186
;
187+
$routes->import(__DIR__.'/Controller', 'annotation');
187188
}
188189
}

0 commit comments

Comments
 (0)