TYPO3 API  SVNRelease
BaseTestCase.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * Base testcase for the Extbase extension.
00030  */
00031 abstract class Tx_Extbase_Tests_Unit_BaseTestCase extends tx_phpunit_testcase {
00032 
00033     /**
00034      * @var Tx_Extbase_Object_ObjectManagerInterface The object manager
00035      */
00036     protected $objectManager;
00037 
00038     /**
00039      * Constructs a test case with the given name.
00040      *
00041      * @param  string $name
00042      * @param  array  $data
00043      * @param  string $dataName
00044      */
00045     public function __construct($name = NULL, array $data = array(), $dataName = '') {
00046         parent::__construct($name, $data, $dataName);
00047         if (!class_exists('Tx_Extbase_Utility_ClassLoader')) {
00048             require(t3lib_extmgm::extPath('extbase') . 'Classes/Utility/ClassLoader.php');
00049         }
00050         spl_autoload_register(array('Tx_Extbase_Utility_ClassLoader', 'loadClass'));
00051     }
00052 
00053     /**
00054      * Injects an untainted clone of the object manager and all its referencing
00055      * objects for every test.
00056      *
00057      * @return void
00058      */
00059     public function runBare() {
00060         $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
00061         $this->objectManager =  clone $objectManager;
00062         parent::runBare();
00063     }
00064 
00065     /**
00066      * Returns a mock object which allows for calling protected methods and access
00067      * of protected properties.
00068      *
00069      * @param string $className Full qualified name of the original class
00070      * @param array $methods
00071      * @param array $arguments
00072      * @param string $mockClassName
00073      * @param boolean $callOriginalConstructor
00074      * @param boolean $callOriginalClone
00075      * @param boolean $callAutoload
00076      * @return object
00077      * @author Robert Lemke <robert@typo3.org>
00078      * @api
00079      */
00080     protected function getAccessibleMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE) {
00081         return $this->getMock($this->buildAccessibleProxy($originalClassName), $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload);
00082     }
00083 
00084 
00085     /**
00086      * Creates a proxy class of the specified class which allows
00087      * for calling even protected methods and access of protected properties.
00088      *
00089      * @param protected $className Full qualified name of the original class
00090      * @return string Full qualified name of the built class
00091      */
00092     protected function buildAccessibleProxy($className) {
00093         $accessibleClassName = uniqid('AccessibleTestProxy');
00094         $class = new ReflectionClass($className);
00095         $abstractModifier = $class->isAbstract() ? 'abstract ' : '';
00096         eval('
00097             ' . $abstractModifier . 'class ' . $accessibleClassName . ' extends ' . $className . ' {
00098                 public function _call($methodName) {
00099                     $args = func_get_args();
00100                     return call_user_func_array(array($this, $methodName), array_slice($args, 1));
00101                 }
00102                 public function _callRef($methodName, &$arg1 = NULL, &$arg2 = NULL, &$arg3 = NULL, &$arg4 = NULL, &$arg5= NULL, &$arg6 = NULL, &$arg7 = NULL, &$arg8 = NULL, &$arg9 = NULL) {
00103                     switch (func_num_args()) {
00104                         case 0 : return $this->$methodName();
00105                         case 1 : return $this->$methodName($arg1);
00106                         case 2 : return $this->$methodName($arg1, $arg2);
00107                         case 3 : return $this->$methodName($arg1, $arg2, $arg3);
00108                         case 4 : return $this->$methodName($arg1, $arg2, $arg3, $arg4);
00109                         case 5 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5);
00110                         case 6 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
00111                         case 7 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7);
00112                         case 8 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7, $arg8);
00113                         case 9 : return $this->$methodName($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7, $arg8, $arg9);
00114                     }
00115                 }
00116                 public function _set($propertyName, $value) {
00117                     $this->$propertyName = $value;
00118                 }
00119                 public function _setRef($propertyName, &$value) {
00120                     $this->$propertyName = $value;
00121                 }
00122                 public function _get($propertyName) {
00123                     return $this->$propertyName;
00124                 }
00125             }
00126         ');
00127         return $accessibleClassName;
00128     }
00129 
00130 }
00131 ?>