File: ResourceStreamTest.php

package info (click to toggle)
php-async-aws-core 1.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 988 kB
  • sloc: php: 6,837; makefile: 32
file content (78 lines) | stat: -rw-r--r-- 2,112 bytes parent folder | download | duplicates (2)
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
<?php

declare(strict_types=1);

namespace AsyncAws\Core\Tests\Unit\Stream;

use AsyncAws\Core\Stream\ResourceStream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class ResourceStreamTest extends TestCase
{
    /**
     * @dataProvider provideLengths
     */
    #[DataProvider('provideLengths')]
    public function testLength($content, ?int $expected): void
    {
        $stream = ResourceStream::create($content);

        self::assertSame($expected, $stream->length());
    }

    /**
     * @dataProvider provideStrings
     */
    #[DataProvider('provideStrings')]
    public function testStringify($content, string $expected): void
    {
        $stream = ResourceStream::create($content);

        self::assertSame($expected, $stream->stringify());
    }

    /**
     * @dataProvider provideChunks
     */
    #[DataProvider('provideChunks')]
    public function testChunk($content, int $size, array $expected): void
    {
        $stream = ResourceStream::create($content, $size);
        self::assertSame($expected, iterator_to_array($stream));
    }

    public static function provideLengths(): iterable
    {
        $resource = fopen(__DIR__ . '/../../../LICENSE', 'r');
        yield [$resource, 1099];

        $resource = fopen('php://temp', 'rw+');
        fwrite($resource, 'Hello World');
        yield [$resource, 11];

        $resource = fopen('php://temp', 'rw+');
        fwrite($resource, 'Hello World');
        fseek($resource, 5);
        yield [$resource, 11];
    }

    public static function provideStrings(): iterable
    {
        $resource = fopen('php://temp', 'rw+');
        fwrite($resource, 'Hello World');
        yield [$resource, 'Hello World'];

        $resource = fopen('php://temp', 'rw+');
        fwrite($resource, 'Hello World');
        fseek($resource, 5);
        yield [$resource, 'Hello World'];
    }

    public static function provideChunks(): iterable
    {
        $resource = fopen('php://temp', 'rw+');
        fwrite($resource, 'Hello World');
        yield [$resource, 3, ['Hel', 'lo ', 'Wor', 'ld']];
    }
}