* Start on some unit tests for GlobalFunctions
authorBrion Vibber <brion@users.mediawiki.org>
Mon, 18 Oct 2004 09:53:47 +0000 (09:53 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Mon, 18 Oct 2004 09:53:47 +0000 (09:53 +0000)
* Add makefile & readme for the lazy

tests/DatabaseTest.php
tests/GlobalTest.php [new file with mode: 0644]
tests/Makefile [new file with mode: 0644]
tests/README [new file with mode: 0644]
tests/RunTests.php

index e62194f..8a16cb2 100644 (file)
@@ -20,6 +20,39 @@ class DatabaseTest extends PHPUnit_TestCase {
                unset( $this->db );
        }
        
+       function testAddQuotesNull() {
+               $this->assertEquals(
+                       'NULL',
+                       $this->db->addQuotes( NULL ) );
+       }
+       
+       function testAddQuotesInt() {
+               # returning just "1234" should be ok too, though...
+               # maybe
+               $this->assertEquals(
+                       "'1234'",
+                       $this->db->addQuotes( 1234 ) );
+       }
+       
+       function testAddQuotesFloat() {
+               # returning just "1234.5678" would be ok too, though
+               $this->assertEquals(
+                       "'1234.5678'",
+                       $this->db->addQuotes( 1234.5678 ) );
+       }
+
+       function testAddQuotesString() {
+               $this->assertEquals(
+                       "'string'",
+                       $this->db->addQuotes( 'string' ) );
+       }
+       
+       function testAddQuotesStringQuote() {
+               $this->assertEquals(
+                       "'string\'s cause trouble'",
+                       $this->db->addQuotes( "string's cause trouble" ) );
+       }
+
        function testFillPreparedEmpty() {
                $sql = $this->db->fillPrepared(
                        'SELECT * FROM interwiki', array() );
diff --git a/tests/GlobalTest.php b/tests/GlobalTest.php
new file mode 100644 (file)
index 0000000..65f84e8
--- /dev/null
@@ -0,0 +1,127 @@
+<?php
+
+require_once( 'PHPUnit.php' );
+require_once( '../includes/Defines.php' );
+require_once( '../includes/GlobalFunctions.php' );
+
+class GlobalTest extends PHPUnit_TestCase {
+       function GlobalTest( $name ) {
+               $this->PHPUnit_TestCase( $name );
+       }
+       
+       function setUp() {
+               $this->save = array();
+               $saveVars = array( 'wgReadOnlyFile' );
+               foreach( $saveVars as $var ) {
+                       if( isset( $GLOBALS[$var] ) ) {
+                               $this->save[$var] = $GLOBALS[$var];
+                       }
+               }
+               $GLOBALS['wgReadOnlyFile'] = '/tmp/testReadOnly-' . mt_rand();
+       }
+       
+       function tearDown() {
+               foreach( $this->save as $var => $data ) {
+                       $GLOBALS[$var] = $data;
+               }
+       }
+       
+       function testDecodeLatin() {
+               $this->assertEquals(
+                       "\xe9cole",
+                       do_html_entity_decode( '&eacute;cole', ENT_COMPAT, 'iso-8859-1' ) );
+       }
+
+       function testDecodeUnicode() {
+               $this->assertEquals(
+                       "\xc3\xa9cole",
+                       do_html_entity_decode( '&eacute;cole', ENT_COMPAT, 'utf-8' ) );
+       }
+
+       function testRandom() {
+               # This could hypothetically fail, but it shouldn't ;)
+               $this->assertFalse(
+                       wfRandom() == wfRandom() );
+       }
+       
+       function testUrlencode() {
+               $this->assertEquals(
+                       "%E7%89%B9%E5%88%A5:Contributions/Foobar",
+                       wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
+       }
+       
+       function testUtf8Sequence1() {
+               $this->assertEquals(
+                       'A',
+                       wfUtf8Sequence( 65 ) );
+       }
+       
+       function testUtf8Sequence2() {
+               $this->assertEquals(
+                       "\xc4\x88",
+                       wfUtf8Sequence( 0x108 ) );
+       }
+
+       function testUtf8Sequence3() {
+               $this->assertEquals(
+                       "\xe3\x81\x8b",
+                       wfUtf8Sequence( 0x304b ) );
+       }
+
+       function testUtf8Sequence4() {
+               $this->assertEquals(
+                       "\xf0\x90\x91\x90",
+                       wfUtf8Sequence( 0x10450 ) );
+       }
+       
+       function testMungeToUtf8() {
+               $this->assertEquals(
+                       "\xc4\x88io bonas dans l'\xc3\xa9cole!",
+                       wfMungeToUtf8( "&#x108;io bonas dans l'&#233;cole!" ) );
+       }
+       
+       function testUtf8ToHTML() {
+               $this->assertEquals(
+                       "&#264;io bonas dans l'&#233;cole!",
+                       wfUtf8ToHTML( "\xc4\x88io bonas dans l'\xc3\xa9cole!" ) );
+       }
+       
+       function testReadOnlyEmpty() {
+               $this->assertFalse( wfReadOnly() );
+       }
+       
+       function testReadOnlySet() {
+               $f = fopen( $GLOBALS['wgReadOnlyFile'], "wt" );
+               fwrite( $f, 'Message' );
+               fclose( $f );
+               $this->assertTrue( wfReadOnly() );
+               
+               unlink( $GLOBALS['wgReadOnlyFile'] );
+               $this->assertFalse( wfReadOnly() );
+       }
+       
+       function testQuotedPrintable() {
+               $this->assertEquals(
+                       "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
+                       wfQuotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
+       }
+       
+       function testTime() {
+               $start = wfTime();
+               $this->assertType( 'double', $start );
+               $end = wfTime();
+               $this->assertTrue( $end > $start, "Time is running backwards!" );
+       }
+       
+       function testArrayToCGI() {
+               $this->assertEquals(
+                       "baz=AT%26T&foo=bar",
+                       wfArrayToCGI(
+                               array( 'baz' => 'AT&T', 'ignore' => '' ),
+                               array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
+       }
+       
+       /* TODO: many more! */
+}
+
+?>
\ No newline at end of file
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644 (file)
index 0000000..d980c9c
--- /dev/null
@@ -0,0 +1,6 @@
+.PHONY: help test
+help:
+       # Run 'make test' to run the unit tests.
+
+test:
+       php RunTests.php
diff --git a/tests/README b/tests/README
new file mode 100644 (file)
index 0000000..de15318
--- /dev/null
@@ -0,0 +1,8 @@
+Some quickie unit tests done with the PHPUnit testing framework. To run the
+test suite, run 'make test' in this dir or 'php RunTests.php'
+
+You can install PHPUnit via pear like this:
+# pear install PHPUnit
+
+Or fetch and install it manually:
+http://pear.php.net/package/PHPUnit
index d88dc80..fcc8481 100644 (file)
@@ -3,10 +3,16 @@ error_reporting( E_ALL );
 define( "MEDIAWIKI", true );
 
 require_once( 'PHPUnit.php' );
-require_once( 'DatabaseTest.php' );
 
-$suite = new PHPUnit_TestSuite( "DatabaseTest" );
-$result = PHPUnit::run( $suite );
-echo $result->toString();
+$tests = array(
+       'GlobalTest',
+       'DatabaseTest',
+       );
+foreach( $tests as $test ) {
+       require_once( $test . '.php' );
+       $suite = new PHPUnit_TestSuite( $test );
+       $result = PHPUnit::run( $suite );
+       echo $result->toString();
+}
 
 ?>
\ No newline at end of file