Few test corrections to preview where it goes. (#358)

* Few test corrections to preview where it goes.

* Removed TODO. Fixed dependency
This commit is contained in:
Temirkhan 2017-05-22 15:47:11 +04:00 committed by Tobias Nyholm
parent 487c1edd77
commit 6d56fa6a0d

View File

@ -9,37 +9,148 @@
namespace Mailgun\Tests; namespace Mailgun\Tests;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Http\Message\RequestFactory;
use Mailgun\RequestBuilder; use Mailgun\RequestBuilder;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
class RequestBuilderTest extends \PHPUnit_Framework_TestCase class RequestBuilderTest extends PHPUnit_Framework_TestCase
{ {
/**
* @var MockObject|RequestFactory
*/
private $requestFactory;
/**
* @var RequestBuilder
*/
private $requestBuilder;
/**
* Environment preset
*/
protected function setUp()
{
parent::setUp();
$this->requestFactory = $this->getMockBuilder(RequestFactory::class)
->disableOriginalConstructor()
->getMock();
$this->requestBuilder = new RequestBuilder();
//Everything but testing class is mock. Otherwise it wouldn't be unit testing
$this->requestBuilder->setRequestFactory($this->requestFactory);
}
/**
* Environment reset
*/
protected function tearDown()
{
parent::tearDown();
$this->requestFactory = null;
$this->requestBuilder = null;
}
public function testCreateSimpleStream() public function testCreateSimpleStream()
{ {
$builder = new RequestBuilder(); $this->requestFactory
$request = $builder->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], 'content'); ->expects($this->once())
->method('createRequest')
->with(
$this->equalTo('GET'),
$this->equalTo('http://foo.bar'),
$this->callback(function (array $headers) {
$this->assertArrayHasKey('Content-Type', $headers);
$this->assertEquals('application/json', $headers['Content-Type']);
$body = $request->getBody()->__toString(); return true;
$contentType = $request->getHeaderLine('Content-Type'); }),
$this->equalTo('content')
)
->willReturn($request = $this->getMock(RequestInterface::class));
$this->assertContains('content', $body); $result = $this->requestBuilder
$this->assertEquals('application/json', $contentType); ->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], 'content');
$this->assertSame($request, $result);
} }
public function testCreateMultipartStream() public function testCreateMultipartStream()
{ {
$item0 = ['content' => 'foobar', 'name' => 'username']; $multipartStreamBuilder = $this->createMultipartStreamBuilder();
$item1 = ['content' => 'Stockholm', 'name' => 'city'];
$builder = new RequestBuilder(); $item0 = ['content' => 'foobar', 'name' => 'username', 'some_stuff' => 'some value'];
$request = $builder->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], [$item0, $item1]); $item1 = ['content' => 'Stockholm', 'name' => 'city', 'other_stuff' => 'other value'];
$body = [$item0, $item1];
$body = $request->getBody()->__toString();
$contentType = $request->getHeaderLine('Content-Type');
$this->assertContains('foobar', $body); foreach ($body as $index => $item) {
$this->assertContains('username', $body); $multipartStreamBuilder
$this->assertContains('Stockholm', $body); ->expects($this->at($index))
$this->assertContains('city', $body); ->method('addResource')
$this->assertRegExp('|^multipart/form-data; boundary=(?P<quote>"?)[\w\Q\'()+,-./:=?\E]{1,69}(?P=quote)$|si', $contentType); ->with(
$this->equalTo($item['name']),
$this->equalTo($item['content']),
$this->callback(function (array $data) use ($item) {
unset($item['name'], $item['content']);
$this->assertEquals($item, $data);
return true;
})
)
->willReturn($multipartStreamBuilder);
}
$multipartStreamBuilder
->expects($this->once())
->method('build')
->willReturn($stream = $this->getMock(StreamInterface::class));
$multipartStreamBuilder
->expects($this->once())
->method('getBoundary')
->willReturn('some boundary');
$multipartStreamBuilder
->expects($this->once())
->method('reset')
->willReturn($multipartStreamBuilder);
$this->requestFactory
->expects($this->once())
->method('createRequest')
->with(
$this->equalTo('GET'),
$this->equalTo('http://foo.bar'),
$this->callback(function (array $headers) {
$this->assertArrayHasKey('Content-Type', $headers);
$this->assertEquals('multipart/form-data; boundary="some boundary"', $headers['Content-Type']);
return true;
}),
$this->equalTo($stream)
)
->willReturn($request = $this->getMock(RequestInterface::class));
$this->requestBuilder->setMultipartStreamBuilder($multipartStreamBuilder);
$result = $this->requestBuilder
->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], [$item0, $item1]);
$this->assertSame($request, $result);
}
/**
* Creates multipart stream builder
*
* @return MockObject|MultipartStreamBuilder
*/
private function createMultipartStreamBuilder()
{
return $this->getMockBuilder(MultipartStreamBuilder::class)->disableOriginalConstructor()->getMock();
} }
} }