|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\Test; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use rcsofttech85\FileHandler\Exception\HashException; |
| 8 | +use rcsofttech85\FileHandler\FileHandler; |
| 9 | +use rcsofttech85\FileHandler\FileHashChecker; |
| 10 | +use Symfony\Component\Dotenv\Dotenv; |
| 11 | + |
| 12 | +class FileHashCheckerTest extends TestCase |
| 13 | +{ |
| 14 | + private static string $file; |
| 15 | + private FileHashChecker|null $fileHasher = null; |
| 16 | + |
| 17 | + public static function setUpBeforeClass(): void |
| 18 | + { |
| 19 | + parent::setUpBeforeClass(); |
| 20 | + |
| 21 | + $dotenv = new Dotenv(); |
| 22 | + $dotenv->load('.env'); |
| 23 | + |
| 24 | + $file = $_ENV['FILE_NAME']; // this file contains list of all hashes |
| 25 | + |
| 26 | + self::$file = $file; |
| 27 | + |
| 28 | + |
| 29 | + file_put_contents("test", "hello world"); |
| 30 | + } |
| 31 | + |
| 32 | + public static function tearDownAfterClass(): void |
| 33 | + { |
| 34 | + parent::tearDownAfterClass(); |
| 35 | + unlink("test"); |
| 36 | + unlink("sample"); |
| 37 | + } |
| 38 | + |
| 39 | + #[Test] |
| 40 | + public function shouldGenerateValidHashForDifferentAlgo() |
| 41 | + { |
| 42 | + $expectedHash = "644bcc7e564373040999aac89e7622f3ca71fba1d972fd94a31c3bfbf24e3938"; |
| 43 | + |
| 44 | + $actualHash = $this->fileHasher->hashFile(); //default ALGO_256 |
| 45 | + |
| 46 | + $this->assertEquals($expectedHash, $actualHash); |
| 47 | + |
| 48 | + $expectedHash = "840006653e9ac9e95117a15c915caab81662918e925de9e004f774ff82d7079a40d4d27b1b372657c61d46d470304c88c788b3a4527ad074d1dccbee5dbaa99a"; |
| 49 | + |
| 50 | + $actualHash = $this->fileHasher->hashFile(FileHashChecker::ALGO_512); |
| 51 | + |
| 52 | + $this->assertEquals($expectedHash, $actualHash); |
| 53 | + } |
| 54 | + |
| 55 | + #[Test] |
| 56 | + public function checkFileIntegrityReturnsTrueIfHashMatch() |
| 57 | + { |
| 58 | + $isVerified = $this->fileHasher->verifyHash(new FileHandler(), self::$file); |
| 59 | + |
| 60 | + $this->assertTrue($isVerified); |
| 61 | + } |
| 62 | + |
| 63 | + #[Test] |
| 64 | + public function shouldReturnFalseIfFileIsModified() |
| 65 | + { |
| 66 | + $backup = file_get_contents("test"); |
| 67 | + file_put_contents("test", "modified", FILE_APPEND); |
| 68 | + |
| 69 | + $isVerified = $this->fileHasher->verifyHash(new FileHandler(), self::$file); |
| 70 | + |
| 71 | + $this->assertfalse($isVerified); |
| 72 | + |
| 73 | + file_put_contents("test", $backup); |
| 74 | + } |
| 75 | + |
| 76 | + #[Test] |
| 77 | + public function shouldReturnFalseIfDifferentAlgoIsUsedForVerifyHash() |
| 78 | + { |
| 79 | + $isVerified = $this->fileHasher->verifyHash(new FileHandler(), self::$file, FileHashChecker::ALGO_512); |
| 80 | + |
| 81 | + $this->assertFalse($isVerified); |
| 82 | + } |
| 83 | + |
| 84 | + #[Test] |
| 85 | + public function shouldThrowExceptionIfFileIsNotHashed() |
| 86 | + { |
| 87 | + file_put_contents("sample", "this file is not hashed"); |
| 88 | + $this->fileHasher = new FileHashChecker("sample"); |
| 89 | + |
| 90 | + $this->expectException(HashException::class); |
| 91 | + $this->expectExceptionMessage("this file is not hashed"); |
| 92 | + $this->fileHasher->verifyHash(new FileHandler(), self::$file, FileHashChecker::ALGO_512); |
| 93 | + } |
| 94 | + |
| 95 | + protected function setUp(): void |
| 96 | + { |
| 97 | + parent::setUp(); |
| 98 | + $this->fileHasher = new FileHashChecker("test"); |
| 99 | + } |
| 100 | + |
| 101 | + protected function tearDown(): void |
| 102 | + { |
| 103 | + parent::tearDown(); |
| 104 | + $this->fileHasher = null; |
| 105 | + } |
| 106 | +} |
0 commit comments