Skip to content
This repository has been archived by the owner on Nov 8, 2020. It is now read-only.

Commit

Permalink
Other response and requests
Browse files Browse the repository at this point in the history
  • Loading branch information
klapuch committed Jan 1, 2019
1 parent d0107a2 commit cda8c32
Show file tree
Hide file tree
Showing 14 changed files with 397 additions and 2 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ language: php
php:
- 7.1
- 7.2
- 7.3

before_install:
- composer self-update
Expand Down
34 changes: 34 additions & 0 deletions Core/CachedRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types = 1);

namespace Klapuch\Application;

use Klapuch\Application;
use Klapuch\Output;

final class CachedRequest implements Application\Request {
/** @var \Klapuch\Output\Format|null */
private $body;

/** @var mixed[]|null */
private $headers;

/** @var \Klapuch\Application\Request */
private $origin;

public function __construct(Application\Request $origin) {
$this->origin = $origin;
}

public function body(): Output\Format {
if ($this->body === null)
$this->body = $this->origin->body();
return $this->body;
}

public function headers(): array {
if ($this->headers === null)
$this->headers = $this->origin->headers();
return $this->headers;
}
}
43 changes: 43 additions & 0 deletions Core/CachedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types = 1);

namespace Klapuch\Application;

use Klapuch\Application;
use Klapuch\Output;

final class CachedResponse implements Application\Response {
/** @var \Klapuch\Output\Format|null */
private $body;

/** @var mixed[]|null */
private $headers;

/** @var int|null */
private $status;

/** @var \Klapuch\Application\Response */
private $origin;

public function __construct(Application\Response $origin) {
$this->origin = $origin;
}

public function body(): Output\Format {
if ($this->body === null)
$this->body = $this->origin->body();
return $this->body;
}

public function headers(): array {
if ($this->headers === null)
$this->headers = $this->origin->headers();
return $this->headers;
}

public function status(): int {
if ($this->status === null)
$this->status = $this->origin->status();
return $this->status;
}
}
24 changes: 24 additions & 0 deletions Core/EmptyResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types = 1);

namespace Klapuch\Application;

use Klapuch\Application;
use Klapuch\Output;

final class EmptyResponse implements Application\Response {
public function body(): Output\Format {
return new Output\EmptyFormat();
}

public function headers(): array {
return [
'Content-Type' => 'text/plain',
'Content-Length' => 0,
];
}

public function status(): int {
return 204;
}
}
36 changes: 36 additions & 0 deletions Core/FriendlyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types = 1);

namespace Klapuch\Application;

use Klapuch\Application;
use Klapuch\Output;

final class FriendlyRequest implements Application\Request {
/** @var \Klapuch\Application\Request */
private $origin;

/** @var string */
private $message;

public function __construct(Application\Request $origin, string $message) {
$this->origin = $origin;
$this->message = $message;
}

/**
* @throws \UnexpectedValueException
* @return \Klapuch\Output\Format
*/
public function body(): Output\Format {
try {
return $this->origin->body();
} catch (\UnexpectedValueException $e) {
throw new \UnexpectedValueException($this->message, $e->getCode(), $e);
}
}

public function headers(): array {
return $this->origin->headers();
}
}
33 changes: 33 additions & 0 deletions Core/JsonResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types = 1);

namespace Klapuch\Application;

use Klapuch\Application;
use Klapuch\Output;

/**
* JSON response
*/
final class JsonResponse implements Application\Response {
private const HEADERS = ['Content-Type' => 'application/json; charset=utf8'];

/** @var \Klapuch\Application\Response */
private $origin;

public function __construct(Application\Response $origin) {
$this->origin = $origin;
}

public function body(): Output\Format {
return $this->origin->body();
}

public function headers(): array {
return self::HEADERS + $this->origin->headers();
}

public function status(): int {
return $this->origin->status();
}
}
42 changes: 42 additions & 0 deletions Core/PlainResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types = 1);

namespace Klapuch\Application;

