scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2 namespace GuzzleHttp\Tests\Stream;
3  
4 use GuzzleHttp\Stream\FnStream;
5 use GuzzleHttp\Stream\NoSeekStream;
6 use GuzzleHttp\Stream\Stream;
7 use GuzzleHttp\Stream\Utils;
8  
9 class UtilsTest extends \PHPUnit_Framework_TestCase
10 {
11 public function testCopiesToString()
12 {
13 $s = Stream::factory('foobaz');
14 $this->assertEquals('foobaz', Utils::copyToString($s));
15 $s->seek(0);
16 $this->assertEquals('foo', Utils::copyToString($s, 3));
17 $this->assertEquals('baz', Utils::copyToString($s, 3));
18 $this->assertEquals('', Utils::copyToString($s));
19 }
20  
21 public function testCopiesToStringStopsWhenReadFails()
22 {
23 $s1 = Stream::factory('foobaz');
24 $s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]);
25 $result = Utils::copyToString($s1);
26 $this->assertEquals('', $result);
27 }
28  
29 public function testCopiesToStringStopsWhenReadFailsWithMaxLen()
30 {
31 $s1 = Stream::factory('foobaz');
32 $s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]);
33 $result = Utils::copyToString($s1, 10);
34 $this->assertEquals('', $result);
35 }
36  
37 public function testCopiesToStream()
38 {
39 $s1 = Stream::factory('foobaz');
40 $s2 = Stream::factory('');
41 Utils::copyToStream($s1, $s2);
42 $this->assertEquals('foobaz', (string) $s2);
43 $s2 = Stream::factory('');
44 $s1->seek(0);
45 Utils::copyToStream($s1, $s2, 3);
46 $this->assertEquals('foo', (string) $s2);
47 Utils::copyToStream($s1, $s2, 3);
48 $this->assertEquals('foobaz', (string) $s2);
49 }
50  
51 public function testStopsCopyToStreamWhenWriteFails()
52 {
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);
58 }
59  
60 public function testStopsCopyToSteamWhenWriteFailsWithMaxLen()
61 {
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);
67 }
68  
69 public function testStopsCopyToSteamWhenReadFailsWithMaxLen()
70 {
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);
76 }
77  
78 public function testReadsLines()
79 {
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));
84 }
85  
86 public function testReadsLinesUpToMaxLength()
87 {
88 $s = Stream::factory("12345\n");
89 $this->assertEquals("123", Utils::readline($s, 4));
90 $this->assertEquals("45\n", Utils::readline($s));
91 }
92  
93 public function testReadsLineUntilFalseReturnedFromRead()
94 {
95 $s = $this->getMockBuilder('GuzzleHttp\Stream\Stream')
96 ->setMethods(['read', 'eof'])
97 ->disableOriginalConstructor()
98 ->getMock();
99 $s->expects($this->exactly(2))
100 ->method('read')
101 ->will($this->returnCallback(function () {
102 static $c = false;
103 if ($c) {
104 return false;
105 }
106 $c = true;
107 return 'h';
108 }));
109 $s->expects($this->exactly(2))
110 ->method('eof')
111 ->will($this->returnValue(false));
112 $this->assertEquals("h", Utils::readline($s));
113 }
114  
115 public function testCalculatesHash()
116 {
117 $s = Stream::factory('foobazbar');
118 $this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
119 }
120  
121 /**
122 * @expectedException \GuzzleHttp\Stream\Exception\SeekException
123 */
124 public function testCalculatesHashThrowsWhenSeekFails()
125 {
126 $s = new NoSeekStream(Stream::factory('foobazbar'));
127 $s->read(2);
128 Utils::hash($s, 'md5');
129 }
130  
131 public function testCalculatesHashSeeksToOriginalPosition()
132 {
133 $s = Stream::factory('foobazbar');
134 $s->seek(4);
135 $this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
136 $this->assertEquals(4, $s->tell());
137 }
138  
139 public function testOpensFilesSuccessfully()
140 {
141 $r = Utils::open(__FILE__, 'r');
142 $this->assertInternalType('resource', $r);
143 fclose($r);
144 }
145  
146 /**
147 * @expectedException \RuntimeException
148 * @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r
149 */
150 public function testThrowsExceptionNotWarning()
151 {
152 Utils::open('/path/to/does/not/exist', 'r');
153 }
154  
155 public function testProxiesToFactory()
156 {
157 $this->assertEquals('foo', (string) Utils::create('foo'));
158 }
159 }