-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: Support file uploads in feature tests (#7987) #10574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Test\Mock; | ||
|
|
||
| use CodeIgniter\HTTP\Files\FileCollection; | ||
| use CodeIgniter\HTTP\Files\UploadedFile; | ||
|
|
||
| class MockFileCollection extends FileCollection | ||
| { | ||
| /** | ||
| * @param array<string, array<array-key, UploadedFile>|UploadedFile> $files | ||
| */ | ||
| public function __construct(array $files) | ||
| { | ||
| $this->files = $files; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Test\Mock; | ||
|
|
||
| use CodeIgniter\HTTP\Files\UploadedFile; | ||
|
|
||
| /** | ||
| * An uploaded file for feature tests, using a regular local file as its source. | ||
| */ | ||
| class MockUploadedFile extends UploadedFile | ||
| { | ||
| public function __construct( | ||
| string $path, | ||
| string $originalName, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Give omitted mock MIME metadata a string value The new constructor permits Normalize an omitted MIME type to a string in the mock, while preserving explicitly supplied client MIME values, and cover construction without the third argument. The result should be usable through
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in e10a3a7. The mock normalizes omitted client MIME metadata to an empty string and preserves an explicit MIME value. The regression covers both cases and calls getClientMimeType(). |
||
| ?string $mimeType = null, | ||
| ?int $size = null, | ||
| ?int $error = UPLOAD_ERR_OK, | ||
| ?string $clientPath = null, | ||
| ) { | ||
| parent::__construct($path, $originalName, $mimeType ?? '', $size, $error, $clientPath); | ||
| } | ||
|
|
||
| public function isValid(): bool | ||
| { | ||
| return is_file($this->path) && $this->error === UPLOAD_ERR_OK; | ||
| } | ||
|
|
||
| protected function moveFile(string $destination): bool | ||
| { | ||
| return rename($this->path, $destination); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Preserve multipart behavior after request-body formatting
A feature test that first calls
withBodyFormat('json')->post(...), thenwithFiles(['document' => $file])->post(...), sends the second request withContent-Type: application/jsoneven thoughgetFile('document')is populated. I reproduced this in a route-level test: expectedmultipart/form-data:present, actualapplication/json:present.bodyFormatpersists, and the latersetRequestBody()call overwrites this header and JSON-encodes the form fields. Controllers or filters expecting multipart therefore reject an otherwise valid upload test.Make attached files select multipart consistently during body setup, and add a regression for JSON request -> upload request -> ordinary request. The upload's files and multipart override should apply only to that upload request, without leaving upload state on the following request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e10a3a7. Body formatting now skips requests with uploaded files, preserving multipart for that request while the following request still uses the persistent JSON format. The new route-level regression covers JSON → upload → ordinary requests.