Repository navigation
Expand file tree
/
Copy pathDockerRawStreamTest.php
More file actions
67 lines (58 loc) · 2.76 KB
/
Copy pathDockerRawStreamTest.php
File metadata and controls
67 lines (58 loc) · 2.76 KB
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
<?php
declare(strict_types=1);
namespace Docker\Tests\Stream;
use Docker\Stream\DockerRawStream;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
class DockerRawStreamTest extends TestCase
{
public static function streamProvider(): iterable
{
$frames = self::frame(1, "hello\n").self::frame(2, "error\n").self::frame(1, "again\n");
yield 'framed output' => [$frames, true, 8192, "hello\nagain\n", "error\n"];
yield 'one byte per read' => [$frames, true, 1, "hello\nagain\n", "error\n"];
yield 'split headers and payloads' => [$frames, true, 7, "hello\nagain\n", "error\n"];
yield 'empty framed stream' => ['', true, 8192, '', ''];
yield 'zero-length frame' => [self::frame(1, '').self::frame(1, 'hello'), true, 8192, 'hello', ''];
yield 'short TTY output' => ["hello\n", false, 8192, "hello\n", ''];
yield 'TTY output across reads' => ["terminal output\r\n", false, 2, "terminal output\r\n", ''];
yield 'empty TTY stream' => ['', false, 8192, '', ''];
yield 'TTY bytes resembling frames' => [$frames, false, 3, $frames, ''];
}
/** @dataProvider streamProvider */
public function testReadsNonSeekableStream(string $body, bool $multiplexed, int $chunkSize, string $expectedStdout, string $expectedStderr): void
{
$position = 0;
$source = $this->createMock(StreamInterface::class);
$source->method('eof')->willReturnCallback(static function () use (&$position, $body): bool {
return $position >= \strlen($body);
});
$source->method('read')->willReturnCallback(static function (int $length) use (&$position, $body, $chunkSize): string {
if ($length <= 0) {
throw new \InvalidArgumentException('Read length must be positive');
}
$chunk = substr($body, $position, min($length, $chunkSize));
$position += \strlen($chunk);
return $chunk;
});
$source->expects(self::never())->method('seek');
$source->expects(self::never())->method('rewind');
$source->expects(self::never())->method('getContents');
$stream = new DockerRawStream($source, $multiplexed);
$stdout = '';
$stderr = '';
$stream->onStdout(static function (string $output) use (&$stdout): void {
$stdout .= $output;
});
$stream->onStderr(static function (string $output) use (&$stderr): void {
$stderr .= $output;
});
$stream->wait();
self::assertSame($expectedStdout, $stdout);
self::assertSame($expectedStderr, $stderr);
}
private static function frame(int $type, string $output): string
{
return pack('CxxxN', $type, \strlen($output)).$output;
}
}