Skip to content

Commit

Permalink
some bug fixed. add some test class
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 29, 2018
1 parent 5092631 commit d2a3819
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/Component/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function get(string $name, $default = null)
public function add($name, $value)
{
if (isset($this[$name])) {
return null;
return $this;
}

$this[$name] = $value;
Expand All @@ -85,7 +85,9 @@ public function add($name, $value)
*/
public function set($name, $value)
{
return $this[$name] = $value;
$this[$name] = $value;

return $this;
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ public function set($name, $value): self
return $this;
}

/**
* @param string $name
* @param mixed $value
* @return self
*/
public function add($name, $value): self
{
if (!\is_array($value)) {
$value = ['value' => (string)$value];
}

return parent::add($name, \array_replace($this->defaults, $value));
}

/**
* Convert to `Set-Cookie` headers
* @return string[]
Expand All @@ -72,7 +86,7 @@ public function toHeaders(): array
{
$headers = [];
foreach ($this as $name => $properties) {
$headers[] = $this->toHeader($name, $properties);
$headers[] = $this->toHeaderLine($name, $properties);
}

return $headers;
Expand All @@ -84,7 +98,7 @@ public function toHeaders(): array
* @param array $properties Cookie properties
* @return string
*/
protected function toHeader(string $name, array $properties): string
protected function toHeaderLine(string $name, array $properties): string
{
$result = \urlencode($name) . '=' . \urlencode($properties['value']);

Expand Down
45 changes: 24 additions & 21 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ public function add($key, $value)
return $this;
}

return parent::add($this->normalizeKey($key), $value);
return parent::add($this->normalizeKey($key), [
'value' => (array)$value,
'originalKey' => $key
]);
}

/**
Expand All @@ -136,12 +139,12 @@ public function remove($key)
* @param string $key
* @return bool|string
*/
public function normalizeKey($key)
public function normalizeKey(string $key)
{
$key = str_replace('_', '-', strtolower($key));
$key = \str_replace('_', '-', \strtolower($key));

if (strpos($key, 'Http-') === 0) {
$key = substr($key, 5);
if (\strpos($key, 'Http-') === 0) {
$key = \substr($key, 5);
}

return $key;
Expand All @@ -157,12 +160,12 @@ public function getAcceptLanguages(): array
$ls = [];

if ($value = $this->getLine('Accept-Language')) {
if (strpos($value, ';')) {
list($value,) = explode(';', $value, 2);
if (\strpos($value, ';')) {
list($value,) = \explode(';', $value, 2);
}

$value = str_replace(' ', '', $value);
$ls = explode(',', $value);
$value = \str_replace(' ', '', $value);
$ls = \explode(',', $value);
}

return $ls;
Expand All @@ -178,32 +181,32 @@ public function getAcceptEncodes(): array
$ens = [];

if ($value = $this->getLine('Accept-Encoding')) {
if (strpos($value, ';')) {
list($value,) = explode(';', $value, 2);
if (\strpos($value, ';')) {
list($value,) = \explode(';', $value, 2);
}

$value = str_replace(' ', '', $value);
$ens = explode(',', $value);
$value = \str_replace(' ', '', $value);
$ens = \explode(',', $value);
}

return $ens;
}

/**
* @param bool $toString
* @return array
* @param bool $join to String
* @return array|string
*/
public function toHeaderLines(bool $toString = false): array
public function toHeaderLines(bool $join = false)
{
$output = [];

foreach ($this as $name => $info) {
$name = ucwords($name, '-');
$value = implode(',', $info['value']);
$name = \ucwords($name, '-');
$value = \implode(',', $info['value']);
$output[] = "$name: $value\r\n";
}

return $toString ? implode('', $output) : $output;
return $join ? \implode('', $output) : $output;
}

/**
Expand All @@ -214,8 +217,8 @@ public function getLines(): array
$output = [];

foreach ($this as $name => $info) {
$name = ucwords($name, '-');
$output[$name] = implode(',', $info['value']);
$name = \ucwords($name, '-');
$output[$name] = \implode(',', $info['value']);
}

return $output;
Expand Down
4 changes: 4 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ public function getStatusCode(): int
*/
public function getReasonPhrase(): string
{
if ($this->reasonPhrase === null && ($code = $this->status)) {
$this->reasonPhrase = self::$messages[$code] ?? '';
}

return $this->reasonPhrase;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public function withAttribute($name, $value)
* @param array $attributes
* @return ServerRequest
*/
public function withAttributes(array $attributes): ServerRequest
public function withAttributes(array $attributes): self
{
$clone = clone $this;
$clone->attributes = new Collection($attributes);
Expand Down
7 changes: 6 additions & 1 deletion src/Traits/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ public function isMethod($method): bool
return $this->getMethod() === strtoupper($method);
}

public function withMethod($method): RequestTrait
/**
* @param $method
* @return RequestTrait
* @throws \InvalidArgumentException
*/
public function withMethod($method): self
{
$method = (string)$this->filterMethod($method);

Expand Down
29 changes: 29 additions & 0 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: Inhere
* Date: 2018/3/28 0028
* Time: 22:05
*/

namespace Inhere\Http\Test;

use Inhere\Http\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;

/**
* Class ResponseTest
* @package Inhere\Http\Test
*/
class ResponseTest extends TestCase
{
public function testClass()
{
$res = new Response();

$this->assertInstanceOf(ResponseInterface::class, $res);
$this->assertSame(200, $res->getStatusCode());
$this->assertInternalType('string', $res->getReasonPhrase());
}
}
32 changes: 32 additions & 0 deletions test/ServerRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: Inhere
* Date: 2018/3/28 0028
* Time: 22:05
*/

namespace Inhere\Http\Test;

use Inhere\Http\ServerRequest;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

/**
* Class RequestTest
* @package Inhere\Http\Test
*/
class ServerRequestTest extends TestCase
{
public function testClass()
{
$req = new ServerRequest();

$this->assertInstanceOf(RequestInterface::class, $req);

$req = $req->withAttribute('key', 'value');

$this->assertInstanceOf(RequestInterface::class, $req);
$this->assertSame('value', $req->getAttribute('key'));
}
}

0 comments on commit d2a3819

Please sign in to comment.