Ver código fonte

test: add full unit tests for Utils class

M1Screw 1 ano atrás
pai
commit
4a987c03ac

+ 1 - 0
composer.json

@@ -58,6 +58,7 @@
         }
     },
     "require-dev": {
+        "dg/bypass-finals": "^1.6",
         "nunomaduro/phpinsights": "*",
         "phpunit/phpunit": "^10|^11"
     },

+ 54 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "29d028fe09575d21b97024d48e6f4ebf",
+    "content-hash": "f7e6edab9c07262546075ea15e3c323c",
     "packages": [
         {
             "name": "alipaysdk/openapi",
@@ -6452,6 +6452,59 @@
             },
             "time": "2023-01-05T11:28:13+00:00"
         },
+        {
+            "name": "dg/bypass-finals",
+            "version": "v1.6.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/dg/bypass-finals.git",
+                "reference": "efe2fe04bae9f0de271dd462afc049067889e6d1"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/dg/bypass-finals/zipball/efe2fe04bae9f0de271dd462afc049067889e6d1",
+                "reference": "efe2fe04bae9f0de271dd462afc049067889e6d1",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=7.1"
+            },
+            "require-dev": {
+                "nette/tester": "^2.3",
+                "phpstan/phpstan": "^0.12"
+            },
+            "type": "library",
+            "autoload": {
+                "classmap": [
+                    "src/"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "BSD-3-Clause",
+                "GPL-2.0",
+                "GPL-3.0"
+            ],
+            "authors": [
+                {
+                    "name": "David Grudl",
+                    "homepage": "https://davidgrudl.com"
+                }
+            ],
+            "description": "Removes final keyword from source code on-the-fly and allows mocking of final methods and classes",
+            "keywords": [
+                "finals",
+                "mocking",
+                "phpunit",
+                "testing",
+                "unit"
+            ],
+            "support": {
+                "issues": "https://github.com/dg/bypass-finals/issues",
+                "source": "https://github.com/dg/bypass-finals/tree/v1.6.0"
+            },
+            "time": "2023-11-19T22:19:30+00:00"
+        },
         {
             "name": "friendsofphp/php-cs-fixer",
             "version": "v3.49.0",

+ 4 - 0
phpunit.xml

@@ -20,4 +20,8 @@
             <directory>src</directory>
         </include>
     </source>
+
+    <php>
+        <cookie name="testKey" value="testValue"/>
+    </php>
 </phpunit>

+ 2 - 0
src/Services/Boot.php

@@ -4,6 +4,8 @@ declare(strict_types=1);
 
 namespace App\Services;
 
+use function date_default_timezone_set;
+use function microtime;
 use function Sentry\init;
 
 final class Boot

+ 0 - 4
src/Services/View.php

@@ -62,14 +62,10 @@ final class View
         return [
             'appName' => $_ENV['appName'],
             'baseUrl' => $_ENV['baseUrl'],
-
             'jump_delay' => $_ENV['jump_delay'],
-
             'enable_kill' => $_ENV['enable_kill'],
             'enable_change_email' => $_ENV['enable_change_email'],
-
             'enable_r2_client_download' => $_ENV['enable_r2_client_download'],
-
             'jsdelivr_url' => $_ENV['jsdelivr_url'],
         ];
     }

+ 3 - 1
src/Utils/ClassHelper.php

@@ -4,7 +4,10 @@ declare(strict_types=1);
 
 namespace App\Utils;
 
+use function array_filter;
+use function array_keys;
 use function is_null;
