1+ <?php
2+ namespace Ddrv \Tests \Iptool ;
3+
4+ use PHPUnit \Framework \TestCase ;
5+ use Ddrv \Iptool \Iptool ;
6+ use Ddrv \Iptool \Converter ;
7+
8+ /**
9+ * @covers Iptool
10+ * @covers Converter
11+ */
12+ class IptoolTest extends TestCase
13+ {
14+ public function testCreateDatabase ()
15+ {
16+ $ time = time ();
17+ $ author = 'Unit Test ' ;
18+ $ license = 'Test License ' ;
19+ $ tmpDir = __DIR__ .DIRECTORY_SEPARATOR .'tmp ' ;
20+ $ csvDir = __DIR__ .DIRECTORY_SEPARATOR .'csv ' ;
21+ $ dbFile = __DIR__ .DIRECTORY_SEPARATOR .'dat ' .DIRECTORY_SEPARATOR .'iptool.test.dat ' ;
22+ $ converter = new Converter ($ tmpDir );
23+
24+ $ converter ->setAuthor ($ author );
25+ $ converter ->setTime ($ time );
26+ $ converter ->setLicense ($ license );
27+
28+ $ converter ->addCSV ('infoCSV ' ,$ csvDir .DIRECTORY_SEPARATOR .'info.csv ' ,2 );
29+ $ converter ->addCSV ('networksCSV ' ,$ csvDir .DIRECTORY_SEPARATOR .'networks.csv ' ,1 );
30+
31+ $ info = array (
32+ 'interval ' => array (
33+ 'type ' => 'int ' ,
34+ 'column ' => 1 ,
35+ ),
36+ 'caption ' => array (
37+ 'type ' => 'string ' ,
38+ 'column ' => 2 ,
39+ 'transform ' => 'low ' ,
40+ ),
41+ 'extendedInfo ' => array (
42+ 'type ' => 'string ' ,
43+ 'column ' => 3 ,
44+ ),
45+ );
46+ $ converter ->addRegister ('info ' ,'infoCSV ' ,0 , $ info );
47+
48+ $ networks = array (
49+ 'info ' => 2 ,
50+ );
51+ $ converter ->addNetworks ('networksCSV ' , 'ip ' , 0 , 1 , $ networks );
52+ $ converter ->create ($ dbFile );
53+
54+ $ iptool = new Iptool ($ dbFile );
55+ $ meta = $ iptool ->about ();
56+
57+ $ this ->assertSame ($ meta ['created ' ], $ time );
58+ $ this ->assertSame ($ meta ['author ' ], $ author );
59+ $ this ->assertSame ($ meta ['license ' ], $ license );
60+ $ this ->assertSame ($ meta ['networks ' ]['count ' ], 4 );
61+ $ this ->assertSame ($ meta ['networks ' ]['data ' ]['info ' ][0 ], 'interval ' );
62+ $ this ->assertSame ($ meta ['networks ' ]['data ' ]['info ' ][1 ], 'caption ' );
63+ $ this ->assertSame ($ meta ['networks ' ]['data ' ]['info ' ][2 ], 'extendedInfo ' );
64+
65+ $ info = $ iptool ->find ('0.0.0.1 ' );
66+ $ this ->assertSame ($ info ['network ' ]['first ' ], '0.0.0.0 ' );
67+ $ this ->assertSame ($ info ['network ' ]['last ' ], '63.255.255.255 ' );
68+ $ this ->assertSame ($ info ['data ' ]['info ' ]['interval ' ], 1 );
69+ $ this ->assertSame ($ info ['data ' ]['info ' ]['caption ' ], 'some info 5 ' );
70+ $ this ->assertSame ($ info ['data ' ]['info ' ]['extendedInfo ' ], 'some info 6 ' );
71+
72+ $ info = $ iptool ->find ('64.0.10.0 ' );
73+ $ this ->assertSame ($ info ['network ' ]['first ' ], '64.0.0.0 ' );
74+ $ this ->assertSame ($ info ['network ' ]['last ' ], '127.255.255.255 ' );
75+ $ this ->assertSame ($ info ['data ' ]['info ' ]['interval ' ], 2 );
76+ $ this ->assertSame ($ info ['data ' ]['info ' ]['caption ' ], 'some info 7 ' );
77+ $ this ->assertSame ($ info ['data ' ]['info ' ]['extendedInfo ' ], 'some info 8 ' );
78+
79+ $ info = $ iptool ->find ('128.100.0.13 ' );
80+ $ this ->assertSame ($ info ['network ' ]['first ' ], '128.0.0.0 ' );
81+ $ this ->assertSame ($ info ['network ' ]['last ' ], '191.255.255.255 ' );
82+ $ this ->assertSame ($ info ['data ' ]['info ' ]['interval ' ], 3 );
83+ $ this ->assertSame ($ info ['data ' ]['info ' ]['caption ' ], 'some info 1 ' );
84+ $ this ->assertSame ($ info ['data ' ]['info ' ]['extendedInfo ' ], 'some info 2 ' );
85+
86+ $ info = $ iptool ->find ('202.100.0.13 ' );
87+ $ this ->assertSame ($ info ['network ' ]['first ' ], '192.0.0.0 ' );
88+ $ this ->assertSame ($ info ['network ' ]['last ' ], '255.255.255.255 ' );
89+ $ this ->assertSame ($ info ['data ' ]['info ' ]['interval ' ], 4 );
90+ $ this ->assertSame ($ info ['data ' ]['info ' ]['caption ' ], 'some info 3 ' );
91+ $ this ->assertSame ($ info ['data ' ]['info ' ]['extendedInfo ' ], 'some info 4 ' );
92+
93+ unlink ($ dbFile );
94+ $ tmpFiles = glob ($ tmpDir .DIRECTORY_SEPARATOR .'* ' );
95+ foreach ($ tmpFiles as $ tmpFile ) {
96+ unlink ($ tmpFile );
97+ }
98+ }
99+ }
0 commit comments