File: CURLStringFileTest.php

package info (click to toggle)
php-symfony-polyfill 1.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 8,520 kB
  • sloc: php: 127,232; makefile: 69
file content (59 lines) | stat: -rw-r--r-- 1,699 bytes parent folder | download | duplicates (3)
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
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Polyfill\Tests\Php81;

use PHPUnit\Framework\TestCase;

/**
 * @requires extension curl
 * @requires PHP 7.4
 */
class CURLStringFileTest extends TestCase
{
    private static $server;

    public static function setUpBeforeClass(): void
    {
        $spec = [
            1 => ['file', '/dev/null', 'w'],
            2 => ['file', '/dev/null', 'w'],
        ];
        if (!self::$server = @proc_open(('\\' === \DIRECTORY_SEPARATOR ? '' : 'exec ').\PHP_BINARY.' -S localhost:8086', $spec, $pipes, __DIR__.'/fixtures')) {
            self::markTestSkipped('Unable to start PHP server.');
        }
        sleep(1);
    }

    public static function tearDownAfterClass(): void
    {
        if (self::$server) {
            proc_terminate(self::$server);
            proc_close(self::$server);
        }
    }

    public function testCurlFileShowsContents()
    {
        $file = new \CURLStringFile('Hello', 'symfony.txt', 'text/plain');
        $data = ['test_file' => $file];

        $ch = curl_init('http://localhost:8086/curl-echo-server.php');

        curl_setopt($ch, \CURLOPT_POST, 1);
        curl_setopt($ch, \CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);

        $this->assertEquals(200, curl_getinfo($ch, \CURLINFO_HTTP_CODE));
        $this->assertSame('{"test_file":{"name":"symfony.txt","type":"text\/plain","error":0,"size":5}}', $response);
    }
}