2018-08-11 15:05:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Http;
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2018-08-16 18:13:53 +02:00
|
|
|
use Engelsystem\Test\Unit\Http\Stub\MessageTraitResponseImplementation;
|
2019-04-17 10:09:50 +02:00
|
|
|
use Nyholm\Psr7\Stream;
|
2018-08-11 15:05:55 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\MessageInterface;
|
|
|
|
use Psr\Http\Message\StreamInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
|
|
2018-08-16 18:13:53 +02:00
|
|
|
class MessageTraitResponseTest extends TestCase
|
2018-08-11 15:05:55 +02:00
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
use ArraySubsetAsserts;
|
|
|
|
|
2018-08-11 15:05:55 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait
|
|
|
|
*/
|
|
|
|
public function testCreate()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$this->assertInstanceOf(MessageInterface::class, $message);
|
|
|
|
$this->assertInstanceOf(SymfonyResponse::class, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::getProtocolVersion
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::withProtocolVersion
|
|
|
|
*/
|
|
|
|
public function testGetProtocolVersion()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$newMessage = $message->withProtocolVersion('0.1');
|
|
|
|
$this->assertNotEquals($message, $newMessage);
|
|
|
|
$this->assertEquals('0.1', $newMessage->getProtocolVersion());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::getHeaders
|
|
|
|
*/
|
|
|
|
public function testGetHeaders()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$newMessage = $message->withHeader('Foo', 'bar');
|
|
|
|
|
|
|
|
$this->assertNotEquals($message, $newMessage);
|
|
|
|
$this->assertArraySubset(['Foo' => ['bar']], $newMessage->getHeaders());
|
|
|
|
|
|
|
|
$newMessage = $message->withHeader('lorem', ['ipsum', 'dolor']);
|
|
|
|
$this->assertArraySubset(['lorem' => ['ipsum', 'dolor']], $newMessage->getHeaders());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::hasHeader
|
|
|
|
*/
|
|
|
|
public function testHasHeader()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$this->assertFalse($message->hasHeader('test'));
|
|
|
|
|
|
|
|
$newMessage = $message->withHeader('test', '12345');
|
|
|
|
$this->assertTrue($newMessage->hasHeader('Test'));
|
|
|
|
$this->assertTrue($newMessage->hasHeader('test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::getHeader
|
|
|
|
*/
|
|
|
|
public function testGetHeader()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$newMessage = $message->withHeader('foo', 'bar');
|
|
|
|
|
|
|
|
$this->assertEquals(['bar'], $newMessage->getHeader('Foo'));
|
|
|
|
$this->assertEquals([], $newMessage->getHeader('LoremIpsum'));
|
2019-11-22 11:15:27 +01:00
|
|
|
|
|
|
|
$newMessage = $message
|
|
|
|
->withHeader('foo', 'bar')
|
|
|
|
->withAddedHeader('foo', 'batz');
|
|
|
|
$this->assertEquals(['bar', 'batz'], $newMessage->getHeader('foo'));
|
2018-08-11 15:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::getHeaderLine
|
|
|
|
*/
|
|
|
|
public function testGetHeaderLine()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$newMessage = $message->withHeader('foo', ['bar', 'bla']);
|
|
|
|
|
|
|
|
$this->assertEquals('', $newMessage->getHeaderLine('Lorem-Ipsum'));
|
|
|
|
$this->assertEquals('bar,bla', $newMessage->getHeaderLine('Foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::withHeader
|
|
|
|
*/
|
|
|
|
public function testWithHeader()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$newMessage = $message->withHeader('foo', 'bar');
|
|
|
|
|
|
|
|
$this->assertNotEquals($message, $newMessage);
|
|
|
|
$this->assertArraySubset(['foo' => ['bar']], $newMessage->getHeaders());
|
|
|
|
|
|
|
|
$newMessage = $newMessage->withHeader('Foo', ['lorem', 'ipsum']);
|
|
|
|
$this->assertArraySubset(['Foo' => ['lorem', 'ipsum']], $newMessage->getHeaders());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::withAddedHeader
|
|
|
|
*/
|
|
|
|
public function testWithAddedHeader()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$newMessage = $message->withHeader('foo', 'bar');
|
|
|
|
|
|
|
|
$this->assertNotEquals($message, $newMessage);
|
|
|
|
$this->assertArraySubset(['foo' => ['bar']], $newMessage->getHeaders());
|
|
|
|
|
|
|
|
$newMessage = $newMessage->withAddedHeader('Foo', ['lorem', 'ipsum']);
|
|
|
|
$this->assertArraySubset(['Foo' => ['bar', 'lorem', 'ipsum']], $newMessage->getHeaders());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::withoutHeader
|
|
|
|
*/
|
|
|
|
public function testWithoutHeader()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = (new MessageTraitResponseImplementation())->withHeader('foo', 'bar');
|
2018-08-11 15:05:55 +02:00
|
|
|
$this->assertTrue($message->hasHeader('foo'));
|
|
|
|
|
|
|
|
$newMessage = $message->withoutHeader('Foo');
|
|
|
|
$this->assertNotEquals($message, $newMessage);
|
|
|
|
$this->assertFalse($newMessage->hasHeader('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::getBody
|
|
|
|
*/
|
|
|
|
public function testGetBody()
|
|
|
|
{
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = (new MessageTraitResponseImplementation())->setContent('Foo bar!');
|
2018-08-11 15:05:55 +02:00
|
|
|
$body = $message->getBody();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(StreamInterface::class, $body);
|
|
|
|
$this->assertEquals('Foo bar!', $body->getContents());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\MessageTrait::withBody
|
|
|
|
*/
|
|
|
|
public function testWithBody()
|
|
|
|
{
|
2019-04-17 10:09:50 +02:00
|
|
|
$stream = Stream::create('Test content');
|
2018-08-16 18:13:53 +02:00
|
|
|
$message = new MessageTraitResponseImplementation();
|
2018-08-11 15:05:55 +02:00
|
|
|
$newMessage = $message->withBody($stream);
|
|
|
|
|
|
|
|
$this->assertNotEquals($message, $newMessage);
|
|
|
|
$this->assertEquals('Test content', $newMessage->getContent());
|
|
|
|
}
|
|
|
|
}
|