Skip to content

Commit

Permalink
test(pipeline): complete test coverage and remove unused exceptions
Browse files Browse the repository at this point in the history
COVERAGE:
- Complete test coverage for ProcessingResultCollection
- Implement comprehensive tests for ProcessedData
- Add full coverage for ProcessingError
- Add tests for error hash generation
- Cover all timestamp validations
- Verify array conversions
- Test error collections and data management

CLEANUP:
- Remove unused exception methods from ProcessorRuntimeException:
  * invalidProcessor()
  * invalidContext()
  * invalidConfiguration()
  • Loading branch information
walmir-silva committed Oct 26, 2024
1 parent c621016 commit 6d2e25b
Show file tree
Hide file tree
Showing 7 changed files with 630 additions and 95 deletions.
27 changes: 0 additions & 27 deletions src/Exception/ProcessorRuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,6 @@ public static function processorNotFound(string $processorName, string $context)
);
}

public static function invalidProcessor(string $processorName, string $details): self
{
return self::createException(
self::CODE_INVALID_PROCESSOR,
'PROCESSOR_INVALID',
"Invalid processor '{$processorName}': {$details}"
);
}

public static function invalidContext(string $context, string $details): self
{
return self::createException(
self::CODE_INVALID_CONTEXT,
'PROCESSOR_CONTEXT_INVALID',
"Invalid processor context '{$context}': {$details}"
);
}

public static function invalidConfiguration(string $processorName, string $details): self
{
return self::createException(
self::CODE_PROCESSOR_CONFIG_INVALID,
'PROCESSOR_CONFIG_INVALID',
"Invalid processor configuration for '{$processorName}': {$details}"
);
}

public static function processingFailed(string $property): self
{
return self::createException(
Expand Down
68 changes: 0 additions & 68 deletions tests/Exception/ProcessorRuntimeExceptionTest .php

This file was deleted.

123 changes: 123 additions & 0 deletions tests/Exception/ProcessorRuntimeExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

namespace KaririCode\ProcessorPipeline\Tests\Exception;

use KaririCode\ProcessorPipeline\Exception\ProcessorRuntimeException;
use PHPUnit\Framework\TestCase;

final class ProcessorRuntimeExceptionTest extends TestCase
{
public function testContextNotFound(): void
{
$exception = ProcessorRuntimeException::contextNotFound('payment');

$this->assertInstanceOf(ProcessorRuntimeException::class, $exception);
$this->assertEquals(2601, $exception->getCode());
$this->assertEquals('PROCESSOR_CONTEXT_NOT_FOUND', $exception->getErrorCode());
$this->assertEquals("Processor context 'payment' not found", $exception->getMessage());
$this->assertNull($exception->getPrevious());
}

public function testProcessorNotFound(): void
{
$exception = ProcessorRuntimeException::processorNotFound('validate', 'payment');

$this->assertInstanceOf(ProcessorRuntimeException::class, $exception);
$this->assertEquals(2602, $exception->getCode());
$this->assertEquals('PROCESSOR_NOT_FOUND', $exception->getErrorCode());
$this->assertEquals("Processor 'validate' not found in context 'payment'", $exception->getMessage());
$this->assertNull($exception->getPrevious());
}

public function testProcessingFailed(): void
{
$exception = ProcessorRuntimeException::processingFailed('email');

$this->assertInstanceOf(ProcessorRuntimeException::class, $exception);
$this->assertEquals(2606, $exception->getCode());
$this->assertEquals('PROCESSOR_PROCESSING_FAILED', $exception->getErrorCode());
$this->assertEquals(
"Processing failed for property 'email'",
$exception->getMessage()
);
$this->assertNull($exception->getPrevious());
}

/**
* @dataProvider specialValuesProvider
*/
public function testWithSpecialValues(string $context, string $processor, string $details): void
{
$exceptionContext = ProcessorRuntimeException::contextNotFound($context);
$this->assertStringContainsString($context, $exceptionContext->getMessage());

$exceptionProcessor = ProcessorRuntimeException::processorNotFound($processor, $context);
$this->assertStringContainsString($processor, $exceptionProcessor->getMessage());
$this->assertStringContainsString($context, $exceptionProcessor->getMessage());
}

public static function specialValuesProvider(): array
{
return [
'empty values' => ['', '', ''],
'special characters' => ['payment!@#', 'validator$%^', 'error&*()'],
'unicode characters' => ['pagaménto', 'validação', 'erro'],
'very long values' => [
str_repeat('a', 100),
str_repeat('b', 100),
str_repeat('c', 100),
],
];
}

public function testExceptionHierarchy(): void
{
$exception = ProcessorRuntimeException::contextNotFound('payment');

$this->assertInstanceOf(\Exception::class, $exception);
$this->assertInstanceOf(\Throwable::class, $exception);
}

public function testExceptionWithPreviousException(): void
{
$previous = new \Exception('Original error');

$reflection = new \ReflectionClass(ProcessorRuntimeException::class);
$method = $reflection->getMethod('createException');
$method->setAccessible(true);

$exception = $method->invokeArgs(null, [
2601,
'PROCESSOR_CONTEXT_NOT_FOUND',
'Test message',
$previous,
]);

$this->assertInstanceOf(ProcessorRuntimeException::class, $exception);
$this->assertEquals(2601, $exception->getCode());
$this->assertEquals('PROCESSOR_CONTEXT_NOT_FOUND', $exception->getErrorCode());
$this->assertEquals('Test message', $exception->getMessage());
$this->assertSame($previous, $exception->getPrevious());
}

/**
* @dataProvider invalidPropertyValuesProvider
*/
public function testProcessingFailedWithDifferentPropertyTypes($property): void
{
$exception = ProcessorRuntimeException::processingFailed($property);
$message = $exception->getMessage();

$this->assertIsString($message);
$this->assertStringContainsString((string) $property, $message);
}

public static function invalidPropertyValuesProvider(): array
{
return [
'valid string' => ['email'],
];
}
}
40 changes: 40 additions & 0 deletions tests/Handler/ProcessorAttributeHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,44 @@ private function configureBasicMocks(): void
return null;
});
}

