[TASK] Integrate possibility to call inaccessible methods in BaseTestCase
authorOliver Hader <oliver@typo3.org>
Tue, 20 Nov 2012 11:17:24 +0000 (12:17 +0100)
committerOliver Hader <oliver.hader@typo3.org>
Tue, 20 Nov 2012 13:18:48 +0000 (14:18 +0100)
The BaseTestCase already has a possibility to create full blown
accessible object proxies. However if one does not want to have
the class stack cluttered with temporary existing classes, there
shall be a possibility to directly invoke protected/private
methods of a fixture object.

The new method callInaccessibleMethod method is taken and
rewritten from ModuleControllerTest.

Example to use it in unit tests:
$this->fixture = new \TYPO3\CMS\Core\SomeClassName();
$result = $this->callInaccessibleMethod(
  $this->fixture,
  'privateMethod',
  $first, $second, $third, ...
);

Change-Id: I2c90c136c350129b28809d8b8b88286149894a10
Resolves: #43145
Releases: 6.0
Reviewed-on: http://review.typo3.org/16598
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Oliver Hader
Tested-by: Oliver Hader
typo3/sysext/backend/Tests/Unit/Module/ModuleControllerTest.php
typo3/sysext/core/Tests/BaseTestCase.php

index 2f64aef..5474d28 100644 (file)
@@ -34,31 +34,23 @@ namespace TYPO3\CMS\Backend\Tests\Unit\Module;
 class ModuleControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
-        * @var TYPO3\CMS\Backend\Module\ModuleController
+        * @var \TYPO3\CMS\Backend\Module\ModuleController
         */
        protected $moduleController;
 
-       /**
-        * Helper function to call protected or private methods
-        *
-        * @param string $className The className
-        * @param string $name the name of the method to call
-        * @param mixed $argument The argument for the method call (only one in this test class needed)
-        * @return mixed
-        */
-       public function callInaccessibleMethod($className, $name, $argument) {
-               $class = new \ReflectionClass($className);
-               $object = new $className();
-               $method = $class->getMethod($name);
-               $method->setAccessible(TRUE);
-               return $method->invoke($object, $argument);
+       protected function setUp() {
+               $this->moduleController = new \TYPO3\CMS\Backend\Module\ModuleController();
+       }
+
+       protected function tearDown() {
+               unset($this->moduleController);
        }
 
        /**
         * @test
         */
        public function createEntryFromRawDataGeneratesMenuEntry() {
-               $entry = $this->callInaccessibleMethod('TYPO3\\CMS\\Backend\\Module\\ModuleController', 'createEntryFromRawData', array());
+               $entry = $this->callInaccessibleMethod($this->moduleController, 'createEntryFromRawData', array());
                $this->assertInstanceOf('TYPO3\\CMS\\Backend\\Domain\\Model\\Module\\BackendModule', $entry);
        }
 
@@ -77,7 +69,8 @@ class ModuleControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        'description' => 'descriptionTest',
                        'navigationComponentId' => 'navigationComponentIdTest'
                );
-               $entry = $this->callInaccessibleMethod('TYPO3\\CMS\\Backend\\Module\\ModuleController', 'createEntryFromRawData', $rawModule);
+               /** @var $entry \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule */
+               $entry = $this->callInaccessibleMethod($this->moduleController, 'createEntryFromRawData', $rawModule);
                $this->assertEquals('nameTest', $entry->getName());
                $this->assertEquals('titleTest', $entry->getTitle());
                $this->assertEquals('linkTest', $entry->getLink());
@@ -94,7 +87,8 @@ class ModuleControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $rawModule = array(
                        'path' => 'pathTest'
                );
-               $entry = $this->callInaccessibleMethod('TYPO3\\CMS\\Backend\\Module\\ModuleController', 'createEntryFromRawData', $rawModule);
+               /** @var $entry \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule */
+               $entry = $this->callInaccessibleMethod($this->moduleController, 'createEntryFromRawData', $rawModule);
                $this->assertEquals('pathTest', $entry->getLink());
        }
 
index 9d9d568..af2a359 100644 (file)
@@ -199,5 +199,23 @@ abstract class BaseTestCase extends \PHPUnit_Framework_TestCase {
 
                return $accessibleClassName;
        }
+
+       /**
+        * Helper function to call protected or private methods
+        *
+        * @param object $object The object to be invoked
+        * @param string $name the name of the method to call
+        * @return mixed
+        */
+       protected function callInaccessibleMethod($object, $name) {
+               // Remove first two arguments ($object and $name)
+               $arguments = func_get_args();
+               array_splice($arguments, 0, 2);
+
+               $reflectionObject = new \ReflectionObject($object);
+               $reflectionMethod = $reflectionObject->getMethod($name);
+               $reflectionMethod->setAccessible(TRUE);
+               return $reflectionMethod->invokeArgs($object, $arguments);
+       }
 }
-?>
+?>
\ No newline at end of file