如何在Laravel中测试基于Base64的图像上传

huangapple go评论87阅读模式
英文:

How to test a base64 image upload in Laravel

问题

I have an API that accepts a 64 base code image, it receives the image from another service:

{
    "token_image": "BASE64 IMAGEN"
}

In my test, I am testing it like this:

$response = ....
....
$attributes = ["token_image" => "/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBBAAgICAgI................."];

json("POST", "/route", $attributes)->assertOk()->assertJson($response);

In my test, I am using the saloon library, which allows me to mock and save the response for the first time. In subsequent requests, it doesn't hit the route; it just uses the recorded response. However, I can't add the attribute like this:

["token_image" => "/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBBAAgICAgI................."]

Because the base64 image is really huge and has too many string characters. What can I do? Thanks.

英文:

I have an API that accepts a 64 base code image, it receives the image from other service

> {
> "token_image": "BASE64 IMAGEN", }

and in the test, I am testing it like this:

$response = ....
....
$attributes = ["token_image"=>"/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBBAAgICAgI................."];
    
    json("POST", "/route", 
        $attributes
    )->assertOk()
        ->assertJson($response);

In my test I am using the saloon library, that allows to mock and save the response for the first time, and in subsequences requests it doesn't hit the route, it just uses the recorded response, but the point is that I can't just add the attribute like this:

> ["token_image"=>"/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBBAAgICAgI................."]

Because the base64 image is really huge, it has too much string characters, what can I do? Thanks.

答案1

得分: 1

在你的tests文件夹里创建一个名为fixtures的文件夹。

在该fixtures文件夹里创建一个名为my-base64-image.txt的文件。

按照字面意思,将你的base64图像文本移动到上一步创建的文件中。

最后,使用这个fixture来替代硬编码,像这样:

$attributes = [
    "token_image" => file_get_contents(__DIR__ . '/../fixtures/my-base64-image.txt'),
];

注意:将../部分更改为正确的路径,因为上述示例是基于我的文件夹结构(我不知道你的结构)。


上面只是一个例子,我是说,在每个测试中重复路径到fixtures文件夹不是我会做的事情,相反,在所有其他测试类扩展的TestCase类中,或者在某些trait中,尝试像这样:

protected function loadFixture($name)
{
    return file_get_contents(__DIR__ . '/../fixtures/' . $name);
}
英文:

What you need is called "fixture", basically:

  • Inside your tests folder, create a folder named fixtures.
  • Create my-base64-image.txt inside said fixtures folder.
  • As the name suggests, move your base64-image-text inside the file created in previous step.

Finally, use said fixture instead of hard-coding, like:

$attributes = [
    "token_image" => file_get_contents(__DIR__ . '/../fixtures/my-base64-image.txt'),
];

>Note to change the "../" part to the right path, because above is based on my own folder structure (as I don't know yours).


Above is just an example, I mean, repeating path to fixtures folder in each test is something I would never do, and instead, in the TestCase class which all other test-classes extend, or in some trait, try something like:

protected function loadFixture($name)
{
    return file_get_contents(__DIR__ . '/../fixtures/' . $name);
}

huangapple
  • 本文由 发表于 2023年3月23日 08:46:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定