File tree Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 50
50
use Platine \Filesystem \DirectoryInterface ;
51
51
use Platine \Filesystem \FileInterface ;
52
52
use Platine \Stdlib \Helper \Path ;
53
+ use RuntimeException ;
53
54
54
55
/**
55
56
* @class File
@@ -135,6 +136,19 @@ public function getType(): string
135
136
return 'file ' ;
136
137
}
137
138
139
+ /**
140
+ * {@inheritdoc}
141
+ */
142
+ public function checksum (): string
143
+ {
144
+ $ res = md5_file ($ this ->path );
145
+ if ($ res === false ) {
146
+ throw new RuntimeException (sprintf ('can not get checksum of file [%s] ' , $ this ->path ));
147
+ }
148
+
149
+ return $ res ;
150
+ }
151
+
138
152
/**
139
153
* {@inheritdoc}
140
154
*/
Original file line number Diff line number Diff line change @@ -72,6 +72,12 @@ public function create(
72
72
*/
73
73
public function read (): string ;
74
74
75
+ /**
76
+ * Return the checksum of file content
77
+ * @return string
78
+ */
79
+ public function checksum (): string ;
80
+
75
81
/**
76
82
* Write the file content
77
83
* @param string $content
Original file line number Diff line number Diff line change 11
11
use Platine \Filesystem \Adapter \Local \File ;
12
12
use Platine \Filesystem \Adapter \Local \LocalAdapter ;
13
13
use Platine \Filesystem \FileInterface ;
14
+ use RuntimeException ;
14
15
15
16
/**
16
17
* File class tests
@@ -64,6 +65,7 @@ public function testWrite(): void
64
65
$ t = new File ($ file ->url (), $ adapter );
65
66
$ t ->write ('bar ' );
66
67
$ this ->assertEquals ('bar ' , $ t ->read ());
68
+ $ this ->assertEquals ('37b51d194a7513e45b56f6524f2d51f2 ' , $ t ->checksum ());
67
69
}
68
70
69
71
public function testCreate (): void
@@ -79,6 +81,18 @@ public function testCreate(): void
79
81
$ this ->assertTrue ($ file ->isFile ());
80
82
}
81
83
84
+ public function testCannotGetChecksum (): void
85
+ {
86
+ global $ mock_md5_file_to_false ;
87
+
88
+ $ mock_md5_file_to_false = true ;
89
+
90
+ $ t = new File ($ this ->vfsPath ->url (), new LocalAdapter ($ this ->vfsPath ->url ()));
91
+ $ file = $ t ->create ('my_file ' , 'bar ' );
92
+ $ this ->expectException (RuntimeException::class);
93
+ $ file ->checksum ();
94
+ }
95
+
82
96
public function testGetType (): void
83
97
{
84
98
$ adapter = $ this ->getMockInstance (
Original file line number Diff line number Diff line change 11
11
$ mock_is_file_to_true = false ;
12
12
$ mock_is_dir_to_true = false ;
13
13
$ mock_scandir_to_false = false ;
14
+ $ mock_md5_file_to_false = false ;
14
15
$ mock_fileperms_to_false = false ;
15
16
$ mock_is_writable_to_false = false ;
16
17
$ mock_is_writable_to_true = false ;
17
18
19
+ function md5_file (string $ path )
20
+ {
21
+ global $ mock_md5_file_to_false ;
22
+ if ($ mock_md5_file_to_false ) {
23
+ return false ;
24
+ }
25
+
26
+ return \md5_file ($ path );
27
+ }
28
+
18
29
function is_writable (string $ key )
19
30
{
20
31
global $ mock_is_writable_to_true ,
You can’t perform that action at this time.
0 commit comments