2
namespace GuzzleHttp\Tests\Stream;
4
use GuzzleHttp\Stream\FnStream;
5
use GuzzleHttp\Stream\NoSeekStream;
6
use GuzzleHttp\Stream\Stream;
7
use GuzzleHttp\Stream\Utils;
9
class UtilsTest extends \PHPUnit_Framework_TestCase
11
public function testCopiesToString()
13
$s = Stream::factory('foobaz');
14
$this->assertEquals('foobaz', Utils::copyToString($s));
16
$this->assertEquals('foo', Utils::copyToString($s, 3));
17
$this->assertEquals('baz', Utils::copyToString($s, 3));
18
$this->assertEquals('', Utils::copyToString($s));
21
public function testCopiesToStringStopsWhenReadFails()
23
$s1 = Stream::factory('foobaz');
24
$s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]);
25
$result = Utils::copyToString($s1);
26
$this->assertEquals('', $result);
29
public function testCopiesToStringStopsWhenReadFailsWithMaxLen()
31
$s1 = Stream::factory('foobaz');
32
$s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]);
33
$result = Utils::copyToString($s1, 10);
34
$this->assertEquals('', $result);
37
public function testCopiesToStream()
39
$s1 = Stream::factory('foobaz');
40
$s2 = Stream::factory('');
41
Utils::copyToStream($s1, $s2);
42
$this->assertEquals('foobaz', (string) $s2);
43
$s2 = Stream::factory('');
45
Utils::copyToStream($s1, $s2, 3);
46
$this->assertEquals('foo', (string) $s2);
47
Utils::copyToStream($s1, $s2, 3);
48
$this->assertEquals('foobaz', (string) $s2);
51
public function testStopsCopyToStreamWhenWriteFails()
53
$s1 = Stream::factory('foobaz');
54
$s2 = Stream::factory('');
55
$s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]);
56
Utils::copyToStream($s1, $s2);
57
$this->assertEquals('', (string) $s2);
60
public function testStopsCopyToSteamWhenWriteFailsWithMaxLen()
62
$s1 = Stream::factory('foobaz');
63
$s2 = Stream::factory('');
64
$s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]);
65
Utils::copyToStream($s1, $s2, 10);
66
$this->assertEquals('', (string) $s2);
69
public function testStopsCopyToSteamWhenReadFailsWithMaxLen()
71
$s1 = Stream::factory('foobaz');
72
$s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]);
73
$s2 = Stream::factory('');
74
Utils::copyToStream($s1, $s2, 10);
75
$this->assertEquals('', (string) $s2);
78
public function testReadsLines()
80
$s = Stream::factory("foo\nbaz\nbar");
81
$this->assertEquals("foo\n", Utils::readline($s));
82
$this->assertEquals("baz\n", Utils::readline($s));
83
$this->assertEquals("bar", Utils::readline($s));
86
public function testReadsLinesUpToMaxLength()
88
$s = Stream::factory("12345\n");
89
$this->assertEquals("123", Utils::readline($s, 4));
90
$this->assertEquals("45\n", Utils::readline($s));
93
public function testReadsLineUntilFalseReturnedFromRead()
95
$s = $this->getMockBuilder('GuzzleHttp\Stream\Stream')
96
->setMethods(['read', 'eof'])
97
->disableOriginalConstructor()
99
$s->expects($this->exactly(2))
101
->will($this->returnCallback(function () {
109
$s->expects($this->exactly(2))
111
->will($this->returnValue(false));
112
$this->assertEquals("h", Utils::readline($s));
115
public function testCalculatesHash()
117
$s = Stream::factory('foobazbar');
118
$this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
122
* @expectedException \GuzzleHttp\Stream\Exception\SeekException
124
public function testCalculatesHashThrowsWhenSeekFails()
126
$s = new NoSeekStream(Stream::factory('foobazbar'));
128
Utils::hash($s, 'md5');
131
public function testCalculatesHashSeeksToOriginalPosition()
133
$s = Stream::factory('foobazbar');
135
$this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
136
$this->assertEquals(4, $s->tell());
139
public function testOpensFilesSuccessfully()
141
$r = Utils::open(__FILE__, 'r');
142
$this->assertInternalType('resource', $r);
147
* @expectedException \RuntimeException
148
* @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r
150
public function testThrowsExceptionNotWarning()
152
Utils::open('/path/to/does/not/exist', 'r');
155
public function testProxiesToFactory()
157
$this->assertEquals('foo', (string) Utils::create('foo'));