Passed
Push — master ( 283335...4a2fc2 )
by Fran
18:22 queued 08:26
created

PSFSTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testRouter() 0 14 1
A testConfig() 0 15 1
A testPre() 0 6 1
A testSecurity() 0 6 1
A testRequest() 0 46 2
A testDispatcher() 0 11 1
1
<?php
2
3
namespace PSFS\tests;
4
5
use PHPUnit\Framework\TestCase;
6
use PSFS\base\config\Config;
7
use PSFS\base\Request;
8
use PSFS\base\Router;
9
use PSFS\base\Security;
10
use PSFS\base\types\helpers\AdminHelper;
11
use PSFS\base\types\helpers\RequestHelper;
12
use PSFS\bootstrap;
0 ignored issues
show
Bug introduced by
The type PSFS\bootstrap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use PSFS\Dispatcher;
14
15
class PSFSTest extends TestCase
16
{
17
18
    /**
19
     * Basic test for the basic functionality
20
     */
21
    public function testDispatcher()
22
    {
23
        /** @var \PSFS\Dispatcher $dispatcher */
24
        $dispatcher = Dispatcher::getInstance();
25
26
        // Is instance of Dispatcher?
27
        $this->assertTrue($dispatcher instanceof Dispatcher);
28
29
        // Did timestamp generated?
30
        $this->assertTrue($dispatcher->getTs() > 0);
31
        restore_error_handler();
32
    }
33
34
    /**
35
     * Basic test for Config functionality
36
     */
37
    public function testConfig()
38
    {
39
        $config = Config::getInstance();
40
41
        // Is config instance?
42
        $this->assertTrue($config instanceof Config);
43
44
        // Is the platform configured?
45
        $this->assertTrue(is_bool($config->isConfigured()));
46
47
        // Is the platform in debug mode?
48
        $this->assertTrue(is_bool($config->getDebugMode()));
49
50
        // Check the variable extraction
51
        $this->assertEmpty($config->get(uniqid()));
52
    }
53
54
    /**
55
     * Basic test for Router functionality
56
     */
57
    public function testRouter()
58
    {
59
        $router = Router::getInstance();
60
61
        // Is ROuter instance?
62
        $this->assertTrue($router instanceof Router);
63
64
        // Check if route file exists
65
        $this->assertFileExists(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json");
66
67
        // CHecks if we have admin routes as minimal routes
68
        $adminRoutes = AdminHelper::getAdminRoutes($router->getRoutes());
69
        $this->assertNotEmpty($adminRoutes);
70
        $this->assertArrayHasKey("PSFS", $adminRoutes);
71
    }
72
73
    /**
74
     * Basic test for Security functionality
75
     */
76
    public function testSecurity()
77
    {
78
        $security = Security::getInstance();
79
80
        // Is Security instance?
81
        $this->assertTrue($security instanceof Security);
82
    }
83
84
    /**
85
     * Basic test for Request functionality
86
     */
87
    public function testRequest()
88
    {
89
        $request = Request::getInstance();
90
91
        // Is Request instance?
92
        $this->assertTrue($request instanceof Request);
93
94
        // Check headers, uploads and cookies checkers
95
        $this->assertTrue(is_bool($request->hasHeader("session")));
96
        $this->assertTrue(is_bool($request->hasUpload()));
97
        $this->assertTrue(is_bool($request->hasCookies()));
98
        $this->assertTrue(is_bool($request->isAjax()));
99
100
        // Checks if timestamp was generated
101
        $this->assertNotNull($request->getTs());
102
103
        // Check cors
104
        $corsHeaders = RequestHelper::getCorsHeaders();
105
        $this->assertIsArray($corsHeaders, 'Wrong returned headers');
106
        $headers = [
107
            'Access-Control-Allow-Methods',
108
            'Access-Control-Allow-Headers',
109
            'Access-Control-Allow-Origin',
110
            'Access-Control-Expose-Headers',
111
            'Origin',
112
            'X-Requested-With',
113
            'Content-Type',
114
            'Accept',
115
            'Authorization',
116
            'Cache-Control',
117
            'Content-Language',
118
            'Accept-Language',
119
            'X-API-SEC-TOKEN',
120
            'X-API-USER-TOKEN',
121
            'X-API-LANG',
122
            'X-FIELD-TYPE',
123
        ];
124
        foreach ($headers as $header) {
125
            $this->assertTrue(in_array($header, $corsHeaders), sprintf('%s not found in CORS array', $header));
126
        }
127
128
        // Verify IPs
129
        $currentIp = str_replace("\n", "", file_get_contents('http://checkip.amazonaws.com'));
130
        $this->assertTrue(RequestHelper::validateIpAddress($currentIp), 'IP validation error');
131
        $this->assertNotTrue(RequestHelper::validateIpAddress('350.168.458.a'), 'IP validation error');
132
        $this->assertNotTrue(RequestHelper::validateIpAddress('350.168.458.1500'), 'IP validation error');
133
    }
134
135
    public function testPre()
136
    {
137
        ob_start();
138
        pre('test');
139
        $return = ob_get_clean();
140
        $this->assertNotEmpty($return);
141
    }
142
}
143