Skip to content

Commit c0e2b88

Browse files
committed
chore: Add unit test suit for Formatters
1 parent d84e0f1 commit c0e2b88

File tree

8 files changed

+164
-50
lines changed

8 files changed

+164
-50
lines changed

app/Audit/AbstractAuditLogFormatter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717

1818
abstract class AbstractAuditLogFormatter implements IAuditLogFormatter
1919
{
20-
protected AuditContext $ctx;
20+
protected ?AuditContext $ctx = null;
21+
22+
protected string $event_type;
23+
24+
public function __construct(string $event_type)
25+
{
26+
$this->event_type = $event_type;
27+
}
2128

2229
final public function setContext(AuditContext $ctx): void
2330
{

app/Audit/AuditLogFormatterFactory.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace App\Audit;
1+
<?php
2+
namespace App\Audit;
23
/**
34
* Copyright 2025 OpenStack Foundation
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,13 +28,10 @@ class AuditLogFormatterFactory implements IAuditLogFormatterFactory
2728

2829
public function __construct()
2930
{
30-
try
31-
{
32-
Log::debug("AuditLogFormatterFactory::construct loading audit_log config");
31+
try {
3332
$this->config = config('audit_log', []);
34-
}
35-
catch(\Exception $ex){
36-
Log::error('Failed to load audit_log configuration', ['exception' => $ex]);
33+
} catch (\Exception $ex) {
34+
$this->config = [];
3735
}
3836
}
3937

@@ -64,8 +62,7 @@ public function make(AuditContext $ctx, $subject, $eventType): ?IAuditLogFormatt
6462
$targetEntity = $type;
6563
}
6664
Log::debug("AuditLogFormatterFactory::make getTypeClass targetEntity {$targetEntity}");
67-
}
68-
elseif (method_exists($subject, 'getMapping')) {
65+
} elseif (method_exists($subject, 'getMapping')) {
6966
$mapping = $subject->getMapping();
7067
$targetEntity = $mapping['targetEntity'] ?? null;
7168
Log::debug("AuditLogFormatterFactory::make getMapping targetEntity {$targetEntity}");
@@ -78,7 +75,8 @@ public function make(AuditContext $ctx, $subject, $eventType): ?IAuditLogFormatt
7875
$prop->setAccessible(true);
7976
$mapping = $prop->getValue($subject);
8077
$targetEntity = $mapping['targetEntity'] ?? null;
81-
if ($targetEntity) break;
78+
if ($targetEntity)
79+
break;
8280
}
8381
}
8482
}
@@ -108,26 +106,27 @@ public function make(AuditContext $ctx, $subject, $eventType): ?IAuditLogFormatt
108106
break;
109107
case IAuditStrategy::EVENT_ENTITY_CREATION:
110108
$formatter = $this->getFormatterByContext($subject, $eventType, $ctx);
111-
if(is_null($formatter)) {
112-
$formatter = new EntityCreationAuditLogFormatter();
109+
if (is_null($formatter)) {
110+
$formatter = new EntityCreationAuditLogFormatter($eventType);
113111
}
114112
break;
115113
case IAuditStrategy::EVENT_ENTITY_DELETION:
116114
$formatter = $this->getFormatterByContext($subject, $eventType, $ctx);
117-
if(is_null($formatter)) {
115+
if (is_null($formatter)) {
118116
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
119117
$formatter = new EntityDeletionAuditLogFormatter($child_entity_formatter);
120118
}
121119
break;
122120
case IAuditStrategy::EVENT_ENTITY_UPDATE:
123121
$formatter = $this->getFormatterByContext($subject, $eventType, $ctx);
124-
if(is_null($formatter)) {
122+
if (is_null($formatter)) {
125123
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
126124
$formatter = new EntityUpdateAuditLogFormatter($child_entity_formatter);
127125
}
128126
break;
129127
}
130-
if ($formatter === null) return null;
128+
if ($formatter === null)
129+
return null;
131130
$formatter->setContext($ctx);
132131
return $formatter;
133132
}
@@ -173,4 +172,4 @@ private function routeMatches(string $route, string $actual_route): bool
173172
{
174173
return strcmp($actual_route, $route) === 0;
175174
}
176-
}
175+
}

app/Audit/ConcreteFormatters/EntityDeletionAuditLogFormatter.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
use App\Audit\AbstractAuditLogFormatter;
1919
use App\Audit\ConcreteFormatters\ChildEntityFormatters\IChildEntityAuditLogFormatter;
20-
use models\summit\SummitAttendeeBadgePrint;
21-
use ReflectionClass;
2220

2321
/**
2422
* Class EntityDeletionAuditLogFormatter
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Audit\ConcreteFormatters;
4+
5+
use App\Audit\AbstractAuditLogFormatter;
6+
use App\Audit\Interfaces\IAuditStrategy;
7+
use Illuminate\Support\Facades\Log;
8+
use Models\UserAction;
9+
10+
class UserActionAuditLogFormatter extends AbstractAuditLogFormatter
11+
{
12+
public function format($subject, array $change_set): ?string
13+
{
14+
if (!$subject instanceof UserAction) {
15+
return null;
16+
}
17+
18+
try {
19+
$id = $subject->getId() ?? 'unknown';
20+
$title = $subject->getUserAction() ?? 'Unknown UserAction';
21+
22+
switch ($this->event_type) {
23+
case IAuditStrategy::EVENT_ENTITY_CREATION:
24+
return sprintf("Presentation Attendee Vote (%s) for '%s' created by user %s", $id, $title, $this->getUserInfo());
25+
case IAuditStrategy::EVENT_ENTITY_UPDATE:
26+
$details = $this->buildChangeDetails($change_set);
27+
return sprintf("Presentation Attendee Vote (%s) for '%s' updated: %s by user %s", $id, $title, $details, $this->getUserInfo());
28+
case IAuditStrategy::EVENT_ENTITY_DELETION:
29+
return sprintf("Presentation Attendee Vote (%s) for '%s' deleted by user %s", $id, $title, $this->getUserInfo());
30+
}
31+
} catch (\Exception $ex) {
32+
Log::warning("PresentationAttendeeVoteAuditLogFormatter error: " . $ex->getMessage());
33+
}
34+
35+
return null;
36+
}
37+
}

app/Audit/IAuditLogFormatter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace App\Audit;
1+
<?php
2+
namespace App\Audit;
23

34
/**
45
* Copyright 2022 OpenStack Foundation
@@ -31,5 +32,5 @@ public function setContext(AuditContext $ctx): void;
3132
* @param array $change_set
3233
* @return string|null
3334
*/
34-
public function format($subject, array $change_set):?string;
35-
}
35+
public function format($subject, array $change_set): ?string;
36+
}

config/audit_log.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
return [
44
'entities' => [
5+
\Models\UserAction::class => [
6+
'enabled' => true,
7+
'strategy' => App\Audit\ConcreteFormatters\UserActionAuditLogFormatter::class
8+
],
59
]
610
];

phpunit.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
<testsuites>
1212
<testsuite name="Application Test Suite">
1313
<directory>./tests/</directory>
14+
<exclude>./tests/OpenTelemetry/</exclude>
15+
<exclude>./tests/TestCase.php</exclude>
16+
</testsuite>
17+
<testsuite name="OTEL">
18+
<directory>./tests/OpenTelemetry/</directory>
19+
<exclude>./tests/OpenTelemetry/ExampleTest.php</exclude>
1420
</testsuite>
1521
</testsuites>
1622
<php>

0 commit comments

Comments
 (0)