Welcome Guest, Not a member yet? Register   Sign In
Creating a PHPUnit Unit Test with File Upload via POST Route Using HTTP Testing
#1

Hello CodeIgniter community,

I hope this message finds you well. I'm currently working on a CodeIgniter 4 project and I'm looking to implement a PHPUnit unit test that involves making a POST request with file upload to a specific route in my application. I'm specifically interested in using CodeIgniter's HTTP Testing features to accomplish this.

Here's a breakdown of what I aim to achieve:

1. Simulate a POST request to a designated route.
2. Include the capability to upload a file as part of this POST request.
3. Perform assertions to verify that the route handles the file upload correctly.
4. I've been researching CodeIgniter's HTTP Testing documentation and PHPUnit, but I could use some guidance on structuring this particular test case. If anyone in the community has experience with creating such tests, I would greatly appreciate your insights and any code examples you can share.

Here are some specific questions I have:

1. How can I use CodeIgniter's HTTP Testing features to simulate a file upload in my PHPUnit test case?
2. What are the recommended assertions I should use to ensure that the file upload is handled correctly by the target route?
3. Are there any best practices or tips related to CodeIgniter 4 and HTTP Testing that I should keep in mind while setting up this test?

Having a clear understanding of how to create this unit test will be invaluable in enhancing the robustness of my application.

I'd like to express my gratitude in advance for any assistance and expertise you can provide.

Best regards
Reply
#2

I found this:
https://www.reddit.com/r/codeigniter/com...deigniter/

I'm not sure but you probably can't write test for file uploading unless you hack CI4.
Reply
#3

(09-27-2023, 02:23 AM)kenjis Wrote: I found this:
https://www.reddit.com/r/codeigniter/com...deigniter/

I'm not sure but you probably can't write test for file uploading unless you hack CI4.


Thanks for the fast reply Kenjis.
I tried your reference on Reddit and I am looking around again about $_FILES.
Actually, the $_FILES is readable and not NULL. But the isValid() function knows that the file is not the uploaded file from POST, because the is_uploaded_file() function from PHP can validate that. So I think when the testing is running, I don't use the isValid() function to validate.
Then the move() function from the getFile() function validates the uploaded file too. So the function must be switched to copy() from PHP.
So after some modification and not by hacking the CodeIgniter 4, it actually works!

Here the how I skip the isValid() function for unit testing:
PHP Code:
if (ENVIRONMENT === 'testing') {
  $result $this->uploadedFileProcess($fileData$filePath$customFileName$sendToS3$acl);
} else {
  if (!$fileData->isValid() || $fileData->hasMoved()) {
    $result = [
      'error' => $fileData->getError(),
      'message' => $fileData->getErrorString()
    ];
  } else {
    $result $this->uploadedFileProcess($fileData$filePath$customFileName$sendToS3$acl);
  }


Here the how I skip the move() function for unit testing:
PHP Code:
if (ENVIRONMENT === 'testing') {
  copy($fileData->getTempName(), $filePath.$fileName);
} else {
  $fileData->move($filePath$fileName);


Thank you for your assistance Kenjis. If you don't mention that reference again, maybe I can't make it. Really appreciate.

Thank you very much.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB