[TASK] Split DataHandler::copyRecord_procBasedOnFieldType
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / 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 // Unset properties of test classes to safe memory
62 $reflection = new \ReflectionObject($this);
63 foreach ($reflection->getProperties() as $property) {
64 $declaringClass = $property->getDeclaringClass()->getName();
65 if (
66 !$property->isStatic()
67 && $declaringClass !== \TYPO3\CMS\Core\Tests\UnitTestCase::class
68 && $declaringClass !== \TYPO3\CMS\Core\Tests\BaseTestCase::class
69 && strpos($property->getDeclaringClass()->getName(), 'PHPUnit_') !== 0
70 ) {
71 $propertyName = $property->getName();
72 unset($this->$propertyName);
73 }
74 }
75 unset($reflection);
76
77 // Delete registered test files and directories
78 foreach ($this->testFilesToDelete as $absoluteFileName) {
79 $absoluteFileName = GeneralUtility::fixWindowsFilePath(PathUtility::getCanonicalPath($absoluteFileName));
80 if (!GeneralUtility::validPathStr($absoluteFileName)) {
81 throw new \RuntimeException('tearDown() cleanup: Filename contains illegal characters', 1410633087);
82 }
83 if (!StringUtility::beginsWith($absoluteFileName, PATH_site . 'typo3temp/')) {
84 throw new \RuntimeException(
85 'tearDown() cleanup: Files to delete must be within typo3temp/',
86 1410633412
87 );
88 }
89 // file_exists returns false for links pointing to not existing targets, so handle links before next check.
90 if (@is_link($absoluteFileName) || @is_file($absoluteFileName)) {
91 unlink($absoluteFileName);
92 } elseif(@is_dir($absoluteFileName)) {
93 GeneralUtility::rmdir($absoluteFileName, TRUE);
94 } else {
95 throw new \RuntimeException('tearDown() cleanup: File, link or directory does not exist', 1410633510);
96 }
97 }
98 $this->testFilesToDelete = array();
99 }
100
101 }