Skip to content

Commit 10a5678

Browse files
authored
Update logging interfaces Add tests for LoggerAware and LoggerConfigurable interfaces (#20)
* add size method * feat: Normalize Interfaces - Refined and standardized interface definitions across the project - Applied Single Responsibility Principle (SRP) to ensure each interface has a clear, singular purpose - Consistent naming conventions were implemented for better readability and maintainability - Added thorough documentation for each interface, including method descriptions and expected behaviors - Organized interfaces within appropriate namespaces to prevent naming collisions and maintain a logical structure - Ensured parameter names in interface methods are consistent with implementing classes - Avoided including constructors in interfaces to maintain flexibility These changes enhance the overall clarity, maintainability, and professional quality of the codebase. * Refactor and Normalize Collection Interface and Implementation - Refactored `Collection` interface for consistency and adherence to best practices. - Ensured all tests are comprehensive and validate the expected behavior. * feat: Add BinaryHeap implementation and comprehensive tests - Implemented BinaryHeap class with support for both min-heap and max-heap operations. - Methods include: add, poll, peek, size, isEmpty, heapifyUp, heapifyDown, swap, and compare. - Created unit tests for BinaryHeap class ensuring 100% coverage. - Tested all key operations: add, poll, peek, heapifyUp, heapifyDown, swap, and compare. - Included edge case tests for min-heap and max-heap scenarios. - Ensured compliance with type safety and PHP 8.0+ features. - Added comprehensive documentation and examples for BinaryHeap usage. This commit enhances the KaririCode Framework by providing a robust, type-safe, and efficient BinaryHeap data structure with extensive unit tests. * docs: Update README files in English and Portuguese - Enhanced the overview and key features sections for clarity and completeness. - Added detailed descriptions for all implemented data structures: - TreeSet - ArrayDeque - ArrayQueue - TreeMap - LinkedList - BinaryHeap - HashMap - Included complexity analysis and key methods for each data structure. - Provided usage examples for each data structure to demonstrate functionality. - Updated installation instructions and requirements. - Updated acknowledgments and roadmap sections. * style: Normalize code formatting and style * feat: Create Container interface and add unit tests - Define the Container interface with methods: `set`, `get`, and `has`. - Implement unit tests for `set` method to ensure services are registered correctly. - Add unit tests for `get` method to verify correct retrieval of services by identifier. - Include unit tests for `has` method to check existence of services in the container. - Normalize the `@category` comments in the interface documentation for consistency. These changes establish a foundational contract for dependency injection within the KaririCode project and ensure robust and reliable functionality through comprehensive testing * feat: Add logging interfaces and corresponding tests - Created Configurable interface for managing configuration settings. - Created Loggable interface to define logging methods for various log levels. - Created ContextAware interface for managing context information in logs. - Created FormatterAware interface for handling log formatter settings. - Created HandlerAware interface for managing log handlers. - Created ProcessorAware interface for managing log processors. - Created LoggingManager interface that combines various logging functionalities. - Created LogFormatter interface for defining log formatters. - Created LogHandler interface for handling log records. - Created LogLevel interface for defining log levels. - Created LogProcessor interface for processing log records before handling. - Created LogRotator interface for managing log file rotation. - Added unit tests for each interface to ensure correct functionality: - ConfigurableTest: Tests for setConfig and getConfig methods. - LoggableTest: Tests for various logging level methods. - ContextAwareTest: Tests for withContext and getContext methods. - FormatterAwareTest: Tests for setFormatter and getFormatter methods. - HandlerAwareTest: Tests for addHandler, pushHandler, popHandler, and getHandlers methods. - ProcessorAwareTest: Tests for addProcessor and getProcessors methods. - LoggingManagerTest: Comprehensive tests for all LoggingManager methods. - LogFormatterTest: Test for the format method. - LogHandlerTest: Test for the handle method. - LogLevelTest: Test for the getLevel method. - LogProcessorTest: Test for the process method. - LogRotatorTest: Test for the rotate method. * feat: Update logging interfaces Add tests for LoggerAware and LoggerConfigurable interfaces - Revised and enhanced logging interfaces for better consistency and flexibility. - Implemented new methods to support asynchronous logging. - Improved documentation of the interfaces to facilitate integration. - Added unit tests to ensure the robustness of the new implementations. - Added unit test for LoggerAware interface to verify setLogger method. - Added unit test for LoggerConfigurable interface to verify configure method. - Ensured proper mock setup and expectations in tests.
1 parent 3a13adb commit 10a5678

File tree

13 files changed

+209
-3
lines changed

13 files changed

+209
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ temp/
5959
tmp/
6060
.vscode/launch.json
6161
.vscode/extensions.json
62+
tests/lista_de_arquivos.php
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging\Behavioral;
6+
7+
/**
8+
* LoggerConfigurable interface.
9+
*
10+
* This interface defines methods for managing configuration.
11+
* Configuration settings can affect the behavior of logging.
12+
*
13+
* @category Logging
14+
*
15+
* @author Walmir Silva <walmir.silva@kariricode.org>
16+
* @license MIT
17+
*
18+
* @see https://kariricode.org/
19+
*/
20+
interface LoggerConfigurable
21+
{
22+
/**
23+
* Configures the logger.
24+
*/
25+
public function configure(array $config): void;
26+
}

src/Logging/LogFormatter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,13 @@ interface LogFormatter
3131
* @return string the formatted log record as a string
3232
*/
3333
public function format(ImmutableValue $record): string;
34+
35+
/**
36+
* Formats a set of log records.
37+
*
38+
* @param ImmutableValue[] $records
39+
*
40+
* @return string The formatted log records
41+
*/
42+
public function formatBatch(array $records): string;
3443
}

