@@ -13,14 +13,14 @@ use Tester\Assert;
13
13
require __DIR__ . '/../bootstrap.php ' ;
14
14
15
15
16
- function randomStr ()
16
+ function randomStr (): string
17
17
{
18
18
$ s = str_repeat ('LaTrine ' , rand (100 , 20000 ));
19
19
return md5 ($ s , true ) . $ s ;
20
20
}
21
21
22
22
23
- function checkStr ($ s )
23
+ function checkStr (string $ s ): bool
24
24
{
25
25
return substr ($ s , 0 , 16 ) === md5 (substr ($ s , 16 ), true );
26
26
}
@@ -32,42 +32,38 @@ set_time_limit(0);
32
32
33
33
// clear playground
34
34
for ($ i = 0 ; $ i <= COUNT_FILES ; $ i ++) {
35
- file_put_contents ( ' nette.safe:// ' . TEMP_DIR . '/testfile ' . $ i, randomStr () );
35
+ @ unlink ( TEMP_DIR . '/testfile ' . $ i );
36
36
}
37
37
38
38
// 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 ];
40
40
41
- for ($ counter = 0 ; $ counter < 300 ; $ counter ++) {
41
+ for ($ counter = 0 ; $ counter < 3000 ; $ counter ++) {
42
42
// write
43
43
$ ok = @file_put_contents ('nette.safe:// ' . TEMP_DIR . '/testfile ' . rand (0 , COUNT_FILES ), randomStr ());
44
44
if ($ ok === false ) {
45
45
$ hits ['cantwrite ' ]++;
46
46
}
47
47
48
48
// 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 ));
53
50
54
51
// read
55
52
$ res = @file_get_contents ('nette.safe:// ' . TEMP_DIR . '/testfile ' . rand (0 , COUNT_FILES ));
56
53
57
54
// compare
58
55
if ($ res === false ) {
59
56
$ hits ['notfound ' ]++;
57
+ } elseif ($ res === '' ) {
58
+ $ hits ['empty ' ]++;
60
59
} elseif (checkStr ($ res )) {
61
60
$ hits ['ok ' ]++;
62
61
} else {
63
- $ hits ['error ' ]++;
62
+ $ hits ['notsame ' ]++;
64
63
}
65
64
}
66
65
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