Skip to content
Snippets Groups Projects
Commit 5ed88714 authored by Thomas Flori's avatar Thomas Flori
Browse files

fix code quality issues

parent e58f5d28
No related branches found
No related tags found
No related merge requests found
Pipeline #739 passed with stages
in 3 minutes and 49 seconds
......@@ -36,6 +36,7 @@ quality:
stage: test
needs: []
image: iras/php-composer:8.2
allow_failure: true
cache:
key: php-dependencies-8.2
paths:
......
......@@ -4,8 +4,8 @@
"type": "library",
"license": "MIT",
"scripts": {
"code-style": "phpcs --colors --standard=PSR2 src && phpcs --colors --standard=PSR2 --ignore=example tests",
"code-quality": "phpmd src,tests ansi ruleset.xml",
"code-style": "phpcs --colors --standard=PSR2 src && phpcs --colors --standard=PSR2 tests",
"code-quality": "phpmd src ansi ruleset.xml",
"test": "phpunit --stderr",
"coverage": "phpunit --stderr --coverage-text"
},
......
......@@ -8,23 +8,13 @@
http://pmd.sf.net/ruleset_xml_schema.xsd">
<rule ref="rulesets/design.xml" />
<rule ref="rulesets/codesize.xml" />
<rule ref="rulesets/unusedcode.xml" />
<rule ref="rulesets/cleancode.xml">
<exclude name="ElseExpression" />
<exclude name="StaticAccess" />
</rule>
<rule ref="rulesets/cleancode.xml/StaticAccess">
<properties>
<property name="exceptions">
<value>
\Hamcrest\Matchers,
\Mockery
</value>
</property>
</properties>
<exclude name="BooleanArgumentFlag" />
</rule>
<rule ref="rulesets/controversial.xml">
......
......@@ -29,7 +29,7 @@ trait MessageTrait
return $this->protocol;
}
public function withProtocolVersion($version): MessageInterface
public function withProtocolVersion(string $version): MessageInterface
{
if ($this->protocol === $version) {
return $this;
......@@ -44,12 +44,12 @@ trait MessageTrait
return $this->headers;
}
public function hasHeader($header): bool
public function hasHeader(string $header): bool
{
return isset($this->headerNames[strtolower($header)]);
}
public function getHeader($header): array
public function getHeader(string $header): array
{
$header = strtolower($header);
......@@ -62,24 +62,24 @@ trait MessageTrait
return $this->headers[$header];
}
public function getHeaderLine($header): string
public function getHeaderLine(string $header): string
{
return implode(', ', $this->getHeader($header));
}
public function withHeader($header, $value): MessageInterface
public function withHeader(string $header, $value): MessageInterface
{
$new = clone $this;
return $new->setHeader($header, $value);
}
public function withAddedHeader($header, $value): MessageInterface
public function withAddedHeader(string $header, $value): MessageInterface
{
$new = clone $this;
return $new->addHeader($header, $value);
}
public function withoutHeader($header): MessageInterface
public function withoutHeader(string $header): MessageInterface
{
$normalized = strtolower($header);
......
......@@ -115,7 +115,8 @@ abstract class Request implements RequestInterface
return;
}
if (($port = $this->uri->getPort()) !== null) {
$port = $this->uri->getPort();
if ($port !== null) {
$host .= ':' . $port;
}
......
......@@ -5,14 +5,65 @@ namespace Tal;
/**
* Class Server
*
* This class is a stupid wrapper for php functions for testing.
*
* @package Tal
* @author Thomas Flori <thflori@gmail.com>
* @codeCoverageIgnore trivial
*/
class Server
{
public static function getHost()
{
if (isset($_SERVER['HTTP_HOST'])) {
$hostHeaderParts = explode(':', $_SERVER['HTTP_HOST']);
return $hostHeaderParts[0];
}
if (isset($_SERVER['SERVER_NAME'])) {
return $_SERVER['SERVER_NAME'];
}
if (isset($_SERVER['SERVER_ADDR'])) {
return $_SERVER['SERVER_ADDR'];
}
return null;
}
public static function getPort()
{
if (isset($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'], ':') !== false) {
return explode(':', $_SERVER['HTTP_HOST'])[1];
}
if (isset($_SERVER['SERVER_PORT'])) {
return $_SERVER['SERVER_PORT'];
}
return null;
}
public static function getPath()
{
if (isset($_SERVER['REQUEST_URI'])) {
$requestUriParts = explode('?', $_SERVER['REQUEST_URI']);
return $requestUriParts[0];
}
return null;
}
public static function getQuery()
{
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '?') !== false) {
return explode('?', $_SERVER['REQUEST_URI'])[1];
}
if (isset($_SERVER['QUERY_STRING'])) {
return $_SERVER['QUERY_STRING'];
}
return null;
}
/**
* Send a raw HTTP header
*
......@@ -21,6 +72,7 @@ class Server
* @param bool $replace
* @param int $responseCode
* @return void
* @codeCoverageIgnore trivial wrapper
*/
public function header($string, $replace = true, $responseCode = null)
{
......@@ -32,6 +84,7 @@ class Server
*
* @param $string
* @return void
* @codeCoverageIgnore trivial wrapper
*/
public function echo($string)
{
......
......@@ -195,38 +195,18 @@ class ServerRequest extends Request implements ServerRequestInterface
{
$uri = new Uri('');
$uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
$hasPort = false;
if (isset($_SERVER['HTTP_HOST'])) {
$hostHeaderParts = explode(':', $_SERVER['HTTP_HOST']);
$uri = $uri->withHost($hostHeaderParts[0]);
if (isset($hostHeaderParts[1])) {
$hasPort = true;
$uri = $uri->withPort($hostHeaderParts[1]);
}
} elseif (isset($_SERVER['SERVER_NAME'])) {
$uri = $uri->withHost($_SERVER['SERVER_NAME']);
} elseif (isset($_SERVER['SERVER_ADDR'])) {
$uri = $uri->withHost($_SERVER['SERVER_ADDR']);
}
$uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http')
->withHost(Server::getHost() ?? '')
->withPort(Server::getPort());
if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
$uri = $uri->withPort($_SERVER['SERVER_PORT']);
}
$hasQuery = false;
if (isset($_SERVER['REQUEST_URI'])) {
$requestUriParts = explode('?', $_SERVER['REQUEST_URI']);
$uri = $uri->withPath($requestUriParts[0]);
if (isset($requestUriParts[1])) {
$hasQuery = true;
$uri = $uri->withQuery($requestUriParts[1]);
}
$path = Server::getPath();
if ($path !== null) {
$uri = $uri->withPath($path);
}
if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
$uri = $uri->withQuery($_SERVER['QUERY_STRING']);
$query = Server::getQuery();
if ($query !== null) {
$uri = $uri->withQuery($query);
}
return $uri;
......
......@@ -2,6 +2,7 @@
namespace Tal;
use InvalidArgumentException;
use Tal\Psr7Extended\ServerResponseInterface;
class ServerResponse extends Response implements ServerResponseInterface
......@@ -109,7 +110,7 @@ class ServerResponse extends Response implements ServerResponseInterface
$sameSite = false
) {
if (preg_match('/[=,; \t\r\n\013\014]/', $name)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'Cookie names cannot contain any of the following \'=,; \t\r\n\013\014\''
);
}
......
......@@ -11,15 +11,15 @@ class RequestTest extends TestCase
{
public function testRequestUriMayBeString()
{
$r = new Request('GET', '/');
$this->assertEquals('/', (string) $r->getUri());
$request = new Request('GET', '/');
$this->assertEquals('/', (string) $request->getUri());
}
public function testRequestUriMayBeUri()
{
$uri = new Uri('/');
$r = new Request('GET', $uri);
$this->assertSame($uri, $r->getUri());
$request = new Request('GET', $uri);
$this->assertSame($uri, $request->getUri());
}
public function testValidateRequestUri()
......@@ -30,23 +30,23 @@ class RequestTest extends TestCase
public function testCanConstructWithBody()
{
$r = new Request('GET', '/', [], 'baz');
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
$this->assertEquals('baz', (string) $r->getBody());
$request = new Request('GET', '/', [], 'baz');
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $request->getBody());
$this->assertEquals('baz', (string) $request->getBody());
}
public function testNullBody()
{
$r = new Request('GET', '/', [], null);
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
$this->assertSame('', (string) $r->getBody());
$request = new Request('GET', '/', [], null);
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $request->getBody());
$this->assertSame('', (string) $request->getBody());
}
public function testFalseyBody()
{
$r = new Request('GET', '/', [], '0');
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
$this->assertSame('0', (string) $r->getBody());
$request = new Request('GET', '/', [], '0');
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $request->getBody());
$this->assertSame('0', (string) $request->getBody());
}
public function testConstructorDoesNotReadStreamBody()
......@@ -59,132 +59,132 @@ class RequestTest extends TestCase
}
]);
$r = new Request('GET', '/', [], $body);
$request = new Request('GET', '/', [], $body);
$this->assertFalse($streamIsRead);
$this->assertSame($body, $r->getBody());
$this->assertSame($body, $request->getBody());
}
public function testCapitalizesMethod()
{
$r = new Request('get', '/');
$this->assertEquals('GET', $r->getMethod());
$request = new Request('get', '/');
$this->assertEquals('GET', $request->getMethod());
}
public function testCapitalizesWithMethod()
{
$r = new Request('GET', '/');
$this->assertEquals('PUT', $r->withMethod('put')->getMethod());
$request = new Request('GET', '/');
$this->assertEquals('PUT', $request->withMethod('put')->getMethod());
}
public function testWithUri()
{
$r1 = new Request('GET', '/');
$u1 = $r1->getUri();
$u2 = new Uri('http://www.example.com');
$r2 = $r1->withUri($u2);
$this->assertNotSame($r1, $r2);
$this->assertSame($u2, $r2->getUri());
$this->assertSame($u1, $r1->getUri());
$request1 = new Request('GET', '/');
$uri1 = $request1->getUri();
$uri2 = new Uri('http://www.example.com');
$request2 = $request1->withUri($uri2);
$this->assertNotSame($request1, $request2);
$this->assertSame($uri2, $request2->getUri());
$this->assertSame($uri1, $request1->getUri());
}
public function testSameInstanceWhenSameUri()
{
$r1 = new Request('GET', 'http://foo.com');
$r2 = $r1->withUri($r1->getUri());
$this->assertSame($r1, $r2);
$request1 = new Request('GET', 'http://foo.com');
$request2 = $request1->withUri($request1->getUri());
$this->assertSame($request1, $request2);
}
public function testWithRequestTarget()
{
$r1 = new Request('GET', '/');
$r2 = $r1->withRequestTarget('*');
$this->assertEquals('*', $r2->getRequestTarget());
$this->assertEquals('/', $r1->getRequestTarget());
$request1 = new Request('GET', '/');
$request2 = $request1->withRequestTarget('*');
$this->assertEquals('*', $request2->getRequestTarget());
$this->assertEquals('/', $request1->getRequestTarget());
}
public function testRequestTargetDoesNotAllowSpaces()
{
$this->expectException(\InvalidArgumentException::class);
$r1 = new Request('GET', '/');
$r1->withRequestTarget('/foo bar');
$request1 = new Request('GET', '/');
$request1->withRequestTarget('/foo bar');
}
public function testRequestTargetDefaultsToSlash()
{
$r1 = new Request('GET', '');
$this->assertEquals('/', $r1->getRequestTarget());
$r2 = new Request('GET', '*');
$this->assertEquals('*', $r2->getRequestTarget());
$r3 = new Request('GET', 'http://foo.com/bar baz/');
$this->assertEquals('/bar%20baz/', $r3->getRequestTarget());
$request1 = new Request('GET', '');
$this->assertEquals('/', $request1->getRequestTarget());
$request2 = new Request('GET', '*');
$this->assertEquals('*', $request2->getRequestTarget());
$request3 = new Request('GET', 'http://foo.com/bar baz/');
$this->assertEquals('/bar%20baz/', $request3->getRequestTarget());
}
public function testBuildsRequestTarget()
{
$r1 = new Request('GET', 'http://foo.com/baz?bar=bam');
$this->assertEquals('/baz?bar=bam', $r1->getRequestTarget());
$request1 = new Request('GET', 'http://foo.com/baz?bar=bam');
$this->assertEquals('/baz?bar=bam', $request1->getRequestTarget());
}
public function testBuildsRequestTargetWithFalseyQuery()
{
$r1 = new Request('GET', 'http://foo.com/baz?0');
$this->assertEquals('/baz?0', $r1->getRequestTarget());
$request1 = new Request('GET', 'http://foo.com/baz?0');
$this->assertEquals('/baz?0', $request1->getRequestTarget());
}
public function testHostIsAddedFirst()
{
$r = new Request('GET', 'http://foo.com/baz?bar=bam', ['Foo' => 'Bar']);
$request = new Request('GET', 'http://foo.com/baz?bar=bam', ['Foo' => 'Bar']);
$this->assertEquals([
'Host' => ['foo.com'],
'Foo' => ['Bar']
], $r->getHeaders());
], $request->getHeaders());
}
public function testCanGetHeaderAsCsv()
{
$r = new Request('GET', 'http://foo.com/baz?bar=bam', [
$request = new Request('GET', 'http://foo.com/baz?bar=bam', [
'Foo' => ['a', 'b', 'c']
]);
$this->assertEquals('a, b, c', $r->getHeaderLine('Foo'));
$this->assertEquals('', $r->getHeaderLine('Bar'));
$this->assertEquals('a, b, c', $request->getHeaderLine('Foo'));
$this->assertEquals('', $request->getHeaderLine('Bar'));
}
public function testHostIsNotOverwrittenWhenPreservingHost()
{
$r = new Request('GET', 'http://foo.com/baz?bar=bam', ['Host' => 'a.com']);
$this->assertEquals(['Host' => ['a.com']], $r->getHeaders());
$r2 = $r->withUri(new Uri('http://www.foo.com/bar'), true);
$this->assertEquals('a.com', $r2->getHeaderLine('Host'));
$request = new Request('GET', 'http://foo.com/baz?bar=bam', ['Host' => 'a.com']);
$this->assertEquals(['Host' => ['a.com']], $request->getHeaders());
$request2 = $request->withUri(new Uri('http://www.foo.com/bar'), true);
$this->assertEquals('a.com', $request2->getHeaderLine('Host'));
}
public function testOverridesHostWithUri()
{
$r = new Request('GET', 'http://foo.com/baz?bar=bam');
$this->assertEquals(['Host' => ['foo.com']], $r->getHeaders());
$r2 = $r->withUri(new Uri('http://www.baz.com/bar'));
$this->assertEquals('www.baz.com', $r2->getHeaderLine('Host'));
$request = new Request('GET', 'http://foo.com/baz?bar=bam');
$this->assertEquals(['Host' => ['foo.com']], $request->getHeaders());
$request2 = $request->withUri(new Uri('http://www.baz.com/bar'));
$this->assertEquals('www.baz.com', $request2->getHeaderLine('Host'));
}
public function testAggregatesHeaders()
{
$r = new Request('GET', '', [
$request = new Request('GET', '', [
'ZOO' => 'zoobar',
'zoo' => ['foobar', 'zoobar']
]);
$this->assertEquals(['ZOO' => ['zoobar', 'foobar', 'zoobar']], $r->getHeaders());
$this->assertEquals('zoobar, foobar, zoobar', $r->getHeaderLine('zoo'));
$this->assertEquals(['ZOO' => ['zoobar', 'foobar', 'zoobar']], $request->getHeaders());
$this->assertEquals('zoobar, foobar, zoobar', $request->getHeaderLine('zoo'));
}
public function testAddsPortToHeader()
{
$r = new Request('GET', 'http://foo.com:8124/bar');
$this->assertEquals('foo.com:8124', $r->getHeaderLine('host'));
$request = new Request('GET', 'http://foo.com:8124/bar');
$this->assertEquals('foo.com:8124', $request->getHeaderLine('host'));
}
public function testAddsPortToHeaderAndReplacePreviousPort()
{
$r = new Request('GET', 'http://foo.com:8124/bar');
$r = $r->withUri(new Uri('http://foo.com:8125/bar'));
$this->assertEquals('foo.com:8125', $r->getHeaderLine('host'));
$request = new Request('GET', 'http://foo.com:8124/bar');
$request = $request->withUri(new Uri('http://foo.com:8125/bar'));
$this->assertEquals('foo.com:8125', $request->getHeaderLine('host'));
}
}
......@@ -10,20 +10,20 @@ class ClientResponseTest extends TestCase
{
public function testDefaultConstructor()
{
$r = new Response();
$this->assertSame(200, $r->getStatusCode());
$this->assertSame('1.1', $r->getProtocolVersion());
$this->assertSame('OK', $r->getReasonPhrase());
$this->assertSame([], $r->getHeaders());
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
$this->assertSame('', (string) $r->getBody());
$response = new Response();
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('1.1', $response->getProtocolVersion());
$this->assertSame('OK', $response->getReasonPhrase());
$this->assertSame([], $response->getHeaders());
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $response->getBody());
$this->assertSame('', (string) $response->getBody());
}
public function testCanConstructWithStatusCode()
{
$r = new Response(404);
$this->assertSame(404, $r->getStatusCode());
$this->assertSame('Not Found', $r->getReasonPhrase());
$response = new Response(404);
$this->assertSame(404, $response->getStatusCode());
$this->assertSame('Not Found', $response->getReasonPhrase());
}
public function testConstructorDoesNotReadStreamBody()
......@@ -36,215 +36,215 @@ class ClientResponseTest extends TestCase
}
]);
$r = new Response(200, [], $body);
$response = new Response(200, [], $body);
$this->assertFalse($streamIsRead);
$this->assertSame($body, $r->getBody());
$this->assertSame($body, $response->getBody());
}
public function testStatusCanBeNumericString()
{
$r = new Response('404');
$r2 = $r->withStatus('201');
$this->assertSame(404, $r->getStatusCode());
$this->assertSame('Not Found', $r->getReasonPhrase());
$this->assertSame(201, $r2->getStatusCode());
$this->assertSame('Created', $r2->getReasonPhrase());
$response = new Response('404');
$response2 = $response->withStatus('201');
$this->assertSame(404, $response->getStatusCode());
$this->assertSame('Not Found', $response->getReasonPhrase());
$this->assertSame(201, $response2->getStatusCode());
$this->assertSame('Created', $response2->getReasonPhrase());
}
public function testCanConstructWithHeaders()
{
$r = new Response(200, ['Foo' => 'Bar']);
$this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
$this->assertSame('Bar', $r->getHeaderLine('Foo'));
$this->assertSame(['Bar'], $r->getHeader('Foo'));
$response = new Response(200, ['Foo' => 'Bar']);
$this->assertSame(['Foo' => ['Bar']], $response->getHeaders());
$this->assertSame('Bar', $response->getHeaderLine('Foo'));
$this->assertSame(['Bar'], $response->getHeader('Foo'));
}
public function testCanConstructWithHeadersAsArray()
{
$r = new Response(200, [
$response = new Response(200, [
'Foo' => ['baz', 'bar']
]);
$this->assertSame(['Foo' => ['baz', 'bar']], $r->getHeaders());
$this->assertSame('baz, bar', $r->getHeaderLine('Foo'));
$this->assertSame(['baz', 'bar'], $r->getHeader('Foo'));
$this->assertSame(['Foo' => ['baz', 'bar']], $response->getHeaders());
$this->assertSame('baz, bar', $response->getHeaderLine('Foo'));
$this->assertSame(['baz', 'bar'], $response->getHeader('Foo'));
}
public function testCanConstructWithBody()
{
$r = new Response(200, [], 'baz');
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
$this->assertSame('baz', (string) $r->getBody());
$response = new Response(200, [], 'baz');
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $response->getBody());
$this->assertSame('baz', (string) $response->getBody());
}
public function testNullBody()
{
$r = new Response(200, [], null);
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
$this->assertSame('', (string) $r->getBody());
$response = new Response(200, [], null);
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $response->getBody());
$this->assertSame('', (string) $response->getBody());
}
public function testFalseyBody()
{
$r = new Response(200, [], '0');
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
$this->assertSame('0', (string) $r->getBody());
$response = new Response(200, [], '0');
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $response->getBody());
$this->assertSame('0', (string) $response->getBody());
}
public function testCanConstructWithReason()
{
$r = new Response(200, [], null, '1.1', 'bar');
$this->assertSame('bar', $r->getReasonPhrase());
$response = new Response(200, [], null, '1.1', 'bar');
$this->assertSame('bar', $response->getReasonPhrase());
$r = new Response(200, [], null, '1.1', '0');
$this->assertSame('0', $r->getReasonPhrase(), 'Falsey reason works');
$response = new Response(200, [], null, '1.1', '0');
$this->assertSame('0', $response->getReasonPhrase(), 'Falsey reason works');
}
public function testCanConstructWithProtocolVersion()
{
$r = new Response(200, [], null, '1000');
$this->assertSame('1000', $r->getProtocolVersion());
$response = new Response(200, [], null, '1000');
$this->assertSame('1000', $response->getProtocolVersion());
}
public function testWithStatusCodeAndNoReason()
{
$r = (new Response())->withStatus(201);
$this->assertSame(201, $r->getStatusCode());
$this->assertSame('Created', $r->getReasonPhrase());
$response = (new Response())->withStatus(201);
$this->assertSame(201, $response->getStatusCode());
$this->assertSame('Created', $response->getReasonPhrase());
}
public function testWithStatusCodeAndReason()
{
$r = (new Response())->withStatus(201, 'Foo');
$this->assertSame(201, $r->getStatusCode());
$this->assertSame('Foo', $r->getReasonPhrase());
$response = (new Response())->withStatus(201, 'Foo');
$this->assertSame(201, $response->getStatusCode());
$this->assertSame('Foo', $response->getReasonPhrase());
$r = (new Response())->withStatus(201, '0');
$this->assertSame(201, $r->getStatusCode());
$this->assertSame('0', $r->getReasonPhrase(), 'Falsey reason works');
$response = (new Response())->withStatus(201, '0');
$this->assertSame(201, $response->getStatusCode());
$this->assertSame('0', $response->getReasonPhrase(), 'Falsey reason works');
}
public function testWithProtocolVersion()
{
$r = (new Response())->withProtocolVersion('1000');
$this->assertSame('1000', $r->getProtocolVersion());
$response = (new Response())->withProtocolVersion('1000');
$this->assertSame('1000', $response->getProtocolVersion());
}
public function testSameInstanceWhenSameProtocol()
{
$r = new Response();
$this->assertSame($r, $r->withProtocolVersion('1.1'));
$response = new Response();
$this->assertSame($response, $response->withProtocolVersion('1.1'));
}
public function testWithBody()
{
$b = Psr7\Utils::streamFor('0');
$r = (new Response())->withBody($b);
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
$this->assertSame('0', (string) $r->getBody());
$body = Psr7\Utils::streamFor('0');
$response = (new Response())->withBody($body);
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $response->getBody());
$this->assertSame('0', (string) $response->getBody());
}
public function testSameInstanceWhenSameBody()
{
$r = new Response();
$b = $r->getBody();
$this->assertSame($r, $r->withBody($b));
$response = new Response();
$body = $response->getBody();
$this->assertSame($response, $response->withBody($body));
}
public function testWithHeader()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withHeader('baZ', 'Bam');
$this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
$this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam']], $r2->getHeaders());
$this->assertSame('Bam', $r2->getHeaderLine('baz'));
$this->assertSame(['Bam'], $r2->getHeader('baz'));
$response = new Response(200, ['Foo' => 'Bar']);
$response2 = $response->withHeader('baZ', 'Bam');
$this->assertSame(['Foo' => ['Bar']], $response->getHeaders());
$this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam']], $response2->getHeaders());
$this->assertSame('Bam', $response2->getHeaderLine('baz'));
$this->assertSame(['Bam'], $response2->getHeader('baz'));
}
public function testWithHeaderAsArray()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withHeader('baZ', ['Bam', 'Bar']);
$this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
$this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam', 'Bar']], $r2->getHeaders());
$this->assertSame('Bam, Bar', $r2->getHeaderLine('baz'));
$this->assertSame(['Bam', 'Bar'], $r2->getHeader('baz'));
$response = new Response(200, ['Foo' => 'Bar']);
$response2 = $response->withHeader('baZ', ['Bam', 'Bar']);
$this->assertSame(['Foo' => ['Bar']], $response->getHeaders());
$this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam', 'Bar']], $response2->getHeaders());
$this->assertSame('Bam, Bar', $response2->getHeaderLine('baz'));
$this->assertSame(['Bam', 'Bar'], $response2->getHeader('baz'));
}
public function testWithHeaderReplacesDifferentCase()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withHeader('foO', 'Bam');
$this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
$this->assertSame(['foO' => ['Bam']], $r2->getHeaders());
$this->assertSame('Bam', $r2->getHeaderLine('foo'));
$this->assertSame(['Bam'], $r2->getHeader('foo'));
$response = new Response(200, ['Foo' => 'Bar']);
$response2 = $response->withHeader('foO', 'Bam');
$this->assertSame(['Foo' => ['Bar']], $response->getHeaders());
$this->assertSame(['foO' => ['Bam']], $response2->getHeaders());
$this->assertSame('Bam', $response2->getHeaderLine('foo'));
$this->assertSame(['Bam'], $response2->getHeader('foo'));
}
public function testWithAddedHeader()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withAddedHeader('foO', 'Baz');
$this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
$this->assertSame(['Foo' => ['Bar', 'Baz']], $r2->getHeaders());
$this->assertSame('Bar, Baz', $r2->getHeaderLine('foo'));
$this->assertSame(['Bar', 'Baz'], $r2->getHeader('foo'));
$response = new Response(200, ['Foo' => 'Bar']);
$response2 = $response->withAddedHeader('foO', 'Baz');
$this->assertSame(['Foo' => ['Bar']], $response->getHeaders());
$this->assertSame(['Foo' => ['Bar', 'Baz']], $response2->getHeaders());
$this->assertSame('Bar, Baz', $response2->getHeaderLine('foo'));
$this->assertSame(['Bar', 'Baz'], $response2->getHeader('foo'));
}
public function testWithAddedHeaderAsArray()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withAddedHeader('foO', ['Baz', 'Bam']);
$this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
$this->assertSame(['Foo' => ['Bar', 'Baz', 'Bam']], $r2->getHeaders());
$this->assertSame('Bar, Baz, Bam', $r2->getHeaderLine('foo'));
$this->assertSame(['Bar', 'Baz', 'Bam'], $r2->getHeader('foo'));
$response = new Response(200, ['Foo' => 'Bar']);
$response2 = $response->withAddedHeader('foO', ['Baz', 'Bam']);
$this->assertSame(['Foo' => ['Bar']], $response->getHeaders());
$this->assertSame(['Foo' => ['Bar', 'Baz', 'Bam']], $response2->getHeaders());
$this->assertSame('Bar, Baz, Bam', $response2->getHeaderLine('foo'));
$this->assertSame(['Bar', 'Baz', 'Bam'], $response2->getHeader('foo'));
}
public function testWithAddedHeaderThatDoesNotExist()
{
$r = new Response(200, ['Foo' => 'Bar']);
$r2 = $r->withAddedHeader('nEw', 'Baz');
$this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
$this->assertSame(['Foo' => ['Bar'], 'nEw' => ['Baz']], $r2->getHeaders());
$this->assertSame('Baz', $r2->getHeaderLine('new'));
$this->assertSame(['Baz'], $r2->getHeader('new'));
$response = new Response(200, ['Foo' => 'Bar']);
$response2 = $response->withAddedHeader('nEw', 'Baz');
$this->assertSame(['Foo' => ['Bar']], $response->getHeaders());
$this->assertSame(['Foo' => ['Bar'], 'nEw' => ['Baz']], $response2->getHeaders());
$this->assertSame('Baz', $response2->getHeaderLine('new'));
$this->assertSame(['Baz'], $response2->getHeader('new'));
}
public function testWithoutHeaderThatExists()
{
$r = new Response(200, ['Foo' => 'Bar', 'Baz' => 'Bam']);
$r2 = $r->withoutHeader('foO');
$this->assertTrue($r->hasHeader('foo'));
$this->assertSame(['Foo' => ['Bar'], 'Baz' => ['Bam']], $r->getHeaders());
$this->assertFalse($r2->hasHeader('foo'));
$this->assertSame(['Baz' => ['Bam']], $r2->getHeaders());
$response = new Response(200, ['Foo' => 'Bar', 'Baz' => 'Bam']);
$response2 = $response->withoutHeader('foO');
$this->assertTrue($response->hasHeader('foo'));
$this->assertSame(['Foo' => ['Bar'], 'Baz' => ['Bam']], $response->getHeaders());
$this->assertFalse($response2->hasHeader('foo'));
$this->assertSame(['Baz' => ['Bam']], $response2->getHeaders());
}
public function testWithoutHeaderThatDoesNotExist()
{
$r = new Response(200, ['Baz' => 'Bam']);
$r2 = $r->withoutHeader('foO');
$this->assertSame($r, $r2);
$this->assertFalse($r2->hasHeader('foo'));
$this->assertSame(['Baz' => ['Bam']], $r2->getHeaders());
$response = new Response(200, ['Baz' => 'Bam']);
$response2 = $response->withoutHeader('foO');
$this->assertSame($response, $response2);
$this->assertFalse($response2->hasHeader('foo'));
$this->assertSame(['Baz' => ['Bam']], $response2->getHeaders());
}
public function testSameInstanceWhenRemovingMissingHeader()
{
$r = new Response();
$this->assertSame($r, $r->withoutHeader('foo'));
$response = new Response();
$this->assertSame($response, $response->withoutHeader('foo'));
}
public function testHeaderValuesAreTrimmed()
{
$r1 = new Response(200, ['OWS' => " \t \tFoo\t \t "]);
$r2 = (new Response())->withHeader('OWS', " \t \tFoo\t \t ");
$r3 = (new Response())->withAddedHeader('OWS', " \t \tFoo\t \t ");
$response1 = new Response(200, ['OWS' => " \t \tFoo\t \t "]);
$response2 = (new Response())->withHeader('OWS', " \t \tFoo\t \t ");
$response3 = (new Response())->withAddedHeader('OWS', " \t \tFoo\t \t ");
foreach ([$r1, $r2, $r3] as $r) {
$this->assertSame(['OWS' => ['Foo']], $r->getHeaders());
$this->assertSame('Foo', $r->getHeaderLine('OWS'));
$this->assertSame(['Foo'], $r->getHeader('OWS'));
foreach ([$response1, $response2, $response3] as $response) {
$this->assertSame(['OWS' => ['Foo']], $response->getHeaders());
$this->assertSame('Foo', $response->getHeaderLine('OWS'));
$this->assertSame(['Foo'], $response->getHeader('OWS'));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment