Skip to content

Commit 5c1e88b

Browse files
committed
feat: ripristino unit test basilare
1 parent 68ca448 commit 5c1e88b

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
},
6868
"require-dev": {
6969
"friendsofphp/php-cs-fixer": "^3.53",
70+
"phpunit/phpunit": "^11.5",
7071
"rector/rector": "^1.0"
7172
},
7273
"autoload": {

tests/GeneratorTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
use Util\Generator;
4+
5+
class GeneratorTest extends \PHPUnit\Framework\TestCase
6+
{
7+
public function testNumbersWithPrefix()
8+
{
9+
$this->test(null, '|TEST');
10+
}
11+
12+
public function testNumbersWithSuffix()
13+
{
14+
$this->test('|TEST');
15+
}
16+
17+
public function testCommonNumbers()
18+
{
19+
$this->test();
20+
}
21+
22+
public function testDates()
23+
{
24+
$this->test('/YYYY');
25+
$this->test('/yy');
26+
27+
$this->test(null, 'YYYY-');
28+
$this->test(null, 'yy-');
29+
}
30+
31+
protected function test($prefix = null, $suffix = null)
32+
{
33+
$date = date('Y-m-d H:i:s');
34+
$info = Generator::dateToPattern($date);
35+
36+
// Individuazione valori relativi a suffisso e prefisso
37+
$prefix_value = Generator::complete($prefix, $info);
38+
$suffix_value = Generator::complete($suffix, $info);
39+
40+
$step = 3;
41+
42+
// Pattern di base con numero di caratteri incrementale
43+
$pattern = $prefix.'#'.$suffix;
44+
45+
$previous = null;
46+
for ($i = 0; $i < 10000; $i = $i + $step) {
47+
$value = $prefix_value.$this->pad($i + 1, $length).$suffix_value;
48+
$this->assertEquals($value, Generator::generate($pattern, $previous, $step, $info));
49+
50+
$previous = $value;
51+
}
52+
53+
// Pattern con padding
54+
$length = 5;
55+
$pattern = $prefix.str_repeat('#', $length).$suffix;
56+
57+
$previous = null;
58+
for ($i = 0; $i < 10000; $i = $i + $step) {
59+
$value = $prefix_value.$this->pad($i + 1, $length).$suffix_value;
60+
$this->assertEquals($value, Generator::generate($pattern, $previous, $step, $info));
61+
62+
$previous = $value;
63+
}
64+
}
65+
66+
protected function pad($number, $length)
67+
{
68+
return str_pad($number, $length, '0', STR_PAD_LEFT);
69+
}
70+
}

tests/_bootstrap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Codeception\Util\Autoload;
4+
5+
// Caricamento delle dipendenze e delle librerie del progetto
6+
$namespaces = require_once __DIR__.'/../config/namespaces.php';
7+
foreach ($namespaces as $path => $namespace) {
8+
Autoload::addNamespace($namespace.'\\', __DIR__.'/../'.$path.'/custom/src');
9+
Autoload::addNamespace($namespace.'\\', __DIR__.'/../'.$path.'/src');
10+
}

0 commit comments

Comments
 (0)