2016-11-23 23:55:05 +03:00
|
|
|
<?php
|
|
|
|
|
2016-11-24 01:02:12 +03:00
|
|
|
/*
|
2017-11-22 11:37:04 +03:00
|
|
|
* Copyright (C) 2013 Mailgun
|
2016-11-24 01:02:12 +03:00
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
2016-12-06 21:12:52 +03:00
|
|
|
* of the MIT license. See the LICENSE file for details.
|
2016-11-24 01:02:12 +03:00
|
|
|
*/
|
|
|
|
|
2019-01-09 22:18:58 +03:00
|
|
|
namespace Mailgun\Tests\HttpClient;
|
2016-11-23 23:55:05 +03:00
|
|
|
|
2017-05-22 14:47:11 +03:00
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder;
|
|
|
|
use Http\Message\RequestFactory;
|
2019-01-09 22:18:58 +03:00
|
|
|
use Mailgun\HttpClient\RequestBuilder;
|
|
|
|
use Mailgun\Tests\MailgunTestCase;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-05-22 14:47:11 +03:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
use Psr\Http\Message\StreamInterface;
|
2016-11-23 23:55:05 +03:00
|
|
|
|
2019-01-09 22:18:58 +03:00
|
|
|
class RequestBuilderTest extends MailgunTestCase
|
2016-11-23 23:55:05 +03:00
|
|
|
{
|
2017-05-22 14:47:11 +03:00
|
|
|
/**
|
|
|
|
* @var MockObject|RequestFactory
|
|
|
|
*/
|
|
|
|
private $requestFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RequestBuilder
|
|
|
|
*/
|
|
|
|
private $requestBuilder;
|
|
|
|
|
|
|
|
/**
|
2017-11-22 11:37:04 +03:00
|
|
|
* Environment preset.
|
2017-05-22 14:47:11 +03:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-22 11:37:04 +03:00
|
|
|
* Environment reset.
|
2017-05-22 14:47:11 +03:00
|
|
|
*/
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
$this->requestFactory = null;
|
|
|
|
$this->requestBuilder = null;
|
|
|
|
}
|
|
|
|
|
2016-11-23 23:55:05 +03:00
|
|
|
public function testCreateSimpleStream()
|
|
|
|
{
|
2017-05-22 14:47:11 +03:00
|
|
|
$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('application/json', $headers['Content-Type']);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}),
|
|
|
|
$this->equalTo('content')
|
|
|
|
)
|
2019-01-09 22:18:58 +03:00
|
|
|
->willReturn($request = $this->getMockBuilder(RequestInterface::class)->getMock());
|
2016-11-23 23:55:05 +03:00
|
|
|
|
2017-05-22 14:47:11 +03:00
|
|
|
$result = $this->requestBuilder
|
|
|
|
->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], 'content');
|
2016-11-23 23:55:05 +03:00
|
|
|
|
2017-05-22 14:47:11 +03:00
|
|
|
$this->assertSame($request, $result);
|
2016-11-23 23:55:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateMultipartStream()
|
|
|
|
{
|
2017-05-22 14:47:11 +03:00
|
|
|
$multipartStreamBuilder = $this->createMultipartStreamBuilder();
|
2016-11-23 23:55:05 +03:00
|
|
|
|
2017-05-22 14:47:11 +03:00
|
|
|
$item0 = ['content' => 'foobar', 'name' => 'username', 'some_stuff' => 'some value'];
|
|
|
|
$item1 = ['content' => 'Stockholm', 'name' => 'city', 'other_stuff' => 'other value'];
|
2017-11-22 11:37:04 +03:00
|
|
|
$body = [$item0, $item1];
|
2016-11-23 23:55:05 +03:00
|
|
|
|
2017-05-22 14:47:11 +03:00
|
|
|
foreach ($body as $index => $item) {
|
|
|
|
$multipartStreamBuilder
|
|
|
|
->expects($this->at($index))
|
|
|
|
->method('addResource')
|
|
|
|
->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')
|
2019-01-09 22:18:58 +03:00
|
|
|
->willReturn($stream = $this->getMockBuilder(StreamInterface::class)->getMock());
|
2017-05-22 14:47:11 +03:00
|
|
|
|
|
|
|
$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)
|
|
|
|
)
|
2019-01-09 22:18:58 +03:00
|
|
|
->willReturn($request = $this->getMockBuilder(RequestInterface::class)->getMock());
|
2017-05-22 14:47:11 +03:00
|
|
|
|
|
|
|
$this->requestBuilder->setMultipartStreamBuilder($multipartStreamBuilder);
|
|
|
|
$result = $this->requestBuilder
|
|
|
|
->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], [$item0, $item1]);
|
|
|
|
|
|
|
|
$this->assertSame($request, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-22 11:37:04 +03:00
|
|
|
* Creates multipart stream builder.
|
2017-05-22 14:47:11 +03:00
|
|
|
*
|
|
|
|
* @return MockObject|MultipartStreamBuilder
|
|
|
|
*/
|
|
|
|
private function createMultipartStreamBuilder()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder(MultipartStreamBuilder::class)->disableOriginalConstructor()->getMock();
|
2016-11-23 23:55:05 +03:00
|
|
|
}
|
|
|
|
}
|