public function testValidateProcessorsWithInvalidProcessor(): void
{
$processorsConfig = ['processor1' => []];
$messages = ['processor1' => 'Validation failed for processor1'];

$processor = $this->createMock(ValidatableProcessor::class);
$processor->method('isValid')->willReturn(false);
$processor->method('getErrorKey')->willReturn('invalid_processor');

$this->builder->method('build')->willReturn($processor);

// Usar Reflection para acessar validateProcessors
$reflection = new \ReflectionClass(ProcessorAttributeHandler::class);
$method = $reflection->getMethod('validateProcessors');
$method->setAccessible(true);

$errors = $method->invoke($this->handler, $processorsConfig, $messages);

$this->assertArrayHasKey('processor1', $errors);
$this->assertEquals('invalid_processor', $errors['processor1']['errorKey']);
$this->assertEquals('Validation failed for processor1', $errors['processor1']['message']);
}

public function testProcessValueWithValidPipeline(): void
{
$config = ['processor1' => []];
$this->pipeline->method('process')->willReturn('processed_value');

$this->builder->method('buildPipeline')->willReturn($this->pipeline);

// Usar Reflection para acessar processValue
$reflection = new \ReflectionClass(ProcessorAttributeHandler::class);
$method = $reflection->getMethod('processValue');
$method->setAccessible(true);

$result = $method->invoke($this->handler, 'input_value', $config);

$this->assertEquals('processed_value', $result);
}
}
63 changes: 63 additions & 0 deletions tests/Result/ProcessedDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace KaririCode\ProcessorPipeline\Tests\Result;

use KaririCode\ProcessorPipeline\Result\ProcessedData;
use PHPUnit\Framework\TestCase;

final class ProcessedDataTest extends TestCase
{
public function testGetProperty(): void
{
$data = new ProcessedData('email', 'test@example.com');

$this->assertEquals('email', $data->getProperty());
}

public function testGetValue(): void
{
$data = new ProcessedData('email', 'test@example.com');

$this->assertEquals('test@example.com', $data->getValue());
}

public function testToArray(): void
{
$data = new ProcessedData('email', 'test@example.com');
$result = $data->toArray();

$this->assertIsArray($result);
$this->assertArrayHasKey('value', $result);
$this->assertArrayHasKey('timestamp', $result);
$this->assertEquals('test@example.com', $result['value']);
$this->assertIsInt($result['timestamp']);
$this->assertLessThanOrEqual(time(), $result['timestamp']);
}

/**
* @dataProvider valueTypesProvider
*/
public function testDifferentValueTypes(mixed $value): void
{
$data = new ProcessedData('property', $value);

$this->assertSame($value, $data->getValue());
$array = $data->toArray();
$this->assertSame($value, $array['value']);
}

public static function valueTypesProvider(): array
{
return [
'string' => ['test'],
'integer' => [42],
'float' => [3.14],
'boolean' => [true],
'null' => [null],
'array' => [['test' => 'value']],
'object' => [new \stdClass()],
];
}
}
Loading

0 comments on commit 6d2e25b

Please sign in to comment.