@@ -13,14 +13,14 @@ use Tester\Assert;
1313require __DIR__ . '/../bootstrap.php ' ;
1414
1515
16- function randomStr ()
16+ function randomStr (): string
1717{
1818 $ s = str_repeat ('LaTrine ' , rand (100 , 20000 ));
1919 return md5 ($ s , true ) . $ s ;
2020}
2121
2222
23- function checkStr ($ s )
23+ function checkStr (string $ s ): bool
2424{
2525 return substr ($ s , 0 , 16 ) === md5 (substr ($ s , 16 ), true );
2626}
@@ -32,42 +32,38 @@ set_time_limit(0);
3232
3333// clear playground
3434for ($ i = 0 ; $ i <= COUNT_FILES ; $ i ++) {
35- file_put_contents ( ' nette.safe:// ' . TEMP_DIR . '/testfile ' . $ i, randomStr () );
35+ @ unlink ( TEMP_DIR . '/testfile ' . $ i );
3636}
3737
3838// test loop
39- $ hits = ['ok ' => 0 , 'notfound ' => 0 , 'error ' => 0 , 'cantwrite ' => 0 , 'cantdelete ' => 0 ];
39+ $ hits = ['ok ' => 0 , 'notfound ' => 0 , 'notsame ' => 0 , 'empty ' => 0 , 'cantwrite ' => 0 ];
4040
41- for ($ counter = 0 ; $ counter < 300 ; $ counter ++) {
41+ for ($ counter = 0 ; $ counter < 3000 ; $ counter ++) {
4242 // write
4343 $ ok = @file_put_contents ('nette.safe:// ' . TEMP_DIR . '/testfile ' . rand (0 , COUNT_FILES ), randomStr ());
4444 if ($ ok === false ) {
4545 $ hits ['cantwrite ' ]++;
4646 }
4747
4848 // delete
49- /*$ok = @unlink('nette.safe://' . TEMP_DIR . '/testfile' . rand(0, COUNT_FILES));
50- if (!$ok) {
51- $hits['cantdelete']++;
52- }*/
49+ @unlink ('nette.safe:// ' . TEMP_DIR . '/testfile ' . rand (0 , COUNT_FILES ));
5350
5451 // read
5552 $ res = @file_get_contents ('nette.safe:// ' . TEMP_DIR . '/testfile ' . rand (0 , COUNT_FILES ));
5653
5754 // compare
5855 if ($ res === false ) {
5956 $ hits ['notfound ' ]++;
57+ } elseif ($ res === '' ) {
58+ $ hits ['empty ' ]++;
6059 } elseif (checkStr ($ res )) {
6160 $ hits ['ok ' ]++;
6261 } else {
63- $ hits ['error ' ]++;
62+ $ hits ['notsame ' ]++;
6463 }
6564}
6665
67- Assert::same ([
68- 'ok ' => $ counter , // should be 1000. If unlink() is used, sum [ok] + [notfound] should be 1000
69- 'notfound ' => 0 , // means 'file not found', should be 0 if unlink() is not used
70- 'error ' => 0 , // means 'file contents is damaged', MUST be 0
71- 'cantwrite ' => 0 , // means 'somebody else is writing this file'
72- 'cantdelete ' => 0 , // means 'unlink() has timeout', should be 0
73- ], $ hits );
66+ var_export ($ hits );
67+ Assert::same ($ counter , $ hits ['ok ' ] + $ hits ['notfound ' ]);
68+ Assert::same (0 , $ hits ['notsame ' ], 'file contents is damaged ' );
69+ Assert::same (0 , $ hits ['empty ' ], 'file hasn \'t been written yet ' );
0 commit comments