chengkun
2025-09-15 3c9050e82e582414dc7b208c8283fe47be37eeba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
 
namespace AlibabaCloud\Tea\FileForm\Tests;
 
use AlibabaCloud\Tea\FileForm\FileForm;
use AlibabaCloud\Tea\FileForm\FileForm\FileField;
use AlibabaCloud\Tea\FileForm\FileFormStream;
use GuzzleHttp\Psr7\Stream;
use PHPUnit\Framework\TestCase;
 
/**
 * @internal
 * @coversNothing
 */
class FileFormTest extends TestCase
{
    public function testFileFromStream()
    {
        $boundary = FileForm::getBoundary();
        $stream   = FileForm::toFileForm([], $boundary);
        $this->assertTrue($stream instanceof FileFormStream);
        $stream->write($boundary);
        $this->assertTrue(\strlen($boundary) === $stream->getSize());
    }
 
    public function testSet()
    {
        $fileField = new FileField([
            'filename'    => 'fake filename',
            'contentType' => 'content type',
            'content'     => null,
        ]);
 
        $this->assertEquals('fake filename', $fileField->filename);
        $this->assertEquals('content type', $fileField->contentType);
    }
 
    public function testRead()
    {
        $fileField              = new FileField();
        $fileField->filename    = 'haveContent';
        $fileField->contentType = 'contentType';
        $fileField->content     = new Stream(fopen('data://text/plain;base64,' . base64_encode('This is file test. This sentence must be long'), 'r'));
 
        $fileFieldNoContent              = new FileField();
        $fileFieldNoContent->filename    = 'noContent';
        $fileFieldNoContent->contentType = 'contentType';
        $fileFieldNoContent->content     = null;
 
        $map = [
            'key'      => 'value',
            'testKey'  => 'testValue',
            'haveFile' => $fileField,
            'noFile'   => $fileFieldNoContent,
        ];
 
        $stream = FileForm::toFileForm($map, 'testBoundary');
 
        $result = $stream->getContents();
        $target = "--testBoundary\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\nvalue\r\n--testBoundary\r\nContent-Disposition: form-data; name=\"testKey\"\r\n\r\ntestValue\r\n--testBoundary\r\nContent-Disposition: form-data; name=\"haveFile\"; filename=\"haveContent\"\r\nContent-Type: contentType\r\n\r\nThis is file test. This sentence must be long\r\n--testBoundary--\r\n";
 
        $this->assertEquals($target, $result);
    }
 
    public function testReadFile()
    {
        $fileField              = new FileField();
        $fileField->filename    = 'composer.json';
        $fileField->contentType = 'application/json';
        $fileField->content     = new Stream(fopen(__DIR__ . '/../composer.json', 'r'));
        $map                    = [
            'name'      => 'json_file',
            'type'      => 'application/json',
            'json_file' => $fileField,
        ];
 
        $boundary   = FileForm::getBoundary();
        $fileStream = FileForm::toFileForm($map, $boundary);
        $this->assertTrue(false !== strpos($fileStream->getContents(), 'json_file'));
    }
}