Caius
Caius

Reputation: 2264

Issue while testing multiple files upload. Laravel 5.3

I'm having a strange issue here. I'm trying to "fake" a files upload with "Illuminate\Http\UploadedFile". Since I've noticed that each instance of the class handles a single file, I've managed to have a loop that returns an array of instances. In this way I was thinking that I could just take that array and smack it into the body of the request.

The testing call looks like this:

$this->json("patch", "api/assets/1", ["damages" => $this->prepareDamagesPhotos());

While the implementation of prepareDamagesPhotos():

protected function prepareDamagePhotos($how_many = 3, $image_name_no_extension_or_id = "dmg")
    {
        if ($how_many > 5) {
            throw new InvalidArgumentException("Cannot load more than 5 damage images");
        }

        if ($how_many == 0) {
            throw new InvalidArgumentException("Cannot load 0 images");
        }

        $images = [];

        for ($i = 1; $i < $how_many + 1; $i++) {

            $path = public_path("uploads/testing/damages/images/" . $image_name_no_extension_or_id . "#0" . $i . ".png");

            $image = new \Illuminate\Http\UploadedFile(
                $path,
                $image_name_no_extension_or_id . "#0" . $i . ".png",
                "image/png",
                filesize(public_path("uploads/testing/damages/images/". $image_name_no_extension_or_id . "#0" . $i .
                                     ".png")),
                NULL,
                TRUE
            );

            array_push($images, $image);
        }

        return $images;
    }

The whole thing works, but when it comes to die 'n dump the $input->all(), this is the output:

array:1 [
  "damages" => array:3 [
    0 => []
    1 => []
    2 => []
  ]
]

This is not good...

I'm wondering what's wrong with this approach since passing a single instance of UploadedFile works like a charm. Why the array gets flushed by its values but keeps its structure? O.o

I've also tried to find a way to handle this in another way, but all that I've found was concerned on a single file upload. Which I repeat, already works like a charm.

I'm really confused about... Any help?

Upvotes: 0

Views: 257

Answers (1)

Caius
Caius

Reputation: 2264

I've found another way to handle this, maybe not the proper solution but I'm open to better implementations suggestions...

Changing the method from json to call and passing the files into the fourth parameter seems to work:

$result = $this->call("patch", "api/assets/1", [], [], $this->prepareDamagePhotos());

The files does not get lost anymore in the cyberspace!

Upvotes: 1

Related Questions