Skip to content

Commit 8b0690b

Browse files
committed
test: Cleanup
1 parent af98022 commit 8b0690b

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

src/Internal/ContextProcessor/FallbackProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function canProcess(object $value): bool
2020
return true;
2121
}
2222

23-
public function process(object $value, callable $processor): string
23+
public function process(object $value, callable $processor): array
2424
{
2525
$result = ['@class' => $value::class] + \get_object_vars($value);
2626
foreach ($result as $k => &$v) {

src/Internal/DefaultProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static function createDefault(): self
2323
new ThrowableProcessor(),
2424
new FallbackProcessor(),
2525
];
26+
return $self;
2627
}
2728

2829
/**

src/RpcLogger.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@
1111
use RoadRunner\Logger\Logger as AppLogger;
1212
use RoadRunner\Logger\LogLevel;
1313
use RoadRunner\PsrLogger\Internal\DefaultProcessor;
14-
use RoadRunner\PsrLogger\Internal\ObjectProcessor;
15-
use RoadRunner\PsrLogger\Internal\ContextProcessor\ObjectProcessorManager;
1614

1715
class RpcLogger implements LoggerInterface
1816
{
1917
use LoggerTrait;
2018

2119
private readonly AppLogger $logger;
22-
private readonly ObjectProcessor $contextProcessor;
20+
private readonly \Closure $objectProcessor;
2321

2422
public function __construct(AppLogger $logger, ?callable $processor = null)
2523
{
2624
$this->logger = $logger;
27-
$this->contextProcessor = $processor ?? DefaultProcessor::createDefault();
25+
$this->objectProcessor = ($processor ?? DefaultProcessor::createDefault())(...);
2826
}
2927

3028
/**
@@ -45,7 +43,7 @@ public function log($level, \Stringable|string $message, array $context = []): v
4543
});
4644

4745
// Process context data for structured logging using the processor manager
48-
$processedContext = $this->contextProcessor->processContext($context);
46+
$processedContext = ($this->objectProcessor)($context);
4947

5048
match ($normalizedLevel) {
5149
PsrLogLevel::EMERGENCY,

tests/Unit/RpcLoggerTest.php

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
use PHPUnit\Framework\TestCase;
1010
use Psr\Log\InvalidArgumentException as PsrInvalidArgumentException;
1111
use Psr\Log\LogLevel as PsrLogLevel;
12+
use RoadRunner\AppLogger\DTO\V1\LogEntry;
1213
use RoadRunner\Logger\Logger as AppLogger;
1314
use RoadRunner\Logger\LogLevel;
14-
use RoadRunner\PsrLogger\Internal\ContextProcessor\ContextProcessorInterface;
15-
use RoadRunner\PsrLogger\Internal\ContextProcessor\ContextProcessorManager;
15+
use RoadRunner\PsrLogger\Internal\DefaultProcessor;
16+
use RoadRunner\PsrLogger\Internal\ObjectProcessor;
1617
use RoadRunner\PsrLogger\RpcLogger;
1718

1819
#[CoversClass(RpcLogger::class)]
@@ -58,7 +59,7 @@ public function testLogWithEmergencyLevels(string $level): void
5859
$this->assertSame(1, $this->rpc->getCallCount());
5960
$lastCall = $this->rpc->getLastCall();
6061
$this->assertSame('ErrorWithContext', $lastCall['method']);
61-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
62+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
6263
}
6364

6465
public function testLogWithWarningLevel(): void
@@ -71,7 +72,7 @@ public function testLogWithWarningLevel(): void
7172
$this->assertSame(1, $this->rpc->getCallCount());
7273
$lastCall = $this->rpc->getLastCall();
7374
$this->assertSame('WarningWithContext', $lastCall['method']);
74-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
75+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
7576
}
7677

7778
#[DataProvider('infoLevelsProvider')]
@@ -85,7 +86,7 @@ public function testLogWithInfoLevels(string $level): void
8586
$this->assertSame(1, $this->rpc->getCallCount());
8687
$lastCall = $this->rpc->getLastCall();
8788
$this->assertSame('InfoWithContext', $lastCall['method']);
88-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
89+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
8990
}
9091

9192
public function testLogWithDebugLevel(): void
@@ -98,7 +99,7 @@ public function testLogWithDebugLevel(): void
9899
$this->assertSame(1, $this->rpc->getCallCount());
99100
$lastCall = $this->rpc->getLastCall();
100101
$this->assertSame('DebugWithContext', $lastCall['method']);
101-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
102+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
102103
}
103104

104105
public function testLogWithStringableMessage(): void
@@ -140,7 +141,7 @@ public function testLogWithCaseInsensitiveLevel(): void
140141
$this->assertSame(1, $this->rpc->getCallCount());
141142
$lastCall = $this->rpc->getLastCall();
142143
$this->assertSame('ErrorWithContext', $lastCall['method']);
143-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
144+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
144145
}
145146

146147
public function testLogWithMixedCaseLevel(): void
@@ -153,7 +154,7 @@ public function testLogWithMixedCaseLevel(): void
153154
$this->assertSame(1, $this->rpc->getCallCount());
154155
$lastCall = $this->rpc->getLastCall();
155156
$this->assertSame('WarningWithContext', $lastCall['method']);
156-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
157+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
157158
}
158159

159160
public function testLogWithEnumLevel(): void
@@ -372,7 +373,7 @@ public function testLogWithScalarContext(): void
372373
$this->assertSame(1, $this->rpc->getCallCount());
373374
$lastCall = $this->rpc->getLastCall();
374375
$this->assertSame('InfoWithContext', $lastCall['method']);
375-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
376+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
376377
}
377378

378379
public function testLogWithDateTimeContext(): void
@@ -391,7 +392,7 @@ public function testLogWithDateTimeContext(): void
391392
$this->assertSame(1, $this->rpc->getCallCount());
392393
$lastCall = $this->rpc->getLastCall();
393394
$this->assertSame('InfoWithContext', $lastCall['method']);
394-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
395+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
395396
}
396397

397398
public function testLogWithExceptionContext(): void
@@ -409,7 +410,7 @@ public function testLogWithExceptionContext(): void
409410
$this->assertSame(1, $this->rpc->getCallCount());
410411
$lastCall = $this->rpc->getLastCall();
411412
$this->assertSame('ErrorWithContext', $lastCall['method']);
412-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
413+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
413414
}
414415

415416
public function testLogWithStringableContext(): void
@@ -432,7 +433,7 @@ public function __toString(): string
432433
$this->assertSame(1, $this->rpc->getCallCount());
433434
$lastCall = $this->rpc->getLastCall();
434435
$this->assertSame('InfoWithContext', $lastCall['method']);
435-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
436+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
436437
}
437438

438439
public function testLogWithNestedArrayContext(): void
@@ -457,7 +458,7 @@ public function testLogWithNestedArrayContext(): void
457458
$this->assertSame(1, $this->rpc->getCallCount());
458459
$lastCall = $this->rpc->getLastCall();
459460
$this->assertSame('DebugWithContext', $lastCall['method']);
460-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
461+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
461462
}
462463

463464
public function testLogWithObjectContext(): void
@@ -479,7 +480,7 @@ public function testLogWithObjectContext(): void
479480
$this->assertSame(1, $this->rpc->getCallCount());
480481
$lastCall = $this->rpc->getLastCall();
481482
$this->assertSame('InfoWithContext', $lastCall['method']);
482-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
483+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
483484
}
484485

485486
public function testLogWithResourceContext(): void
@@ -499,7 +500,7 @@ public function testLogWithResourceContext(): void
499500
$this->assertSame(1, $this->rpc->getCallCount());
500501
$lastCall = $this->rpc->getLastCall();
501502
$this->assertSame('InfoWithContext', $lastCall['method']);
502-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
503+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
503504
}
504505

505506
public function testLogWithMixedComplexContext(): void
@@ -538,19 +539,19 @@ public function __toString(): string
538539
$this->assertSame(1, $this->rpc->getCallCount());
539540
$lastCall = $this->rpc->getLastCall();
540541
$this->assertSame('WarningWithContext', $lastCall['method']);
541-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
542+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
542543
}
543544

544545
public function testCustomProcessorIntegration(): void
545546
{
546547
// Create a custom processor for email addresses
547-
$emailProcessor = new class implements ContextProcessorInterface {
548+
$emailProcessor = new class implements ObjectProcessor {
548549
public function canProcess(mixed $value): bool
549550
{
550551
return \is_string($value) && \filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
551552
}
552553

553-
public function process(mixed $value, callable $recursiveProcessor): mixed
554+
public function process(mixed $value, callable $processor): mixed
554555
{
555556
// Mask email for privacy
556557
$parts = \explode('@', $value);
@@ -559,8 +560,7 @@ public function process(mixed $value, callable $recursiveProcessor): mixed
559560
};
560561

561562
// Create processor manager with custom processor added first
562-
$processorManager = new ContextProcessorManager();
563-
$processorManager->addProcessor($emailProcessor);
563+
$processorManager = DefaultProcessor::createDefault()->withObjectProcessors($emailProcessor);
564564

565565
// Create logger with custom processor manager
566566
$logger = new RpcLogger($this->appLogger, $processorManager);
@@ -577,19 +577,19 @@ public function process(mixed $value, callable $recursiveProcessor): mixed
577577
$this->assertSame(1, $this->rpc->getCallCount());
578578
$lastCall = $this->rpc->getLastCall();
579579
$this->assertSame('InfoWithContext', $lastCall['method']);
580-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
580+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
581581
}
582582

583583
public function testMultipleCustomProcessors(): void
584584
{
585585
// Custom processor for URLs
586-
$urlProcessor = new class implements ContextProcessorInterface {
586+
$urlProcessor = new class implements ObjectProcessor {
587587
public function canProcess(mixed $value): bool
588588
{
589589
return \is_string($value) && \filter_var($value, FILTER_VALIDATE_URL) !== false;
590590
}
591591

592-
public function process(mixed $value, callable $recursiveProcessor): mixed
592+
public function process(mixed $value, callable $processor): mixed
593593
{
594594
$parsed = \parse_url($value);
595595
return [
@@ -601,21 +601,21 @@ public function process(mixed $value, callable $recursiveProcessor): mixed
601601
};
602602

603603
// Custom processor for credit card numbers (mock)
604-
$ccProcessor = new class implements ContextProcessorInterface {
604+
$ccProcessor = new class implements ObjectProcessor {
605605
public function canProcess(mixed $value): bool
606606
{
607607
return \is_string($value) && \preg_match('/^\d{4}-?\d{4}-?\d{4}-?\d{4}$/', $value);
608608
}
609609

610-
public function process(mixed $value, callable $recursiveProcessor): mixed
610+
public function process(mixed $value, callable $processor): mixed
611611
{
612612
return '****-****-****-' . \substr($value, -4);
613613
}
614614
};
615615

616-
$processorManager = new ContextProcessorManager();
617-
$processorManager->addProcessor($urlProcessor);
618-
$processorManager->addProcessor($ccProcessor);
616+
$processorManager = \RoadRunner\PsrLogger\Internal\DefaultProcessor::createDefault()
617+
->withObjectProcessors($urlProcessor)
618+
->withObjectProcessors($ccProcessor);
619619

620620
$logger = new RpcLogger($this->appLogger, $processorManager);
621621

@@ -631,7 +631,7 @@ public function process(mixed $value, callable $recursiveProcessor): mixed
631631
$this->assertSame(1, $this->rpc->getCallCount());
632632
$lastCall = $this->rpc->getLastCall();
633633
$this->assertSame('WarningWithContext', $lastCall['method']);
634-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
634+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
635635
}
636636

637637
public function testDefaultProcessorManagerWhenNoneProvided(): void
@@ -650,39 +650,39 @@ public function testDefaultProcessorManagerWhenNoneProvided(): void
650650
$this->assertSame(1, $this->rpc->getCallCount());
651651
$lastCall = $this->rpc->getLastCall();
652652
$this->assertSame('ErrorWithContext', $lastCall['method']);
653-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
653+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
654654
}
655655

656656
public function testProcessorOrdering(): void
657657
{
658658
// Create processors for the same type to test ordering
659-
$firstProcessor = new class implements ContextProcessorInterface {
659+
$firstProcessor = new class implements ObjectProcessor {
660660
public function canProcess(mixed $value): bool
661661
{
662662
return \is_int($value);
663663
}
664664

665-
public function process(mixed $value, callable $recursiveProcessor): mixed
665+
public function process(mixed $value, callable $processor): mixed
666666
{
667667
return 'first:' . $value;
668668
}
669669
};
670670

671-
$secondProcessor = new class implements ContextProcessorInterface {
671+
$secondProcessor = new class implements ObjectProcessor {
672672
public function canProcess(mixed $value): bool
673673
{
674674
return \is_int($value);
675675
}
676676

677-
public function process(mixed $value, callable $recursiveProcessor): mixed
677+
public function process(mixed $value, callable $processor): mixed
678678
{
679679
return 'second:' . $value;
680680
}
681681
};
682682

683-
$processorManager = new ContextProcessorManager();
684-
$processorManager->addProcessor($firstProcessor); // Added first, should be used
685-
$processorManager->addProcessor($secondProcessor); // Added second, should be skipped
683+
$processorManager = \RoadRunner\PsrLogger\Internal\DefaultProcessor::createDefault()
684+
->withObjectProcessors($firstProcessor) // Added first, should be used
685+
->withObjectProcessors($secondProcessor); // Added second, should be skipped
686686

687687
$logger = new RpcLogger($this->appLogger, $processorManager);
688688

@@ -695,7 +695,7 @@ public function process(mixed $value, callable $recursiveProcessor): mixed
695695

696696
// The first processor should have been used
697697
// We can't directly inspect the processed context, but we know it was processed
698-
$this->assertInstanceOf(\RoadRunner\AppLogger\DTO\V1\LogEntry::class, $lastCall['payload']);
698+
$this->assertInstanceOf(LogEntry::class, $lastCall['payload']);
699699
}
700700

701701
protected function setUp(): void

0 commit comments

Comments
 (0)