1+ <?php
2+
3+ namespace Http \Socket \Tests ;
4+
5+ use Http \Socket \Stream ;
6+
7+ class StreamTest extends \PHPUnit_Framework_TestCase
8+ {
9+ public function createSocket ($ body , $ useSize = true )
10+ {
11+ $ socket = fopen ('php://memory ' , 'rw ' );
12+ fwrite ($ socket , $ body );
13+ fseek ($ socket , 0 );
14+
15+ return new Stream ($ socket , $ useSize ? strlen ($ body ) : null );
16+ }
17+
18+ public function testToString ()
19+ {
20+ $ stream = $ this ->createSocket ("Body " );
21+
22+ $ this ->assertEquals ("Body " , $ stream ->__toString ());
23+ $ stream ->close ();
24+ }
25+
26+ public function testSubsequentCallIsEmpty ()
27+ {
28+ $ stream = $ this ->createSocket ("Body " );
29+
30+ $ this ->assertEquals ("Body " , $ stream ->getContents ());
31+ $ this ->assertEmpty ($ stream ->getContents ());
32+ $ stream ->close ();
33+ }
34+
35+ public function testDetach ()
36+ {
37+ $ stream = $ this ->createSocket ("Body " );
38+ $ socket = $ stream ->detach ();
39+
40+ $ this ->assertTrue (is_resource ($ socket ));
41+ $ this ->assertNull ($ stream ->detach ());
42+ }
43+
44+ public function testTell ()
45+ {
46+ $ stream = $ this ->createSocket ("Body " );
47+
48+ $ this ->assertEquals (0 , $ stream ->tell ());
49+ $ this ->assertEquals ("Body " , $ stream ->getContents ());
50+ $ this ->assertEquals (4 , $ stream ->tell ());
51+ }
52+
53+ public function testEof ()
54+ {
55+ $ socket = fopen ('php://memory ' , 'rw+ ' );
56+ fwrite ($ socket , "Body " );
57+ fseek ($ socket , 0 );
58+ $ stream = new Stream ($ socket );
59+
60+ $ this ->assertEquals ("Body " , $ stream ->getContents ());
61+ fwrite ($ socket , "\0" );
62+ $ this ->assertTrue ($ stream ->eof ());
63+ $ stream ->close ();
64+ }
65+
66+ public function testNotSeekable ()
67+ {
68+ $ stream = $ this ->createSocket ("Body " );
69+
70+ $ this ->assertFalse ($ stream ->isSeekable ());
71+
72+ try {
73+ $ stream ->seek (0 );
74+ } catch (\Exception $ e ) {
75+ $ this ->assertInstanceOf ('Http\Socket\Exception\StreamException ' , $ e );
76+ }
77+ }
78+
79+ public function testNoRewing ()
80+ {
81+ $ stream = $ this ->createSocket ("Body " );
82+
83+ try {
84+ $ stream ->rewind ();
85+ } catch (\Exception $ e ) {
86+ $ this ->assertInstanceOf ('Http\Socket\Exception\StreamException ' , $ e );
87+ }
88+ }
89+
90+ public function testNotWritable ()
91+ {
92+ $ stream = $ this ->createSocket ("Body " );
93+
94+ $ this ->assertFalse ($ stream ->isWritable ());
95+
96+ try {
97+ $ stream ->write ("Test " );
98+ } catch (\Exception $ e ) {
99+ $ this ->assertInstanceOf ('Http\Socket\Exception\StreamException ' , $ e );
100+ }
101+ }
102+
103+ public function testIsReadable ()
104+ {
105+ $ stream = $ this ->createSocket ("Body " );
106+
107+ $ this ->assertTrue ($ stream ->isReadable ());
108+ }
109+
110+ public function testTimeout ()
111+ {
112+ $ socket = fsockopen ("php.net " , 80 );
113+ socket_set_timeout ($ socket , 0 , 100 );
114+
115+ $ stream = new Stream ($ socket );
116+
117+ try {
118+ $ stream ->getContents ();
119+ } catch (\Exception $ e ) {
120+ $ this ->assertInstanceOf ('Http\Socket\Exception\TimeoutException ' , $ e );
121+ }
122+ }
123+
124+ public function testMetadatas ()
125+ {
126+ $ stream = $ this ->createSocket ("Body " , false );
127+
128+ $ this ->assertEquals ("PHP " , $ stream ->getMetadata ("wrapper_type " ));
129+ $ this ->assertEquals ("MEMORY " , $ stream ->getMetadata ("stream_type " ));
130+ $ this ->assertEquals ("php://memory " , $ stream ->getMetadata ("uri " ));
131+ $ this ->assertFalse ($ stream ->getMetadata ("timed_out " ));
132+ $ this ->assertFalse ($ stream ->getMetadata ("eof " ));
133+ $ this ->assertTrue ($ stream ->getMetadata ("blocked " ));
134+ }
135+
136+ public function testClose ()
137+ {
138+ $ socket = fopen ('php://memory ' , 'rw+ ' );
139+ fwrite ($ socket , "Body " );
140+ fseek ($ socket , 0 );
141+
142+ $ stream = new Stream ($ socket );
143+ $ stream ->close ();
144+
145+ $ this ->assertFalse (is_resource ($ socket ));
146+ }
147+ }
148+
0 commit comments