File: LazyOpenStreamTest.php

package info (click to toggle)
php-guzzlehttp-psr7 1.7.0-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 780 kB
  • sloc: php: 7,816; makefile: 37
file content (71 lines) | stat: -rw-r--r-- 1,910 bytes parent folder | download
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
<?php

namespace GuzzleHttp\Tests\Psr7;

use GuzzleHttp\Psr7\LazyOpenStream;

class LazyOpenStreamTest extends BaseTest
{
    private $fname;

    /**
     * @before
     */
    public function setUpTest()
    {
        $this->fname = tempnam(sys_get_temp_dir(), 'tfile');

        if (file_exists($this->fname)) {
            unlink($this->fname);
        }
    }

    /**
     * @after
     */
    public function tearDownTest()
    {
        if (file_exists($this->fname)) {
            unlink($this->fname);
        }
    }

    public function testOpensLazily()
    {
        $l = new LazyOpenStream($this->fname, 'w+');
        $l->write('foo');
        $this->assertInternalTypeGuzzle('array', $l->getMetadata());
        $this->assertFileExists($this->fname);
        $this->assertSame('foo', file_get_contents($this->fname));
        $this->assertSame('foo', (string) $l);
    }

    public function testProxiesToFile()
    {
        file_put_contents($this->fname, 'foo');
        $l = new LazyOpenStream($this->fname, 'r');
        $this->assertSame('foo', $l->read(4));
        $this->assertTrue($l->eof());
        $this->assertSame(3, $l->tell());
        $this->assertTrue($l->isReadable());
        $this->assertTrue($l->isSeekable());
        $this->assertFalse($l->isWritable());
        $l->seek(1);
        $this->assertSame('oo', $l->getContents());
        $this->assertSame('foo', (string) $l);
        $this->assertSame(3, $l->getSize());
        $this->assertInternalTypeGuzzle('array', $l->getMetadata());
        $l->close();
    }

    public function testDetachesUnderlyingStream()
    {
        file_put_contents($this->fname, 'foo');
        $l = new LazyOpenStream($this->fname, 'r');
        $r = $l->detach();
        $this->assertInternalTypeGuzzle('resource', $r);
        fseek($r, 0);
        $this->assertSame('foo', stream_get_contents($r));
        fclose($r);
    }
}