Browse Source

test: add tools & config tests

M1Screw 1 year ago
parent
commit
724908ac81

+ 12 - 12
composer.lock

@@ -123,16 +123,16 @@
         },
         },
         {
         {
             "name": "aws/aws-sdk-php",
             "name": "aws/aws-sdk-php",
-            "version": "3.297.2",
+            "version": "3.297.3",
             "source": {
             "source": {
                 "type": "git",
                 "type": "git",
                 "url": "https://github.com/aws/aws-sdk-php.git",
                 "url": "https://github.com/aws/aws-sdk-php.git",
-                "reference": "bbf516a4a88f829f92cc396628be705966880dbd"
+                "reference": "6d108ecd1825f066d47a995cb2bc211d97ef0d7a"
             },
             },
             "dist": {
             "dist": {
                 "type": "zip",
                 "type": "zip",
-                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/bbf516a4a88f829f92cc396628be705966880dbd",
-                "reference": "bbf516a4a88f829f92cc396628be705966880dbd",
+                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/6d108ecd1825f066d47a995cb2bc211d97ef0d7a",
+                "reference": "6d108ecd1825f066d47a995cb2bc211d97ef0d7a",
                 "shasum": ""
                 "shasum": ""
             },
             },
             "require": {
             "require": {
@@ -212,9 +212,9 @@
             "support": {
             "support": {
                 "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
                 "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
                 "issues": "https://github.com/aws/aws-sdk-php/issues",
                 "issues": "https://github.com/aws/aws-sdk-php/issues",
-                "source": "https://github.com/aws/aws-sdk-php/tree/3.297.2"
+                "source": "https://github.com/aws/aws-sdk-php/tree/3.297.3"
             },
             },
-            "time": "2024-01-26T19:09:20+00:00"
+            "time": "2024-01-29T19:12:30+00:00"
         },
         },
         {
         {
             "name": "bacon/bacon-qr-code",
             "name": "bacon/bacon-qr-code",
@@ -4326,16 +4326,16 @@
         },
         },
         {
         {
             "name": "sentry/sentry",
             "name": "sentry/sentry",
-            "version": "4.4.0",
+            "version": "4.5.0",
             "source": {
             "source": {
                 "type": "git",
                 "type": "git",
                 "url": "https://github.com/getsentry/sentry-php.git",
                 "url": "https://github.com/getsentry/sentry-php.git",
-                "reference": "95a428a59ebddf786a27f09d19ec395a32f62082"
+                "reference": "a6e06f0b7a17e7f68e11297427da76bfe01a3ca3"
             },
             },
             "dist": {
             "dist": {
                 "type": "zip",
                 "type": "zip",
-                "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/95a428a59ebddf786a27f09d19ec395a32f62082",
-                "reference": "95a428a59ebddf786a27f09d19ec395a32f62082",
+                "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/a6e06f0b7a17e7f68e11297427da76bfe01a3ca3",
+                "reference": "a6e06f0b7a17e7f68e11297427da76bfe01a3ca3",
                 "shasum": ""
                 "shasum": ""
             },
             },
             "require": {
             "require": {
@@ -4399,7 +4399,7 @@
             ],
             ],
             "support": {
             "support": {
                 "issues": "https://github.com/getsentry/sentry-php/issues",
                 "issues": "https://github.com/getsentry/sentry-php/issues",
-                "source": "https://github.com/getsentry/sentry-php/tree/4.4.0"
+                "source": "https://github.com/getsentry/sentry-php/tree/4.5.0"
             },
             },
             "funding": [
             "funding": [
                 {
                 {
@@ -4411,7 +4411,7 @@
                     "type": "custom"
                     "type": "custom"
                 }
                 }
             ],
             ],
-            "time": "2024-01-23T09:49:55+00:00"
+            "time": "2024-01-29T16:16:10+00:00"
         },
         },
         {
         {
             "name": "slim/http",
             "name": "slim/http",

+ 1 - 1
phpunit.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?xml version="1.0" encoding="UTF-8"?>
 <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
+         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
          bootstrap="vendor/autoload.php"
          bootstrap="vendor/autoload.php"
          cacheDirectory=".phpunit.cache"
          cacheDirectory=".phpunit.cache"
          executionOrder="depends,defects"
          executionOrder="depends,defects"

+ 56 - 0
tests/App/Services/CacheTest.php

@@ -0,0 +1,56 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services;
+
+use PHPUnit\Framework\TestCase;
+
+class CacheTest extends TestCase
+{
+    /**
+     * @covers App\Services\Cache::getRedisConfig
+     */
+    public function testGetRedisConfig()
+    {
+        // Scenario 1: All parameters are set
+        $_ENV['redis_host'] = 'localhost';
+        $_ENV['redis_port'] = '6379';
+        $_ENV['redis_connect_timeout'] = '1.0';
+        $_ENV['redis_read_timeout'] = '1.0';
+        $_ENV['redis_username'] = 'username';
+        $_ENV['redis_password'] = 'password';
+        $_ENV['redis_ssl'] = true;
+        $_ENV['redis_ssl_context'] = [];
+
+        $expected1 = [
+            'host' => 'localhost',
+            'port' => '6379',
+            'connectTimeout' => '1.0',
+            'readTimeout' => '1.0',
+            'auth' => [
+                'user' => 'username',
+                'pass' => 'password',
+            ],
+            'ssl' => [],
+        ];
+
+        $result1 = Cache::getRedisConfig();
+        $this->assertEquals($expected1, $result1);
+
+        // Scenario 2: Optional parameters are not set
+        $_ENV['redis_username'] = '';
+        $_ENV['redis_password'] = '';
+        $_ENV['redis_ssl'] = false;
+
+        $expected2 = [
+            'host' => 'localhost',
+            'port' => '6379',
+            'connectTimeout' => '1.0',
+            'readTimeout' => '1.0',
+        ];
+
+        $result2 = Cache::getRedisConfig();
+        $this->assertEquals($expected2, $result2);
+    }
+}

+ 64 - 0
tests/App/Services/DBTest.php

@@ -0,0 +1,64 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services;
+
+use PHPUnit\Framework\TestCase;
+
+class DBTest extends TestCase
+{
+    /**
+     * @covers App\Services\DB::getConfig
+     */
+    public function testGetConfig()
+    {
+        // Scenario 1: All parameters are set
+        $_ENV['db_driver'] = 'mysql';
+        $_ENV['db_host'] = 'localhost';
+        $_ENV['db_socket'] = '/tmp/mysql.sock';
+        $_ENV['db_database'] = 'test_db';
+        $_ENV['db_username'] = 'username';
+        $_ENV['db_password'] = 'password';
+        $_ENV['db_charset'] = 'utf8';
+        $_ENV['db_collation'] = 'utf8_unicode_ci';
+        $_ENV['db_prefix'] = '';
+        $_ENV['db_port'] = '3306';
+
+        $expected1 = [
+            'driver' => 'mysql',
+            'host' => 'localhost',
+            'unix_socket' => '/tmp/mysql.sock',
+            'database' => 'test_db',
+            'username' => 'username',
+            'password' => 'password',
+            'charset' => 'utf8',
+            'collation' => 'utf8_unicode_ci',
+            'prefix' => '',
+            'port' => '3306',
+        ];
+
+        $result1 = DB::getConfig();
+        $this->assertEquals($expected1, $result1);
+
+        // Scenario 2: Missing parameters
+        $_ENV['db_socket'] = '';
+        $_ENV['db_port'] = '';
+
+        $expected2 = [
+            'driver' => 'mysql',
+            'host' => 'localhost',
+            'unix_socket' => '',
+            'database' => 'test_db',
+            'username' => 'username',
+            'password' => 'password',
+            'charset' => 'utf8',
+            'collation' => 'utf8_unicode_ci',
+            'prefix' => '',
+            'port' => '',
+        ];
+
+        $result2 = DB::getConfig();
+        $this->assertEquals($expected2, $result2);
+    }
+}

+ 71 - 4
tests/App/Utils/ToolsTest.php

@@ -138,6 +138,23 @@ class ToolsTest extends TestCase
         $this->assertEquals($expected, $result);
         $this->assertEquals($expected, $result);
     }
     }
 
 
+    /**
+     * @covers App\Utils\Tools::getDir
+     */
+    public function testGetDir()
+    {
+        // Scenario 1: Valid directory
+        $dir1 = 'tests/testDir';
+        $expected1 = ['emptyDir', 'file1', 'file2', 'file3']; // Replace with actual expected result
+        $result1 = Tools::getDir($dir1);
+        $this->assertEqualsCanonicalizing($expected1, $result1);
+
+        // Scenario 2: Directory with .gitkeep
+        $dir2 = 'tests/testDir/emptyDir';
+        $result2 = Tools::getDir($dir2);
+        $this->assertEqualsCanonicalizing(['.gitkeep'], $result2);
+    }
+
     /**
     /**
      * @covers App\Utils\Tools::isParamValidate
      * @covers App\Utils\Tools::isParamValidate
      */
      */
@@ -148,12 +165,52 @@ class ToolsTest extends TestCase
     }
     }
 
 
     /**
     /**
-     * @covers App\Utils\Tools::isEmail
+     * @covers App\Utils\Tools::getSsMethod
      */
      */
