1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenEngine\Mika\Core\Components\Http\Message\Stream\Tests; |
4
|
|
|
|
5
|
|
|
use OpenEngine\Mika\Core\Components\Http\Message\Stream\StreamFactory; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class StreamTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
private const FILE = __DIR__ . '/Dummy/temp.text'; |
11
|
|
|
|
12
|
|
|
public function testCreate(): void |
13
|
|
|
{ |
14
|
|
|
$stream = (new StreamFactory)->createStream('Testing streams'); |
15
|
|
|
self::assertStringEndsWith('Testing streams', $stream->__toString()); |
16
|
|
|
$stream->close(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testModes(): void |
20
|
|
|
{ |
21
|
|
|
$stream = (new StreamFactory)->createStreamFromFile(self::FILE); |
22
|
|
|
self::assertFalse($stream->isWritable()); |
23
|
|
|
$stream->close(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testModes2(): void |
27
|
|
|
{ |
28
|
|
|
$stream = (new StreamFactory)->createStreamFromFile(self::FILE, 'w'); |
29
|
|
|
self::assertTrue($stream->isWritable()); |
30
|
|
|
self::assertFalse($stream->isReadable()); |
31
|
|
|
$stream->close(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testWriteAndReadFile(): void |
35
|
|
|
{ |
36
|
|
|
$stream = (new StreamFactory)->createStreamFromFile(self::FILE, 'w+'); |
37
|
|
|
|
38
|
|
|
self::assertEquals(10, $stream->write('It is test')); |
39
|
|
|
|
40
|
|
|
$stream->rewind(); |
41
|
|
|
|
42
|
|
|
self::assertStringEndsWith('It', $stream->read(2)); |
43
|
|
|
$stream->close(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testCreateByResource(): void |
47
|
|
|
{ |
48
|
|
|
$res = fopen(self::FILE, 'w+'); |
49
|
|
|
$stream = (new StreamFactory)->createStreamFromResource($res); |
50
|
|
|
|
51
|
|
|
self::assertEquals(9, $stream->write('Framework')); |
52
|
|
|
self::assertStringEndsWith('Framework', $stream->__toString()); |
53
|
|
|
|
54
|
|
|
$stream->close(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|