Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in
17
 *     the documentation and/or other materials provided with the
18
 *     distribution.
19
 *
20
 *   * Neither the name of Sebastian Bergmann nor the names of his
21
 *     contributors may be used to endorse or promote products derived
22
 *     from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @category   Testing
38
 * @package    PHPUnit
39
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link       http://www.phpunit.de/
43
 * @since      File available since Release 3.0.0
44
 */
45
 
46
require_once 'PHPUnit/Framework.php';
47
require_once 'PHPUnit/Util/Filter.php';
48
 
49
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
50
 
51
/**
52
 * A TestCase that expects a specified output.
53
 *
54
 * @category   Testing
55
 * @package    PHPUnit
56
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
57
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
58
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
59
 * @version    Release: 3.4.15
60
 * @link       http://www.phpunit.de/
61
 * @since      Class available since Release 3.0.0
62
 */
63
abstract class PHPUnit_Extensions_OutputTestCase extends PHPUnit_Framework_TestCase
64
{
65
    /**
66
     * @var    string
67
     */
68
    protected $expectedRegex = NULL;
69
 
70
    /**
71
     * @var    string
72
     */
73
    protected $expectedString = NULL;
74
 
75
    /**
76
     * @var    string
77
     */
78
    protected $output = '';
79
 
80
    /**
81
     * @var    boolean
82
     */
83
    protected $obActive = FALSE;
84
 
85
    /**
86
     * @var    mixed
87
     */
88
    protected $outputCallback = FALSE;
89
 
90
    /**
91
     * @return bool
92
     */
93
    public function setOutputCallback($callback)
94
    {
95
        if (is_callable($callback)) {
96
            $this->outputCallback = $callback;
97
            $set = TRUE;
98
        } else {
99
            $set = FALSE;
100
        }
101
 
102
        return $set;
103
    }
104
 
105
    /**
106
     * @return string
107
     */
108
    public function normalizeOutput($buffer)
109
    {
110
        return str_replace("\r", '', $buffer);
111
    }
112
 
113
    /**
114
     * @return string
115
     */
116
    public function getActualOutput()
117
    {
118
        if (!$this->obActive) {
119
            return $this->output;
120
        } else {
121
            return ob_get_contents();
122
        }
123
    }
124
 
125
    /**
126
     * @return string
127
     */
128
    public function expectedRegex()
129
    {
130
        return $this->expectedRegex;
131
    }
132
 
133
    /**
134
     * @param  string  $expectedRegex
135
     */
136
    public function expectOutputRegex($expectedRegex)
137
    {
138
        if ($this->expectedString !== NULL) {
139
            throw new PHPUnit_Framework_Exception;
140
        }
141
 
142
        if (is_string($expectedRegex) || is_null($expectedRegex)) {
143
            $this->expectedRegex = $expectedRegex;
144
        }
145
    }
146
 
147
    /**
148
     * @return string
149
     */
150
    public function expectedString()
151
    {
152
        return $this->expectedOutput;
153
    }
154
 
155
    /**
156
     * @param  string  $expectedString
157
     */
158
    public function expectOutputString($expectedString)
159
    {
160
        if ($this->expectedRegex !== NULL) {
161
            throw new PHPUnit_Framework_Exception;
162
        }
163
 
164
        if (is_string($expectedString) || is_null($expectedString)) {
165
            $this->expectedString = $expectedString;
166
        }
167
    }
168
 
169
    /**
170
     * @return mixed
171
     * @throws RuntimeException
172
     */
173
    protected function runTest()
174
    {
175
        ob_start();
176
        $this->obActive = TRUE;
177
 
178
        try {
179
            $testResult = parent::runTest();
180
        }
181
 
182
        catch (Exception $e) {
183
            ob_end_clean();
184
            $this->obActive = FALSE;
185
            throw $e;
186
        }
187
 
188
        if ($this->outputCallback === FALSE) {
189
            $this->output = ob_get_contents();
190
        } else {
191
            $this->output = call_user_func_array($this->outputCallback, array(ob_get_contents()));
192
        }
193
 
194
        ob_end_clean();
195
        $this->obActive = FALSE;
196
 
197
        if ($this->expectedRegex !== NULL) {
198
            $this->assertRegExp($this->expectedRegex, $this->output);
199
            $this->expectedRegex = NULL;
200
        }
201
 
202
        else if ($this->expectedString !== NULL) {
203
            $this->assertEquals($this->expectedString, $this->output);
204
            $this->expectedString = NULL;
205
        }
206
 
207
        return $testResult;
208
    }
209
}
210
?>