[TASK] Make base test classes always available
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Tests / UnitTestCase.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\PathUtility;
19 use TYPO3\CMS\Core\Utility\StringUtility;
20
21 /**
22 * Base test case for unit tests.
23 *
24 * This class currently only inherits the base test case. However, it is recommended
25 * to extend this class for unit test cases instead of the base test case because if,
26 * at some point, specific behavior needs to be implemented for unit tests, your test cases
27 * will profit from it automatically.
28 *
29 */
30 abstract class UnitTestCase extends BaseTestCase
31 {
32 /**
33 * @todo make LoadedExtensionsArray serializable instead
34 *
35 * @var array
36 */
37 protected $backupGlobalsBlacklist = array('TYPO3_LOADED_EXT');
38
39 /**
40 * Absolute path to files that should be removed after a test.
41 * Handled in tearDown. Tests can register here to get any files
42 * within typo3temp/ or typo3conf/ext cleaned up again.
43 *
44 * @var array
45 */
46 protected $testFilesToDelete = array();
47
48 /**
49 * Unset all additional properties of test classes to help PHP
50 * garbage collection. This reduces memory footprint with lots
51 * of tests.
52 *
53 * If owerwriting tearDown() in test classes, please call
54 * parent::tearDown() at the end. Unsetting of own properties
55 * is not needed this way.
56 *
57 * @throws \RuntimeException
58 * @return void
59 */
60 protected function tearDown()
61 {
62 // Unset properties of test classes to safe memory
63 $reflection = new \ReflectionObject($this);
64 foreach ($reflection->getProperties() as $property) {
65 $declaringClass = $property->getDeclaringClass()->getName();
66 if (
67 !$property->isStatic()
68 && $declaringClass !== \TYPO3\CMS\Core\Tests\UnitTestCase::class
69 && $declaringClass !== \TYPO3\CMS\Core\Tests\BaseTestCase::class
70 && strpos($property->getDeclaringClass()->getName(), 'PHPUnit_') !== 0
71 ) {
72 $propertyName = $property->getName();
73 unset($this->$propertyName);
74 }
75 }
76 unset($reflection);
77
78 // Delete registered test files and directories
79 foreach ($this->testFilesToDelete as $absoluteFileName) {
80 $absoluteFileName = GeneralUtility::fixWindowsFilePath(PathUtility::getCanonicalPath($absoluteFileName));
81 if (!GeneralUtility::validPathStr($absoluteFileName)) {
82 throw new \RuntimeException('tearDown() cleanup: Filename contains illegal characters', 1410633087);
83 }
84 if (!StringUtility::beginsWith($absoluteFileName, PATH_site . 'typo3temp/')) {
85 throw new \RuntimeException(
86 'tearDown() cleanup: Files to delete must be within typo3temp/',
87 1410633412
88 );
89 }
90 // file_exists returns false for links pointing to not existing targets, so handle links before next check.
91 if (@is_link($absoluteFileName) || @is_file($absoluteFileName)) {
92 unlink($absoluteFileName);
93 } elseif (@is_dir($absoluteFileName)) {
94 GeneralUtility::rmdir($absoluteFileName, true);
95 } else {
96 throw new \RuntimeException('tearDown() cleanup: File, link or directory does not exist', 1410633510);
97 }
98 }
99 $this->testFilesToDelete = array();
100 }
101 }