[TASK] Extbase: Raised version to 1.1.0alpha1
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / BaseTestCase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 require_once(t3lib_extMgm::extPath('phpunit') . 'class.tx_phpunit_testcase.php');
29
30 /**
31 * Base testcase for the Extbase extension.
32 */
33 abstract class Tx_Extbase_BaseTestCase extends tx_phpunit_testcase {
34
35 /**
36 * @var Tx_Extbase_Object_ManagerInterface The object manager
37 */
38 protected $objectManager;
39
40 /**
41 * Constructs a test case with the given name.
42 *
43 * @param string $name
44 * @param array $data
45 * @param string $dataName
46 */
47 public function __construct($name = NULL, array $data = array(), $dataName = '') {
48 parent::__construct($name, $data, $dataName);
49 if (!class_exists('Tx_Extbase_Utility_ClassLoader')) {
50 require(t3lib_extmgm::extPath('extbase') . 'Classes/Utility/ClassLoader.php');
51 }
52 spl_autoload_register(array('Tx_Extbase_Utility_ClassLoader', 'loadClass'));
53 }
54
55 /**
56 * Injects an untainted clone of the object manager and all its referencing
57 * objects for every test.
58 *
59 * @return void
60 */
61 public function runBare() {
62 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_Manager');
63 $this->objectManager = clone $objectManager;
64 parent::runBare();
65 }
66
67 /**
68 * Returns a mock object which allows for calling protected methods and access
69 * of protected properties.
70 *
71 * @param string $className Full qualified name of the original class
72 * @param array $methods
73 * @param array $arguments
74 * @param string $mockClassName
75 * @param boolean $callOriginalConstructor
76 * @param boolean $callOriginalClone
77 * @param boolean $callAutoload
78 * @return object
79 * @author Robert Lemke <robert@typo3.org>
80 * @api
81 */
82 protected function getAccessibleMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE) {
83 return $this->getMock($this->buildAccessibleProxy($originalClassName), $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload);
84 }
85
86
87 /**
88 * Creates a proxy class of the specified class which allows
89 * for calling even protected methods and access of protected properties.
90 *
91 * @param protected $className Full qualified name of the original class
92 * @return string Full qualified name of the built class
93 */
94 protected function buildAccessibleProxy($className) {
95 $accessibleClassName = uniqid('AccessibleTestProxy');
96 $class = new ReflectionClass($className);
97 $abstractModifier = $class->isAbstract() ? 'abstract ' : '';
98 eval('
99 ' . $abstractModifier . 'class ' . $accessibleClassName . ' extends ' . $className . ' {
100 public function _call($methodName) {
101 $args = func_get_args();
102 return call_user_func_array(array($this, $methodName), array_slice($args, 1));
103 }
104 public function _callRef($methodName, &$arg1 = NULL, &$arg2 = NULL, &$arg3 = NULL, &$arg4 = NULL, &$arg5= NULL, &$arg6 = NULL, &$arg7 = NULL, &$arg8 = NULL, &$arg9 = NULL) {
105 switch (func_num_args()) {
106 case 0 : return $this->$methodName();
107 case 1 : return $this->$methodName($arg1);
108 case 2 : return $this->$methodName($arg1, $arg2);
109 case 3 : return $this->$methodName($arg1, $arg2, $arg3);
110 case 4 : return $this->$methodName($arg1, $arg2, $arg3, $arg4);
111 case 5 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5);
112 case 6 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
113 case 7 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7);
114 case 8 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7, $arg8);
115 case 9 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7, $arg8, $arg9);
116 }
117 }
118 public function _set($propertyName, $value) {
119 $this->$propertyName = $value;
120 }
121 public function _setRef($propertyName, &$value) {
122 $this->$propertyName = $value;
123 }
124 public function _get($propertyName) {
125 return $this->$propertyName;
126 }
127 }
128 ');
129 return $accessibleClassName;
130 }
131
132 }
133 ?>