use Klapuch\Application;
use Klapuch\Output;

final class PlainResponse implements Application\Response {
private const OK = 200;

/** @var \Klapuch\Output\Format */
private $format;

/** @var mixed[] */
private $headers;

/** @var int */
private $status;

public function __construct(
Output\Format $format,
array $headers = [],
int $status = self::OK
) {
$this->format = $format;
$this->headers = $headers;
$this->status = $status;
}

public function body(): Output\Format {
return $this->format;
}

public function headers(): array {
return $this->headers;
}

public function status(): int {
return $this->status;
}
}
15 changes: 15 additions & 0 deletions Tests/TestCase/Mockery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types = 1);

namespace Klapuch\Application\TestCase;

trait Mockery {
protected function mock(string $class): \Mockery\MockInterface {
return \Mockery::mock($class);
}

protected function tearDown(): void {
parent::tearDown();
\Mockery::close();
}
}
30 changes: 30 additions & 0 deletions Tests/Unit/CachedRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types = 1);
/**
* @testCase
* @phpVersion > 7.1
*/
namespace Klapuch\Application\Unit;

use Klapuch\Application;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

/**
* @testCase
*/
final class CachedRequestTest extends \Tester\TestCase {
use Application\TestCase\Mockery;

public function testMultipleCallsWithSingleExecution(): void {
$origin = $this->mock(Application\Request::class);
$origin->shouldReceive('body')->once();
$origin->shouldReceive('headers')->once();
$response = new Application\CachedRequest($origin);
Assert::equal($response->body(), $response->body());
Assert::equal($response->headers(), $response->headers());
}
}

(new CachedRequestTest())->run();
32 changes: 32 additions & 0 deletions Tests/Unit/CachedResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types = 1);
/**
* @testCase
* @phpVersion > 7.1
*/
namespace Klapuch\Application\Unit;

use Klapuch\Application;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

/**
* @testCase
*/
final class CachedResponseTest extends \Tester\TestCase {
use Application\TestCase\Mockery;

public function testMultipleCallsWithSingleExecution(): void {
$origin = $this->mock(Application\Response::class);
$origin->shouldReceive('body')->once();
$origin->shouldReceive('headers')->once();
$origin->shouldReceive('status')->once();
$response = new Application\CachedResponse($origin);
Assert::equal($response->body(), $response->body());
Assert::equal($response->headers(), $response->headers());
Assert::equal($response->status(), $response->status());
}
}

(new CachedResponseTest())->run();
23 changes: 23 additions & 0 deletions Tests/Unit/EmptyResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types = 1);
/**
* @testCase
* @phpVersion > 7.1
*/
namespace Klapuch\Application\Unit;

use Klapuch\Application;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

/**
* @testCase
*/
final class EmptyResponseTest extends \Tester\TestCase {
public function testNoResponseCode(): void {
Assert::same(204, (new Application\EmptyResponse())->status());
}
}

(new EmptyResponseTest())->run();
39 changes: 39 additions & 0 deletions Tests/Unit/FriendlyRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types = 1);
/**
* @testCase
* @phpVersion > 7.1
*/
namespace Klapuch\Application\Unit;

use Klapuch\Application;
use Klapuch\Output;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

/**
* @testCase
*/
final class FriendlyRequestTest extends \Tester\TestCase {
public function testRethrowingWithSameCodeAndChainedPrevious(): void {
$e = Assert::exception(static function () {
(new Application\FriendlyRequest(
new class implements Application\Request {
public function body(): Output\Format {
throw new \UnexpectedValueException('Foo', 402);
}

public function headers(): array {
}
},
'This is cool'
))->body()->serialization();
}, \UnexpectedValueException::class, 'This is cool', 402);
Assert::type(\UnexpectedValueException::class, $e->getPrevious());
Assert::same('Foo', $e->getPrevious()->getMessage());
Assert::same(402, $e->getPrevious()->getCode());
}
}

(new FriendlyRequestTest())->run();

0 comments on commit cda8c32

Please sign in to comment.