src/Logging/LogHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ interface LogHandler
3434
* @param array $record the log record to be processed, represented as an associative array
3535
*/
3636
public function handle(ImmutableValue $record): void;
37+
38+
/**
39+
* Checks whether the handler is handling the given log level.
40+
*/
41+
public function isHandling(ImmutableValue $record): bool;
3742
}

src/Logging/LogProcessor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace KaririCode\Contract\Logging;
66

7+
use KaririCode\Contract\ImmutableValue;
8+
79
/**
810
* Interface LogProcessor.
911
*
@@ -32,5 +34,5 @@ interface LogProcessor
3234
*
3335
* @return array the processed log record
3436
*/
35-
public function process(array $record): array;
37+
public function process(ImmutableValue $record): ImmutableValue;
3638
}

src/Logging/Logger.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging;
6+
7+
use KaririCode\Contract\Logging\Behavioral\Loggable;
8+
9+
/**
10+
* Interface Logger.
11+
*
12+
* @category Logging
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface Logger extends Loggable
20+
{
21+
}

src/Logging/LoggerExtended.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging;
6+
7+
use KaririCode\Contract\Logging\Structural\FormatterAware;
8+
use KaririCode\Contract\Logging\Structural\HandlerAware;
9+
use KaririCode\Contract\Logging\Structural\ProcessorAware;
10+
11+
/**
12+
* Interface Logger.
13+
*
14+
* @category Logging
15+
*
16+
* @author Walmir Silva <walmir.silva@kariricode.org>
17+
* @license MIT
18+
*
19+
* @see https://kariricode.org/
20+
*/
21+
interface LoggerExtendedInterface extends Logger
22+
{
23+
/**
24+
* Adds a new handler to the logger.
25+
*/
26+
public function addHandler(HandlerAware $handler): self;
27+
28+
/**
29+
* Adds a new processor to the logger.
30+
*/
31+
public function addProcessor(ProcessorAware $processor): self;
32+
33+
/**
34+
* Sets a formatter for the logger.
35+
*/
36+
public function setFormatter(FormatterAware $formatter): self;
37+
38+
/**
39+
* Gets the name of the logger.
40+
*/
41+
public function getName(): string;
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Logging\Structural;
6+
7+
use KaririCode\Contract\Logging\Logger;
8+
9+
/**
10+
* LoggerAware interface.
11+
*
12+
* @category Logging
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface LoggerAware
20+
{
21+
/**
22+
* Sets a logger instance on the object.
23+
*/
24+
public function setLogger(Logger $logger): void;
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Tests\Logging\Behavioral;
6+
7+
use KaririCode\Contract\Logging\Behavioral\LoggerConfigurable;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class LoggerConfigurableTest extends TestCase
11+
{
12+
public function testConfigure(): void
13+
{
14+
$config = ['level' => 'debug', 'format' => 'json'];
15+
$loggerConfigurableMock = $this->createMock(LoggerConfigurable::class);
16+
17+
$loggerConfigurableMock->expects($this->once())
18+
->method('configure')
19+
->with($this->equalTo($config));
20+
21+
$loggerConfigurableMock->configure($config);
22+
}
23+
}

tests/Logging/LogFormatterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,21 @@ public function testFormat(): void
2323

2424
$this->assertSame($formattedRecord, $formatterMock->format($recordMock));
2525
}
26+
27+
public function testFormatBatch(): void
28+
{
29+
$recordMock1 = $this->createMock(ImmutableValue::class);
30+
$recordMock2 = $this->createMock(ImmutableValue::class);
31+
$records = [$recordMock1, $recordMock2];
32+
$formattedRecords = 'Formatted log records';
33+
34+
$formatterMock = $this->createMock(LogFormatter::class);
35+
36+
$formatterMock->expects($this->once())
37+
->method('formatBatch')
38+
->with($this->equalTo($records))
39+
->willReturn($formattedRecords);
40+
41+
$this->assertSame($formattedRecords, $formatterMock->formatBatch($records));
42+
}
2643
}

0 commit comments

Comments
 (0)