]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Tests/Framework/TestCaseTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Tests / Framework / TestCaseTest.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, 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-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 2.0.0
45  */
46
47 require_once 'PHPUnit/Framework/TestCase.php';
48
49 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Error.php';
50 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Failure.php';
51 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'NoArgTestCaseTest.php';
52 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'SetupFailure.php';
53 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Success.php';
54 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TearDownFailure.php';
55 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ThrowExceptionTestCase.php';
56 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ThrowNoExceptionTestCase.php';
57 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TornDown.php';
58 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TornDown2.php';
59 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TornDown3.php';
60 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TornDown4.php';
61 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'WasRun.php';
62
63 $GLOBALS['a']  = 'a';
64 $_ENV['b']     = 'b';
65 $_POST['c']    = 'c';
66 $_GET['d']     = 'd';
67 $_COOKIE['e']  = 'e';
68 $_SERVER['f']  = 'f';
69 $_FILES['g']   = 'g';
70 $_REQUEST['h'] = 'h';
71
72 /**
73  *
74  *
75  * @category   Testing
76  * @package    PHPUnit
77  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
78  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
79  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
80  * @version    Release: 3.3.17
81  * @link       http://www.phpunit.de/
82  * @since      Class available since Release 2.0.0
83  */
84 class Framework_TestCaseTest extends PHPUnit_Framework_TestCase
85 {
86     public function testCaseToString()
87     {
88         $this->assertEquals(
89           'testCaseToString(Framework_TestCaseTest)',
90           $this->toString()
91         );
92     }
93
94     public function testError()
95     {
96         $this->verifyError(new Error);
97     }
98
99     public function testExceptionRunningAndTearDown()
100     {
101         $result = new PHPUnit_Framework_TestResult();
102         $t      = new TornDown4;
103
104         $t->run($result);
105
106         $errors = $result->errors();
107
108         $this->assertEquals(
109           'tearDown',
110           $errors[0]->thrownException()->getMessage()
111         );
112     }
113
114     public function testFailure()
115     {
116         $this->verifyFailure(new Failure);
117     }
118
119     /* PHP does not support anonymous classes
120     public function testNamelessTestCase()
121     {
122     }
123     */
124
125     public function testNoArgTestCasePasses()
126     {
127         $result = new PHPUnit_Framework_TestResult();
128         $t      = new PHPUnit_Framework_TestSuite('NoArgTestCaseTest');
129
130         $t->run($result);
131
132         $this->assertEquals(1, count($result));
133         $this->assertEquals(0, $result->failureCount());
134         $this->assertEquals(0, $result->errorCount());
135     }
136
137     public function testRunAndTearDownFails()
138     {
139         $fails = new TornDown2;
140
141         $this->verifyError($fails);
142         $this->assertTrue($fails->tornDown);
143     }
144
145     public function testSetupFails()
146     {
147         $this->verifyError(new SetupFailure);
148     }
149
150     public function testSuccess()
151     {
152         $this->verifySuccess(new Success);
153     }
154
155     public function testTearDownAfterError()
156     {
157         $fails = new TornDown;
158
159         $this->verifyError($fails);
160         $this->assertTrue($fails->tornDown);
161     }
162
163     public function testTearDownFails()
164     {
165         $this->verifyError(new TearDownFailure);
166     }
167
168     public function testTearDownSetupFails()
169     {
170         $fails = new TornDown3;
171
172         $this->verifyError($fails);
173         $this->assertFalse($fails->tornDown);
174     }
175
176     public function testWasRun()
177     {
178         $test = new WasRun;
179         $test->run();
180
181         $this->assertTrue($test->wasRun);
182     }
183
184     public function testException()
185     {
186         $test = new ThrowExceptionTestCase('test');
187         $test->setExpectedException('Exception');
188
189         $result = $test->run();
190
191         $this->assertEquals(1, count($result));
192         $this->assertTrue($result->wasSuccessful());
193     }
194
195     public function testNoException()
196     {
197         $test = new ThrowNoExceptionTestCase('test');
198         $test->setExpectedException('Exception');
199
200         $result = $test->run();
201
202         $this->assertEquals(1, $result->failureCount());
203         $this->assertEquals(1, count($result));
204     }
205
206     public function testWrongException()
207     {
208         $test = new ThrowExceptionTestCase('test');
209         $test->setExpectedException('RuntimeException');
210
211         $result = $test->run();
212
213         $this->assertEquals(1, $result->errorCount());
214         $this->assertEquals(1, count($result));
215     }
216
217     public function testGlobalsBackupPre()
218     {
219         global $a;
220
221         $this->assertEquals('a', $a);
222         $this->assertEquals('a', $GLOBALS['a']);
223         $this->assertEquals('b', $_ENV['b']);
224         $this->assertEquals('c', $_POST['c']);
225         $this->assertEquals('d', $_GET['d']);
226         $this->assertEquals('e', $_COOKIE['e']);
227         $this->assertEquals('f', $_SERVER['f']);
228         $this->assertEquals('g', $_FILES['g']);
229         $this->assertEquals('h', $_REQUEST['h']);
230
231         $GLOBALS['a']   = 'aa';
232         $GLOBALS['foo'] = 'bar';
233         $_ENV['b']      = 'bb';
234         $_POST['c']     = 'cc';
235         $_GET['d']      = 'dd';
236         $_COOKIE['e']   = 'ee';
237         $_SERVER['f']   = 'ff';
238         $_FILES['g']    = 'gg';
239         $_REQUEST['h']  = 'hh';
240
241         $this->assertEquals('aa', $a);
242         $this->assertEquals('aa', $GLOBALS['a']);
243         $this->assertEquals('bar', $GLOBALS['foo']);
244         $this->assertEquals('bb', $_ENV['b']);
245         $this->assertEquals('cc', $_POST['c']);
246         $this->assertEquals('dd', $_GET['d']);
247         $this->assertEquals('ee', $_COOKIE['e']);
248         $this->assertEquals('ff', $_SERVER['f']);
249         $this->assertEquals('gg', $_FILES['g']);
250         $this->assertEquals('hh', $_REQUEST['h']);
251     }
252
253     public function testGlobalsBackupPost()
254     {
255         global $a;
256
257         $this->assertEquals('a', $a);
258         $this->assertEquals('a', $GLOBALS['a']);
259         $this->assertEquals('b', $_ENV['b']);
260         $this->assertEquals('c', $_POST['c']);
261         $this->assertEquals('d', $_GET['d']);
262         $this->assertEquals('e', $_COOKIE['e']);
263         $this->assertEquals('f', $_SERVER['f']);
264         $this->assertEquals('g', $_FILES['g']);
265         $this->assertEquals('h', $_REQUEST['h']);
266
267         $this->assertArrayNotHasKey('foo', $GLOBALS);
268     }
269
270     protected function verifyError(PHPUnit_Framework_TestCase $test)
271     {
272         $result = $test->run();
273
274         $this->assertEquals(1, $result->errorCount());
275         $this->assertEquals(0, $result->failureCount());
276         $this->assertEquals(1, count($result));
277     }
278
279     protected function verifyFailure(PHPUnit_Framework_TestCase $test)
280     {
281         $result = $test->run();
282
283         $this->assertEquals(0, $result->errorCount());
284         $this->assertEquals(1, $result->failureCount());
285         $this->assertEquals(1, count($result));
286     }
287
288     protected function verifySuccess(PHPUnit_Framework_TestCase $test)
289     {
290         $result = $test->run();
291
292         $this->assertEquals(0, $result->errorCount());
293         $this->assertEquals(0, $result->failureCount());
294         $this->assertEquals(1, count($result));
295     }
296 }
297 ?>