diff --git a/.gitignore b/.gitignore index ae21c53..3c5ce2c 100644 --- a/.gitignore +++ b/.gitignore @@ -2232,6 +2232,4 @@ FodyWeavers.xsd /vendor/phpunit/phpunit/src/TextUI/Command/Commands/CheckPhpConfigurationCommand.php /vendor/phpunit/phpunit/src/Framework/Exception/ErrorLogNotWritableException.php /vendor/phpunit/phpunit/src/Runner/ShutdownHandler.php -/.idea/codeStyles/ - -/tests/secret_not_in_git/ \ No newline at end of file +/.idea/codeStyles/ \ No newline at end of file diff --git a/tests/123.php b/tests/123.php deleted file mode 100644 index a814366..0000000 --- a/tests/123.php +++ /dev/null @@ -1 +0,0 @@ -ToString(); + } + + /** + * Преобразует строковое представление GUID в объект GUID. + * + * @param string $guid Строковое представление GUID. + * + * @return GUID Преобразует строковое представление GUID в объект GUID или GUID_EMPTY. + */ + public static function GUIDFromString (string $guid): GUID + { + return GUID::Parse($guid, true); + } + + /** + * Проверяет равенство двух GUID. + * + * @param GUID $guid1 GUID 1. + * @param GUID $guid2 GUID 2. + * + * @return bool true, если GUID равны, false - иначе. + */ + public static function IsGUIDEqual (GUID $guid1, GUID $guid2): bool + { + return $guid1->IsEqualsTo($guid2); + } + + /** + * Преобразует объект перечисления в значение. + * + * @param BackedEnum $enum Перечисление. + * + * @return int Значение перечисления. + */ + public static function BackedEnumToInt (BackedEnum $enum): int + { + return $enum->value; + } + + /** + * + * @param int $value Значение перечисления. + * + * @return BackedEnum Перечисление. + */ + public static function BackedEnumFromInt (int $value): BackedEnum + { + return BackedEnum::tryFrom($value); + } + + /** + * Сравнивает два значения перечисления. + * + * @param BackedEnum $enum1 Перечисление 1. + * @param BackedEnum $enum2 Перечисление 2. + * + * @return bool true, если значения равны, false - иначе. + */ + public static function IsBackedEnumEqual (BackedEnum $enum1, BackedEnum $enum2): bool + { + return $enum1->value === $enum2->value; + } + + /** + * Преобразует логическое значение в целочисленное. + * + * @param bool $boolValue Значение. + * + * @return int Значение 0 или 1. + */ + public static function BoolToInt (bool $boolValue): int + { + return $boolValue ? 1 : 0; + } + + /** + * Преобразует целочисленное значение в логическое. + * + * @param int $value Значение 0 или 1. + * + * @return bool Логическое значение. + */ + public static function BoolFromInt (int $value): bool + { + return $value > 0; + } + + /** + * Сравнивает два логических значения. + * + * @param bool $bool1 Значение 1. + * @param bool $bool2 Значение 2. + * + * @return bool true, если значения равны, false - иначе. + */ + public static function IsBoolEqual (bool $bool1, bool $bool2): bool + { + return ($bool1 == true && $bool2 == true) || ($bool1 == false && $bool2 == false); + } + + /** + * Преобразует массив в строку. + * + * @param array $array Массив. + * + * @return string Сериализованный массив. + */ + public static function ArrayToString (array $array): string + { + return serialize($array); + } + + /** + * Преобразует строковое представление массива в массив. + * + * @param string $value Строковое представление массива. + * + * @return array Массив. + */ + public static function ArrayFromString (string $value): array + { + return unserialize($value); + } + + /** + * Сравнивает два массива. + * + * @param array $array1 Массив 1. + * @param array $array2 Массив 2. + * + * @return bool true, если значения равны, false - иначе. + */ + public static function IsArrayEqual (array $array1, array $array2): bool + { + // Получаем разницу между массивами + $difference = array_diff($array1, $array2); + + // Если разница пуста, значит массивы равны + return empty($difference); + } + } \ No newline at end of file diff --git a/tests/secret_not_in_git/DatabaseTest.php b/tests/secret_not_in_git/DatabaseTest.php new file mode 100644 index 0000000..5837720 --- /dev/null +++ b/tests/secret_not_in_git/DatabaseTest.php @@ -0,0 +1,121 @@ +IncludeReq(); + + $db = $this->GetDatabase(); + + if (!$db->IsTableExist('users')) + die('Таблица не существует. Сперва запустите тест testCreateTable / Table does not exist. Run testCreateTable first'); + + if ($db->IsExist( + table: 'users', + where: new ConditionBuilder()->WhereEquals('login', 'account1') + )) + die('Запись с логином account1 уже существует в таблице users. Сперва удалите запись с логином account1 (можно использовать метод testDelete) / Record with login account1 already exists in table users. Delete record with login account1 first'); + + $model = new UserModel( + id: GUID::Generate(), + login: "account1", + password: "password1", + salt: GUID_EMPTY, + firstName: "firstName1", + middleName: "middleName1", + lastName: "lastName1", + email: "email1@ya.ru", + isEmailVerified: false, + groupName: "default", + createdAt: time(), + updatedAt: 0, + userData: ['key1' => 'value1', 'key2' => 'value2'] + ); + + $result = $db->Insert('users', $model); + + $this->assertTrue($result !== false); + } + + public function testQuery () {} + + public function testQueryLast () {} + + public function testCreateTable () + { + $this->IncludeReq(); + + $db = $this->GetDatabase(); + + $db->CreateTable( + tableName: 'users', + dbItemClass: '\goodboyalex\php_db_components_pack\tests\secret_not_in_git\UserModel' + ); + + $this->assertTrue($db->IsTableExist('users')); + } + + public function testQueryFirst () {} + + public function testCount () {} + + public function testGetCol () {} + + public function testGetRows () {} + + public function testIsTableExist () {} + + public function testDropTable () {} + + public function testGetValue () {} + + public function testUpdate () {} + + public function testIsExist () {} + + public function testDelete () {} + + private function IncludeReq (): void + { + require_once 'Converters.php'; + require_once 'UserModel.php'; + } + + private function GetDatabase (): Database + { + $config = new DBConfig( + driver: DBDriver::MSSQL, + host: '46.172.4.201', + port: 17879, + name: 'php_db_components_test', + userName: "db_user", + password: 'hxcssuy:oI.CQ%.zbw=As$Lj7=]+nf' + ); + + $onException = fn (Exception $exception, bool $terminate) + => $terminate + ? die($exception->getMessage()) + : print $exception->getMessage(); + + return new Database($config, $onException); + } + } \ No newline at end of file diff --git a/tests/secret_not_in_git/UserModel.php b/tests/secret_not_in_git/UserModel.php new file mode 100644 index 0000000..f1dfc18 --- /dev/null +++ b/tests/secret_not_in_git/UserModel.php @@ -0,0 +1,157 @@ +Id = $id; + $this->Login = $login; + $this->Password = $password; + $this->Salt = $salt; + $this->FirstName = $firstName; + $this->MiddleName = $middleName; + $this->LastName = $lastName; + $this->Email = $email; + $this->IsEmailVerified = $isEmailVerified; + $this->GroupName = $groupName; + $this->CreatedAt = $createdAt; + $this->ModifiedAt = $updatedAt; + $this->UserData = $userData; + } + } \ No newline at end of file