File: CallableStreamTest.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 (76 lines) | stat: -rw-r--r-- 2,099 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
<?php

declare(strict_types=1);

namespace AsyncAws\Core\Tests\Unit\Stream;

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

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

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

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

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

    /**
     * @dataProvider provideChunks
     */
    #[DataProvider('provideChunks')]
    public function testChunk(callable $content, array $expected): void
    {
        $stream = CallableStream::create($content);

        self::assertSame($expected, iterator_to_array($stream));
    }

    public static function provideLengths(): iterable
    {
        yield [function () {
            return 'Hello world';
        }, null];
    }

    public static function provideStrings(): iterable
    {
        $f = static function (string ...$items) {
            return static function () use (&$items) {
                return array_shift($items) ?? '';
            };
        };
        yield [$f('Hello world'), 'Hello world'];
        yield [$f('Hello', ' ', 'world'), 'Hello world'];
        yield [$f('Hello', ' ', '', 'world'), 'Hello '];
    }

    public static function provideChunks(): iterable
    {
        $f = static function (string ...$items) {
            return static function () use (&$items) {
                return array_shift($items) ?? '';
            };
        };
        yield [$f('Hello world'), ['Hello world']];
        yield [$f('Hello', ' ', 'world'), ['Hello', ' ', 'world']];
        yield [$f('Hello', ' ', '', 'world'), ['Hello', ' ']];
    }
}