HttpRequestTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 156
rs 10
wmc 14
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 4 1
A testMethodcontructor_unsetsGlobalVars() 0 8 1
A testMethodgetRequestMethod() 0 15 1
A testMethodsetRequestMethod() 0 5 1
A testMethodisGET() 0 5 1
A testMethodisPOST() 0 5 1
A testMethodisPUT() 0 5 1
A testMethodisDELETE() 0 5 1
A testMethodisAjax() 0 16 1
A testMethodgetServerProtocol() 0 7 1
A testMethodIsSecure() 0 8 1
A testMethodgetBaseURL() 0 17 1
A testMethodgetServerName() 0 6 1
1
<?php
2
3
namespace KochTest\Http;
4
5
use Koch\Http\HttpRequest;
6
7
class HttpRequestTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var HttpRequest
11
     */
12
    protected $request;
13
14
    /**
15
     * Sets up the fixture, for example, opens a network connection.
16
     * This method is called before a test is executed.
17
     */
18
    public function setUp()
19
    {
20
        $this->request = new HttpRequest();
21
    }
22
23
    /**
24
     * Tears down the fixture, for example, closes a network connection.
25
     * This method is called after a test is executed.
26
     */
27
    public function tearDown()
28
    {
29
        unset($this->request);
30
    }
31
32
    public function testMethodcontructor_unsetsGlobalVars()
0 ignored issues
show
Coding Style introduced by
function testMethodcontructor_unsetsGlobalVars() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
testMethodcontructor_unsetsGlobalVars uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
33
    {
34
        $this->assertFalse(isset($_REQUEST));
35
36
        //  Undefined variable: GLOBALS
37
        //  it's not possible to unset globals - phpunit side effect?
38
        //$this->assertFalse(isset($GLOBALS));
0 ignored issues
show
Unused Code Comprehensibility introduced by
90% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
    }
40
41
    public function testMethodgetRequestMethod()
0 ignored issues
show
Coding Style introduced by
testMethodgetRequestMethod uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
42
    {
43
        $_SERVER['REQUEST_METHOD']              = 'someMethod';
44
        $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'OverrideMethodName';
45
        $this->assertEquals('OverrideMethodName', HttpRequest::getRequestMethod());
46
47
        unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
48
        $_SERVER['REQUEST_METHOD'] = 'HEAD';
49
        $this->assertEquals('GET', HttpRequest::getRequestMethod());
50
51
        $this->request->setRequestMethod('BEAVIS');
52
        $this->assertEquals('BEAVIS', HttpRequest::getRequestMethod());
53
54
        unset($_SERVER['REQUEST_METHOD']);
55
    }
56
57
    public function testMethodsetRequestMethod()
58
    {
59
        $this->request->setRequestMethod('BUTTHEAD');
60
        $this->assertEquals('BUTTHEAD', HttpRequest::getRequestMethod());
61
    }
62
63
    public function testMethodisGET()
64
    {
65
        $this->request->setRequestMethod('GET');
66
        $this->assertTrue($this->request->isGet());
67
    }
68
69
    public function testMethodisPOST()
70
    {
71
        $this->request->setRequestMethod('POST');
72
        $this->assertTrue($this->request->isPost());
73
    }
74
75
    public function testMethodisPUT()
76
    {
77
        $this->request->setRequestMethod('PUT');
78
        $this->assertTrue($this->request->isPut());
79
    }
80
81
    public function testMethodisDELETE()
82
    {
83
        $this->request->setRequestMethod('DELETE');
84
        $this->assertTrue($this->request->isDelete());
85
    }
86
87
    public function testMethodisAjax()
0 ignored issues
show
Coding Style introduced by
testMethodisAjax uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
88
    {
89
        $isAjax = $this->request->isAjax();
90
        $this->assertFalse($isAjax);
91
92
        $_SERVER['X-Requested-With'] = 'XMLHttpRequest';
93
        $isAjax                      = $this->request->isAjax();
94
        $this->assertTrue($isAjax);
95
96
        $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
97
        $isAjax                           = $this->request->isAjax();
98
        $this->assertTrue($isAjax);
99
100
        unset($_SERVER['X-Requested-With']);
101
        unset($_SERVER['HTTP_X_REQUESTED_WITH']);
102
    }
103
104
    /*public function testMethodgetPost()
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
    {
106
        $_POST['POST-ABC'] = '123';
107
        $result = $this->request->getPost();
108
        $this->assertArrayHasKey('POST-ABC', $result);
109
110
        // ArrayAccess via offsetExists
111
        $this->assertEquals($this->request['POST-ABC'], '123');
112
    }
113
114
    public function testMethodgetGet()
115
    {
116
        $_GET['GET-ABC'] = '123';
117
        $result = $this->request->getGET();
118
        $this->assertArrayHasKey('GETABC', $result);
119
    }*/
120
121
    public function testMethodgetServerProtocol()
0 ignored issues
show
Coding Style introduced by
testMethodgetServerProtocol uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
122
    {
123
        $this->assertEquals($this->request->getServerProtocol(), 'http://');
124
125
        $_SERVER['HTTPS'] = 'on';
126
        $this->assertEquals($this->request->getServerProtocol(), 'https://');
127
    }
128
129
    public function testMethodIsSecure()
0 ignored issues
show
Coding Style introduced by
testMethodIsSecure uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
130
    {
131
        $_SERVER['HTTPS'] = 'NO';
132
        $this->assertFalse($this->request->isSecure());
133
134
        $_SERVER['HTTPS'] = '1';
135
        $this->assertTrue($this->request->isSecure());
136
    }
137
138
    public function testMethodgetBaseURL()
0 ignored issues
show
Coding Style introduced by
testMethodgetBaseURL uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
139
    {
140
        $_SERVER['HTTPS']       = 'off';
141
        $_SERVER['SERVER_NAME'] = 'localhost';
142
        $_SERVER['SERVER_PORT'] = 80;
143
        $this->assertEquals($this->request->getBaseURL(), 'http://localhost');
144
145
        /*$_SERVER['HTTPS'] = 'on';
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
146
        $_SERVER['SERVER_NAME'] = 'localhost';
147
        $_SERVER['SERVER_PORT'] = 123;
148
        $this->assertEquals($this->request->getBaseURL(), 'https://localhost:123');
149
150
        $_SERVER['HTTPS'] = 'on';
151
        $_SERVER['SERVER_NAME'] = 'localhost';
152
        $_SERVER['SERVER_PORT'] = 443;
153
        $this->assertEquals($this->request->getBaseURL(), 'https://localhost');*/
154
    }
155
156
    public function testMethodgetServerName()
0 ignored issues
show
Coding Style introduced by
testMethodgetServerName uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
157
    {
158
        $name                   = 'ServerName';
159
        $_SERVER['SERVER_NAME'] = $name;
160
        $this->assertEquals($this->request->getServerName(), $name);
161
    }
162
}
163