tox /share/test/web/response/response-test.php

Language PHP Lines 304
MD5 Hash 5984ccb7945b4833ada81c09a809b68e
Repository https://github.com/php-tox/tox.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
 * Defines the test case for Tox\Web\Response\Response.
 *
 * This file is part of Tox.
 *
 * Tox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tox is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright Š 2012-2013 PHP-Tox.org
 * @license   GNU General Public License, version 3
 */

namespace Tox\Web\Response;

use PHPUnit_Framework_TestCase;

require_once __DIR__ . '/../../../../src/core/assembly.php';
require_once __DIR__ . '/../../../../src/application/ioutput.php';
require_once __DIR__ . '/../../../../src/application/output/output.php';
require_once __DIR__ . '/../../../../src/web/iresponse.php';
require_once __DIR__ . '/../../../../src/web/response/response.php';

require_once __DIR__ . '/../../../../src/core/exception.php';
require_once __DIR__ . '/../../../../src/application/output/closedoutputexception.php';
require_once __DIR__ . '/../../../../src/web/response/headersreadonlyexception.php';
require_once __DIR__ . '/../../../../src/web/response/illegalhttpstatuscodeexception.php';

require_once __DIR__ . '/../../../../src/application/ioutputtask.php';
require_once __DIR__ . '/../../../../src/web/ihttpheadersprocessor.php';
require_once __DIR__ . '/../../../../src/web/response/httpheadersprocessor.php';
require_once __DIR__ . '/../../../../src/web/ipagecacheagent.php';
require_once __DIR__ . '/../../../../src/web/response/pagecacheagent.php';
require_once __DIR__ . '/../../../../src/application/iview.php';
require_once __DIR__ . '/../../../../src/application/view/view.php';
require_once __DIR__ . '/../../../../src/application/istreamingview.php';
require_once __DIR__ . '/../../../../src/application/view/streamingview.php';
require_once __DIR__ . '/../../../../src/web/ipagecache.php';

use Exception as PHPException;

use Tox;

/**
 * Tests Tox\Web\Response\Response.
 *
 * @internal
 *
 * @package tox.web.response
 * @author  Snakevil Zen <zsnakevil@gmail.com>
 */
class ResponseTest extends PHPUnit_Framework_TestCase
{
    public function testAddingHeaders()
    {
        $o_out = new Response;
        $this->assertSame($o_out, $o_out->addHeader('foo', microtime()));
        $o_out->addHeader('foo', 'bar')->addHeader('bar', 'blah.1')->addHeader('bar', 'blah.2', false);
        $this->assertEquals(array('foo' => array('bar'), 'bar' => array('blah.1', 'blah.2')), $o_out->getHeaders());
    }

    public function testHeadersSentOnOutputtingButNotBuffering()
    {
        $o_out = new Response;
        $o_hhp = $this->getMock('Tox\\Web\\IHTTPHeadersProcessor');
        $o_hhp->expects($this->never())->method('preOutput');
        $this->assertSame($o_out, $o_out->setHeadersProcessor($o_hhp));
        $o_out = new Response;
        $o_hhp = $this->getMock('Tox\\Web\\IHTTPHeadersProcessor');
        $o_hhp->expects($this->once())->method('preOutput');
        ob_start();
        $o_out->setHeadersProcessor($o_hhp)->close();
        ob_end_clean();
    }

