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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace GuzzleHttp\Tests\Stream;
use GuzzleHttp\Stream\FnStream;
use GuzzleHttp\Stream\NoSeekStream;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Stream\Utils;
class UtilsTest extends \PHPUnit_Framework_TestCase
{
public function testCopiesToString()
{
$s = Stream::factory('foobaz');
$this->assertEquals('foobaz', Utils::copyToString($s));
$s->seek(0);
$this->assertEquals('foo', Utils::copyToString($s, 3));
$this->assertEquals('baz', Utils::copyToString($s, 3));
$this->assertEquals('', Utils::copyToString($s));
}
public function testCopiesToStringStopsWhenReadFails()
{
$s1 = Stream::factory('foobaz');
$s1 = FnStream::decorate($s1, [
'read' => function () {
return false;
}
]);
$result = Utils::copyToString($s1);
$this->assertEquals('', $result);
}
public function testCopiesToStream()
{
$s1 = Stream::factory('foobaz');
$s2 = Stream::factory('');
Utils::copyToStream($s1, $s2);
$this->assertEquals('foobaz', (string) $s2);
$s2 = Stream::factory('');
$s1->seek(0);
Utils::copyToStream($s1, $s2, 3);
$this->assertEquals('foo', (string) $s2);
Utils::copyToStream($s1, $s2, 3);
$this->assertEquals('foobaz', (string) $s2);
}
public function testStopsCopyToStreamWhenWriteFails()
{
$s1 = Stream::factory('foobaz');
$s2 = Stream::factory('');
$s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]);
Utils::copyToStream($s1, $s2);
$this->assertEquals('', (string) $s2);
}
public function testStopsCopyToSteamWhenWriteFailsWithMaxLen()
{
$s1 = Stream::factory('foobaz');
$s2 = Stream::factory('');
$s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]);
Utils::copyToStream($s1, $s2, 10);
$this->assertEquals('', (string) $s2);
}
public function testStopsCopyToSteamWhenReadFailsWithMaxLen()
{
$s1 = Stream::factory('foobaz');
$s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]);
$s2 = Stream::factory('');
Utils::copyToStream($s1, $s2, 10);
$this->assertEquals('', (string) $s2);
}
public function testReadsLines()
{
$s = Stream::factory("foo\nbaz\nbar");
$this->assertEquals("foo\n", Utils::readline($s));
$this->assertEquals("baz\n", Utils::readline($s));
$this->assertEquals("bar", Utils::readline($s));
}
public function testReadsLinesUpToMaxLength()
{
$s = Stream::factory("12345\n");
$this->assertEquals("123", Utils::readline($s, 4));
$this->assertEquals("45\n", Utils::readline($s));
}
public function testReadsLineUntilFalseReturnedFromRead()
{
$s = $this->getMockBuilder('GuzzleHttp\Stream\Stream')
->setMethods(['read', 'eof'])
->disableOriginalConstructor()
->getMock();
$s->expects($this->exactly(2))
->method('read')
->will($this->returnCallback(function () {
static $c = false;
if ($c) {
return false;
}
$c = true;
return 'h';
}));
$s->expects($this->exactly(2))
->method('eof')
->will($this->returnValue(false));
$this->assertEquals("h", Utils::readline($s));
}
public function testCalculatesHash()
{
$s = Stream::factory('foobazbar');
$this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
}
/**
* @expectedException \GuzzleHttp\Stream\Exception\SeekException
*/
public function testCalculatesHashThrowsWhenSeekFails()
{
$s = new NoSeekStream(Stream::factory('foobazbar'));
$s->read(2);
Utils::hash($s, 'md5');
}
public function testCalculatesHashSeeksToOriginalPosition()
{
$s = Stream::factory('foobazbar');
$s->seek(4);
$this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
$this->assertEquals(4, $s->tell());
}
public function testOpensFilesSuccessfully()
{
$r = Utils::open(__FILE__, 'r');
$this->assertInternalType('resource', $r);
fclose($r);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r
*/
public function testThrowsExceptionNotWarning()
{
Utils::open('/path/to/does/not/exist', 'r');
}
public function testProxiesToFactory()
{
$this->assertEquals('foo', (string) Utils::create('foo'));
}
}