Skip to content

Commit a0b9865

Browse files
committed
Replaceable
1 parent c4fa279 commit a0b9865

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Some shared debug stuff for the Peso Framework.
1414
## Services
1515

1616
* BlackHoleService. Accepts all valid requests and returns the corresponding "not found" error
17+
* ReplaceableService. A mutable wrapper that allows switching the underlying service object

infection.json5.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
$schema: "https://raw.githubusercontent.com/infection/infection/0.32.0/resources/schema.json",
3+
source: {
4+
directories: [
5+
"src",
6+
],
7+
},
8+
logs: {
9+
text: "reports/infection.log",
10+
html: "reports/infection.html",
11+
},
12+
mutators: {
13+
"@default": true,
14+
},
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Peso\Debug\Services;
6+
7+
use Peso\Core\Responses\ConversionResponse;
8+
use Peso\Core\Responses\ErrorResponse;
9+
use Peso\Core\Responses\ExchangeRateResponse;
10+
use Peso\Core\Services\PesoServiceInterface;
11+
12+
final class ReplaceableService implements PesoServiceInterface
13+
{
14+
public function __construct(
15+
public PesoServiceInterface $service,
16+
) {
17+
}
18+
19+
public function send(object $request): ExchangeRateResponse|ConversionResponse|ErrorResponse
20+
{
21+
return $this->service->send($request);
22+
}
23+
24+
public function supports(object $request): bool
25+
{
26+
return $this->service->supports($request);
27+
}
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Peso\Debug\Tests\Services;
6+
7+
use Peso\Core\Exceptions\ExchangeRateNotFoundException;
8+
use Peso\Core\Exceptions\RequestNotSupportedException;
9+
use Peso\Core\Requests\CurrentExchangeRateRequest;
10+
use Peso\Core\Responses\ErrorResponse;
11+
use Peso\Core\Services\NullService;
12+
use Peso\Debug\Services\BlackHoleService;
13+
use Peso\Debug\Services\ReplaceableService;
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\TestCase;
16+
17+
#[CoversClass(ReplaceableService::class)]
18+
final class ReplaceableServiceTest extends TestCase
19+
{
20+
public function testSend(): void
21+
{
22+
$service = new ReplaceableService(new BlackHoleService());
23+
24+
$response = $service->send(new CurrentExchangeRateRequest('EUR', 'PHP'));
25+
self::assertInstanceOf(ErrorResponse::class, $response);
26+
self::assertInstanceOf(ExchangeRateNotFoundException::class, $response->exception);
27+
28+
$service->service = new NullService();
29+
30+
$response = $service->send(new CurrentExchangeRateRequest('EUR', 'PHP'));
31+
self::assertInstanceOf(ErrorResponse::class, $response);
32+
self::assertInstanceOf(RequestNotSupportedException::class, $response->exception);
33+
}
34+
35+
public function testSupports(): void
36+
{
37+
$service = new ReplaceableService(new BlackHoleService());
38+
39+
self::assertTrue($service->supports(new CurrentExchangeRateRequest('EUR', 'PHP')));
40+
41+
$service->service = new NullService();
42+
43+
self::assertFalse($service->supports(new CurrentExchangeRateRequest('EUR', 'PHP')));
44+
}
45+
}

0 commit comments

Comments
 (0)