英文:
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 namedfixtures
. - Create
my-base64-image.txt
inside saidfixtures
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论