scratch

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 86  →  ?path2? @ 87
/vendor/guzzlehttp/guzzle/tests/Post/MultipartBodyTest.php
@@ -0,0 +1,129 @@
<?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 testGetContentsCanCap()
{
$b = $this->getTestBody();
$c = (string) $b;
$b->seek(0);
$this->assertSame(substr($c, 0, 10), $b->getContents(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());
}
}
/vendor/guzzlehttp/guzzle/tests/Post/PostBodyTest.php
@@ -0,0 +1,219 @@
<?php
 
namespace GuzzleHttp\Tests\Post;
 
use GuzzleHttp\Message\Request;
use GuzzleHttp\Post\PostBody;
use GuzzleHttp\Post\PostFile;
use GuzzleHttp\Query;
 
/**
* @covers GuzzleHttp\Post\PostBody
*/
class PostBodyTest extends \PHPUnit_Framework_TestCase
{
public function testWrapsBasicStreamFunctionality()
{
$b = new PostBody();
$this->assertTrue($b->isSeekable());
$this->assertTrue($b->isReadable());
$this->assertFalse($b->isWritable());
$this->assertFalse($b->write('foo'));
}
 
public function testApplyingWithNothingDoesNothing()
{
$b = new PostBody();
$m = new Request('POST', '/');
$b->applyRequestHeaders($m);
$this->assertFalse($m->hasHeader('Content-Length'));
$this->assertFalse($m->hasHeader('Content-Type'));
}
 
public function testCanForceMultipartUploadsWhenApplying()
{
$b = new PostBody();
$b->forceMultipartUpload(true);
$m = new Request('POST', '/');
$b->applyRequestHeaders($m);
$this->assertContains(
'multipart/form-data',
$m->getHeader('Content-Type')
);
}
 
public function testApplyingWithFilesAddsMultipartUpload()
{
$b = new PostBody();
$p = new PostFile('foo', fopen(__FILE__, 'r'));
$b->addFile($p);
$this->assertEquals([$p], $b->getFiles());
$this->assertNull($b->getFile('missing'));
$this->assertSame($p, $b->getFile('foo'));
$m = new Request('POST', '/');
$b->applyRequestHeaders($m);
$this->assertContains(
'multipart/form-data',
$m->getHeader('Content-Type')
);
$this->assertTrue($m->hasHeader('Content-Length'));
}
 
public function testApplyingWithFieldsAddsMultipartUpload()
{
$b = new PostBody();
$b->setField('foo', 'bar');
$this->assertEquals(['foo' => 'bar'], $b->getFields());
$m = new Request('POST', '/');
$b->applyRequestHeaders($m);
$this->assertContains(
'application/x-www-form',
$m->getHeader('Content-Type')
);
$this->assertTrue($m->hasHeader('Content-Length'));
}
 
public function testMultipartWithNestedFields()
{
$b = new PostBody();
$b->setField('foo', ['bar' => 'baz']);
$b->forceMultipartUpload(true);
$this->assertEquals(['foo' => ['bar' => 'baz']], $b->getFields());
$m = new Request('POST', '/');
$b->applyRequestHeaders($m);
$this->assertContains(
'multipart/form-data',
$m->getHeader('Content-Type')
);
$this->assertTrue($m->hasHeader('Content-Length'));
$contents = $b->getContents();
$this->assertContains('name="foo[bar]"', $contents);
$this->assertNotContains('name="foo"', $contents);
}
 
public function testCountProvidesFieldsAndFiles()
{
$b = new PostBody();
$b->setField('foo', 'bar');
$b->addFile(new PostFile('foo', fopen(__FILE__, 'r')));
$this->assertEquals(2, count($b));
$b->clearFiles();
$b->removeField('foo');
$this->assertEquals(0, count($b));
$this->assertEquals([], $b->getFiles());
$this->assertEquals([], $b->getFields());
}
 
public function testHasFields()
{
$b = new PostBody();
$b->setField('foo', 'bar');
$b->setField('baz', '123');
$this->assertEquals('bar', $b->getField('foo'));
$this->assertEquals('123', $b->getField('baz'));
$this->assertNull($b->getField('ahh'));
$this->assertTrue($b->hasField('foo'));
$this->assertFalse($b->hasField('test'));
$b->replaceFields(['abc' => '123']);
$this->assertFalse($b->hasField('foo'));
$this->assertTrue($b->hasField('abc'));
}
 
public function testConvertsFieldsToQueryStyleBody()
{
$b = new PostBody();
$b->setField('foo', 'bar');
$b->setField('baz', '123');
$this->assertEquals('foo=bar&baz=123', $b);
$this->assertEquals(15, $b->getSize());
$b->seek(0);
$this->assertEquals('foo=bar&baz=123', $b->getContents());
$b->seek(0);
$this->assertEquals('foo=bar&baz=123', $b->read(1000));
$this->assertEquals(15, $b->tell());
$this->assertTrue($b->eof());
}
 
public function testCanSpecifyQueryAggregator()
{
$b = new PostBody();
$b->setField('foo', ['baz', 'bar']);
$this->assertEquals('foo%5B0%5D=baz&foo%5B1%5D=bar', (string) $b);
$b = new PostBody();
$b->setField('foo', ['baz', 'bar']);
$agg = Query::duplicateAggregator();
$b->setAggregator($agg);
$this->assertEquals('foo=baz&foo=bar', (string) $b);
}
 
public function testDetachesAndCloses()
{
$b = new PostBody();
$b->setField('foo', 'bar');
$b->detach();
$this->assertTrue($b->close());
$this->assertEquals('', $b->read(10));
}
 
public function testCreatesMultipartUploadWithMultiFields()
{
$b = new PostBody();
$b->setField('testing', ['baz', 'bar']);
$b->setField('other', 'hi');
$b->setField('third', 'there');
$b->addFile(new PostFile('foo', fopen(__FILE__, 'r')));
$s = (string) $b;
$this->assertContains(file_get_contents(__FILE__), $s);
$this->assertContains('testing=bar', $s);
$this->assertContains(
'Content-Disposition: form-data; name="third"',
$s
);
$this->assertContains(
'Content-Disposition: form-data; name="other"',
$s
);
}
 
public function testMultipartWithBase64Fields()
{
$b = new PostBody();
$b->setField('foo64', '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc=');
$b->forceMultipartUpload(true);
$this->assertEquals(
['foo64' => '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc='],
$b->getFields()
);
$m = new Request('POST', '/');
$b->applyRequestHeaders($m);
$this->assertContains(
'multipart/form-data',
$m->getHeader('Content-Type')
);
$this->assertTrue($m->hasHeader('Content-Length'));
$contents = $b->getContents();
$this->assertContains('name="foo64"', $contents);
$this->assertContains(
'/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc=',
$contents
);
}
 
public function testMultipartWithAmpersandInValue()
{
$b = new PostBody();
$b->setField('a', 'b&c=d');
$b->forceMultipartUpload(true);
$this->assertEquals(['a' => 'b&c=d'], $b->getFields());
$m = new Request('POST', '/');
$b->applyRequestHeaders($m);
$this->assertContains(
'multipart/form-data',
$m->getHeader('Content-Type')
);
$this->assertTrue($m->hasHeader('Content-Length'));
$contents = $b->getContents();
$this->assertContains('name="a"', $contents);
$this->assertContains('b&c=d', $contents);
}
}
/vendor/guzzlehttp/guzzle/tests/Post/PostFileTest.php
@@ -0,0 +1,68 @@
<?php
 
