diff --git a/composer.json b/composer.json index a3fa6d04c..ba9b487c0 100755 --- a/composer.json +++ b/composer.json @@ -67,6 +67,7 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.53", + "phpunit/phpunit": "^11.5", "rector/rector": "^1.0" }, "autoload": { diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php new file mode 100644 index 000000000..a5d41eae8 --- /dev/null +++ b/tests/GeneratorTest.php @@ -0,0 +1,70 @@ +test(null, '|TEST'); + } + + public function testNumbersWithSuffix() + { + $this->test('|TEST'); + } + + public function testCommonNumbers() + { + $this->test(); + } + + public function testDates() + { + $this->test('/YYYY'); + $this->test('/yy'); + + $this->test(null, 'YYYY-'); + $this->test(null, 'yy-'); + } + + protected function test($prefix = null, $suffix = null) + { + $date = date('Y-m-d H:i:s'); + $info = Generator::dateToPattern($date); + + // Individuazione valori relativi a suffisso e prefisso + $prefix_value = Generator::complete($prefix, $info); + $suffix_value = Generator::complete($suffix, $info); + + $step = 3; + + // Pattern di base con numero di caratteri incrementale + $pattern = $prefix.'#'.$suffix; + + $previous = null; + for ($i = 0; $i < 10000; $i = $i + $step) { + $value = $prefix_value.$this->pad($i + 1, $length).$suffix_value; + $this->assertEquals($value, Generator::generate($pattern, $previous, $step, $info)); + + $previous = $value; + } + + // Pattern con padding + $length = 5; + $pattern = $prefix.str_repeat('#', $length).$suffix; + + $previous = null; + for ($i = 0; $i < 10000; $i = $i + $step) { + $value = $prefix_value.$this->pad($i + 1, $length).$suffix_value; + $this->assertEquals($value, Generator::generate($pattern, $previous, $step, $info)); + + $previous = $value; + } + } + + protected function pad($number, $length) + { + return str_pad($number, $length, '0', STR_PAD_LEFT); + } +} \ No newline at end of file diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php new file mode 100644 index 000000000..cbcb837df --- /dev/null +++ b/tests/_bootstrap.php @@ -0,0 +1,10 @@ + $namespace) { + Autoload::addNamespace($namespace.'\\', __DIR__.'/../'.$path.'/custom/src'); + Autoload::addNamespace($namespace.'\\', __DIR__.'/../'.$path.'/src'); +} \ No newline at end of file