From 40938505c7adedffc31ae2de455e7c812f926d9d Mon Sep 17 00:00:00 2001 From: rurban Date: Fri, 7 May 2004 20:47:35 +0000 Subject: [PATCH] PHPUnit tests by Dan Frankowski git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@3456 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- tests/unit/lib/InlineParserTest.php | 76 +++++++++++++++++++ tests/unit/readme.html | 66 +++++++++++++++++ tests/unit/test.php | 109 ++++++++++++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 tests/unit/lib/InlineParserTest.php create mode 100644 tests/unit/readme.html create mode 100644 tests/unit/test.php diff --git a/tests/unit/lib/InlineParserTest.php b/tests/unit/lib/InlineParserTest.php new file mode 100644 index 000000000..34d7da5da --- /dev/null +++ b/tests/unit/lib/InlineParserTest.php @@ -0,0 +1,76 @@ + + */ + +require_once 'lib/InlineParser.php'; +require_once 'PHPUnit.php'; + +class InlineParserTest extends PHPUnit_TestCase { + + // constructor of the test suite + function StringTest($name) { + $this->PHPUnit_TestCase($name); + } + + function testNoWikiWords() { + $str1 = 'This has no wiki words, and is all text.'; + $xmlc1 = TransformInline($str1); + $this->assertTrue(isa($xmlc1, 'XmlContent')); + $c1 = $xmlc1->getContent(); + $this->assertEquals(1, count($c1)); + $this->assertEquals($str1, $c1[0]); + } + + function testWikiWord() { + $ww = 'WikiWord'; + $str1 = "This has 1 $ww."; + $xml = TransformInline($str1); + $this->assertTrue(isa($xml, 'XmlContent')); + $c1 = $xml->getContent(); + $this->assertEquals(3, count($c1)); + $this->assertTrue(isa($c1[1], 'Cached_WikiLink')); + + $this->assertEquals('This has 1 ', $c1[0]); + $this->assertEquals($ww, $c1[1]->asString()); + $this->assertEquals('.', $c1[2]); + } + + // todo... + function testLinks() { + $tests = array("[label|link]", + "[ label | link.jpg ]", + "[ image.jpg | link ]", + "[ Upload:image.jpg | link ]", + "[ http://server/image.jpg | link ]", + "[ label | http://server/link ]", + "[ label | Upload:link ]", + "[ label | phpwiki:action=link ]", + "Upload:image.jpg", + "http://server/image.jpg", + "http://server/link", + "[http:/server/~name/]", + "http:/server/~name/" + ); + for ($i=0; $i < count($tests); $i++) { + $xml = TransformInline($tests[$i]); + $this->assertTrue(isa($xml, 'XmlContent')); + $cl = $xml->getContent(); + $this->assertEquals(1, count($c1)); + $this->assertTrue(isa($c1[0], 'Cached_WikiLink')); + } + } + +} + + +// (c-file-style: "gnu") +// Local Variables: +// mode: php +// tab-width: 8 +// c-basic-offset: 4 +// c-hanging-comment-ender-p: nil +// indent-tabs-mode: nil +// End: +?> diff --git a/tests/unit/readme.html b/tests/unit/readme.html new file mode 100644 index 000000000..52169125c --- /dev/null +++ b/tests/unit/readme.html @@ -0,0 +1,66 @@ + + +PhpWiki unit tests + + + +

Introduction

+ +This directory contains unit tests for PhpWiki. You must have PEAR's PHPUnit package. +These tests are unrelated to maketest.pl in the directory above this +one, which do not use PHPUnit. + +

Prerequisites

+

PHP

+ +You need the php command-line +interface. It was experimental as of PHP 4.2.0, default as of PHP +4.3.0. + +You also need the 'pear' executable. + +

PHPUnit

+ +You can use pear to see if PHPUnit is installed: + +
+/export/scratch/apache/2.0.46/php/bin) % ./pear list
+Installed packages:
+===================
+Package        Version State
+Archive_Tar    0.9     stable
+Console_Getopt 1.0     stable
+DB             1.3     stable
+HTTP           1.2     stable
+HTTP_Upload    0.8.1   stable
+Mail           1.0.1   stable
+Net_SMTP       1.0     stable
+Net_Socket     1.0.1   stable
+PEAR           1.1     stable
+PHPUnit        1.0.0   stable
+XML_Parser     1.0.1   stable
+XML_RPC        1.0.4   stable
+
+ +If it is not installed, you can use 'pear' to install it: + +
+/export/scratch/apache/2.0.46/php/bin) % ./pear install PHPUnit
+
+ +

Running these unit tests

+ +You must be in the phpwiki/tests/unit subdirectory. Then use the php +command-line executable to run the tests. For example: + +
+phpwiki/tests/unit % php test.php
+Run tests ..
+TestCase inlineparsertest->testnowikiwords() passed
+TestCase inlineparsertest->testwikiword() passed
+
+ + + diff --git a/tests/unit/test.php b/tests/unit/test.php new file mode 100644 index 000000000..7a0eb2fc1 --- /dev/null +++ b/tests/unit/test.php @@ -0,0 +1,109 @@ +#! /usr/local/bin/php -Cq + + * + * This file is part of PhpWiki. + * + * PhpWiki is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * PhpWiki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with PhpWiki; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * Unit tests for PhpWiki. + * + * You must have PEAR's PHPUnit package . + * These tests are unrelated to test/maketest.pl, which do not use PHPUnit. + */ + +#################################################################### +# +# Preamble needed to get the tests to run. +# +#################################################################### + + +# Add root dir to the path +$rootdir = getcwd() . '/../../'; +ini_set('include_path', ini_get('include_path') . (substr(PHP_OS,0,3) == 'WIN' ? ';' : ':') . $rootdir); + +# This quiets a warning in config.php +$HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1'; + +# Other needed files +require_once 'index.php'; +require_once 'lib/stdlib.php'; +require_once 'lib/config.php'; // Needed for $WikiNameRegExp + +# Show lots of detail when an assert() in the code fails +function assert_callback( $script, $line, $message ) { + echo "assert failed: script ", $script," line ", $line," :"; + echo "$message"; + echo "Traceback:"; + print_r(debug_backtrace()); + exit; +} +$foo = assert_options( ASSERT_CALLBACK, 'assert_callback'); + +# This is the test DB backend +#require_once( 'lib/WikiDB/backend/cvs.php' ); +$db_params = array(); +$db_params['directory'] = getcwd() . '/testbox'; +$db_params['dbtype'] = 'file'; + +# Mock objects to allow tests to run +require_once( 'lib/WikiDB.php' ); +class MockRequest { + function MockRequest(&$dbparams) { + $this->_dbi = WikiDB::open(&$dbparams); + } + function addArg($arg, $value) { + $this->args[$arg] = $value; + } + function getArg($arg) { + return $this->args[$arg]; + } + + function getDbh() { + return $this->_dbi; + } +} + +$request = new MockRequest($db_params); + +#################################################################### +# +# End of preamble, run the test suite .. +# +#################################################################### + +# Test files +require_once ('PHPUnit.php'); +# lib/config.php might do a cwd() +require_once (dirname(__FILE__).'/lib/InlineParserTest.php'); + +print "Run tests ..\n"; +$suite = new PHPUnit_TestSuite("InlineParserTest"); +$result = PHPUnit::run($suite); + +echo $result -> toString(); + +// (c-file-style: "gnu") +// Local Variables: +// mode: php +// tab-width: 8 +// c-basic-offset: 4 +// c-hanging-comment-ender-p: nil +// indent-tabs-mode: nil +// End: +?> -- 2.45.0