File: MultipartBodyTest.php

package info (click to toggle)
php-guzzlehttp 5.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,560 kB
  • ctags: 2,519
  • sloc: php: 11,610; makefile: 182; python: 22; sh: 6
file content (120 lines) | stat: -rw-r--r-- 3,825 bytes parent folder | download
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
<?php
namespace GuzzleHttp\Tests\Post;

use GuzzleHttp\Post\MultipartBody;
use GuzzleHttp\Post\PostFile;

/**
 * @covers GuzzleHttp\Post\MultipartBody
 */
class MultipartBodyTest extends \PHPUnit_Framework_TestCase
{
    protected function getTestBody()
    {
        return new MultipartBody(['foo' => 'bar'], [
            new PostFile('foo', 'abc', 'foo.txt')
        ], 'abcdef');
    }

    public function testConstructorAddsFieldsAndFiles()
    {
        $b = $this->getTestBody();
        $this->assertEquals('abcdef', $b->getBoundary());
        $c = (string) $b;
        $this->assertContains("--abcdef\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n", $c);
        $this->assertContains("--abcdef\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"foo.txt\"\r\n"
            . "Content-Type: text/plain\r\n\r\nabc\r\n--abcdef--", $c);
    }

    public function testDoesNotModifyFieldFormat()
    {
        $m = new MultipartBody(['foo+baz' => 'bar+bam %20 boo'], [
            new PostFile('foo+bar', 'abc %20 123', 'foo.txt')
        ], 'abcdef');
        $this->assertContains('name="foo+baz"', (string) $m);
        $this->assertContains('name="foo+bar"', (string) $m);
        $this->assertContains('bar+bam %20 boo', (string) $m);
        $this->assertContains('abc %20 123', (string) $m);
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testConstructorValidatesFiles()
    {
        new MultipartBody([], ['bar']);
    }

    public function testConstructorCanCreateBoundary()
    {
        $b = new MultipartBody();
        $this->assertNotNull($b->getBoundary());
    }

    public function testWrapsStreamMethods()
    {
        $b = $this->getTestBody();
        $this->assertFalse($b->write('foo'));
        $this->assertFalse($b->isWritable());
        $this->assertTrue($b->isReadable());
        $this->assertTrue($b->isSeekable());
        $this->assertEquals(0, $b->tell());
    }

    public function testCanDetachFieldsAndFiles()
    {
        $b = $this->getTestBody();
        $b->detach();
        $b->close();
        $this->assertEquals('', (string) $b);
    }

    public function testIsSeekableReturnsTrueIfAllAreSeekable()
    {
        $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
            ->setMethods(['isSeekable', 'isReadable'])
            ->getMockForAbstractClass();
        $s->expects($this->once())
            ->method('isSeekable')
            ->will($this->returnValue(false));
        $s->expects($this->once())
            ->method('isReadable')
            ->will($this->returnValue(true));
        $p = new PostFile('foo', $s, 'foo.php');
        $b = new MultipartBody([], [$p]);
        $this->assertFalse($b->isSeekable());
        $this->assertFalse($b->seek(10));
    }

    public function testReadsFromBuffer()
    {
        $b = $this->getTestBody();
        $c = $b->read(1);
        $c .= $b->read(1);
        $c .= $b->read(1);
        $c .= $b->read(1);
        $c .= $b->read(1);
        $this->assertEquals('--abc', $c);
    }

    public function testCalculatesSize()
    {
        $b = $this->getTestBody();
        $this->assertEquals(strlen($b), $b->getSize());
    }

    public function testCalculatesSizeAndReturnsNullForUnknown()
    {
        $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
            ->setMethods(['getSize', 'isReadable'])
            ->getMockForAbstractClass();
        $s->expects($this->once())
            ->method('getSize')
            ->will($this->returnValue(null));
        $s->expects($this->once())
            ->method('isReadable')
            ->will($this->returnValue(true));
        $b = new MultipartBody([], [new PostFile('foo', $s, 'foo.php')]);
        $this->assertNull($b->getSize());
    }
}