namespace GuzzleHttp\Tests\Post;
 
use GuzzleHttp\Post\PostFile;
use GuzzleHttp\Stream\Stream;
 
/**
* @covers GuzzleHttp\Post\PostFile
*/
class PostFileTest extends \PHPUnit_Framework_TestCase
{
public function testCreatesFromString()
{
$p = new PostFile('foo', 'hi', '/path/to/test.php');
$this->assertInstanceOf('GuzzleHttp\Post\PostFileInterface', $p);
$this->assertEquals('hi', $p->getContent());
$this->assertEquals('foo', $p->getName());
$this->assertEquals('/path/to/test.php', $p->getFilename());
$this->assertEquals(
'form-data; name="foo"; filename="test.php"',
$p->getHeaders()['Content-Disposition']
);
}
 
public function testGetsFilenameFromMetadata()
{
$p = new PostFile('foo', fopen(__FILE__, 'r'));
$this->assertEquals(__FILE__, $p->getFilename());
}
 
public function testDefaultsToNameWhenNoFilenameExists()
{
$p = new PostFile('foo', 'bar');
$this->assertEquals('foo', $p->getFilename());
}
 
public function testCreatesFromMultipartFormData()
{
$mp = $this->getMockBuilder('GuzzleHttp\Post\MultipartBody')
->setMethods(['getBoundary'])
->disableOriginalConstructor()
->getMock();
$mp->expects($this->once())
->method('getBoundary')
->will($this->returnValue('baz'));
 
$p = new PostFile('foo', $mp);
$this->assertEquals(
'form-data; name="foo"',
$p->getHeaders()['Content-Disposition']
);
$this->assertEquals(
'multipart/form-data; boundary=baz',
$p->getHeaders()['Content-Type']
);
}
 
public function testCanAddHeaders()
{
$p = new PostFile('foo', Stream::factory('hi'), 'test.php', [
'X-Foo' => '123',
'Content-Disposition' => 'bar'
]);
$this->assertEquals('bar', $p->getHeaders()['Content-Disposition']);
$this->assertEquals('123', $p->getHeaders()['X-Foo']);
}
}