From 34440f344a88d4887ef6166168da30581f35295c Mon Sep 17 00:00:00 2001 From: M1Screw <14369594+M1Screw@users.noreply.github.com> Date: Wed, 12 Jul 2023 20:13:41 +0800 Subject: [PATCH] feat: config & qrcode unit test --- src/Services/DB.php | 90 ----------------- src/Utils/QRcode.php | 19 +++- tests/App/Services/ConfigTest.php | 161 ++++++++++++++++++++++++++++++ tests/App/Utils/QRcodeTest.php | 36 +++++++ tests/App/Utils/qrcode.png | Bin 0 -> 1225 bytes 5 files changed, 213 insertions(+), 93 deletions(-) create mode 100644 tests/App/Services/ConfigTest.php create mode 100644 tests/App/Utils/QRcodeTest.php create mode 100644 tests/App/Utils/qrcode.png diff --git a/src/Services/DB.php b/src/Services/DB.php index 3864848342..9f0a68466a 100644 --- a/src/Services/DB.php +++ b/src/Services/DB.php @@ -4,99 +4,9 @@ namespace App\Services; -use Closure; use Exception; -use Generator; -use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Capsule\Manager; -use Illuminate\Database\Connection; -use Illuminate\Database\DatabaseTransactionsManager; -use Illuminate\Database\Grammar; -use Illuminate\Database\Query\Builder; -use Illuminate\Database\Query\Expression; -use Illuminate\Database\Query\Grammars\Grammar as QueryGrammar; -use Illuminate\Database\Query\Processors\Processor; -use Illuminate\Database\Schema\Builder as SchemaBuilder; -use PDO; -use PDOStatement; -/** - * @codingStandardsIgnoreStart - * - * @see Connection - * - * @method static void useDefaultQueryGrammar() Set the query grammar to the default implementation. - * @method static void useDefaultSchemaGrammar() Set the schema grammar to the default implementation. - * @method static void useDefaultPostProcessor() Set the query post processor to the default implementation. - * @method static SchemaBuilder getSchemaBuilder() Get a schema builder instance for the connection. - * @method static Builder table(Closure|Builder|string $table, string|null $as = null) Begin a fluent query against a database table. - * @method static Builder query() Get a new query builder instance. - * @method static mixed selectOne(string $query, array $bindings = [], bool $useReadPdo = true) Run a select statement and return a single result. - * @method static array selectFromWriteConnection(string $query, array $bindings = []) Run a select statement against the database. - * @method static array select(string $query, array $bindings = [], bool $useReadPdo = true) Run a select statement against the database. - * @method static Generator cursor(string $query, array $bindings = [], bool $useReadPdo = true) Run a select statement against the database and returns a generator. - * @method static bool insert(string $query, array $bindings = []) Run an insert statement against the database. - * @method static int update(string $query, array $bindings = []) Run an update statement against the database. - * @method static int delete(string $query, array $bindings = []) Run a delete statement against the database. - * @method static bool statement(string $query, array $bindings = []) Execute an SQL statement and return the boolean result. - * @method static int affectingStatement(string $query, array $bindings = []) Run an SQL statement and get the number of rows affected. - * @method static bool unprepared($query) Run a raw, unprepared query against the PDO connection. - * @method static array pretend(Closure $callback) Execute the given callback in "dry run" mode. - * @method static void bindValues(PDOStatement $statement, array $bindings) Bind values to their parameters in the given statement. - * @method static array prepareBindings(array $bindings) Prepare the query bindings for execution. - * @method static void logQuery(string $query, array $bindings, float|null $time = null) Log a query in the connection's query log. - * @method static void reconnect() Reconnect to the database. - * @method static void disconnect() Disconnect from the underlying PDO connection. - * @method static void listen(Closure $callback) Register a database query listener with the connection. - * @method static Expression raw($value) Get a new raw query expression. - * @method static void recordsHaveBeenModified(bool $value = true) Indicate if any records have been modified. - * @method static void forgetRecordModificationState() Reset the record modification state. - * ---- Method about Doctrine which is not available should be here. ----- - * @method static PDO getPdo() Get the current PDO connection. - * @method static PDO|Closure|null getRawPdo() Get the current PDO connection parameter without executing any reconnect logic. - * @method static PDO getReadPdo() Get the current PDO connection used for reading. - * @method static PDO|Closure|null getRawReadPdo() Get the current read PDO connection parameter without executing any reconnect logic. - * @method static Connection setPdo(PDO|Closure|null $pdo) Set the PDO connection. - * @method static Connection setReconnector(callable $reconnector) Set the reconnect instance on the connection. - * @method static string|null getName() Get the database connection name. - * @method static mixed getConfig(string|null $option = null) Get an option from the configuration options. - * @method static string getDriverName() Get the PDO driver name. - * @method static QueryGrammar getQueryGrammar() Get the query grammar used by the connection. - * @method static Connection setQueryGrammar(QueryGrammar $grammar) Set the query grammar used by the connection. - * @method static QueryGrammar getSchemaGrammar() Get the schema grammar used by the connection. - * @method static Connection setSchemaGrammar(QueryGrammar $grammar) Set the schema grammar used by the connection. - * @method static Processor getPostProcessor() Get the query post processor used by the connection. - * @method static Connection setPostProcessor(Processor $processor) Set the query post processor used by the connection. - * @method static Dispatcher getEventDispatcher() Get the event dispatcher used by the connection. - * @method static Connection setEventDispatcher(Dispatcher $events) Set the event dispatcher instance on the connection. - * @method static void unsetEventDispatcher() Unset the event dispatcher for this connection. - * @method static Connection setTransactionManager(DatabaseTransactionsManager $manager) Set the transaction manager instance on the connection. - * @method static void unsetTransactionManager() Unset the transaction manager for this connection. - * @method static bool pretending() Determine if the connection is in a "dry run". - * @method static array getQueryLog() Get the connection query log. - * @method static void flushQueryLog() Clear the query log. - * @method static void enableQueryLog() Enable the query log on the connection. - * @method static void disableQueryLog() Disable the query log on the connection. - * @method static bool logging() Determine whether we're logging queries. - * @method static string getDatabaseName() Get the name of the connected database. - * @method static Connection setDatabaseName(string $database) Set the name of the connected database. - * @method static string getTablePrefix() Get the table prefix for the connection. - * @method static Connection setTablePrefix(string $prefix) Set the table prefix in use by the connection. - * @method static Grammar withTablePrefix(Grammar $grammar) Set the table prefix and return the grammar. - * @method static void resolverFor(string $driver, Closure $callback) Register a connection resolver. - * @method static mixed getResolver(string $driver) Get the connection resolver for the given driver. - * - * @see \Illuminate\Database\Concerns\ManagesTransactions - * - * @method static mixed transaction(Closure $callback, int $attempts = 1) Execute a Closure within a transaction. - * @method static void beginTransaction() Start a new database transaction. - * @method static void commit() Commit the active database transaction. - * @method static void rollBack(int|null $toLevel = null) Rollback the active database transaction. - * @method static int transactionLevel() Get the number of active transactions. - * @method static void afterCommit(callable $callback) Execute the callback after a transaction commits. - * - * @codingStandardsIgnoreEnd - */ final class DB extends Manager { public static function init(): void diff --git a/src/Utils/QRcode.php b/src/Utils/QRcode.php index ff5672e6bc..435ac23a80 100644 --- a/src/Utils/QRcode.php +++ b/src/Utils/QRcode.php @@ -4,7 +4,9 @@ namespace App\Utils; +use Exception; use Zxing\QrReader; +use function file_get_contents; final class QRcode { @@ -17,12 +19,23 @@ final class QRcode */ public static function decode(string $source): ?string { + if (! file_exists($source)) { + return null; + } + $img = file_get_contents($source); - $qrcode = new QrReader($img, QrReader::SOURCE_TYPE_BLOB); - $text = $qrcode->text(); - if ($text === false || $text === '') { + + try { + $qrcode = new QrReader($img, QrReader::SOURCE_TYPE_BLOB); + $text = $qrcode->text(); + } catch (Exception $e) { + $text = ''; + } + + if ($text === '') { return null; } + return $text; } } diff --git a/tests/App/Services/ConfigTest.php b/tests/App/Services/ConfigTest.php new file mode 100644 index 0000000000..06bbbf81e9 --- /dev/null +++ b/tests/App/Services/ConfigTest.php @@ -0,0 +1,161 @@ + 'My App', + 'baseUrl' => 'https://example.com', + 'enable_checkin' => true, + 'checkinMin' => 10, + 'checkinMax' => 20, + 'jump_delay' => 5, + 'enable_analytics_code' => false, + 'enable_kill' => true, + 'enable_change_email' => false, + 'enable_telegram' => true, + 'telegram_bot' => 'my_bot', + 'subscribeLog' => true, + 'subscribeLog_keep_days' => 30, + 'enable_r2_client_download' => true, + ]; + + $mockEnv = [ + 'appName' => 'My App', + 'baseUrl' => 'https://example.com', + 'enable_checkin' => true, + 'checkinMin' => 10, + 'checkinMax' => 20, + 'jump_delay' => 5, + 'enable_analytics_code' => false, + 'enable_kill' => true, + 'enable_change_email' => false, + 'enable_telegram' => true, + 'telegram_bot' => 'my_bot', + 'subscribeLog' => true, + 'subscribeLog_keep_days' => 30, + 'enable_r2_client_download' => true, + ]; + + $config = Config::getPublicConfig(); + + $this->assertSame($mockEnv['appName'], $config['appName']); + $this->assertSame($mockEnv['baseUrl'], $config['baseUrl']); + $this->assertSame($mockEnv['enable_checkin'], $config['enable_checkin']); + $this->assertSame($mockEnv['checkinMin'], $config['checkinMin']); + $this->assertSame($mockEnv['checkinMax'], $config['checkinMax']); + $this->assertSame($mockEnv['jump_delay'], $config['jump_delay']); + $this->assertSame($mockEnv['enable_analytics_code'], $config['enable_analytics_code']); + $this->assertSame($mockEnv['enable_kill'], $config['enable_kill']); + $this->assertSame($mockEnv['enable_change_email'], $config['enable_change_email']); + $this->assertSame($mockEnv['enable_telegram'], $config['enable_telegram']); + $this->assertSame($mockEnv['telegram_bot'], $config['telegram_bot']); + $this->assertSame($mockEnv['subscribeLog'], $config['subscribeLog']); + $this->assertSame($mockEnv['subscribeLog_keep_days'], $config['subscribeLog_keep_days']); + $this->assertSame($mockEnv['enable_r2_client_download'], $config['enable_r2_client_download']); + } + + /** + * @covers App\Services\Config::getRedisConfig + */ + public function testGetRedisConfig(): void + { + $_ENV = [ + 'redis_host' => 'localhost', + 'redis_port' => 6379, + 'redis_timeout' => 10, + 'redis_username' => 'myuser', + 'redis_password' => 'mypassword', + 'redis_ssl' => false, + ]; + + $mockEnv = [ + 'redis_host' => 'localhost', + 'redis_port' => 6379, + 'redis_timeout' => 10, + 'redis_username' => 'myuser', + 'redis_password' => 'mypassword', + 'redis_ssl' => false, + ]; + + $config = Config::getRedisConfig(); + + $this->assertSame($mockEnv['redis_host'], $config['host']); + $this->assertSame($mockEnv['redis_port'], $config['port']); + $this->assertSame($mockEnv['redis_timeout'], $config['connectTimeout']); + $this->assertSame([$mockEnv['redis_username'], $mockEnv['redis_password']], $config['auth']); + $this->assertSame(['verify_peer' => $mockEnv['redis_ssl']], $config['ssl']); + } + + /** + * @covers App\Services\Config::getDbConfig + */ + public function testGetDbConfig(): void + { + $_ENV = [ + 'db_driver' => 'mysql', + 'db_host' => 'localhost', + 'db_socket' => '/var/run/mysqld/mysqld.sock', + 'db_database' => 'mydb', + 'db_username' => 'myuser', + 'db_password' => 'mypassword', + 'db_charset' => 'utf8mb4', + 'db_collation' => 'utf8mb4_unicode_ci', + 'db_prefix' => '', + 'db_port' => 3306, + ]; + + $mockEnv = [ + 'db_driver' => 'mysql', + 'db_host' => 'localhost', + 'db_socket' => '/var/run/mysqld/mysqld.sock', + 'db_database' => 'mydb', + 'db_username' => 'myuser', + 'db_password' => 'mypassword', + 'db_charset' => 'utf8mb4', + 'db_collation' => 'utf8mb4_unicode_ci', + 'db_prefix' => '', + 'db_port' => 3306, + ]; + + $config = Config::getDbConfig(); + + $this->assertSame($mockEnv['db_driver'], $config['driver']); + $this->assertSame($mockEnv['db_host'], $config['host']); + $this->assertSame($mockEnv['db_socket'], $config['unix_socket']); + $this->assertSame($mockEnv['db_database'], $config['database']); + $this->assertSame($mockEnv['db_username'], $config['username']); + $this->assertSame($mockEnv['db_password'], $config['password']); + $this->assertSame($mockEnv['db_charset'], $config['charset']); + $this->assertSame($mockEnv['db_collation'], $config['collation']); + $this->assertSame($mockEnv['db_prefix'], $config['prefix']); + $this->assertSame($mockEnv['db_port'], $config['port']); + } + + /** + * @covers App\Services\Config::getSupportParam + */ + public function testGetSupportParam(): void + { + $params = Config::getSupportParam('ss_aead_method'); + + $this->assertIsArray($params); + $this->assertContains('aes-128-gcm', $params); + $this->assertContains('aes-192-gcm', $params); + $this->assertContains('aes-256-gcm', $params); + $this->assertContains('chacha20-ietf-poly1305', $params); + $this->assertContains('xchacha20-ietf-poly1305', $params); + } +} + diff --git a/tests/App/Utils/QRcodeTest.php b/tests/App/Utils/QRcodeTest.php new file mode 100644 index 0000000000..167f843480 --- /dev/null +++ b/tests/App/Utils/QRcodeTest.php @@ -0,0 +1,36 @@ +assertSame($expectedText, $result); + } + + /** + * @covers App\Utils\QRcode::decode + */ + public function testDecodeWithInvalidData(): void + { + $invalidImg = __DIR__ . '/invalid.png'; + + $result = QRcode::decode($invalidImg); + + $this->assertNull($result); + } +} diff --git a/tests/App/Utils/qrcode.png b/tests/App/Utils/qrcode.png new file mode 100644 index 0000000000000000000000000000000000000000..db332db98d7afd4f9b5972a9199d29c3f0652a96 GIT binary patch literal 1225 zcmeAS@N?(olHy`uVBq!ia0vp^FF=@s2}ow0w$}$zY)RhkE)4%caKYZ?lNlIT>O5T> zLn`LHz3Z44Z7$+)an*}$TZDVJ8ohPl_EwjDwm8k9Y0o9g$1^6asw=Jx%5Hi`>Dqtciex!Iqdhs@4x?6?Omp0ntjhQThBWCulCpT*-<-y zqCacSMZJBV_E%w_@88d#Rx_d6ysY=|x;L}><05P4m6rUyeD31BZX#a_e?x&$E5~e9vo}k88fZ#f+BYbC0jx zGyl}y&8y%3x`Rdj^!dkq=YG0fpIN%^^OjP->bP4gu71wPV%vwxI}gP>{{Q`#`JJ); z)oZK%)8PVW@!EF&@w;8@b6>wkBAkVZ=_m$p=khTYHJT{Smw_4$SC z_AP&OD)W5%?xf=wiE5VF@wrd8?0oa;^RlVG1>E-Qe}8-^A3ZJJezs%2)%!i0e%ZA| zuD8y*Ui|V6W+0rNUtBgT_Pp!vqFc5?yWiQ%rDEC^^>5DR*tEIqb4%})eEE0l@;v_S zzxHhUUHFp=iwETTkN>Tkzx5XX-uv&bzc&4%y8Ntf`CW0ttm}UdhA(Ta-hY4o`R8k{ z*5#SR*fqzCdZ1*QEr06n#6CAozgZf8F!TF^b^F#|uZ-E(gqF3oytPbUKC@K+&AQJk z!_($2wtZ{Iiy1AGEZ_Z~QOZ|(y{7!S?T*#&CvLz@-oW(#ZWsSUU@*VlwdvR2Ip6;F zp=W~3TNZEr)}4=f^+Eh8%l3z{5ZUb_73X8+$Zcd bX@AViowD~PufGH=n;1M@{an^LB{Ts5omOY| literal 0 HcmV?d00001