scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests\Post;
4  
5 use GuzzleHttp\Post\MultipartBody;
6 use GuzzleHttp\Post\PostFile;
7  
8 /**
9 * @covers GuzzleHttp\Post\MultipartBody
10 */
11 class MultipartBodyTest extends \PHPUnit_Framework_TestCase
12 {
13 protected function getTestBody()
14 {
15 return new MultipartBody(['foo' => 'bar'], [
16 new PostFile('foo', 'abc', 'foo.txt')
17 ], 'abcdef');
18 }
19  
20 public function testConstructorAddsFieldsAndFiles()
21 {
22 $b = $this->getTestBody();
23 $this->assertEquals('abcdef', $b->getBoundary());
24 $c = (string) $b;
25 $this->assertContains("--abcdef\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n", $c);
26 $this->assertContains("--abcdef\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"foo.txt\"\r\n"
27 . "Content-Type: text/plain\r\n\r\nabc\r\n--abcdef--", $c);
28 }
29  
30 public function testDoesNotModifyFieldFormat()
31 {
32 $m = new MultipartBody(['foo+baz' => 'bar+bam %20 boo'], [
33 new PostFile('foo+bar', 'abc %20 123', 'foo.txt')
34 ], 'abcdef');
35 $this->assertContains('name="foo+baz"', (string) $m);
36 $this->assertContains('name="foo+bar"', (string) $m);
37 $this->assertContains('bar+bam %20 boo', (string) $m);
38 $this->assertContains('abc %20 123', (string) $m);
39 }
40  
41 /**
42 * @expectedException \InvalidArgumentException
43 */
44 public function testConstructorValidatesFiles()
45 {
46 new MultipartBody([], ['bar']);
47 }
48  
49 public function testConstructorCanCreateBoundary()
50 {
51 $b = new MultipartBody();
52 $this->assertNotNull($b->getBoundary());
53 }
54  
55 public function testWrapsStreamMethods()
56 {
57 $b = $this->getTestBody();
58 $this->assertFalse($b->write('foo'));
59 $this->assertFalse($b->isWritable());
60 $this->assertTrue($b->isReadable());
61 $this->assertTrue($b->isSeekable());
62 $this->assertEquals(0, $b->tell());
63 }
64  
65 public function testCanDetachFieldsAndFiles()
66 {
67 $b = $this->getTestBody();
68 $b->detach();
69 $b->close();
70 $this->assertEquals('', (string) $b);
71 }
72  
73 public function testIsSeekableReturnsTrueIfAllAreSeekable()
74 {
75 $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
76 ->setMethods(['isSeekable', 'isReadable'])
77 ->getMockForAbstractClass();
78 $s->expects($this->once())
79 ->method('isSeekable')
80 ->will($this->returnValue(false));
81 $s->expects($this->once())
82 ->method('isReadable')
83 ->will($this->returnValue(true));
84 $p = new PostFile('foo', $s, 'foo.php');
85 $b = new MultipartBody([], [$p]);
86 $this->assertFalse($b->isSeekable());
87 $this->assertFalse($b->seek(10));
88 }
89  
90 public function testGetContentsCanCap()
91 {
92 $b = $this->getTestBody();
93 $c = (string) $b;
94 $b->seek(0);
95 $this->assertSame(substr($c, 0, 10), $b->getContents(10));
96 }
97  
98 public function testReadsFromBuffer()
99 {
100 $b = $this->getTestBody();
101 $c = $b->read(1);
102 $c .= $b->read(1);
103 $c .= $b->read(1);
104 $c .= $b->read(1);
105 $c .= $b->read(1);
106 $this->assertEquals('--abc', $c);
107 }
108  
109 public function testCalculatesSize()
110 {
111 $b = $this->getTestBody();
112 $this->assertEquals(strlen($b), $b->getSize());
113 }
114  
115 public function testCalculatesSizeAndReturnsNullForUnknown()
116 {
117 $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
118 ->setMethods(['getSize', 'isReadable'])
119 ->getMockForAbstractClass();
120 $s->expects($this->once())
121 ->method('getSize')
122 ->will($this->returnValue(null));
123 $s->expects($this->once())
124 ->method('isReadable')
125 ->will($this->returnValue(true));
126 $b = new MultipartBody([], [new PostFile('foo', $s, 'foo.php')]);
127 $this->assertNull($b->getSize());
128 }
129 }