Passed
Push — master ( 5584fc...9013bb )
by Kirill
04:20
created

StreamsTest::testGetUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 25
rs 9.7333
cc 1
nc 1
nop 0
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+');
0 ignored issues
show
Bug introduced by
It seems like fopen('php://temp', 'wrb+') can also be of type false; however, parameter $stream of Laminas\Diactoros\Stream::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        $stream = new Stream(/** @scrutinizer ignore-type */ fopen('php://temp', 'wrb+'), 'wrb+');
Loading history...
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