TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
UnitTestCase.php
Go to the documentation of this file.
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 
19 
29 abstract class UnitTestCase extends BaseTestCase
30 {
36  protected $backupGlobalsBlacklist = ['TYPO3_LOADED_EXT'];
37 
45  protected $testFilesToDelete = [];
46 
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\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 (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 }
static rmdir($path, $removeNonEmpty=false)