[TASK] Add unit test for t3lib_div::callUserFunction
authorTolleiv Nietsch <info@tolleiv.de>
Sun, 20 May 2012 07:58:55 +0000 (09:58 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Sun, 20 May 2012 10:33:10 +0000 (12:33 +0200)
Releates to #36784 which would have brought some basic tests.
This patchset now has full coverage for the entire functionality
of callUserFunction and #36784 can be reduced to its actual purpose.

Change-Id: I02cfe3e963f90aa3c3986e57bdba4a08c6e4c0d5
Resolves: #37317
Releases: 6.0
Reviewed-on: http://review.typo3.org/11316
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
tests/Unit/t3lib/class.t3lib_divTest.php

index d15f54c..8d15fb4 100644 (file)
@@ -3788,6 +3788,109 @@ class t3lib_divTest extends tx_phpunit_testcase {
        }
 
        ///////////////////////////////////////////////////
+       // Tests concerning callUserFunction
+       ///////////////////////////////////////////////////
+
+       /**
+        * @test
+        * @dataProvider callUserFunctionInvalidParameterDataprovider
+        */
+       public function callUserFunctionWillReturnFalseForInvalidParameters($functionName) {
+               $inputData = array('foo' => 'bar');
+                       // omit the debug() output
+               ob_start();
+               $result = t3lib_div::callUserFunction($functionName, $inputData, $this, 'user_');
+               ob_end_clean();
+               $this->assertFalse($result);
+       }
+
+       /**
+        * @test
+        * @dataProvider callUserFunctionInvalidParameterDataprovider
+        * @expectedException InvalidArgumentException
+        */
+       public function callUserFunctionWillThrowExceptionForInvalidParameters($functionName) {
+               $inputData = array('foo' => 'bar');
+               t3lib_div::callUserFunction($functionName, $inputData, $this, 'user_', 2);
+       }
+       /**
+        * Data provider for callUserFunctionInvalidParameterDataprovider and
+        * callUserFunctionWillThrowExceptionForInvalidParameters.
+        *
+        * @return array
+        */
+       public function callUserFunctionInvalidParameterDataprovider() {
+               return array(
+                       'Function is not prefixed' => array('t3lib_divTest->calledUserFunction'),
+                       'Class doesn\'t exists' => array('t3lib_divTest21345->user_calledUserFunction'),
+                       'No method name' => array('t3lib_divTest'),
+                       'No class name' => array('->user_calledUserFunction'),
+                       'No function name' => array(''),
+               );
+       }
+
+       /**
+        * Above tests already showed that the prefix is checked properly,
+        * therefore this test skips the prefix and enables to inline the instantly
+        * created function (who's name doesn't have a prefix).
+        *
+        * @test
+        */
+       public function callUserFunctionCanCallFunction() {
+               $functionName = create_function('', 'return "Worked fine";');
+               $inputData = array('foo' => 'bar');
+               $result = t3lib_div::callUserFunction($functionName, $inputData, $this, '');
+               $this->assertEquals('Worked fine', $result);
+       }
+
+       /**
+        * @test
+        */
+       public function callUserFunctionCanCallMethod() {
+               $inputData = array('foo' => 'bar');
+               $result = t3lib_div::callUserFunction('t3lib_divTest->user_calledUserFunction', $inputData, $this);
+               $this->assertEquals('Worked fine', $result);
+       }
+
+       /**
+        * @return string
+        */
+       public function user_calledUserFunction() {
+               return "Worked fine";
+       }
+
+       /**
+        * @test
+        */
+       public function callUserFunctionCanPrefixFuncNameWithFilePath() {
+               $inputData = array('foo' => 'bar');
+               $result = t3lib_div::callUserFunction('t3lib/class.t3lib_div.php:t3lib_divTest->user_calledUserFunction', $inputData, $this);
+               $this->assertEquals('Worked fine', $result);
+       }
+
+       /**
+        * @test
+        */
+       public function callUserFunctionCanPersistObjectsBetweenCalls() {
+               $inputData = array('called' => array());
+               t3lib_div::callUserFunction('&t3lib_divTest->user_calledUserFunctionCountCallers', $inputData, $this);
+               t3lib_div::callUserFunction('&t3lib_divTest->user_calledUserFunctionCountCallers', $inputData, $this);
+               $this->assertEquals(1, sizeof($inputData['called']));
+       }
+
+       /**
+        * Takes the object hash and adds it to the passed array. In case
+        * persisting the objects would not work we'd see two different
+        * parent objects.
+        *
+        * @param $params
+        */
+       public function user_calledUserFunctionCountCallers(&$params) {
+               $params['called'][spl_object_hash($this)]++;
+       }
+
+
+       ///////////////////////////////////////////////////
        // Tests concerning hasValidClassPrefix
        ///////////////////////////////////////////////////
 
@@ -4047,4 +4150,4 @@ class t3lib_divTest extends tx_phpunit_testcase {
                $this->assertTrue(preg_match($expectedPreg, t3lib_div::substUrlsInPlainText($input, 1, 'http://example.com/index.php')) == 1);
        }
 }
-?>
\ No newline at end of file
+?>