+use function strtoupper;
 
 final class ClassHelper
 {
@@ -15,7 +18,6 @@ final class ClassHelper
     {
         self::$composer = null;
         self::$classes = [];
-
         self::$composer = require __DIR__ . '/../../vendor/autoload.php';
 
         if (! is_null(self::$composer)) {

+ 73 - 0
tests/App/Services/ViewTest.php

@@ -0,0 +1,73 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services;
+
+use PHPUnit\Framework\TestCase;
+use App\Services\View;
+use App\Models\User;
+
+final class ViewTest extends TestCase
+{
+    private View $view;
+    private User $user;
+
+    protected function setUp(): void
+    {
+        $this->view = new View();
+        $this->user = new User();
+    }
+
+    /**
+     * @covers App\Services\View::getTheme
+     */
+    public function testGetTheme(): void
+    {
+        $this->user->isLogin = true;
+        $this->user->theme = 'tabler';
+
+        $theme = $this->view->getTheme($this->user);
+
+        $this->assertEquals('tabler', $theme);
+
+        $_ENV['theme'] = 'not-tabler';
+        $this->user->isLogin = false;
+
+        $theme = $this->view->getTheme($this->user);
+
+        $this->assertEquals('not-tabler', $theme);
+    }
+
+    /**
+     * @covers App\Services\View::getConfig
+     */
+    public function testGetConfig(): void
+    {
+        $_ENV['appName'] = 'Test App';
+        $_ENV['baseUrl'] = 'http://localhost';
+        $_ENV['jump_delay'] = 3;
+        $_ENV['enable_kill'] = true;
+        $_ENV['enable_change_email'] = true;
+        $_ENV['enable_r2_client_download'] = true;
+        $_ENV['jsdelivr_url'] = 'https://cdn.jsdelivr.net';
+
+        $config = $this->view->getConfig();
+
+        $this->assertIsArray($config);
+        $this->assertArrayHasKey('appName', $config);
+        $this->assertArrayHasKey('baseUrl', $config);
+        $this->assertArrayHasKey('jump_delay', $config);
+        $this->assertArrayHasKey('enable_kill', $config);
+        $this->assertArrayHasKey('enable_change_email', $config);
+        $this->assertArrayHasKey('enable_r2_client_download', $config);
+        $this->assertArrayHasKey('jsdelivr_url', $config);
+        $this->assertEquals('Test App', $config['appName']);
+        $this->assertEquals('http://localhost', $config['baseUrl']);
+        $this->assertEquals(3, $config['jump_delay']);
+        $this->assertTrue($config['enable_kill']);
+        $this->assertTrue($config['enable_change_email']);
+        $this->assertTrue($config['enable_r2_client_download']);
+        $this->assertEquals('https://cdn.jsdelivr.net', $config['jsdelivr_url']);
+    }
+}

+ 41 - 0
tests/App/Utils/ClassHelperTest.php

@@ -0,0 +1,41 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Utils;
+
+use PHPUnit\Framework\TestCase;
+use App\Utils\ClassHelper;
+
+final class ClassHelperTest extends TestCase
+{
+    private ClassHelper $classHelper;
+
+    protected function setUp(): void
+    {
+        $this->classHelper = new ClassHelper();
+    }
+
+    /**
+     * @covers App\Utils\ClassHelper::getClassesByNamespace
+     */
+    public function testGetClassesByNamespace(): void
+    {
+        $namespace = 'App\\Utils';
+        $classes = $this->classHelper->getClassesByNamespace($namespace);
+
+        $this->assertIsArray($classes);
+        $this->assertContains('\App\Utils\ClassHelper', $classes);
+    }
+
+    /**
+     * @covers App\Utils\ClassHelper::getClasses
+     */
+    public function testGetClasses(): void
+    {
+        $classes = $this->classHelper->getClasses();
+
+        $this->assertIsArray($classes);
+        $this->assertContains('\App\Utils\ClassHelper', $classes);
+    }
+}

+ 51 - 0
tests/App/Utils/CookieTest.php

@@ -0,0 +1,51 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Utils;
+
+use PHPUnit\Framework\TestCase;
+use App\Utils\Cookie;
+
+final class CookieTest extends TestCase
+{
+    /**
+     * @covers App\Utils\Cookie::set
+     */
+    public function testSet(): void
+    {
+        $data = ['testKey' => 'testValue'];
+        $time = time() + 3600;
+
+        Cookie::set($data, $time);
+
+        $this->assertEquals('testValue', $_COOKIE['testKey']);
+    }
+
+    /**
+     * @covers App\Utils\Cookie::setWithDomain
+     */
+    public function testSetWithDomain(): void
+    {
+        $data = ['testKey' => 'testValue'];
+        $time = time() + 3600;
+        $domain = 'localhost';
+
+        Cookie::setWithDomain($data, $time, $domain);
+
+        $this->assertEquals('testValue', $_COOKIE['testKey']);
+    }
+
+    /**
+     * @covers App\Utils\Cookie::get
+     */
+    public function testGet(): void
+    {
+        $data = ['testKey' => 'testValue'];
+        $time = time() + 3600;
+
+        Cookie::set($data, $time);
+
+        $this->assertEquals('testValue', Cookie::get('testKey'));
+    }
+}