[FEATURE] TS-list: allow to unique, reverse and sort
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / BaseTestCase.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2005-2013 Robert Lemke (robert@typo3.org)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * The mother of all test cases.
29 *
30 * Don't sub class this test case but rather choose a more specialized base test case,
31 * such as UnitTestCase or FunctionalTestCase
32 *
33 */
34 abstract class BaseTestCase extends \PHPUnit_Framework_TestCase {
35 /**
36 * whether global variables should be backuped
37 *
38 * @var boolean
39 */
40 protected $backupGlobals = TRUE;
41
42 /**
43 * whether static attributes should be backuped
44 *
45 * @var boolean
46 */
47 protected $backupStaticAttributes = FALSE;
48
49 /**
50 * Exclude TYPO3_DB from backup/restore of $GLOBALS
51 * because resource types cannot be handled during serializing
52 *
53 * @var array
54 */
55 protected $backupGlobalsBlacklist = array('TYPO3_DB');
56
57 /**
58 * Creates a mock object which allows for calling protected methods and access of protected properties.
59 *
60 * @param string $originalClassName name of class to create the mock object of, must not be empty
61 * @param array<string> $methods name of the methods to mock
62 * @param array $arguments arguments to pass to constructor
63 * @param string $mockClassName the class name to use for the mock class
64 * @param boolean $callOriginalConstructor whether to call the constructor
65 * @param boolean $callOriginalClone whether to call the __clone method
66 * @param boolean $callAutoload whether to call any autoload function
67 *
68 * @return \PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface
69 * a mock of $originalClassName with access methods added
70 *
71 * @see \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase::getAccessibleMock
72 */
73 protected function getAccessibleMock(
74 $originalClassName, array $methods = array(), array $arguments = array(), $mockClassName = '',
75 $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE
76 ) {
77 if ($originalClassName === '') {
78 throw new \InvalidArgumentException('$originalClassName must not be empty.', 1334701880);
79 }
80
81 return $this->getMock(
82 $this->buildAccessibleProxy($originalClassName),
83 $methods,
84 $arguments,
85 $mockClassName,
86 $callOriginalConstructor,
87 $callOriginalClone,
88 $callAutoload
89 );
90 }
91
92 /**
93 * Creates a proxy class of the specified class which allows
94 * for calling even protected methods and access of protected properties.
95 *
96 * @param string $className Name of class to make available, must not be empty
97 *
98 * @return string Fully qualified name of the built class, will not be empty
99 *
100 * @see Tx_Extbase_Tests_Unit_BaseTestCase::buildAccessibleProxy
101 */
102 protected function buildAccessibleProxy($className) {
103 $accessibleClassName = uniqid('Tx_Phpunit_AccessibleProxy');
104 $class = new \ReflectionClass($className);
105 $abstractModifier = $class->isAbstract() ? 'abstract ' : '';
106
107 eval(
108 $abstractModifier . 'class ' . $accessibleClassName .
109 ' extends ' . $className . ' implements \TYPO3\CMS\Core\Tests\AccessibleObjectInterface {' .
110 'public function _call($methodName) {' .
111 'if ($methodName === \'\') {' .
112 'throw new \InvalidArgumentException(\'$methodName must not be empty.\', 1334663993);' .
113 '}' .
114 '$args = func_get_args();' .
115 'return call_user_func_array(array($this, $methodName), array_slice($args, 1));' .
116 '}' .
117 'public function _callRef(' .
118 '$methodName, &$arg1 = NULL, &$arg2 = NULL, &$arg3 = NULL, &$arg4 = NULL, &$arg5= NULL, &$arg6 = NULL, ' .
119 '&$arg7 = NULL, &$arg8 = NULL, &$arg9 = NULL' .
120 ') {' .
121 'if ($methodName === \'\') {' .
122 'throw new \InvalidArgumentException(\'$methodName must not be empty.\', 1334664210);' .
123 '}' .
124 'switch (func_num_args()) {' .
125 'case 0:' .
126 'throw new RuntimeException(\'The case of 0 arguments is not supposed to happen.\', 1334703124);' .
127 'break;' .
128 'case 1:' .
129 '$returnValue = $this->$methodName();' .
130 'break;' .
131 'case 2:' .
132 '$returnValue = $this->$methodName($arg1);' .
133 'break;' .
134 'case 3:' .
135 '$returnValue = $this->$methodName($arg1, $arg2);' .
136 'break;' .
137 'case 4:' .
138 '$returnValue = $this->$methodName($arg1, $arg2, $arg3);' .
139 'break;' .
140 'case 5:' .
141 '$returnValue = $this->$methodName($arg1, $arg2, $arg3, $arg4);' .
142 'break;' .
143 'case 6:' .
144 '$returnValue = $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5);' .
145 'break;' .
146 'case 7:' .
147 '$returnValue = $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);' .
148 'break;' .
149 'case 8:' .
150 '$returnValue = $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7);' .
151 'break;' .
152 'case 9:' .
153 '$returnValue = $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7, $arg8);' .
154 'break;' .
155 'case 10:' .
156 '$returnValue = $this->$methodName(' .
157 '$arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7, $arg8, $arg9' .
158 ');' .
159 'break;' .
160 'default:' .
161 'throw new \InvalidArgumentException(' .
162 '\'_callRef currently only allows calls to methods with no more than 9 parameters.\'' .
163 ');' .
164 '}' .
165 'return $returnValue;' .
166 '}' .
167 'public function _set($propertyName, $value) {' .
168 'if ($propertyName === \'\') {' .
169 'throw new \InvalidArgumentException(\'$propertyName must not be empty.\', 1334664355);' .
170 '}' .
171 '$this->$propertyName = $value;' .
172 '}' .
173 'public function _setRef($propertyName, &$value) {' .
174 'if ($propertyName === \'\') {' .
175 'throw new \InvalidArgumentException(\'$propertyName must not be empty.\', 1334664545);' .
176 '}' .
177 '$this->$propertyName = $value;' .
178 '}' .
179 'public function _setStatic($propertyName, $value) {' .
180 'if ($propertyName === \'\') {' .
181 'throw new \InvalidArgumentException(\'$propertyName must not be empty.\', 1344242602);' .
182 '}' .
183 'self::$$propertyName = $value;' .
184 '}' .
185 'public function _get($propertyName) {' .
186 'if ($propertyName === \'\') {' .
187 'throw new \InvalidArgumentException(\'$propertyName must not be empty.\', 1334664967);' .
188 '}' .
189 'return $this->$propertyName;' .
190 '}' .
191 'public function _getStatic($propertyName) {' .
192 'if ($propertyName === \'\') {' .
193 'throw new \InvalidArgumentException(\'$propertyName must not be empty.\', 1344242603);' .
194 '}' .
195 'return self::$$propertyName;' .
196 '}' .
197 '}'
198 );
199
200 return $accessibleClassName;
201 }
202
203 /**
204 * Helper function to call protected or private methods
205 *
206 * @param object $object The object to be invoked
207 * @param string $name the name of the method to call
208 * @return mixed
209 */
210 protected function callInaccessibleMethod($object, $name) {
211 // Remove first two arguments ($object and $name)
212 $arguments = func_get_args();
213 array_splice($arguments, 0, 2);
214
215 $reflectionObject = new \ReflectionObject($object);
216 $reflectionMethod = $reflectionObject->getMethod($name);
217 $reflectionMethod->setAccessible(TRUE);
218 return $reflectionMethod->invokeArgs($object, $arguments);
219 }
220 }
221 ?>