From: Kunal Mehta Date: Sun, 11 Jan 2015 01:52:37 +0000 (-0800) Subject: Move DeferredStringifier into libs, add tests X-Git-Tag: 1.31.0-rc.0~12698^2 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=6a55db1a97180fdc6a75b66e94bfe6fd2e7fa180;p=lhc%2Fweb%2Fwiklou.git Move DeferredStringifier into libs, add tests Change-Id: I384d1a3854e957315584d30ec58c48c02fee6a2c --- diff --git a/autoload.php b/autoload.php index 674d4b0f43..72345feaba 100644 --- a/autoload.php +++ b/autoload.php @@ -292,7 +292,7 @@ $wgAutoloadLocalClasses = array( 'DateFormatter' => __DIR__ . '/includes/parser/DateFormatter.php', 'DeadendPagesPage' => __DIR__ . '/includes/specials/SpecialDeadendpages.php', 'DeferrableUpdate' => __DIR__ . '/includes/deferred/DeferredUpdates.php', - 'DeferredStringifier' => __DIR__ . '/includes/DeferredStringifier.php', + 'DeferredStringifier' => __DIR__ . '/includes/libs/DeferredStringifier.php', 'DeferredUpdates' => __DIR__ . '/includes/deferred/DeferredUpdates.php', 'DeleteAction' => __DIR__ . '/includes/actions/DeleteAction.php', 'DeleteArchivedFiles' => __DIR__ . '/maintenance/deleteArchivedFiles.php', diff --git a/includes/DeferredStringifier.php b/includes/DeferredStringifier.php deleted file mode 100644 index bd32949833..0000000000 --- a/includes/DeferredStringifier.php +++ /dev/null @@ -1,55 +0,0 @@ -params = func_get_args(); - array_shift( $this->params ); - $this->callback = $callback; - } - - /** - * Returns a string generated by callback - * - * @return string - */ - public function __toString() { - if ( $this->result === null ) { - $this->result = call_user_func_array( $this->callback, $this->params ); - } - return $this->result; - } -} diff --git a/includes/libs/DeferredStringifier.php b/includes/libs/DeferredStringifier.php new file mode 100644 index 0000000000..bd32949833 --- /dev/null +++ b/includes/libs/DeferredStringifier.php @@ -0,0 +1,55 @@ +params = func_get_args(); + array_shift( $this->params ); + $this->callback = $callback; + } + + /** + * Returns a string generated by callback + * + * @return string + */ + public function __toString() { + if ( $this->result === null ) { + $this->result = call_user_func_array( $this->callback, $this->params ); + } + return $this->result; + } +} diff --git a/tests/phpunit/includes/libs/DeferredStringifierTest.php b/tests/phpunit/includes/libs/DeferredStringifierTest.php new file mode 100644 index 0000000000..9aaf113456 --- /dev/null +++ b/tests/phpunit/includes/libs/DeferredStringifierTest.php @@ -0,0 +1,39 @@ +newInstanceArgs( $params ); + $this->assertEquals( $expected, (string)$ds ); + } + + public static function provideToString() { + return array( + // No args + array( array( function() { + return 'foo'; + } ), 'foo' ), + // Has args + array( array( function( $i ) { + return $i; + }, 'bar' ), 'bar' ), + ); + } + + /** + * Verify that the callback is not called if + * it is never converted to a string + */ + public function testCallbackNotCalled() { + $ds = new DeferredStringifier( function() { + throw new Exception( 'This should not be reached!' ); + } ); + // No exception was thrown + $this->assertTrue( true ); + } +}