Skip to content

Commit

Permalink
[HttpServer] WIP: Add TraditionalServerRequestBuilderTest
Browse files Browse the repository at this point in the history
  • Loading branch information
il-masaru-yamagishi committed Aug 28, 2024
1 parent 96541e5 commit a4f1365
Show file tree
Hide file tree
Showing 11 changed files with 885 additions and 170 deletions.
4 changes: 2 additions & 2 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="friendsofphp/php-cs-fixer" version="^3.62.0" installed="3.62.0" location="./tools/php-cs-fixer" copy="false"/>
<phar name="phpstan/phpstan" version="^1.11.10" installed="1.11.10" location="./tools/phpstan" copy="false"/>
<phar name="friendsofphp/php-cs-fixer" version="^3.62.0" installed="3.63.1" location="./tools/php-cs-fixer" copy="false"/>
<phar name="phpstan/phpstan" version="^1.11.10" installed="1.12.0" location="./tools/phpstan" copy="false"/>
<phar name="vimeo/psalm" version="^5.25.0" installed="5.25.0" location="./tools/psalm" copy="false"/>
</phive>
3 changes: 2 additions & 1 deletion src/HttpMessage/HasMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
trait HasMethod
{
protected string $method = 'GET';
protected string $method = 'GET'; // default

public function getMethod(): string
{
Expand All @@ -27,6 +27,7 @@ public function getMethod(): string
public function withMethod(string $method): RequestInterface
{
$new_instance = clone $this;
// This should not modify the case of the method
$new_instance->method = $method;

return $new_instance;
Expand Down
10 changes: 9 additions & 1 deletion src/HttpMessage/HasProtocolVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
trait HasProtocolVersion
{
protected string $protocol_version = '1.1';
protected string $protocol_version = '1.1'; // default

/**
* Get protocol version(e.g. HTTP/1.1 returns "1.1")
Expand All @@ -32,9 +32,17 @@ public function getProtocolVersion(): string
*/
public function withProtocolVersion(string $version): static
{
$this->validateProtocolVersion($version);
$new_instance = clone $this;
$new_instance->protocol_version = $version;

return $new_instance;
}

protected function validateProtocolVersion(string $version): void
{
if (!preg_match('/^\d\.\d$/', $version)) {
throw new \InvalidArgumentException(\sprintf('Invalid protocol version, "%s" given', $version));
}
}
}
23 changes: 15 additions & 8 deletions src/HttpMessage/HasUploadedFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
*/
trait HasUploadedFiles
{
/** @var array<array-key, UploadedFileInterface> $uploaded_files */
/** @var array<string, mixed> $uploaded_files */
protected array $uploaded_files = [];

/**
* Get uploaded files
* @return array<array-key, UploadedFileInterface>
* @return array<array-key, mixed>
*/
public function getUploadedFiles(): array
{
Expand All @@ -41,13 +41,20 @@ public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
{
$new_instance = clone $this;
$new_instance->uploaded_files = [];
foreach ($uploadedFiles as $key => $uploaded_file) {
if (!$uploaded_file instanceof UploadedFileInterface) {
throw new InvalidArgumentException('Invalid uploaded file');
}
$new_instance->uploaded_files[$key] = $uploaded_file;
}
$this->validateUploadedFiles($uploadedFiles);
$new_instance->uploaded_files = $uploadedFiles;

return $new_instance;
}

protected function validateUploadedFiles(mixed $uploaded_files): void
{
if (\is_array($uploaded_files)) {
foreach ($uploaded_files as $uploaded_file) {
$this->validateUploadedFiles($uploaded_file);
}
} elseif ($uploaded_files instanceof UploadedFileInterface === false) {
throw new InvalidArgumentException('Invalid uploaded file');
}
}
}
1 change: 1 addition & 0 deletions src/HttpMessage/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
// case-sensitive
$this->method = $method;
$this->uri = $uri instanceof UriInterface ? $uri : new Uri($uri);
$this->validateProtocolVersion($protocol_version);
$this->protocol_version = $protocol_version;
if ($body !== null && $body !== '') {
$this->body = new Stream($body);
Expand Down
13 changes: 4 additions & 9 deletions src/HttpMessage/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;

/**
Expand All @@ -34,9 +33,9 @@ class ServerRequest extends Request implements ServerRequestInterface
* @param StreamInterface|resource|string|null $body
* @param string $protocol_version
* @param array<string, mixed> $server_params
* @param array<string, string> $cookie_params
* @param array<string, mixed> $cookie_params
* @param array<array-key, mixed> $query_params
* @param array<string, UploadedFileInterface> $uploaded_files
* @param array<string, mixed> $uploaded_files
* @param array<array-key, mixed>|object|null $parsed_body
* @param array<array-key, mixed> $attributes
* @throws InvalidArgumentException
Expand Down Expand Up @@ -74,12 +73,8 @@ public function __construct(
$this->query_params[$name] = $value;
}
}
foreach ($uploaded_files as $name => $uploaded_file) {
if (!$uploaded_file instanceof UploadedFileInterface) {
throw new InvalidArgumentException('Invalid uploaded file');
}
$this->uploaded_files[$name] = $uploaded_file;
}
$this->validateUploadedFiles($uploaded_files);
$this->uploaded_files = $uploaded_files;
$this->parsed_body = $parsed_body;
$this->attributes = $attributes;
}
Expand Down
11 changes: 11 additions & 0 deletions src/HttpMessage/Tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public function testProtocolVersion(): void
self::assertSame('2.0', $message2->getProtocolVersion());
}

#[Test]
public function testInvalidProtocolVersion(): void
{
$message = new class extends Message {};

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid protocol version, "2" given');

$message->withProtocolVersion('2');
}

#[Test]
public function testBody(): void
{
Expand Down
1 change: 0 additions & 1 deletion src/HttpMessage/Tests/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public function testInvalidUploadedFile(): void

$this->expectExceptionMessage('Invalid uploaded file');

// @phpstan-ignore argument.type
new ServerRequest(method: 'GET', uploaded_files: ['a.txt' => $file]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/HttpServer/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* @author Masaru Yamagishi <[email protected]>
* @license Apache-2.0
* @codeCoverageIgnoreFile because all of methods are wrapper of PHP built-in functions
*/

namespace Rayleigh\HttpServer;
Expand All @@ -15,6 +14,7 @@
/**
* Header and body emitter function wrapper
* @package Rayleigh\HttpServer
* @codeCoverageIgnore because all of methods are wrapper of PHP built-in functions
*/
/* final readonly */ class Emitter
{
Expand Down
Loading

0 comments on commit a4f1365

Please sign in to comment.