From ed7c4e74d548f940511917fd2aa9d171aba07ef8 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 25 Apr 2024 09:12:25 +0700 Subject: [PATCH] test: Add more Hash test cases --- tests/Unit/Model/HashTest.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Model/HashTest.php b/tests/Unit/Model/HashTest.php index a81fbcd..9aee3da 100644 --- a/tests/Unit/Model/HashTest.php +++ b/tests/Unit/Model/HashTest.php @@ -10,10 +10,13 @@ class HashTest extends TestCase { private ?VirtualFileSystem $fs = null; private string $tmpFile = '/path/to/vendor/composer/tmp-random'; + private string $content = 'hello world'; protected function setUp(): void { $this->fs = new VirtualFileSystem(); + $this->fs->createDirectory(\dirname($this->tmpFile), true); + $this->fs->createFile($this->tmpFile, $this->content); } protected function tearDown(): void @@ -27,12 +30,23 @@ protected function tearDown(): void */ public function testVerifyFile(bool $isValid): void { - $this->fs->createDirectory(\dirname($this->tmpFile), true); - $this->fs->createFile($this->tmpFile, $content = 'hello world'); - $hash = new Hash('md5', $isValid ? md5($content) : 'not valid'); + $hash = new Hash('md5', $isValid ? md5($this->content) : 'not valid'); $this->assertSame($isValid, $hash->verifyFile($this->getTmpFilePath())); } + public function testVerifyNotExistFile(): void + { + $hash = new Hash('any', 'any'); + $this->assertFalse($hash->verifyFile('/path/to/invalid/file.ext')); + } + + public function testInvalidAlgo(): void + { + $this->expectException(\ValueError::class); + $hash = new Hash('invalid', 'any'); + $hash->verifyFile($this->getTmpFilePath()); + } + protected function getTmpFilePath(): string { return $this->fs->path($this->tmpFile);