HttpResponseTest::reflectProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace KochTest\Http;
4
5
use Koch\Http\HttpResponse;
6
7
class HttpResponseTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var HttpResponse
11
     */
12
    protected $response;
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->response = new HttpResponse;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
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->response);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
30
    }
31
32
    public function testProperty_DefaultStatusIs200()
0 ignored issues
show
Coding Style introduced by
function testProperty_DefaultStatusIs200() 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...
33
    {
34
        $this->assertEquals(200, HttpResponse::getStatusCode());
35
    }
36
37
    /**
38
     * @covers Koch\Http\HttpResponse::setStatusCode
39
     * @covers Koch\Http\HttpResponse::getStatusCode
40
     */
41
    public function testSetAndGetStatusCode()
42
    {
43
        $code = '200';
44
        HttpResponse::setStatusCode($code);
45
        $this->assertEquals($code, HttpResponse::getStatusCode());
46
    }
47
48
    public function testGetStatusCodeDescription()
49
    {
50
        $code = '200';
51
        $this->assertEquals('OK', HttpResponse::getStatusCodeDescription($code));
52
    }
53
54
    /**
55
     * @covers Koch\Http\HttpResponse::setContent
56
     * @covers Koch\Http\HttpResponse::getContent
57
     */
58
    public function testSetContent()
59
    {
60
        $content = 'Some Content. This is the response body.';
61
        HttpResponse::setContent($content);
62
        $this->assertEquals($content, HttpResponse::getContent());
63
64
        // append content
65
        $content2 = 'Some additional content to test appending.';
66
        HttpResponse::setContent($content2);
67
        $this->assertEquals($content . $content2, HttpResponse::getContent());
68
69
        // replace content
70
        $content3 = ' This new Content replaces the old content.';
71
        HttpResponse::setContent($content3, true);
72
        $this->assertEquals($content3, HttpResponse::getContent());
73
    }
74
75
    /**
76
     * @covers Koch\Http\HttpResponse::setContentType
77
     * @covers Koch\Http\HttpResponse::getContentType
78
     */
79
    public function testSetAndGetContentType()
80
    {
81
        // default type
82
        $this->assertEquals('text/html', HttpResponse::getContentType());
83
84
        HttpResponse::setContentType('xml');
85
        $this->assertEquals('application/xml', HttpResponse::getContentType());
86
    }
87
88
    /**
89
     * @covers Koch\Http\HttpResponse::setContentType
90
     * @expectedException InvalidArgumentException
91
     * @expectedExceptionMessage Specified type not valid. Use: html, txt, xml or json.
92
     */
93
    public function testSetContentType_throws()
0 ignored issues
show
Coding Style introduced by
function testSetContentType_throws() 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...
94
    {
95
        HttpResponse::setContentType('SomeInvalidType');
96
    }
97
98
    /**
99
     * @covers Koch\Http\HttpResponse::addHeader
100
     */
101
    public function testAddHeader()
102
    {
103
        $name  = 'TestName';
104
        $value = 'TestValue';
105
        HttpResponse::addHeader($name, $value);
106
107
        $this->assertArrayHasKey($name,  self::reflectProperty('headers')->getValue());
108
    }
109
110
    /**
111
     * ReflectProperty reflects a class property,
112
     * changing its scope to public.
113
     * This is used to access and test private properties
114
     * for which no getters are implemented in the public api.
115
     *
116
     * @param string $name Property name.
117
     *
118
     * @return \ReflectionProperty
119
     */
120
    protected static function reflectProperty($name)
121
    {
122
        $class  = new \ReflectionClass('Koch\Http\HttpResponse');
123
        $method = $class->getProperty($name);
124
        $method->setAccessible(true);
125
126
        return $method;
127
    }
128
129
    /**
130
     * @covers Koch\Http\HttpResponse::addHeader
131
     * @covers Koch\Http\HttpResponse::setContent
132
     * @covers Koch\Http\HttpResponse::clearHeaders
133
     * @covers Koch\Http\HttpResponse::getContent
134
     */
135
    public function testClearHeaders()
136
    {
137
        HttpResponse::addHeader('SomeHeader', 'SomeValue');
138
        HttpResponse::setContent('Some Content.');
139
140
        $this->assertTrue(HttpResponse::clearHeaders());
141
142
        $this->assertArrayNotHasKey('SomeHeader',  self::reflectProperty('headers')->getValue());
143
        $this->assertNull(HttpResponse::getContent());
144
    }
145
146
    public function testSetNoCacheHeader()
147
    {
148
        HttpResponse::setNoCacheHeader();
149
150
        $this->assertArrayHasKey('Pragma', self::reflectProperty('headers')->getValue());
151
        $this->assertArrayHasKey('Cache-Control', self::reflectProperty('headers')->getValue());
152
        $this->assertArrayHasKey('Expires',  self::reflectProperty('headers')->getValue());
153
        $this->assertArrayHasKey('Last-Modified', self::reflectProperty('headers')->getValue());
154
    }
155
156
    public function testSendResponse()
157
    {
158
        $content = 'Some Body';
159
        HttpResponse::setContent($content);
160
        HttpResponse::addHeader('Header', 'Content');
161
        HttpResponse::addHeader('Header2', 'Content2');
162
163
        ob_start();
164
        HttpResponse::sendResponse();
165
        $result = ob_get_clean();
166
167
        $this->assertEquals($content, $result);
168
    }
169
}
170