    /**
     * @depends testHeadersSentOnOutputtingButNotBuffering
     */
    public function testHeadersSentBeforeOutputting()
    {
        $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('getHeaders'));
        $o_hhp = $this->getMock(
            'Tox\\Web\\Response\\HTTPHeadersProcessor',
            array('postOutput', 'sendHeader'),
            array($o_out)
        );
        $o_out->setHeadersProcessor($o_hhp)
            ->expects($this->once())->method('getHeaders')->will($this->returnValue(array()));
        ob_start();
        $o_out->close();
        ob_end_clean();
    }

    /**
     * @depends testHeadersSentBeforeOutputting
     * @expectedException Tox\Web\Response\HeadersReadonlyException
     */
    public function testHeadersReadonly()
    {
        $o_out = new Response;
        $o_out->setHeaders(array());
    }

    public function testHeadersAccessableViaMagicMethods()
    {
        $a_values = array(microtime());
        $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('getHeaders', 'setHeaders'));
        $o_out->expects($this->once())->method('getHeaders');
        $o_out->headers;
        $o_out->expects($this->once())->method('setHeaders')->with($this->equalTo($a_values));
        $o_out->headers = $a_values;
    }

    public function testPageCacheSetAfterOutputting()
    {
        $o_out = new Response;
        $o_pca = $this->getMock('Tox\\Web\\Response\\PageCacheAgent', array('preOutput'), array($o_out));
        $this->assertSame($o_out, $o_out->setPageCacheAgent($o_pca));
        $o_pc = $this->getMock('Tox\\Web\\IPageCache');
        $o_pc->expects($this->once())->method('put');
        $this->assertSame($o_out, $o_out->cacheTo($o_pc));
        ob_start();
        $o_out->close();
        ob_end_clean();
    }

    /**
     * @depends testPageCacheSetAfterOutputting
     */
    public function testGivenPageCacheWouldBeLostOnChangineAgent()
    {
        $o_out = new Response;
        $o_pc = $this->getMock('Tox\\Web\\IPageCache');
        $o_pc->expects($this->never())->method('put');
        ob_start();
        $o_out->cacheTo($o_pc)
            ->setPageCacheAgent($this->getMock('Tox\\Web\\Response\\PageCacheAgent', array(), array($o_out)))
            ->close();
        ob_end_clean();
    }

    /**
     * @depends testPageCacheSetAfterOutputting
     */
    public function testEmptyPageWouldBeCachedToo()
    {
        $o_out = new Response;
        $o_pc = $this->getMock('Tox\\Web\\IPageCache');
        $o_pc->expects($this->once())->method('put')->with($this->equalTo(''));
        ob_start();
        $o_out->cacheTo($o_pc)->close();
        ob_end_clean();
    }

    public function test301Redirecting()
    {
        $s_url = microtime();
        $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('addHeader'));
        $o_out->expects($this->at(0))->method('addHeader')
            ->with($this->equalTo('Status'), $this->equalTo('301 Moved Permanently'))
            ->will($this->returnSelf());
        $o_out->expects($this->at(1))->method('addHeader')
            ->with($this->equalTo('Location'), $this->equalTo($s_url))
            ->will($this->returnSelf());
        ob_start();
        try {
            $this->assertSame($o_out, $o_out->redirect($s_url, true));
            $o_out->write('foo');
        } catch (Tox\Application\Output\ClosedOutputException $ex) {
        } catch (PHPException $ex) {
            $this->fail();
        }
        ob_end_clean();
    }

    /**
     * @depends test301Redirecting
     */
    public function test302Redirecting()
    {
        $s_url = microtime();
        $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('addHeader'));
        $o_out->expects($this->at(0))->method('addHeader')
            ->with($this->equalTo('Status'), $this->equalTo('302 Found'))
            ->will($this->returnSelf());
        $o_out->expects($this->at(1))->method('addHeader')
            ->with($this->equalTo('Location'), $this->equalTo($s_url))
            ->will($this->returnSelf());
        ob_start();
        $o_out->redirect($s_url);
        ob_end_clean();
    }

    /**
     * @depends test301Redirecting
     */
    public function testRedirectingWouldDropAllBufferAndHeaders()
    {
        $o_out = new Response;
        $o_hhp = $this->getMock('Tox\\Web\\Response\\HTTPHeadersProcessor', array('sendHeader'), array($o_out));
        $o_hhp->expects($this->exactly(2))->method('sendHeader');
        $o_out->setHeadersProcessor($o_hhp)->write(microtime())->addHeader('foo', 'bar');
        ob_start();
        $o_out->redirect('blah');
        $this->assertEquals('', ob_get_clean());
    }

    /**
     * @dataProvider provideStatus
     */
    public function testSettingStatus($code, $title)
    {
        $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('addHeader'));
        $o_out->expects($this->at(0))->method('addHeader')
            ->with($this->equalTo('Status'), $this->equalTo($title));
        $o_out->setStatus($code);
    }

    /**
     * @depends testSettingStatus
     * @expectedException Tox\Web\Response\IllegalHTTPStatusCodeException
     */
    public function testSettingIllegalStatus()
    {
        $o_out = new Response;
        $o_out->setStatus(987);
    }

    public function testSetAndGetCookie()
    {
        $o_out = new Response;
        $this->assertTrue($o_out->setCookie('test', 'testvalue', 0, '/', 'test.dev'));
        $this->assertTrue($o_out->setCookie('test1', 'testvalue', 10));
        $this->assertEmpty($o_out->exportCookie());
        $this->assertEmpty($o_out->getCookie('test'));

        $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('getCookie'));
        $o_out->expects($this->at(0))
             ->method('getCookie')
            ->with($this->equalTo('test1'))
            ->will($this->returnValue(base64_encode('testvalue')));
        $this->assertEquals('testvalue', base64_decode($o_out->getCookie('test1')));

    }

    public function provideStatus()
    {
        return array(
            array('100', '100 Continue'),
            array('101', '101 Switching Protocols'),
            array('200', '200 OK'),
            array('201', '201 Created'),
            array('202', '202 Accepted'),
            array('203', '203 Non-Authoritative Information'),
            array('204', '204 No Content'),
            array('205', '205 Reset Content'),
            array('206', '206 Partial Content'),
            array('300', '300 Multiple Choices'),
            array('301', '301 Moved Permanently'),
            array('302', '302 Found'),
            array('303', '303 See Other'),
            array('304', '304 Not Modified'),
            array('305', '305 Use Proxy'),
            array('307', '307 Temporary Redirect'),
            array('400', '400 Bad Request'),
            array('401', '401 Unauthorized'),
            array('402', '402 Payment Required'),
            array('403', '403 Forbidden'),
            array('404', '404 Not Found'),
            array('405', '405 Method Not Allowed'),
            array('406', '406 Not Acceptable'),
            array('407', '407 Proxy Authentication Required'),
            array('408', '408 Request Timeout'),
            array('409', '409 Conflict'),
            array('410', '410 Gone'),
            array('411', '411 Length Required'),
            array('412', '412 Precondition Failed'),
            array('413', '413 Request Entity Too Large'),
            array('414', '414 Request-URI Too Long'),
            array('415', '415 Unsupported Media Type'),
            array('416', '416 Requested Range Not Satisfiable'),
            array('417', '417 Expectation Failed'),
            array('500', '500 Internal Server Error'),
            array('501', '501 Not Implemented'),
            array('502', '502 Bad Gateway'),
            array('503', '503 Service Unavailable'),
            array('504', '504 Gateway Timeout'),
            array('505', '505 HTTP Version Not Supported')
        );
    }
}

// vi:ft=php fenc=utf-8 ff=unix ts=4 sts=4 et sw=4 fen fdm=indent fdl=1 tw=120
Back to Top