srchub

srchub Git Source Tree


Root/pluf/src/Pluf/Test/Client.php

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
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Emulates a client to call your views during unit testing.
 *
 * Usage:
 * <code>
 * $client = new Pluf_Test_Client('./path/to/app-views.php');
 * $response = $client->get('/the/page/', array('var'=>'toto'));
 * $response is now the Pluf_HTTP_Response
 * </code>
 *
 */
class Pluf_Test_Client
{
    public $views = '';
    public $dispatcher = '';
    public $cookies = array();
 
    public function __construct($views)
    {
        $this->views = $views;
        $this->dispatcher = new Pluf_Dispatcher();
        $this->dispatcher->loadControllers($this->views);
        $this->clean(false);
    }
 
    protected function clean($keepcookies=true)
    {
        $_REQUEST = array();
        if (!$keepcookies) {
            $_COOKIE = array();
            $this->cookies = array();
        }
        $_SERVER = array();
        $_GET = array();
        $_POST = array();
        $_FILES = array();
        $_SERVER['REQUEST_METHOD'] = '';
        $_SERVER['REQUEST_URI'] = '';
        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
        $_SERVER['HTTP_HOST'] = 'localhost';
    }
 
    protected function dispatch($page)
    {
        $GLOBALS['_PX_tests_templates'] = array();
        $_SERVER['REQUEST_URI'] = $page;
        foreach ($this->cookies as $cookie => $data) {
            $_COOKIE[$cookie] = $data;
        }
        ob_implicit_flush(False);
        list($request, $response) = $this->dispatcher->dispatch($page);
        ob_start();
        $response->render();
        $content = ob_get_contents();
        ob_end_clean();
        $response->content = $content;
        $response->request = $request;
        if (isset($GLOBALS['_PX_tests_templates'])) {
            if (count($GLOBALS['_PX_tests_templates']) == 1) {
                $response->template = $GLOBALS['_PX_tests_templates'][0];
            } else {
                $response->template = $GLOBALS['_PX_tests_templates'];
            }
        }
        foreach ($response->cookies as $cookie => $data) {
            $_COOKIE[$cookie] = $data;
            $this->cookies[$cookie] = $data;
        }
        return $response;
    }
 
    public function get($page, $params=array())
    {
        $this->clean();
        $_GET = $params;
        $_REQUEST = $params;
        $_SERVER['REQUEST_METHOD'] = 'GET';
        $response = $this->dispatch($page);
        $code = $response->status_code;
        if ($code == 302) {
            list($page, $params) = $this->parseRedirect($response->headers['Location']);
            $response = $this->get($page, $params);
        }
        return $response;
    }
 
 
    public function post($page, $params=array(), $files=array())
    {
        $this->clean();
        $_POST = $params;
        $_REQUEST = $params;
        $_FILES = $files; //FIXME need to match the correct array structure
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $response = $this->dispatch($page);
        if ($response->status_code == 302) {
            list($page, $params) = $this->parseRedirect($response->headers['Location']);
            return $this->get($page, $params);
        }
        return $response;
    }
 
    public function parseRedirect($location)
    {
        $page = parse_url($location, PHP_URL_PATH);
        $query = parse_url($location, PHP_URL_QUERY);
        $params = array();
        if (strlen($query)) {
            parse_str($query, $params);
        }
        return array($page, $params);
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.13024s using 11 queries.