Open Journal Systems  3.3.0
EntityBodyTest.php
1 <?php
2 
3 namespace Guzzle\Tests\Http;
4 
7 
13 {
17  public function testFactoryThrowsException()
18  {
19  $body = EntityBody::factory(false);
20  }
21 
22  public function testFactory()
23  {
24  $body = EntityBody::factory('data');
25  $this->assertEquals('data', (string) $body);
26  $this->assertEquals(4, $body->getContentLength());
27  $this->assertEquals('PHP', $body->getWrapper());
28  $this->assertEquals('TEMP', $body->getStreamType());
29 
30  $handle = fopen(__DIR__ . '/../../../../phpunit.xml.dist', 'r');
31  if (!$handle) {
32  $this->fail('Could not open test file');
33  }
34  $body = EntityBody::factory($handle);
35  $this->assertEquals(__DIR__ . '/../../../../phpunit.xml.dist', $body->getUri());
36  $this->assertTrue($body->isLocal());
37  $this->assertEquals(__DIR__ . '/../../../../phpunit.xml.dist', $body->getUri());
38  $this->assertEquals(filesize(__DIR__ . '/../../../../phpunit.xml.dist'), $body->getContentLength());
39 
40  // make sure that a body will return as the same object
41  $this->assertTrue($body === EntityBody::factory($body));
42  }
43 
45  {
46  $body = EntityBody::factory('');
47  $this->assertEquals('PHP', $body->getWrapper());
48  $this->assertEquals('TEMP', $body->getStreamType());
49  $body = EntityBody::factory();
50  $this->assertEquals('PHP', $body->getWrapper());
51  $this->assertEquals('TEMP', $body->getStreamType());
52  }
53 
55  {
56  $body = EntityBody::factory(new QueryString(array('foo' => 'bar')));
57  $this->assertEquals('foo=bar', (string) $body);
58  }
59 
64  {
65  EntityBody::factory(new \stdClass('a'));
66  }
67 
68  public function testHandlesCompression()
69  {
70  $body = EntityBody::factory('testing 123...testing 123');
71  $this->assertFalse($body->getContentEncoding(), '-> getContentEncoding() must initially return FALSE');
72  $size = $body->getContentLength();
73  $body->compress();
74  $this->assertEquals('gzip', $body->getContentEncoding(), '-> getContentEncoding() must return the correct encoding after compressing');
75  $this->assertEquals(gzdeflate('testing 123...testing 123'), (string) $body);
76  $this->assertTrue($body->getContentLength() < $size);
77  $this->assertTrue($body->uncompress());
78  $this->assertEquals('testing 123...testing 123', (string) $body);
79  $this->assertFalse($body->getContentEncoding(), '-> getContentEncoding() must reset to FALSE');
80 
81  if (in_array('bzip2.*', stream_get_filters())) {
82  $this->assertTrue($body->compress('bzip2.compress'));
83  $this->assertEquals('compress', $body->getContentEncoding(), '-> compress() must set \'compress\' as the Content-Encoding');
84  }
85 
86  $this->assertFalse($body->compress('non-existent'), '-> compress() must return false when a non-existent stream filter is used');
87 
88  // Release the body
89  unset($body);
90 
91  // Use gzip compression on the initial content. This will include a
92  // gzip header which will need to be stripped when deflating the stream
93  $body = EntityBody::factory(gzencode('test'));
94  $this->assertSame($body, $body->setStreamFilterContentEncoding('zlib.deflate'));
95  $this->assertTrue($body->uncompress('zlib.inflate'));
96  $this->assertEquals('test', (string) $body);
97  unset($body);
98 
99  // Test using a very long string
100  $largeString = '';
101  for ($i = 0; $i < 25000; $i++) {
102  $largeString .= chr(rand(33, 126));
103  }
104  $body = EntityBody::factory($largeString);
105  $this->assertEquals($largeString, (string) $body);
106  $this->assertTrue($body->compress());
107  $this->assertNotEquals($largeString, (string) $body);
108  $compressed = (string) $body;
109  $this->assertTrue($body->uncompress());
110  $this->assertEquals($largeString, (string) $body);
111  $this->assertEquals($compressed, gzdeflate($largeString));
112 
113  $body = EntityBody::factory(fopen(__DIR__ . '/../TestData/compress_test', 'w'));
114  $this->assertFalse($body->compress());
115  unset($body);
116 
117  unlink(__DIR__ . '/../TestData/compress_test');
118  }
119 
120  public function testDeterminesContentType()
121  {
122  // Test using a string/temp stream
123  $body = EntityBody::factory('testing 123...testing 123');
124  $this->assertNull($body->getContentType());
125 
126  // Use a local file
127  $body = EntityBody::factory(fopen(__FILE__, 'r'));
128  $this->assertContains('text/x-', $body->getContentType());
129  }
130 
131  public function testCreatesMd5Checksum()
132  {
133  $body = EntityBody::factory('testing 123...testing 123');
134  $this->assertEquals(md5('testing 123...testing 123'), $body->getContentMd5());
135 
136  $server = $this->getServer()->enqueue(
137  "HTTP/1.1 200 OK" . "\r\n" .
138  "Content-Length: 3" . "\r\n\r\n" .
139  "abc"
140  );
141 
142  $body = EntityBody::factory(fopen($this->getServer()->getUrl(), 'r'));
143  $this->assertFalse($body->getContentMd5());
144  }
145 
147  {
148  $body = EntityBody::factory('testing 123');
149  $body->seek(4);
150  $this->assertEquals(md5('testing 123'), $body->getContentMd5());
151  $this->assertEquals(4, $body->ftell());
152  $this->assertEquals('ing 123', $body->read(1000));
153  }
154 
156  {
157  $body = EntityBody::factory(array('key1' => 'val1', 'key2' => 'val2'));
158  $this->assertEquals('key1=val1&key2=val2', (string) $body);
159  }
160 
161  public function testAllowsCustomRewind()
162  {
163  $body = EntityBody::factory('foo');
164  $rewound = false;
165  $body->setRewindFunction(function ($body) use (&$rewound) {
166  $rewound = true;
167  return $body->seek(0);
168  });
169  $body->seek(2);
170  $this->assertTrue($body->rewind());
171  $this->assertTrue($rewound);
172  }
173 
178  {
179  $body = EntityBody::factory();
180  $body->setRewindFunction('foo');
181  }
182 }
Guzzle\Tests\Http\EntityBodyTest\testFactoryEnsuresObjectsHaveToStringMethod
testFactoryEnsuresObjectsHaveToStringMethod()
Definition: EntityBodyTest.php:63
Guzzle\Tests\Http\EntityBodyTest\testCustomRewindFunctionMustBeCallable
testCustomRewindFunctionMustBeCallable()
Definition: EntityBodyTest.php:177
Guzzle\Tests\Http\EntityBodyTest\testSeeksToOriginalPosAfterMd5
testSeeksToOriginalPosAfterMd5()
Definition: EntityBodyTest.php:146
Guzzle\Tests\Http\EntityBodyTest\testDeterminesContentType
testDeterminesContentType()
Definition: EntityBodyTest.php:120
Guzzle\Tests\Http\EntityBodyTest
Definition: EntityBodyTest.php:12
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Http\EntityBodyTest\testGetTypeFormBodyFactoring
testGetTypeFormBodyFactoring()
Definition: EntityBodyTest.php:155
Guzzle\Http\QueryString
Definition: QueryString.php:14
Guzzle\Http\EntityBody\factory
static factory($resource='', $size=null)
Definition: EntityBody.php:36
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Tests\Http\EntityBodyTest\testFactoryCanCreateFromObject
testFactoryCanCreateFromObject()
Definition: EntityBodyTest.php:54
Guzzle\Tests\GuzzleTestCase\$server
static $server
Definition: GuzzleTestCase.php:26
Guzzle\Tests\Http\EntityBodyTest\testCreatesMd5Checksum
testCreatesMd5Checksum()
Definition: EntityBodyTest.php:131
Guzzle\Tests\Http\EntityBodyTest\testFactoryCreatesTempStreamByDefault
testFactoryCreatesTempStreamByDefault()
Definition: EntityBodyTest.php:44
Guzzle\Tests\Http\EntityBodyTest\testAllowsCustomRewind
testAllowsCustomRewind()
Definition: EntityBodyTest.php:161
Guzzle\Tests\Http\EntityBodyTest\testFactory
testFactory()
Definition: EntityBodyTest.php:22
Guzzle\Tests\Http\EntityBodyTest\testHandlesCompression
testHandlesCompression()
Definition: EntityBodyTest.php:68
Guzzle\Tests\Http\EntityBodyTest\testFactoryThrowsException
testFactoryThrowsException()
Definition: EntityBodyTest.php:17
Guzzle\Tests\GuzzleTestCase\getServer
static getServer()
Definition: GuzzleTestCase.php:36
Guzzle\Tests\Http
Definition: AbstractEntityBodyDecoratorTest.php:3