TYPO3 CMS  TYPO3_7-6
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 
20 
30 abstract class UnitTestCase extends BaseTestCase
31 {
37  protected $backupGlobalsBlacklist = ['TYPO3_LOADED_EXT'];
38 
46  protected $testFilesToDelete = [];
47 
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 = [];
100  }
101 }
static rmdir($path, $removeNonEmpty=false)
static beginsWith($haystack, $needle)