Skip to content

Commit a400368

Browse files
committed
Merge branch 'release/4.1.0'
2 parents ef2bb44 + 784e3a7 commit a400368

File tree

5 files changed

+78
-30
lines changed

5 files changed

+78
-30
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. This projec
55

66
## Unreleased
77

8+
## [4.1.0] - 2025-10-03
9+
10+
### Added
11+
12+
- Log context for objects that have date time properties will now convert those properties to strings.
13+
- Log context for objects that have date time zones will now convert the time zone to its name.
14+
815
## [4.0.0] - 2025-09-12
916

1017
### Added

src/Toolkit/Loggable/ObjectDecorator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
use CloudCreativity\Modules\Contracts\Toolkit\Loggable\ContextProvider;
1616
use CloudCreativity\Modules\Contracts\Toolkit\Loggable\Contextual;
17+
use DateTimeInterface;
18+
use DateTimeZone;
1719
use Generator;
1820
use IteratorAggregate;
1921
use Ramsey\Uuid\UuidInterface;
@@ -28,8 +30,12 @@
2830
*/
2931
final readonly class ObjectDecorator implements IteratorAggregate, ContextProvider
3032
{
31-
public function __construct(private object $source)
32-
{
33+
private const DATE_FORMAT = 'Y-m-d\TH:i:s.uP';
34+
35+
public function __construct(
36+
private object $source,
37+
private ?string $dateFormat = self::DATE_FORMAT,
38+
) {
3339
}
3440

3541
/**
@@ -44,6 +50,8 @@ public function getIterator(): Generator
4450
$value instanceof Contextual => $value->context(),
4551
$value instanceof UuidInterface => $value->toString(),
4652
$value instanceof UnitEnum => enum_string($value),
53+
$value instanceof DateTimeInterface && $this->dateFormat !== null => $value->format($this->dateFormat),
54+
$value instanceof DateTimeZone => $value->getName(),
4755
default => $value,
4856
};
4957
}

tests/Unit/Application/InboundEventBus/Middleware/LogInboundEventTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CloudCreativity\Modules\Application\InboundEventBus\Middleware\LogInboundEvent;
1616
use CloudCreativity\Modules\Contracts\Toolkit\Messages\IntegrationEvent;
1717
use CloudCreativity\Modules\Tests\Unit\Infrastructure\OutboundEventBus\TestOutboundEvent;
18+
use CloudCreativity\Modules\Toolkit\Loggable\ObjectDecorator;
1819
use CloudCreativity\Modules\Toolkit\ModuleBasename;
1920
use LogicException;
2021
use PHPUnit\Framework\MockObject\MockObject;
@@ -56,10 +57,7 @@ public function test(): void
5657
$this->assertSame($this->event, $received);
5758
});
5859

59-
$context = [
60-
'uuid' => $this->event->uuid->context(),
61-
'occurredAt' => $this->event->occurredAt,
62-
];
60+
$context = (new ObjectDecorator($this->event))->context();
6361

6462
$this->assertSame([
6563
[LogLevel::DEBUG, "Receiving integration event {$eventName}.", ['event' => $context]],
@@ -85,10 +83,7 @@ public function testWithCustomLevels(): void
8583
$this->assertSame($this->event, $received);
8684
});
8785

88-
$context = [
89-
'uuid' => $this->event->uuid->context(),
90-
'occurredAt' => $this->event->occurredAt,
91-
];
86+
$context = (new ObjectDecorator($this->event))->context();
9287

9388
$this->assertSame([
9489
[LogLevel::NOTICE, "Receiving integration event {$eventName}.", ['event' => $context]],
@@ -101,10 +96,7 @@ public function testItLogsAfterTheNextClosureIsInvoked(): void
10196
$expected = new LogicException();
10297
$eventName = ModuleBasename::from($this->event);
10398

104-
$context = [
105-
'uuid' => $this->event->uuid,
106-
'occurredAt' => $this->event->occurredAt,
107-
];
99+
$context = (new ObjectDecorator($this->event))->context();
108100

109101
$this->logger
110102
->expects($this->once())

tests/Unit/Infrastructure/OutboundEventBus/Middleware/LogOutboundEventTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CloudCreativity\Modules\Contracts\Toolkit\Messages\IntegrationEvent;
1616
use CloudCreativity\Modules\Infrastructure\OutboundEventBus\Middleware\LogOutboundEvent;
1717
use CloudCreativity\Modules\Tests\Unit\Infrastructure\OutboundEventBus\TestOutboundEvent;
18+
use CloudCreativity\Modules\Toolkit\Loggable\ObjectDecorator;
1819
use CloudCreativity\Modules\Toolkit\ModuleBasename;
1920
use LogicException;
2021
use PHPUnit\Framework\MockObject\MockObject;
@@ -56,10 +57,7 @@ public function test(): void
5657
$this->assertSame($this->event, $received);
5758
});
5859

59-
$context = [
60-
'uuid' => $this->event->uuid->context(),
61-
'occurredAt' => $this->event->occurredAt,
62-
];
60+
$context = (new ObjectDecorator($this->event))->context();
6361

6462
$this->assertSame([
6563
[LogLevel::DEBUG, "Publishing integration event {$eventName}.", ['event' => $context]],
@@ -85,10 +83,7 @@ public function testWithCustomLevels(): void
8583
$this->assertSame($this->event, $received);
8684
});
8785

88-
$context = [
89-
'uuid' => $this->event->uuid->context(),
90-
'occurredAt' => $this->event->occurredAt,
91-
];
86+
$context = (new ObjectDecorator($this->event))->context();
9287

9388
$this->assertSame([
9489
[LogLevel::NOTICE, "Publishing integration event {$eventName}.", ['event' => $context]],
@@ -101,10 +96,7 @@ public function testItLogsAfterTheNextClosureIsInvoked(): void
10196
$expected = new LogicException();
10297
$eventName = ModuleBasename::from($this->event);
10398

104-
$context = [
105-
'uuid' => $this->event->uuid->context(),
106-
'occurredAt' => $this->event->occurredAt,
107-
];
99+
$context = (new ObjectDecorator($this->event))->context();
108100

109101
$this->logger
110102
->expects($this->once())

tests/Unit/Toolkit/Loggable/ObjectDecoratorTest.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use CloudCreativity\Modules\Toolkit\Loggable\ObjectDecorator;
2121
use CloudCreativity\Modules\Toolkit\Loggable\Sensitive;
2222
use CloudCreativity\Modules\Toolkit\Loggable\SimpleContextFactory;
23+
use DateTimeImmutable;
24+
use DateTimeInterface;
2325
use PHPUnit\Framework\TestCase;
2426
use Ramsey\Uuid\Uuid;
2527
use Ramsey\Uuid\UuidInterface;
@@ -44,8 +46,10 @@ protected function tearDown(): void
4446
public function testItUsesObjectProperties(): void
4547
{
4648
$uuid = Uuid::uuid4();
49+
$date1 = new \DateTimeImmutable();
50+
$date2 = new \DateTime('2025-01-01 12:34:56', new \DateTimeZone('Australia/Sydney'));
4751

48-
$source = new class ($uuid) implements Message {
52+
$source = new class ($uuid, $date1, $date2, $date2->getTimezone()) implements Message {
4953
public string $foo = 'bar';
5054
public string $baz = 'bat';
5155
public ?string $blah = null;
@@ -54,8 +58,12 @@ public function testItUsesObjectProperties(): void
5458
public UnitEnum $enum3 = TestUnitEnum::Baz;
5559
protected string $foobar = 'foobar';
5660

57-
public function __construct(public UuidInterface $uuid)
58-
{
61+
public function __construct(
62+
public UuidInterface $uuid,
63+
public DateTimeInterface $date1,
64+
public DateTimeInterface $date2,
65+
public \DateTimeZone $timeZone,
66+
) {
5967
}
6068
};
6169

@@ -67,16 +75,57 @@ public function __construct(public UuidInterface $uuid)
6775
'enum2' => TestBackedIntEnum::FooBar->name,
6876
'enum3' => TestUnitEnum::Baz->name,
6977
'uuid' => $uuid->toString(),
78+
'date1' => $date1->format('Y-m-d\TH:i:s.uP'),
79+
'date2' => $date2->format('Y-m-d\TH:i:s.uP'),
80+
'timeZone' => 'Australia/Sydney',
7081
];
7182

7283
$decorator = new ObjectDecorator($source);
7384

7485
$this->assertInstanceOf(ContextProvider::class, $decorator);
75-
$this->assertSame(array_keys($expected), $decorator->keys());
7686
$this->assertSame($expected, iterator_to_array($decorator));
7787
$this->assertSame($expected, $decorator->all());
7888
$this->assertSame($expected, $decorator->context());
7989
$this->assertSame($expected, $this->factory->make($source));
90+
$this->assertSame(array_keys($expected), $decorator->keys());
91+
}
92+
93+
public function testItCanCustomiseDateFormat(): void
94+
{
95+
$date = new DateTimeImmutable('2025-01-01 12:34:56', new \DateTimeZone('Australia/Sydney'));
96+
97+
$source = new class ($date) implements Message {
98+
public function __construct(public DateTimeInterface $date)
99+
{
100+
}
101+
};
102+
103+
$expected = [
104+
'date' => $date->format('Y-m-d H:i:s'),
105+
];
106+
107+
$decorator = new ObjectDecorator($source, dateFormat: 'Y-m-d H:i:s');
108+
109+
$this->assertSame($expected, $decorator->context());
110+
}
111+
112+
public function testItCanTurnOffDateFormatting(): void
113+
{
114+
$date = new DateTimeImmutable('2025-01-01 12:34:56', new \DateTimeZone('Australia/Sydney'));
115+
116+
$source = new class ($date) implements Message {
117+
public function __construct(public DateTimeInterface $date)
118+
{
119+
}
120+
};
121+
122+
$expected = [
123+
'date' => $date,
124+
];
125+
126+
$decorator = new ObjectDecorator($source, dateFormat: null);
127+
128+
$this->assertSame($expected, $decorator->context());
80129
}
81130

82131
public function testItExcludesSensitiveProperties(): void

0 commit comments

Comments
 (0)