This repository has been archived by the owner on Nov 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
397 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ language: php | |
php: | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
|
||
before_install: | ||
- composer self-update | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.