Skip to content

Commit

Permalink
Merge pull request #5 from KaririCode-Framework/develop
Browse files Browse the repository at this point in the history
fix: remove unused parameter from createProcessorMock method
  • Loading branch information
walmir-silva authored Oct 24, 2024
2 parents f675fb4 + 3cd3d0e commit c8bcd1a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 53 deletions.
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 11 additions & 12 deletions src/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,32 @@
use KaririCode\PropertyInspector\Utility\PropertyInspector;
use KaririCode\Sanitizer\Attribute\Sanitize;
use KaririCode\Sanitizer\Contract\SanitizationResult;
use KaririCode\Sanitizer\Contract\SanitizationResultProcessor;
use KaririCode\Sanitizer\Processor\DefaultSanitizationResultProcessor;

class Sanitizer implements SanitizerContract
{
private const IDENTIFIER = 'sanitizer';

private ProcessorBuilder $builder;
private PropertyInspector $propertyInspector;
private AttributeHandler $attributeHandler;

public function __construct(
private readonly ProcessorRegistry $registry,
private readonly SanitizationResultProcessor $resultProcessor = new DefaultSanitizationResultProcessor()
private readonly ProcessorRegistry $registry
) {
$this->builder = new ProcessorBuilder($this->registry);
$this->attributeHandler = new AttributeHandler(self::IDENTIFIER, $this->builder);
$this->propertyInspector = new PropertyInspector(
new AttributeAnalyzer(Sanitize::class)
);
}

public function sanitize(mixed $object): SanitizationResult
{
$this->propertyInspector->inspect($object, $this->attributeHandler);
$this->attributeHandler->applyChanges($object);
$attributeHandler = new AttributeHandler(self::IDENTIFIER, $this->builder);
$propertyInspector = new PropertyInspector(
new AttributeAnalyzer(Sanitize::class)
);

$propertyInspector->inspect($object, $attributeHandler);
$attributeHandler->applyChanges($object);

$resultProcessor = new DefaultSanitizationResultProcessor();

return $this->resultProcessor->process($this->attributeHandler);
return $resultProcessor->process($attributeHandler);
}
}
38 changes: 7 additions & 31 deletions tests/SanitizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use KaririCode\Contract\Processor\ProcessorRegistry;
use KaririCode\Sanitizer\Attribute\Sanitize;
use KaririCode\Sanitizer\Contract\SanitizationResult;
use KaririCode\Sanitizer\Contract\SanitizationResultProcessor;
use KaririCode\Sanitizer\Processor\Input\TrimSanitizer;
use KaririCode\Sanitizer\Sanitizer;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -18,18 +16,14 @@ final class SanitizerTest extends TestCase
{
private Sanitizer $sanitizer;
private ProcessorRegistry|MockObject $registry;
private SanitizationResultProcessor|MockObject $resultProcessor;
private SanitizationResult|MockObject $sanitizationResult;

protected function setUp(): void
{
$this->registry = $this->createMock(ProcessorRegistry::class);
$this->resultProcessor = $this->createMock(SanitizationResultProcessor::class);
$this->sanitizationResult = $this->createMock(SanitizationResult::class);
$this->sanitizer = new Sanitizer($this->registry, $this->resultProcessor);
$this->sanitizer = new Sanitizer($this->registry);
}

private function createProcessorMock(string $name, mixed $input, mixed $output): Processor|MockObject
private function createProcessorMock(mixed $input, mixed $output): Processor|MockObject
{
$processor = $this->createMock(Processor::class);
$processor->method('process')
Expand Down Expand Up @@ -76,14 +70,11 @@ public function testSanitizeProcessesObjectProperties(): void
return null;
});

$this->resultProcessor->method('process')
->willReturn($this->sanitizationResult);

$result = $this->sanitizer->sanitize($testObject);

$this->assertSame('Walmir Silva', $testObject->name);
$this->assertSame('walmir.silva@example.com', $testObject->email);
$this->assertSame($this->sanitizationResult, $result);
$this->assertInstanceOf(SanitizationResult::class, $result);
}

public function testSanitizeHandlesNonProcessableAttributes(): void
Expand All @@ -96,7 +87,6 @@ public function testSanitizeHandlesNonProcessableAttributes(): void
};

$trimProcessor = $this->createProcessorMock(
'trim',
' trim me ',
'trim me'
);
Expand All @@ -105,14 +95,11 @@ public function testSanitizeHandlesNonProcessableAttributes(): void
->with('sanitizer', 'trim')
->willReturn($trimProcessor);

$this->resultProcessor->method('process')
->willReturn($this->sanitizationResult);

$result = $this->sanitizer->sanitize($testObject);

$this->assertSame('trim me', $testObject->processable);
$this->assertSame('leave me alone', $testObject->nonProcessable);
$this->assertSame($this->sanitizationResult, $result);
$this->assertInstanceOf(SanitizationResult::class, $result);
}

public function testSanitizeHandlesExceptionsAndUsesFallbackValue(): void
Expand All @@ -130,13 +117,10 @@ public function testSanitizeHandlesExceptionsAndUsesFallbackValue(): void
->with('sanitizer', 'problematic')
->willReturn($problematicProcessor);

$this->resultProcessor->method('process')
->willReturn($this->sanitizationResult);

$result = $this->sanitizer->sanitize($testObject);

$this->assertSame('cause problem', $testObject->problematic);
$this->assertSame($this->sanitizationResult, $result);
$this->assertInstanceOf(SanitizationResult::class, $result);
}

public function testSanitizeHandlesPrivateAndProtectedProperties(): void
Expand Down Expand Up @@ -171,14 +155,11 @@ public function getProtectedProp(): string
->with('sanitizer', 'trim')
->willReturn($trimProcessor);

$this->resultProcessor->method('process')
->willReturn($this->sanitizationResult);

$result = $this->sanitizer->sanitize($testObject);

$this->assertSame('private', $testObject->getPrivateProp());
$this->assertSame('protected', $testObject->getProtectedProp());
$this->assertSame($this->sanitizationResult, $result);
$this->assertInstanceOf(SanitizationResult::class, $result);
}

public function testSanitizeHandlesMultipleProcessorsForSingleProperty(): void
Expand All @@ -189,13 +170,11 @@ public function testSanitizeHandlesMultipleProcessorsForSingleProperty(): void
};

$trimProcessor = $this->createProcessorMock(
'trim',
' hello world ',
'hello world'
);

$uppercaseProcessor = $this->createProcessorMock(
'uppercase',
'hello world',
'HELLO WORLD'
);
Expand All @@ -206,12 +185,9 @@ public function testSanitizeHandlesMultipleProcessorsForSingleProperty(): void
['sanitizer', 'uppercase', $uppercaseProcessor],
]);

$this->resultProcessor->method('process')
->willReturn($this->sanitizationResult);

$result = $this->sanitizer->sanitize($testObject);

$this->assertSame('HELLO WORLD', $testObject->multiProcessed);
$this->assertSame($this->sanitizationResult, $result);
$this->assertInstanceOf(SanitizationResult::class, $result);
}
}

0 comments on commit c8bcd1a

Please sign in to comment.