-    public function testIsEmail()
+    public function testGetSsMethod()
     {
     {
-        $this->assertTrue(Tools::isEmail('[email protected]'));
-        $this->assertFalse(Tools::isEmail('test@example'));
+        // Scenario 1: ss_obfs
+        $expected1 = [
+            'simple_obfs_http',
+            'simple_obfs_http_compatible',
+            'simple_obfs_tls',
+            'simple_obfs_tls_compatible',
+        ];
+        $result1 = Tools::getSsMethod('ss_obfs');
+        $this->assertEquals($expected1, $result1);
+
+        // Scenario 2: default
+        $expected2 = [
+            'aes-128-gcm',
+            'aes-192-gcm',
+            'aes-256-gcm',
+            'chacha20-ietf-poly1305',
+            'xchacha20-ietf-poly1305',
+        ];
+        $result2 = Tools::getSsMethod('default');
+        $this->assertEquals($expected2, $result2);
+
+        // Scenario 3: Random string
+        $expected3 = [
+            'aes-128-gcm',
+            'aes-192-gcm',
+            'aes-256-gcm',
+            'chacha20-ietf-poly1305',
+            'xchacha20-ietf-poly1305',
+        ];
+        $result3 = Tools::getSsMethod('randomString');
+        $this->assertEquals($expected3, $result3);
+
+        // Scenario 4: Empty string
+        $expected4 = [
+            'aes-128-gcm',
+            'aes-192-gcm',
+            'aes-256-gcm',
+            'chacha20-ietf-poly1305',
+            'xchacha20-ietf-poly1305',
+        ];
+        $result4 = Tools::getSsMethod('');
+        $this->assertEquals($expected4, $result4);
     }
     }
 
 
     /**
     /**
@@ -174,6 +231,15 @@ class ToolsTest extends TestCase
         $this->assertEquals($expected2, Tools::isEmailLegal($email2));
         $this->assertEquals($expected2, Tools::isEmailLegal($email2));
     }
     }
 
 
+    /**
+     * @covers App\Utils\Tools::isEmail
+     */
+    public function testIsEmail()
+    {
+        $this->assertTrue(Tools::isEmail('[email protected]'));
+        $this->assertFalse(Tools::isEmail('test@example'));
+    }
+
     /**
     /**
      * @covers App\Utils\Tools::isIPv4
      * @covers App\Utils\Tools::isIPv4
      */
      */
@@ -208,5 +274,6 @@ class ToolsTest extends TestCase
     {
     {
         $this->assertTrue(Tools::isJson('{}'));
         $this->assertTrue(Tools::isJson('{}'));
         $this->assertFalse(Tools::isJson('[]'));
         $this->assertFalse(Tools::isJson('[]'));
+        $this->assertFalse(Tools::isJson('what the'));
     }
     }
 }
 }

+ 0 - 0
tests/testDir/emptyDir/.gitkeep


+ 0 - 0
tests/testDir/file1


+ 0 - 0
tests/testDir/file2


+ 0 - 0
tests/testDir/file3