Skip to content

Commit 84db138

Browse files
authored
add ProcessorValidator and ValidatableProcessor interfaces (#32)
* 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.
1 parent 2174dd6 commit 84db138

5 files changed

Lines changed: 196 additions & 2 deletions

File tree

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: "3.8"
2-
31
services:
42
php:
53
container_name: kariricode-contract
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Processor;
6+
7+
/**
8+
* Interface ProcessorValidator.
9+
*
10+
* Defines the contract for validating processors within the processing pipeline.
11+
* A ProcessorValidator is responsible for ensuring that a processor is in a valid state
12+
* before allowing it to proceed with its task. If validation fails, the validator returns
13+
* an array containing error details, which can be used to handle the failure.
14+
*
15+
* This interface supports custom validation messages, allowing flexibility in
16+
* error reporting based on the context or type of processor.
17+
*
18+
* @category ProcessorPipeline
19+
*
20+
* @author Walmir Silva <walmir.silva@kariricode.org>
21+
* @license MIT
22+
*
23+
* @see https://kariricode.org/
24+
*/
25+
interface ProcessorValidator
26+
{
27+
/**
28+
* Validates a processor and returns any validation errors.
29+
*
30+
* This method checks whether the given processor meets the required validation
31+
* criteria. If the processor is invalid, it returns an array containing the error
32+
* details, including a custom error message if provided. Otherwise, it returns null.
33+
*
34+
* @param Processor $processor the processor to be validated
35+
* @param string $processorName the unique name of the processor, used to identify it in the context of validation
36+
* @param array $messages an array of custom messages for validation errors, keyed by processor name
37+
*
38+
* @return array|null the validation error details, or null if validation passes
39+
*/
40+
public function validate(Processor $processor, string $processorName, array $messages): ?array;
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Processor;
6+
7+
/**
8+
* Interface ValidatableProcessor.
9+
*
10+
* Defines the contract for processors that include validation capabilities.
11+
* A ValidatableProcessor is a specialized Processor that can perform validation
12+
* during its execution, and may return specific error details if validation fails.
13+
*
14+
* This interface extends the basic Processor contract by adding methods to check
15+
* for validity and retrieve validation error information.
16+
*
17+
* @category ProcessorPipeline
18+
*
19+
* @author Walmir Silva <walmir.silva@kariricode.org>
20+
* @license MIT
21+
*
22+
* @see https://kariricode.org/
23+
*/
24+
interface ValidatableProcessor extends Processor
25+
{
26+
/**
27+
* Checks if the processor is in a valid state.
28+
*
29+
* This method determines whether the processor has met all required
30+
* conditions and can continue processing. A processor that fails validation
31+
* may halt the processing pipeline or trigger corrective actions.
32+
*
33+
* @return bool true if the processor is valid, false otherwise
34+
*/
35+
public function isValid(): bool;
36+
37+
/**
38+
* Retrieves the error key if the processor fails validation.
39+
*
40+
* When a processor is deemed invalid by the isValid method, this method
41+
* returns a key that uniquely identifies the validation error. This key
42+
* can be used for logging, error messages, or further handling within the
43+
* processing pipeline.
44+
*
45+
* @return string|null the error key, or null if no validation error occurred
46+
*/
47+
public function getErrorKey(): ?string;
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Tests\Processor;
6+
7+
use KaririCode\Contract\Processor\Processor;
8+
use KaririCode\Contract\Processor\ProcessorValidator;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class ProcessorValidatorTest extends TestCase
13+
{
14+
public function testValidateReturnsNullOnValidProcessor(): void
15+
{
16+
/** @var Processor|MockObject */
17+
$processor = $this->createMock(Processor::class);
18+
/** @var ProcessorValidator|MockObject */
19+
$validator = $this->createMock(ProcessorValidator::class);
20+
21+
$validator->expects($this->once())
22+
->method('validate')
23+
->with($processor, 'testProcessor', [])
24+
->willReturn(null);
25+
26+
$this->assertNull($validator->validate($processor, 'testProcessor', []));
27+
}
28+
29+
public function testValidateReturnsErrorDetailsOnInvalidProcessor(): void
30+
{
31+
/** @var Processor|MockObject */
32+
$processor = $this->createMock(Processor::class);
33+
/** @var ProcessorValidator|MockObject */
34+
$validator = $this->createMock(ProcessorValidator::class);
35+
36+
$errorDetails = ['error' => 'Invalid processor'];
37+
38+
$validator->expects($this->once())
39+
->method('validate')
40+
->with($processor, 'testProcessor', [])
41+
->willReturn($errorDetails);
42+
43+
$this->assertSame($errorDetails, $validator->validate($processor, 'testProcessor', []));
44+
}
45+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Tests\Processor;
6+
7+
use KaririCode\Contract\Processor\ValidatableProcessor;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class ValidatableProcessorTest extends TestCase
12+
{
13+
public function testIsValidReturnsTrueForValidProcessor(): void
14+
{
15+
/** @var ValidatableProcessor|MockObject */
16+
$processor = $this->createMock(ValidatableProcessor::class);
17+
18+
$processor->expects($this->once())
19+
->method('isValid')
20+
->willReturn(true);
21+
22+
$this->assertTrue($processor->isValid());
23+
}
24+
25+
public function testIsValidReturnsFalseForInvalidProcessor(): void
26+
{
27+
/** @var ValidatableProcessor|MockObject */
28+
$processor = $this->createMock(ValidatableProcessor::class);
29+
30+
$processor->expects($this->once())
31+
->method('isValid')
32+
->willReturn(false);
33+
34+
$this->assertFalse($processor->isValid());
35+
}
36+
37+
public function testGetErrorKeyReturnsNullForValidProcessor(): void
38+
{
39+
/** @var ValidatableProcessor|MockObject */
40+
$processor = $this->createMock(ValidatableProcessor::class);
41+
42+
$processor->expects($this->once())
43+
->method('getErrorKey')
44+
->willReturn(null);
45+
46+
$this->assertNull($processor->getErrorKey());
47+
}
48+
49+
public function testGetErrorKeyReturnsErrorKeyForInvalidProcessor(): void
50+
{
51+
/** @var ValidatableProcessor|MockObject */
52+
$processor = $this->createMock(ValidatableProcessor::class);
53+
54+
$errorKey = 'error_key_123';
55+
56+
$processor->expects($this->once())
57+
->method('getErrorKey')
58+
->willReturn($errorKey);
59+
60+
$this->assertSame($errorKey, $processor->getErrorKey());
61+
}
62+
}

0 commit comments

Comments
 (0)