Skip to content

Commit 9d992db

Browse files
committed
Update PHP minimum version to 8.0 and dependencies to latest compatible version
1 parent e404daa commit 9d992db

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/Adapter/Local/File.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use Platine\Filesystem\DirectoryInterface;
5151
use Platine\Filesystem\FileInterface;
5252
use Platine\Stdlib\Helper\Path;
53+
use RuntimeException;
5354

5455
/**
5556
* @class File
@@ -135,6 +136,19 @@ public function getType(): string
135136
return 'file';
136137
}
137138

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+
138152
/**
139153
* {@inheritdoc}
140154
*/

src/FileInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public function create(
7272
*/
7373
public function read(): string;
7474

75+
/**
76+
* Return the checksum of file content
77+
* @return string
78+
*/
79+
public function checksum(): string;
80+
7581
/**
7682
* Write the file content
7783
* @param string $content

tests/Adapter/Local/FileTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Platine\Filesystem\Adapter\Local\File;
1212
use Platine\Filesystem\Adapter\Local\LocalAdapter;
1313
use Platine\Filesystem\FileInterface;
14+
use RuntimeException;
1415

1516
/**
1617
* File class tests
@@ -64,6 +65,7 @@ public function testWrite(): void
6465
$t = new File($file->url(), $adapter);
6566
$t->write('bar');
6667
$this->assertEquals('bar', $t->read());
68+
$this->assertEquals('37b51d194a7513e45b56f6524f2d51f2', $t->checksum());
6769
}
6870

6971
public function testCreate(): void
@@ -79,6 +81,18 @@ public function testCreate(): void
7981
$this->assertTrue($file->isFile());
8082
}
8183

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+
8296
public function testGetType(): void
8397
{
8498
$adapter = $this->getMockInstance(

tests/fixtures/mocks.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@
1111
$mock_is_file_to_true = false;
1212
$mock_is_dir_to_true = false;
1313
$mock_scandir_to_false = false;
14+
$mock_md5_file_to_false = false;
1415
$mock_fileperms_to_false = false;
1516
$mock_is_writable_to_false = false;
1617
$mock_is_writable_to_true = false;
1718

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+
1829
function is_writable(string $key)
1930
{
2031
global $mock_is_writable_to_true,

0 commit comments

Comments
 (0)