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+ }
0 commit comments