[Д] [HashGetType]: Добавлен параметр ByHash (для получения класса хеша путём передачи ему уже вычисленного хэша). [О] [FileHash]: __construct - $hashBy по умолчанию теперь HashGetType::ByHash. [Д] [FileHash]: Добавлены функции сериализации и десериализации (теперь класс реализует интерфейс ISerializable). [Д] [FileHash]: Добавлена функции валидации хэша Validate.
87 lines
3.4 KiB
PHP
87 lines
3.4 KiB
PHP
<?php
|
||
|
||
namespace goodboyalex\php_components_pack\tests\classes;
|
||
|
||
use goodboyalex\php_components_pack\classes\FileHash;
|
||
use goodboyalex\php_components_pack\enums\HashGetType;
|
||
use PHPUnit\Framework\TestCase;
|
||
|
||
class FileHashTest extends TestCase
|
||
{
|
||
public function test__construct ()
|
||
{
|
||
$this->PrepareForTest();
|
||
|
||
$fileHash = new FileHash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
||
$this->assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", $fileHash->Hash);
|
||
|
||
|
||
$fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
|
||
$this->assertEquals("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7", $fileHash->Hash);
|
||
|
||
$fileHash = new FileHash(__DIR__ . '/../data/A.php', HashGetType::ByFile);
|
||
$this->assertEquals("fc8dad93af5de5dd7c7d64e04faadfa22557e577a1538737fe462a5f78699fa2", $fileHash->Hash);
|
||
|
||
$fileHash = new FileHash(__DIR__ . '/../data/B.php', HashGetType::ByFile);
|
||
$this->assertEquals("f419262e46e9461517af46d0c1e4faf25f38990bb50351b691d84f1ad51f2299", $fileHash->Hash);
|
||
}
|
||
|
||
private function PrepareForTest (): void
|
||
{
|
||
require_once __DIR__ . '/../../sources/traits/EnumExtensionsTrait.php';
|
||
require_once __DIR__ . '/../../sources/enums/HashGetType.php';
|
||
require_once __DIR__ . '/../../sources/extensions/StringExtension.php';
|
||
require_once __DIR__ . '/../../sources/interfaces/ISerializable.php';
|
||
require_once __DIR__ . '/../../sources/classes/FileHash.php';
|
||
}
|
||
|
||
public function testIsEqual ()
|
||
{
|
||
$this->PrepareForTest();
|
||
|
||
$fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
|
||
|
||
$this->assertTrue($fileHash->IsEqual(new FileHash("Тестовое слово", HashGetType::ByString)));
|
||
$this->assertFalse($fileHash->IsEqual(new FileHash("Тестовое слово2", HashGetType::ByString)));
|
||
}
|
||
|
||
public function testSerialize ()
|
||
{
|
||
$this->PrepareForTest();
|
||
|
||
$fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
|
||
|
||
$serialized = $fileHash->Serialize();
|
||
|
||
$this->assertEquals("s:64:\"000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7\";", $serialized);
|
||
}
|
||
|
||
public function testUnSerialize ()
|
||
{
|
||
$this->PrepareForTest();
|
||
|
||
$fileHash = new FileHash();
|
||
|
||
$serialized = "s:64:\"000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7\";";
|
||
|
||
$fileHash->UnSerialize($serialized);
|
||
|
||
$this->assertEquals("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7", $fileHash->Hash);
|
||
}
|
||
|
||
public function testValidate ()
|
||
{
|
||
$this->PrepareForTest();
|
||
|
||
$fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
|
||
|
||
$this->assertTrue($fileHash->Validate("Тестовое слово", HashGetType::ByString));
|
||
$this->assertTrue($fileHash->Validate("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7",
|
||
HashGetType::ByHash));
|
||
$this->assertFalse($fileHash->Validate("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7",
|
||
HashGetType::ByString));
|
||
|
||
$fileHash = new FileHash(__DIR__ . '/../data/A.php', HashGetType::ByFile);
|
||
$this->assertTrue($fileHash->Validate(__DIR__ . '/../data/A.php', HashGetType::ByFile));
|
||
}
|
||
} |