Legoktm has uploaded a new change for review.
https://gerrit.wikimedia.org/r/227662
Change subject: Add basic unit tests for utils
......................................................................
Add basic unit tests for utils
Change-Id: Ibf8761083270accb0a73d1dd31bebb857bf69dfb
---
M UrlShortener.hooks.php
M UrlShortener.php
A tests/phpunit/UrlShortenerUtilsTest.php
3 files changed, 66 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UrlShortener
refs/changes/62/227662/1
diff --git a/UrlShortener.hooks.php b/UrlShortener.hooks.php
index 6e881e1..7e5e9ce 100644
--- a/UrlShortener.hooks.php
+++ b/UrlShortener.hooks.php
@@ -26,6 +26,28 @@
}
/**
+ * Load our unit tests
+ */
+ public static function onUnitTestsList( &$files ) {
+ // @codeCoverageIgnoreStart
+ $directoryIterator = new RecursiveDirectoryIterator( __DIR__ .
'/tests/phpunit/' );
+
+ /**
+ * @var SplFileInfo $fileInfo
+ */
+ $ourFiles = array();
+ foreach ( new RecursiveIteratorIterator( $directoryIterator )
as $fileInfo ) {
+ if ( substr( $fileInfo->getFilename(), -8 ) ===
'Test.php' ) {
+ $ourFiles[] = $fileInfo->getPathname();
+ }
+ }
+
+ $files = array_merge( $files, $ourFiles );
+ return true;
+ // @codeCoverageIgnoreEnd
+ }
+
+ /**
* @param $du DatabaseUpdater
* @return bool
*/
diff --git a/UrlShortener.php b/UrlShortener.php
index 285f931..d23a19e 100644
--- a/UrlShortener.php
+++ b/UrlShortener.php
@@ -113,6 +113,7 @@
$wgSpecialPages['UrlShortener'] = 'SpecialUrlShortener';
$wgSpecialPages['UrlRedirector'] = 'SpecialUrlRedirector';
+$wgHooks['UnitTestsList'][] = 'UrlShortenerHooks::onUnitTestsList';
$wgHooks['LoadExtensionSchemaUpdates'][] =
'UrlShortenerHooks::onLoadExtensionSchemaUpdates';
$wgHooks['WebRequestPathInfoRouter'][] =
'UrlShortenerHooks::onWebRequestPathInfoRouter';
diff --git a/tests/phpunit/UrlShortenerUtilsTest.php
b/tests/phpunit/UrlShortenerUtilsTest.php
new file mode 100644
index 0000000..e8db7ad
--- /dev/null
+++ b/tests/phpunit/UrlShortenerUtilsTest.php
@@ -0,0 +1,43 @@
+<?php
+
+class UrlShortenerUtilsTest extends MediaWikiTestCase {
+
+ /**
+ * @dataProvider provideConvertToProtocol
+ * @covers UrlShortenerUtils::convertToProtocol
+ */
+ public function testConvertToProtocol( $input, $proto, $expected ) {
+ $this->assertEquals( $expected,
UrlShortenerUtils::convertToProtocol( $input, $proto ) );
+ }
+
+ public static function provideConvertToProtocol() {
+ return array(
+ array( 'https://example.org/foo?query=bar', PROTO_HTTP,
'http://example.org/foo?query=bar' ),
+ array( 'http://example.org/foo?query=bar', PROTO_HTTP,
'http://example.org/foo?query=bar' ),
+ array( '//example.org/foo?query=bar', PROTO_HTTP,
'http://example.org/foo?query=bar' ),
+ array( 'http://example.org/foo?query=bar', PROTO_HTTPS,
'https://example.org/foo?query=bar' ),
+ array( 'http://example.org/foo?query=bar',
PROTO_RELATIVE, '//example.org/foo?query=bar' ),
+ array( 'https://example.org/foo?query=bar',
PROTO_RELATIVE, '//example.org/foo?query=bar' ),
+ );
+ }
+
+ /**
+ * Test that ids round-trip through encode/decode properly
+ *
+ * @covers UrlShortenerUtils::encodeId
+ * @covers UrlShortenerUtils::decodeId
+ */
+ public function testEncodeAndDecodeIds() {
+ // Set default
+ $this->setMwGlobals(
+ 'wgUrlShortenerIdSet',
+
'023456789ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz$-_.!'
+ );
+ for ( $i = 0; $i < 1000; $i++ ) {
+ $int = rand();
+ $encoded = UrlShortenerUtils::encodeId( $int );
+ $decoded = UrlShortenerUtils::decodeId( $encoded );
+ $this->assertEquals( $int, $decoded );
+ }
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/227662
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibf8761083270accb0a73d1dd31bebb857bf69dfb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UrlShortener
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits