Skip to content

Commit ee489bb

Browse files
authored
add unit tests for ProcessingResul (#34)
* 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. * feat(logging): improve log rotation and update log handler - Removed .phpcs-cache for cleaner repository - Updated src/Logging/LogHandler.php to enhance log handling capabilities - Modified src/Logging/LogLevel.php for better log level management - Enhanced src/Logging/LogRotator.php with improved file rotation logic - Updated src/Logging/Logger.php to integrate changes in log handling and rotation * Refactor:Indexable interface and LogLevel class - Modified the Indexable.php interface to enhance functionality. - Updated the LogLevel.php class to add new log levels. - Made lint adjustments in LogRotator class. * feat(contract): Add Processor interfaces and tests - Introduce new interfaces: Processor, ConfigurableProcessor, Pipeline - Add comprehensive PHPUnit tests for each interface - Enhance code documentation with detailed PHP DocBlocks - Ensure consistency with existing KaririCode coding standards * feat: add ProcessorRegistry interface and unit tests - Introduced `ProcessorRegistry` interface to standardize processor registration and retrieval across contexts. - Added `ProcessorRegistryTest` class to ensure correctness of the new interface, covering scenarios like processor registration, retrieval, and error handling. * feat(processor): add ProcessorBuilder interface and test suite - Add ProcessorBuilder interface in KaririCode\Contract\Processor namespace - Define build() method for constructing individual processors - Define buildPipeline() method for assembling processor pipelines - Create ProcessorBuilderTest class in KaririCode\Contract\Tests\Processor namespace - Implement tests for build() and buildPipeline() methods - Cover success scenarios and exception handling - Ensure proper interaction with ProcessorRegistry * feat(contract): add PHPDoc for ProcessableAttribute interface and create unit tests - Add PHPDoc comments for ProcessableAttribute interface, including method descriptions and author/license information - Create unit tests for ProcessableAttribute interface, testing `getProcessors` and `getFallbackValue` methods * feat(contract): add Sanitizer, Serializer, and Validator interfaces with unit tests - Add `Sanitizer` interface for sanitizing input data in `src/Sanitizer/` - Add `Validator` interface for validating input data in `src/Validator/` - Add `Serializer` interface for serializing and deserializing data in `src/Serializer/` - Create unit tests for each interface to ensure expected behavior * fix(validator): correct return type of Validator interface * feat(contract): add CustomizableMessageAttribute interface - Introduce CustomizableMessageAttribute interface in KaririCode\Contract\Processor\Attribute namespace - Add method getMessage(string $validatorName): ?string to the interface - Create unit tests for CustomizableMessageAttribute in CustomizableMessageAttributeTest - Update existing ProcessableAttribute interface documentation for consistency * feat: add BaseProcessorAttribute abstract class * refactor: adjusty filterValidProcessors in BaseProcessorAttribute * feat(processor): add ProcessorValidator and ValidatableProcessor interfaces with unit tests - Added ProcessorValidator interface to define contract for validating processors in a pipeline. - Added ValidatableProcessor interface to define contract for processors with validation capabilities. - Created unit tests for ProcessorValidator and ValidatableProcessor to ensure proper validation behavior. * feat(validator): Add reset method to ValidatableProcessor interface The reset method is added to ensure proper state reset between validations. This change helps prevent state leakage between multiple validation cycles by allowing processors to clear their validation state before reuse. - Add reset() method with proper PHPDoc - Maintain consistent documentation style - Keep backward compatibility with existing implementations * feat(validator): Add reset method to ValidatableProcessor interface The reset method is added to ensure proper state reset between validations. This change helps prevent state leakage between multiple validation cycles by allowing processors to clear their validation state before reuse. - Add reset() method with proper PHPDoc - Maintain consistent documentation style - Keep backward compatibility with existing implementations * test(processor): add unit tests for ProcessingResult interface - Add test cases for all ProcessingResult interface methods - Include setUp with mock creation - Validate error handling and data processing - Ensure proper type declarations
1 parent 72c834a commit ee489bb

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

src/Processor/ProcessingResult.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Processor;
6+
7+
/**
8+
* Interface ProcessingResult.
9+
*
10+
* Defines the contract for handling processing results, including errors and processed data management.
11+
*
12+
* @category Processor
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface ProcessingResult
20+
{
21+
/**
22+
* Adds an error associated with a specific property.
23+
*
24+
* @param string $property The property where the error occurred
25+
* @param string $errorKey A unique identifier for the type of error
26+
* @param string $message A human-readable error message
27+
*/
28+
public function addError(string $property, string $errorKey, string $message): void;
29+
30+
/**
31+
* Stores processed data for a specific property.
32+
*
33+
* @param string $property The property associated with the processed data
34+
* @param mixed $value The processed value to be stored
35+
*/
36+
public function setProcessedData(string $property, mixed $value): void;
37+
38+
/**
39+
* Checks if there are any errors in the processing result.
40+
*
41+
* @return bool True if there are errors, false otherwise
42+
*/
43+
public function hasErrors(): bool;
44+
45+
/**
46+
* Retrieves all errors that occurred during processing.
47+
*
48+
* @return array<string, array<int, array{errorKey: string, message: string}>>
49+
* A map of property names to their associated errors
50+
*/
51+
public function getErrors(): array;
52+
53+
/**
54+
* Retrieves all processed data.
55+
*
56+
* @return array<string, mixed> A map of property names to their processed values
57+
*/
58+
public function getProcessedData(): array;
59+
60+
/**
61+
* Converts the processing result into an array representation.
62+
*
63+
* @return array{
64+
* isValid: bool,
65+
* errors: array<string, array<int, array{errorKey: string, message: string}>>,
66+
* processedData: array<string, mixed>
67+
* }
68+
*/
69+
public function toArray(): array;
70+
71+
/**
72+
* Clears all stored errors and processed data.
73+
*/
74+
public function clear(): void;
75+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Tests\Processor;
6+
7+
use KaririCode\Contract\Processor\ProcessingResult;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class ProcessingResultTest extends TestCase
12+
{
13+
private ProcessingResult|MockObject $processingResultMock;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
$this->processingResultMock = $this->createMock(ProcessingResult::class);
19+
}
20+
21+
public function testAddError(): void
22+
{
23+
$this->processingResultMock->expects($this->once())
24+
->method('addError')
25+
->with('property', 'error_key', 'error message');
26+
27+
$this->processingResultMock->addError('property', 'error_key', 'error message');
28+
}
29+
30+
public function testSetProcessedData(): void
31+
{
32+
$this->processingResultMock->expects($this->once())
33+
->method('setProcessedData')
34+
->with('property', 'value');
35+
36+
$this->processingResultMock->setProcessedData('property', 'value');
37+
}
38+
39+
public function testHasErrors(): void
40+
{
41+
$this->processingResultMock->expects($this->once())
42+
->method('hasErrors')
43+
->willReturn(true);
44+
45+
$this->assertTrue($this->processingResultMock->hasErrors());
46+
}
47+
48+
public function testGetErrors(): void
49+
{
50+
$expectedErrors = [
51+
'property' => [
52+
[
53+
'errorKey' => 'error_key',
54+
'message' => 'error message',
55+
],
56+
],
57+
];
58+
59+
$this->processingResultMock->expects($this->once())
60+
->method('getErrors')
61+
->willReturn($expectedErrors);
62+
63+
$this->assertSame($expectedErrors, $this->processingResultMock->getErrors());
64+
}
65+
66+
public function testGetProcessedData(): void
67+
{
68+
$expectedData = [
69+
'property' => 'processed value',
70+
];
71+
72+
$this->processingResultMock->expects($this->once())
73+
->method('getProcessedData')
74+
->willReturn($expectedData);
75+
76+
$this->assertSame($expectedData, $this->processingResultMock->getProcessedData());
77+
}
78+
79+
public function testToArray(): void
80+
{
81+
$expectedArray = [
82+
'isValid' => false,
83+
'errors' => [
84+
'property' => [
85+
[
86+
'errorKey' => 'error_key',
87+
'message' => 'error message',
88+
],
89+
],
90+
],
91+
'processedData' => [
92+
'property' => 'processed value',
93+
],
94+
];
95+
96+
$this->processingResultMock->expects($this->once())
97+
->method('toArray')
98+
->willReturn($expectedArray);
99+
100+
$this->assertSame($expectedArray, $this->processingResultMock->toArray());
101+
}
102+
103+
public function testClear(): void
104+
{
105+
$this->processingResultMock->expects($this->once())
106+
->method('clear');
107+
108+
$this->processingResultMock->clear();
109+
}
110+
}

0 commit comments

Comments
 (0)