Skip to content

Commit 28a1bb3

Browse files
committed
refactor: replace TestRPC with RpcSpy for improved RPC call testing
1 parent 2e7bebc commit 28a1bb3

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed

tests/Unit/RpcLoggerTest.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@
1111
use Psr\Log\LogLevel as PsrLogLevel;
1212
use RoadRunner\Logger\Logger as AppLogger;
1313
use RoadRunner\PsrLogger\RpcLogger;
14-
use Spiral\Goridge\RPC\CodecInterface;
15-
use Spiral\Goridge\RPC\RPCInterface;
1614

1715
#[CoversClass(RpcLogger::class)]
1816
class RpcLoggerTest extends TestCase
1917
{
20-
private TestRPC $rpc;
18+
private RpcSpy $rpc;
2119
private AppLogger $appLogger;
2220
private RpcLogger $rpcLogger;
2321

2422
protected function setUp(): void
2523
{
26-
$this->rpc = new TestRPC();
24+
$this->rpc = new RpcSpy();
2725
$this->appLogger = new AppLogger($this->rpc);
2826
$this->rpcLogger = new RpcLogger($this->appLogger);
2927
}
@@ -344,42 +342,3 @@ public static function infoLevelsProvider(): array
344342
];
345343
}
346344
}
347-
348-
/**
349-
* Test spy for capturing RPC calls
350-
*/
351-
class TestRPC implements RPCInterface
352-
{
353-
public array $calls = [];
354-
355-
public function call(string $method, mixed $payload, mixed $options = null): mixed
356-
{
357-
$this->calls[] = ['method' => $method, 'payload' => $payload, 'options' => $options];
358-
return null;
359-
}
360-
361-
public function withCodec(CodecInterface $codec): RPCInterface
362-
{
363-
return $this;
364-
}
365-
366-
public function withServicePrefix(string $service): RPCInterface
367-
{
368-
return $this;
369-
}
370-
371-
public function getLastCall(): ?array
372-
{
373-
return end($this->calls) ?: null;
374-
}
375-
376-
public function getCallCount(): int
377-
{
378-
return count($this->calls);
379-
}
380-
381-
public function reset(): void
382-
{
383-
$this->calls = [];
384-
}
385-
}

tests/Unit/RpcSpy.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RoadRunner\PsrLogger\Tests\Unit;
6+
7+
use Spiral\Goridge\RPC\CodecInterface;
8+
use Spiral\Goridge\RPC\RPCInterface;
9+
10+
/**
11+
* RPC spy for capturing and inspecting RPC calls in unit tests.
12+
*
13+
* This test double implements RPCInterface to allow testing of components
14+
* that depend on RPC communication without making actual RPC calls.
15+
* It records all method calls for later inspection and assertion.
16+
*/
17+
class RpcSpy implements RPCInterface
18+
{
19+
public array $calls = [];
20+
21+
public function call(string $method, mixed $payload, mixed $options = null): mixed
22+
{
23+
$this->calls[] = ['method' => $method, 'payload' => $payload, 'options' => $options];
24+
return null;
25+
}
26+
27+
public function withCodec(CodecInterface $codec): RPCInterface
28+
{
29+
return $this;
30+
}
31+
32+
public function withServicePrefix(string $service): RPCInterface
33+
{
34+
return $this;
35+
}
36+
37+
public function getLastCall(): ?array
38+
{
39+
return end($this->calls) ?: null;
40+
}
41+
42+
public function getCallCount(): int
43+
{
44+
return count($this->calls);
45+
}
46+
47+
public function reset(): void
48+
{
49+
$this->calls = [];
50+
}
51+
}

0 commit comments

Comments
 (0)