1bdd50afdc73851ce4a2701694618c3e5456b233
[Packages/TYPO3.CMS.git] / components / testing_framework / Classes / Core / Unit / UnitTestCase.php
1 <?php
2 namespace TYPO3\Components\TestingFramework\Core;
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
20 /**
21 * Base test case for unit tests.
22 *
23 * This class currently only inherits the base test case. However, it is recommended
24 * to extend this class for unit test cases instead of the base test case because if,
25 * at some point, specific behavior needs to be implemented for unit tests, your test cases
26 * will profit from it automatically.
27 *
28 */
29 abstract class UnitTestCase extends BaseTestCase
30 {
31 /**
32 * @todo make LoadedExtensionsArray serializable instead
33 *
34 * @var array
35 */
36 protected $backupGlobalsBlacklist = ['TYPO3_LOADED_EXT'];
37
38 /**
39 * Absolute path to files that should be removed after a test.
40 * Handled in tearDown. Tests can register here to get any files
41 * within typo3temp/ or typo3conf/ext cleaned up again.
42 *
43 * @var array
44 */
45 protected $testFilesToDelete = [];
46
47 /**
48 * Unset all additional properties of test classes to help PHP
49 * garbage collection. This reduces memory footprint with lots
50 * of tests.
51 *
52 * If overwriting tearDown() in test classes, please call
53 * parent::tearDown() at the end. Unsetting of own properties
54 * is not needed this way.
55 *
56 * @throws \RuntimeException
57 * @return void
58 */
59 protected function tearDown()
60 {
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\Components\TestingFramework\Core\UnitTestCase::class
68 && $declaringClass !== \TYPO3\Components\TestingFramework\Core\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 (strpos($absoluteFileName, PATH_site . 'typo3temp/var/') !== 0) {
84 throw new \RuntimeException(
85 'tearDown() cleanup: Files to delete must be within typo3temp/var/',
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 = [];
99 }
100 }