From b550edf9c357f14f3e739f6d43ae24ea6528cce3 Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Fri, 25 Oct 2024 19:09:55 -0300 Subject: [PATCH] test(validator): enhance reset testing in AbstractValidatorProcessor Add comprehensive test coverage for reset functionality. --- .../Processor/AbstractValidatorProcessor.php | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 tests/Processor/AbstractValidatorProcessor.php diff --git a/tests/Processor/AbstractValidatorProcessor.php b/tests/Processor/AbstractValidatorProcessor.php new file mode 100644 index 0000000..35c173c --- /dev/null +++ b/tests/Processor/AbstractValidatorProcessor.php @@ -0,0 +1,144 @@ +minimumValue = $minimumValue; + } + + public function process(mixed $input): mixed + { + if (!is_int($input)) { + $this->setInvalid('invalidType'); + + return $input; + } + + if ($input < $this->minimumValue) { + $this->setInvalid('belowMinimum'); + + return $input; + } + + return $input; + } + + public function getIsValidState(): bool + { + return $this->isValid; + } + + public function getErrorKeyState(): string + { + return $this->errorKey; + } +} + +final class AbstractValidatorProcessorTest extends TestCase +{ + private TestValidatorProcessor $processor; + + protected function setUp(): void + { + $this->processor = new TestValidatorProcessor(5); + } + + public function testRestoreInitialStateAfterReset(): void + { + $this->assertTrue($this->processor->getIsValidState()); + $this->assertEmpty($this->processor->getErrorKeyState()); + + $this->processor->process('invalid'); + + $this->assertFalse($this->processor->getIsValidState()); + $this->assertNotEmpty($this->processor->getErrorKeyState()); + + $this->processor->reset(); + + $this->assertTrue($this->processor->getIsValidState()); + $this->assertEmpty($this->processor->getErrorKeyState()); + } + + public function testMultipleInvalidationsWithReset(): void + { + $this->processor->process('invalid'); + $this->assertFalse($this->processor->getIsValidState()); + $this->assertSame('invalidType', $this->processor->getErrorKeyState()); + + $this->processor->reset(); + $this->assertTrue($this->processor->getIsValidState()); + $this->assertEmpty($this->processor->getErrorKeyState()); + + $this->processor->process(3); + $this->assertFalse($this->processor->getIsValidState()); + $this->assertSame('belowMinimum', $this->processor->getErrorKeyState()); + + $this->processor->reset(); + $this->assertTrue($this->processor->getIsValidState()); + $this->assertEmpty($this->processor->getErrorKeyState()); + } + + public function testProcessingBehaviorAfterReset(): void + { + $this->processor->process('invalid'); + $this->processor->reset(); + + $validInput = 10; + $result = $this->processor->process($validInput); + + $this->assertSame($validInput, $result); + $this->assertTrue($this->processor->getIsValidState()); + $this->assertEmpty($this->processor->getErrorKeyState()); + } + + public function testResetAfterValidInput(): void + { + $this->processor->process(10); + + $this->assertTrue($this->processor->getIsValidState()); + $this->assertEmpty($this->processor->getErrorKeyState()); + + $this->processor->reset(); + + $this->assertTrue($this->processor->getIsValidState()); + $this->assertEmpty($this->processor->getErrorKeyState()); + } + + public function testSequentialProcessingWithReset(): void + { + $sequence = [ + ['input' => 'invalid', 'expectedValid' => false, 'expectedError' => 'invalidType'], + ['input' => 10, 'expectedValid' => true, 'expectedError' => ''], + ['input' => 3, 'expectedValid' => false, 'expectedError' => 'belowMinimum'], + ]; + + foreach ($sequence as $step) { + $this->processor->reset(); + $this->processor->process($step['input']); + + $this->assertSame($step['expectedValid'], $this->processor->getIsValidState()); + $this->assertSame($step['expectedError'], $this->processor->getErrorKeyState()); + } + } + + public function testConsecutiveResetsValidState(): void + { + $this->processor->process('invalid'); + + for ($i = 0; $i < 3; ++$i) { + $this->processor->reset(); + $this->assertTrue($this->processor->getIsValidState()); + $this->assertEmpty($this->processor->getErrorKeyState()); + } + } +}