|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Spiral\Tests\Streams; |
|
10
|
|
|
|
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use Spiral\Files\Files; |
|
13
|
|
|
use Spiral\Files\FilesInterface; |
|
14
|
|
|
use Spiral\Streams\StreamWrapper; |
|
15
|
|
|
use Laminas\Diactoros\Stream; |
|
16
|
|
|
|
|
17
|
|
|
class StreamsTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
private const FIXTURE_DIRECTORY = __DIR__ . '/fixtures'; |
|
20
|
|
|
|
|
21
|
|
|
public function setUp(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$files = new Files(); |
|
24
|
|
|
$files->ensureDirectory(self::FIXTURE_DIRECTORY, FilesInterface::RUNTIME); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function tearDown(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$files = new Files(); |
|
30
|
|
|
$files->deleteDirectory(self::FIXTURE_DIRECTORY, true); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testGetUri() |
|
34
|
|
|
{ |
|
35
|
|
|
$stream = new Stream('php://memory', 'rw'); |
|
36
|
|
|
$stream->write('sample text'); |
|
37
|
|
|
|
|
38
|
|
|
$filename = StreamWrapper::getFilename($stream); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertFileExists($filename); |
|
41
|
|
|
$this->assertSame(strlen('sample text'), filesize($filename)); |
|
42
|
|
|
$this->assertSame(md5('sample text'), md5_file($filename)); |
|
43
|
|
|
|
|
44
|
|
|
$newFilename = self::FIXTURE_DIRECTORY . '/test.txt'; |
|
45
|
|
|
copy($filename, $newFilename); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertFileExists($newFilename); |
|
48
|
|
|
$this->assertSame(strlen('sample text'), filesize($newFilename)); |
|
49
|
|
|
$this->assertSame(md5('sample text'), md5_file($newFilename)); |
|
50
|
|
|
|
|
51
|
|
|
//Rewinding |
|
52
|
|
|
$this->assertFileExists($newFilename); |
|
53
|
|
|
$this->assertSame(strlen('sample text'), filesize($newFilename)); |
|
54
|
|
|
$this->assertSame(md5('sample text'), md5_file($newFilename)); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertTrue(StreamWrapper::has($filename)); |
|
57
|
|
|
$this->assertFalse(StreamWrapper::has($newFilename)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testGetResource() |
|
61
|
|
|
{ |
|
62
|
|
|
$stream = new Stream('php://memory', 'rw'); |
|
63
|
|
|
$stream->write('sample text'); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertFalse(StreamWrapper::has($stream)); |
|
66
|
|
|
$resource = StreamWrapper::getResource($stream); |
|
67
|
|
|
$this->assertTrue(StreamWrapper::has($stream)); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertIsResource($resource); |
|
70
|
|
|
$this->assertSame('sample text', stream_get_contents($resource, -1, 0)); |
|
71
|
|
|
|
|
72
|
|
|
//Rewinding |
|
73
|
|
|
$this->assertSame('sample text', stream_get_contents($resource, -1, 0)); |
|
74
|
|
|
|
|
75
|
|
|
fseek($resource, 7); |
|
76
|
|
|
$this->assertSame('text', stream_get_contents($resource, -1)); |
|
77
|
|
|
$this->assertSame('sample', stream_get_contents($resource, 6, 0)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testException() |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
fopen('spiral://non-exists', 'rb'); |
|
84
|
|
|
} catch (\Throwable $e) { |
|
85
|
|
|
$this->assertStringContainsString('failed to open stream', $e->getMessage()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
|
|
filemtime('spiral://non-exists'); |
|
91
|
|
|
} catch (\Throwable $e) { |
|
92
|
|
|
$this->assertStringContainsString('stat failed', $e->getMessage()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testWriteIntoStream() |
|
97
|
|
|
{ |
|
98
|
|
|
$stream = new Stream(fopen('php://temp', 'wrb+'), 'wrb+'); |
|
|
|
|
|
|
99
|
|
|
$file = StreamWrapper::getFilename($stream); |
|
100
|
|
|
|
|
101
|
|
|
file_put_contents($file, 'test'); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertSame('test', file_get_contents($file)); |
|
104
|
|
|
|
|
105
|
|
|
StreamWrapper::release($file); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|