]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Framework/TestCase.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Framework / TestCase.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.php';
48 require_once 'PHPUnit/Framework/MockObject/Mock.php';
49 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedAtLeastOnce.php';
50 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedAtIndex.php';
51 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedCount.php';
52 require_once 'PHPUnit/Framework/MockObject/Stub.php';
53 require_once 'PHPUnit/Runner/BaseTestRunner.php';
54 require_once 'PHPUnit/Util/Filter.php';
55
56 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
57
58 if (!class_exists('PHPUnit_Framework_TestCase', FALSE)) {
59
60 /**
61  * A TestCase defines the fixture to run multiple tests.
62  *
63  * To define a TestCase
64  *
65  *   1) Implement a subclass of PHPUnit_Framework_TestCase.
66  *   2) Define instance variables that store the state of the fixture.
67  *   3) Initialize the fixture state by overriding setUp().
68  *   4) Clean-up after a test by overriding tearDown().
69  *
70  * Each test runs in its own fixture so there can be no side effects
71  * among test runs.
72  *
73  * Here is an example:
74  *
75  * <code>
76  * <?php
77  * require_once 'PHPUnit/Framework/TestCase.php';
78  *
79  * class MathTest extends PHPUnit_Framework_TestCase
80  * {
81  *     public $value1;
82  *     public $value2;
83  *
84  *     protected function setUp()
85  *     {
86  *         $this->value1 = 2;
87  *         $this->value2 = 3;
88  *     }
89  * }
90  * ?>
91  * </code>
92  *
93  * For each test implement a method which interacts with the fixture.
94  * Verify the expected results with assertions specified by calling
95  * assert with a boolean.
96  *
97  * <code>
98  * <?php
99  * public function testPass()
100  * {
101  *     $this->assertTrue($this->value1 + $this->value2 == 5);
102  * }
103  * ?>
104  * </code>
105  *
106  * @category   Testing
107  * @package    PHPUnit
108  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
109  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
110  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
111  * @version    Release: 3.3.17
112  * @link       http://www.phpunit.de/
113  * @since      Class available since Release 2.0.0
114  * @abstract
115  */
116 abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
117 {
118     /**
119      * Enable or disable the backup and restoration of the $GLOBALS array.
120      * Overwrite this attribute in a child class of TestCase.
121      * Setting this attribute in setUp() has no effect!
122      *
123      * @var    boolean
124      */
125     protected $backupGlobals = NULL;
126
127     /**
128      * @var    array
129      */
130     protected $globalsBackup = array();
131
132     /**
133      * @var    array
134      */
135     protected $data = array();
136
137     /**
138      * @var    string
139      */
140     protected $dataName = '';
141
142     /**
143      * The name of the expected Exception.
144      *
145      * @var    mixed
146      */
147     protected $expectedException = NULL;
148
149     /**
150      * The message of the expected Exception.
151      *
152      * @var    string
153      */
154     protected $expectedExceptionMessage = '';
155
156     /**
157      * The code of the expected Exception.
158      *
159      * @var    integer
160      */
161     protected $expectedExceptionCode;
162
163     /**
164      * Fixture that is shared between the tests of a test suite.
165      *
166      * @var    mixed
167      */
168     protected $sharedFixture;
169
170     /**
171      * The name of the test case.
172      *
173      * @var    string
174      */
175     protected $name = NULL;
176
177     /**
178      * @var    string
179      */
180     protected $exceptionMessage = NULL;
181
182     /**
183      * @var    integer
184      */
185     protected $exceptionCode = 0;
186
187     /**
188      * @var    Array
189      */
190     protected $iniSettings = array();
191
192     /**
193      * @var    Array
194      */
195     protected $locale = array();
196
197     /**
198      * @var    Array
199      */
200     protected $mockObjects = array();
201
202     /**
203      * @var    integer
204      */
205     protected $status;
206
207     /**
208      * @var    string
209      */
210     protected $statusMessage = '';
211
212     /**
213      * @var    integer
214      */
215     protected $numAssertions = 0;
216
217     /**
218      * @var    array
219      */
220     protected static $superGlobalArrays = array(
221       '_ENV',
222       '_POST',
223       '_GET',
224       '_COOKIE',
225       '_SERVER',
226       '_FILES',
227       '_REQUEST'
228     );
229
230     /**
231      * @var    array
232      */
233     protected static $superGlobalArraysLong = array(
234       'HTTP_ENV_VARS',
235       'HTTP_POST_VARS',
236       'HTTP_GET_VARS',
237       'HTTP_COOKIE_VARS',
238       'HTTP_SERVER_VARS',
239       'HTTP_POST_FILES'
240     );
241
242     /**
243      * Constructs a test case with the given name.
244      *
245      * @param  string $name
246      * @param  array  $data
247      * @param  string $dataName
248      */
249     public function __construct($name = NULL, array $data = array(), $dataName = '')
250     {
251         if ($name !== NULL) {
252             $this->setName($name);
253         }
254
255         $this->data     = $data;
256         $this->dataName = $dataName;
257     }
258
259     /**
260      * Returns a string representation of the test case.
261      *
262      * @return string
263      */
264     public function toString()
265     {
266         $class = new ReflectionClass($this);
267
268         $buffer = sprintf(
269           '%s(%s)',
270
271           $this->getName(FALSE),
272           $class->name
273         );
274
275         return $buffer . $this->getDataSetAsString();
276     }
277
278     /**
279      * Counts the number of test cases executed by run(TestResult result).
280      *
281      * @return integer
282      */
283     public function count()
284     {
285         return 1;
286     }
287
288     /**
289      * Gets the name of a TestCase.
290      *
291      * @param  boolean $withDataSet
292      * @return string
293      */
294     public function getName($withDataSet = TRUE)
295     {
296         if ($withDataSet) {
297             return $this->name . $this->getDataSetAsString(FALSE);
298         } else {
299             return $this->name;
300         }
301     }
302
303     /**
304      * @return string
305      * @since  Method available since Release 3.2.0
306      */
307     public function getExpectedException()
308     {
309         return $this->expectedException;
310     }
311
312     /**
313      * @param  mixed   $exceptionName
314      * @param  string  $exceptionMessage
315      * @param  integer $exceptionCode
316      * @since  Method available since Release 3.2.0
317      */
318     public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = 0)
319     {
320         $this->expectedException        = $exceptionName;
321         $this->expectedExceptionMessage = $exceptionMessage;
322         $this->expectedExceptionCode    = $exceptionCode;
323     }
324
325     /**
326      * Returns the status of this test.
327      *
328      * @return integer
329      * @since  Method available since Release 3.1.0
330      */
331     public function getStatus()
332     {
333         return $this->status;
334     }
335
336     /**
337      * Returns the status message of this test.
338      *
339      * @return string
340      * @since  Method available since Release 3.3.0
341      */
342     public function getStatusMessage()
343     {
344         return $this->statusMessage;
345     }
346
347     /**
348      * Returns whether or not this test has failed.
349      *
350      * @return boolean
351      * @since  Method available since Release 3.0.0
352      */
353     public function hasFailed()
354     {
355         $status = $this->getStatus();
356
357         return $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE ||
358                $status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
359     }
360
361     /**
362      * Runs the test case and collects the results in a TestResult object.
363      * If no TestResult object is passed a new one will be created.
364      *
365      * @param  PHPUnit_Framework_TestResult $result
366      * @return PHPUnit_Framework_TestResult
367      * @throws InvalidArgumentException
368      */
369     public function run(PHPUnit_Framework_TestResult $result = NULL)
370     {
371         if ($result === NULL) {
372             $result = $this->createResult();
373         }
374
375         $result->run($this);
376
377         return $result;
378     }
379
380     /**
381      * Runs the bare test sequence.
382      *
383      */
384     public function runBare()
385     {
386         $this->numAssertions = 0;
387
388         // Backup the $GLOBALS array.
389         if ($this->backupGlobals === NULL || $this->backupGlobals === TRUE) {
390             $this->backupGlobals();
391         }
392
393         // Set up the fixture.
394         $this->setUp();
395
396         // Clean up stat cache.
397         clearstatcache();
398
399         // Run the test.
400         try {
401             // Assert pre-conditions.
402             $this->assertPreConditions();
403
404             $this->runTest();
405
406             // Assert post-conditions.
407             $this->assertPostConditions();
408
409             // Verify Mock Object conditions.
410             foreach ($this->mockObjects as $mockObject) {
411                 $this->numAssertions++;
412                 $mockObject->__phpunit_verify();
413             }
414
415             $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
416         }
417
418         catch (Exception $e) {
419             if ($e instanceof PHPUnit_Framework_IncompleteTest) {
420                 $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
421             }
422
423             if ($e instanceof PHPUnit_Framework_SkippedTest) {
424                 $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
425             }
426
427             if ($e instanceof PHPUnit_Framework_AssertionFailedError) {
428                 $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
429             }
430
431             $this->statusMessage = $e->getMessage();
432         }
433
434         $this->mockObjects = array();
435
436         // Tear down the fixture.
437         $this->tearDown();
438
439         // Clean up stat cache.
440         clearstatcache();
441
442         // Restore the $GLOBALS array.
443         if ($this->backupGlobals === NULL || $this->backupGlobals === TRUE) {
444             $this->restoreGlobals();
445         }
446
447         // Clean up INI settings.
448         foreach ($this->iniSettings as $varName => $oldValue) {
449             ini_set($varName, $oldValue);
450         }
451
452         $this->iniSettings = array();
453
454         // Clean up locale settings.
455         foreach ($this->locale as $category => $locale) {
456             setlocale($category, $locale);
457         }
458
459         // Workaround for missing "finally".
460         if (isset($e)) {
461             throw $e;
462         }
463     }
464
465     /**
466      * Override to run the test and assert its state.
467      *
468      * @throws RuntimeException
469      */
470     protected function runTest()
471     {
472         if ($this->name === NULL) {
473             throw new RuntimeException(
474               'PHPUnit_Framework_TestCase::$name must not be NULL.'
475             );
476         }
477
478         try {
479             $class  = new ReflectionClass($this);
480             $method = $class->getMethod($this->name);
481         }
482
483         catch (ReflectionException $e) {
484             $this->fail($e->getMessage());
485         }
486
487         try {
488             if (empty($this->data)) {
489                 $method->invoke($this);
490             } else {
491                 $method->invokeArgs($this, $this->data);
492             }
493         }
494
495         catch (Exception $e) {
496             if (!$e instanceof PHPUnit_Framework_IncompleteTest &&
497                 !$e instanceof PHPUnit_Framework_SkippedTest &&
498                 is_string($this->expectedException) &&
499                 $e instanceof $this->expectedException) {
500                 if (is_string($this->expectedExceptionMessage) &&
501                     !empty($this->expectedExceptionMessage)) {
502                     $this->assertContains(
503                       $this->expectedExceptionMessage,
504                       $e->getMessage()
505                     );
506                 }
507
508                 if (is_int($this->expectedExceptionCode) &&
509                     $this->expectedExceptionCode !== 0) {
510                     $this->assertEquals(
511                       $this->expectedExceptionCode, $e->getCode()
512                     );
513                 }
514
515                 $this->numAssertions++;
516
517                 return;
518             } else {
519                 throw $e;
520             }
521         }
522
523         if ($this->expectedException !== NULL) {
524             $this->numAssertions++;
525             $this->fail('Expected exception ' . $this->expectedException);
526         }
527     }
528
529     /**
530      * Sets the name of a TestCase.
531      *
532      * @param  string
533      */
534     public function setName($name)
535     {
536         $this->name = $name;
537     }
538
539     /**
540      * Calling this method in setUp() has no effect!
541      *
542      * @param  boolean $backupGlobals
543      * @since  Method available since Release 3.3.0
544      */
545     public function setBackupGlobals($backupGlobals)
546     {
547         if (is_null($this->backupGlobals) && is_bool($backupGlobals)) {
548             $this->backupGlobals = $backupGlobals;
549         }
550     }
551
552     /**
553      * Sets the shared fixture.
554      *
555      * @param  mixed $sharedFixture
556      * @since  Method available since Release 3.1.0
557      */
558     public function setSharedFixture($sharedFixture)
559     {
560         $this->sharedFixture = $sharedFixture;
561     }
562
563     /**
564      * This method is a wrapper for the ini_set() function that automatically
565      * resets the modified php.ini setting to its original value after the
566      * test is run.
567      *
568      * @param  string  $varName
569      * @param  string  $newValue
570      * @throws InvalidArgumentException
571      * @throws RuntimeException
572      * @since  Method available since Release 3.0.0
573      */
574     protected function iniSet($varName, $newValue)
575     {
576         if (!is_string($varName)) {
577             throw new InvalidArgumentException;
578         }
579
580         $currentValue = ini_set($varName, $newValue);
581
582         if ($currentValue !== FALSE) {
583             $this->iniSettings[$varName] = $currentValue;
584         } else {
585             throw new RuntimeException;
586         }
587     }
588
589     /**
590      * This method is a wrapper for the setlocale() function that automatically
591      * resets the locale to its original value after the test is run.
592      *
593      * @param  integer $category
594      * @param  string  $locale
595      * @throws InvalidArgumentException
596      * @throws RuntimeException
597      * @since  Method available since Release 3.1.0
598      */
599     protected function setLocale()
600     {
601         $args = func_get_args();
602
603         if (count($args) < 2) {
604             throw new InvalidArgumentException;
605         }
606
607         $category = $args[0];
608         $locale   = $args[1];
609
610         $categories = array(
611           LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME
612         );
613
614         if (defined('LC_MESSAGES')) {
615             $categories[] = LC_MESSAGES;
616         }
617
618         if (!in_array($category, $categories)) {
619             throw new InvalidArgumentException;
620         }
621
622         if (!is_array($locale) && !is_string($locale)) {
623             throw new InvalidArgumentException;
624         }
625
626         $this->locale[$category] = setlocale($category, NULL);
627
628         $result = call_user_func_array( 'setlocale', $args );
629
630         if ($result === FALSE) {
631             throw new RuntimeException(
632               'The locale functionality is not implemented on your platform, ' .
633               'the specified locale does not exist or the category name is ' .
634               'invalid.'
635             );
636         }
637     }
638
639     /**
640      * Returns a mock object for the specified class.
641      *
642      * @param  string  $className
643      * @param  array   $methods
644      * @param  array   $arguments
645      * @param  string  $mockClassName
646      * @param  boolean $callOriginalConstructor
647      * @param  boolean $callOriginalClone
648      * @param  boolean $callAutoload
649      * @return object
650      * @since  Method available since Release 3.0.0
651      */
652     protected function getMock($className, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
653     {
654         if (!is_string($className) || !is_string($mockClassName)) {
655             throw new InvalidArgumentException;
656         }
657
658         if (!is_array($methods) && !is_null($methods)) {
659             throw new InvalidArgumentException;
660         }
661
662         $mock = PHPUnit_Framework_MockObject_Mock::generate(
663           $className,
664           $methods,
665           $mockClassName,
666           $callOriginalConstructor,
667           $callOriginalClone,
668           $callAutoload
669         );
670
671         if (count($arguments) == 0) {
672             $mockObject = new $mock->mockClassName;
673         } else {
674             $mockClass  = new ReflectionClass($mock->mockClassName);
675             $mockObject = $mockClass->newInstanceArgs($arguments);
676         }
677
678         $this->mockObjects[] = $mockObject;
679
680         return $mockObject;
681     }
682
683     /**
684      * Adds a value to the assertion counter.
685      *
686      * @param integer $count
687      * @since Method available since Release 3.3.3
688      */
689     public function addToAssertionCount($count)
690     {
691         $this->numAssertions += $count;
692     }
693
694     /**
695      * Returns the number of assertions performed by this test.
696      *
697      * @return integer
698      * @since  Method available since Release 3.3.0
699      */
700     public function getNumAssertions()
701     {
702         return $this->numAssertions;
703     }
704
705     /**
706      * Returns a matcher that matches when the method it is evaluated for
707      * is executed zero or more times.
708      *
709      * @return PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount
710      * @since  Method available since Release 3.0.0
711      */
712     protected function any()
713     {
714         return new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
715     }
716
717     /**
718      * Returns a matcher that matches when the method it is evaluated for
719      * is never executed.
720      *
721      * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
722      * @since  Method available since Release 3.0.0
723      */
724     protected function never()
725     {
726         return new PHPUnit_Framework_MockObject_Matcher_InvokedCount(0);
727     }
728
729     /**
730      * Returns a matcher that matches when the method it is evaluated for
731      * is executed at least once.
732      *
733      * @return PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce
734      * @since  Method available since Release 3.0.0
735      */
736     protected function atLeastOnce()
737     {
738         return new PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce;
739     }
740
741     /**
742      * Returns a matcher that matches when the method it is evaluated for
743      * is executed exactly once.
744      *
745      * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
746      * @since  Method available since Release 3.0.0
747      */
748     protected function once()
749     {
750         return new PHPUnit_Framework_MockObject_Matcher_InvokedCount(1);
751     }
752
753     /**
754      * Returns a matcher that matches when the method it is evaluated for
755      * is executed exactly $count times.
756      *
757      * @param  integer $count
758      * @return PHPUnit_Framework_MockObject_Matcher_InvokedCount
759      * @since  Method available since Release 3.0.0
760      */
761     protected function exactly($count)
762     {
763         return new PHPUnit_Framework_MockObject_Matcher_InvokedCount($count);
764     }
765
766     /**
767      * Returns a matcher that matches when the method it is evaluated for
768      * is invoked at the given $index.
769      *
770      * @param  integer $index
771      * @return PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex
772      * @since  Method available since Release 3.0.0
773      */
774     protected function at($index)
775     {
776         return new PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex($index);
777     }
778
779     /**
780      *
781      *
782      * @param  mixed $value
783      * @return PHPUnit_Framework_MockObject_Stub_Return
784      * @since  Method available since Release 3.0.0
785      */
786     protected function returnValue($value)
787     {
788         return new PHPUnit_Framework_MockObject_Stub_Return($value);
789     }
790
791     /**
792      *
793      *
794      * @param  integer $argumentIndex
795      * @return PHPUnit_Framework_MockObject_Stub_ReturnArgument
796      * @since  Method available since Release 3.3.0
797      */
798     protected function returnArgument($argumentIndex)
799     {
800         return new PHPUnit_Framework_MockObject_Stub_ReturnArgument($argumentIndex);
801     }
802
803     /**
804      *
805      *
806      * @param  mixed $callback
807      * @return PHPUnit_Framework_MockObject_Stub_ReturnCallback
808      * @since  Method available since Release 3.3.0
809      */
810     protected function returnCallback($callback)
811     {
812         return new PHPUnit_Framework_MockObject_Stub_ReturnCallback($callback);
813     }
814
815     /**
816      *
817      *
818      * @param  Exception $exception
819      * @return PHPUnit_Framework_MockObject_Stub_Exception
820      * @since  Method available since Release 3.1.0
821      */
822     protected function throwException(Exception $exception)
823     {
824         return new PHPUnit_Framework_MockObject_Stub_Exception($exception);
825     }
826
827     /**
828      *
829      *
830      * @param  mixed $value, ...
831      * @return PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls
832      * @since  Method available since Release 3.0.0
833      */
834     protected function onConsecutiveCalls()
835     {
836         $args = func_get_args();
837
838         return new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($args);
839     }
840
841     /**
842      * @param  mixed $data
843      * @return string
844      * @since  Method available since Release 3.2.1
845      */
846     protected function dataToString($data)
847     {
848         $result = array();
849
850         foreach ($data as $_data) {
851             if (is_array($_data)) {
852                 $result[] = 'array(' . $this->dataToString($_data) . ')';
853             }
854
855             else if (is_object($_data)) {
856                 $object = new ReflectionObject($_data);
857
858                 if ($object->hasMethod('__toString')) {
859                     $result[] = (string)$_data;
860                 } else {
861                     $result[] = get_class($_data);
862                 }
863             }
864
865             else if (is_resource($_data)) {
866                 $result[] = '<resource>';
867             }
868
869             else {
870                 $result[] = var_export($_data, TRUE);
871             }
872         }
873
874         return join(', ', $result);
875     }
876
877     /**
878      * Gets the data set description of a TestCase.
879      *
880      * @param  boolean $includeData
881      * @return string
882      * @since  Method available since Release 3.3.0
883      */
884     protected function getDataSetAsString($includeData = TRUE)
885     {
886         $buffer = '';
887
888         if (!empty($this->data)) {
889             if (is_int($this->dataName)) {
890                 $buffer .= sprintf(' with data set #%d', $this->dataName);
891             } else {
892                 $buffer .= sprintf(' with data set "%s"', $this->dataName);
893             }
894
895             if ($includeData) {
896                 $buffer .= sprintf(' (%s)', $this->dataToString($this->data));
897             }
898         }
899
900         return $buffer;
901     }
902
903     /**
904      * Creates a default TestResult object.
905      *
906      * @return PHPUnit_Framework_TestResult
907      */
908     protected function createResult()
909     {
910         return new PHPUnit_Framework_TestResult;
911     }
912
913     /**
914      * Sets up the fixture, for example, open a network connection.
915      * This method is called before a test is executed.
916      *
917      */
918     protected function setUp()
919     {
920     }
921
922     /**
923      * Performs assertions shared by all tests of a test case.
924      *
925      * This method is called before the execution of a test starts
926      * and after setUp() is called.
927      *
928      * @since  Method available since Release 3.2.8
929      */
930     protected function assertPreConditions()
931     {
932     }
933
934     /**
935      * Performs assertions shared by all tests of a test case.
936      *
937      * This method is called before the execution of a test ends
938      * and before tearDown() is called.
939      *
940      * @since  Method available since Release 3.2.8
941      */
942     protected function assertPostConditions()
943     {
944         // assertPostConditions() was named sharedAssertions() in
945         // PHPUnit 3.0.0-3.2.7.
946         if (method_exists($this, 'sharedAssertions')) {
947             $this->sharedAssertions();
948         }
949     }
950
951     /**
952      * Tears down the fixture, for example, close a network connection.
953      * This method is called after a test is executed.
954      */
955     protected function tearDown()
956     {
957     }
958
959     /**
960      * @since Method available since Release 3.3.0
961      */
962     protected function backupGlobals()
963     {
964         $this->globalsBackup = array();
965
966         if (ini_get('register_long_arrays') == '1') {
967             $superGlobalArrays = array_merge(
968               self::$superGlobalArrays, self::$superGlobalArraysLong
969             );
970         } else {
971             $superGlobalArrays = self::$superGlobalArrays;
972         }
973
974         foreach ($superGlobalArrays as $superGlobalArray) {
975             $this->backupSuperGlobalArray($superGlobalArray);
976         }
977
978         foreach (array_keys($GLOBALS) as $key) {
979             if ($key != 'GLOBALS' && !in_array($key, $superGlobalArrays)) {
980                 $this->globalsBackup['GLOBALS'][$key] = serialize($GLOBALS[$key]);
981             }
982         }
983     }
984
985     /**
986      * @since Method available since Release 3.3.0
987      */
988     protected function restoreGlobals()
989     {
990         if (ini_get('register_long_arrays') == '1') {
991             $superGlobalArrays = array_merge(
992               self::$superGlobalArrays, self::$superGlobalArraysLong
993             );
994         } else {
995             $superGlobalArrays = self::$superGlobalArrays;
996         }
997
998         foreach ($superGlobalArrays as $superGlobalArray) {
999             $this->restoreSuperGlobalArray($superGlobalArray);
1000         }
1001
1002         foreach (array_keys($GLOBALS) as $key) {
1003             if ($key != 'GLOBALS' && !in_array($key, $superGlobalArrays)) {
1004                 if (isset($this->globalsBackup['GLOBALS'][$key])) {
1005                     $GLOBALS[$key] = unserialize($this->globalsBackup['GLOBALS'][$key]);
1006                 } else {
1007                     unset($GLOBALS[$key]);
1008                 }
1009             }
1010         }
1011
1012         $this->globalsBackup = array();
1013     }
1014
1015     protected function backupSuperGlobalArray($superGlobalArray)
1016     {
1017         $this->globalsBackup[$superGlobalArray] = array();
1018
1019         if (isset($GLOBALS[$superGlobalArray])) {
1020             foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
1021                 $this->globalsBackup[$superGlobalArray][$key] = serialize($value);
1022             }
1023         }
1024     }
1025
1026     protected function restoreSuperGlobalArray($superGlobalArray)
1027     {
1028         if (isset($GLOBALS[$superGlobalArray])) {
1029             foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
1030                 if (isset($this->globalsBackup[$superGlobalArray][$key])) {
1031                     $GLOBALS[$superGlobalArray][$key] = unserialize($this->globalsBackup[$superGlobalArray][$key]);
1032                 } else {
1033                     unset($GLOBALS[$superGlobalArray][$key]);
1034                 }
1035             }
1036         }
1037
1038         $this->globalsBackup[$superGlobalArray] = array();
1039     }
1040 }
1041
1042 }
1043 ?>