Skip to content

Commit

Permalink
provide some fake $_SERVER entries
Browse files Browse the repository at this point in the history
  • Loading branch information
sarnowski committed Apr 16, 2012
1 parent 6b6e498 commit c5dcf21
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 72 deletions.
61 changes: 48 additions & 13 deletions _testing/README
@@ -1,23 +1,52 @@
====== DokuWiki test suite ======
DokuWiki Test Suite
=============================================================================

Content of this document:

===== Unit Tests =====
* Requirements
* Installation of PHPUnit via Pear
* Running all Tests
* Create new Tests
* Known Bad Tests
* TODO for test framework

==== Requirements ====
* PHP Unit 3.7

Installation:
The easiest way to install phpunit is via pear:
pear config-set auto_discover 1
pear upgrade
pear install pear.phpunit.de/PHPUnit

==== Run unit tests ====
Requirements
-----------------------------------------------------------------------------

* PHP Unit 3.7


Installation of PHPUnit via Pear
-----------------------------------------------------------------------------

> pear config-set auto_discover 1
> pear upgrade
> pear install pear.phpunit.de/PHPUnit


Running all Tests
-----------------------------------------------------------------------------

> cd _testing/
> phpunit

==== Bad tests ====
Bad tests are tests that do not run out of the box.

Create new Tests
-----------------------------------------------------------------------------

To create a test for DokuWiki, create a *.test.php file within the tests/
folder. Please respect the folder structure and naming convention. Inside the
file, implement a class, extending 'DokuWikiTest'. Every method, starting
with 'test' will be called as a test (e.g. 'testIfThisIsValid');


Known Bad Tests
-----------------------------------------------------------------------------

Every test should be green on every run but the following were broken after
the PHPUnit migration and are disabled by default:

* inc/DifferenceEngine (removed FS#2161, FS#2223)
* inc/html_hilight (runkit)
Expand All @@ -28,7 +57,13 @@ Bad tests are tests that do not run out of the box.
* inc/parser/xhtml_links


==== TODO ====
TODO for the test framework
-----------------------------------------------------------------------------

* plugin tests
* cross platform compatible: especially test windows (hint: tmp dir location)
* optional: add helper methods to TestRequest for easy form submission
* createForm(), ...
* optional: add helper methods to TestReponse for easy response parsing
* findElementById, findElementByXPath, ...

103 changes: 47 additions & 56 deletions _testing/bootstrap.php
Expand Up @@ -58,6 +58,12 @@ function fappend($file, $text) {
set_time_limit(0);
ini_set('memory_limit','2048M');

// prepare temporary directories
define('DOKU_INC', dirname(dirname(__FILE__)).'/');
define('TMP_DIR', '/tmp/dwtests-'.microtime(true));
define('DOKU_CONF', TMP_DIR.'/conf/');
define('DOKU_TMP_DATA', TMP_DIR.'/data/');

// default plugins
$default_plugins = array(
'acl',
Expand All @@ -74,11 +80,35 @@ function fappend($file, $text) {
'usermanager'
);

// prepare temporary directories
define('DOKU_INC', dirname(dirname(__FILE__)).'/');
define('TMP_DIR', '/tmp/dwtests-'.microtime(true));
define('DOKU_CONF', TMP_DIR.'/conf/');
define('DOKU_TMP_DATA', TMP_DIR.'/data/');
// default server variables
$default_server_vars = array(
'QUERY_STRING' => '?id=',
'REQUEST_METHOD' => 'GET',
'CONTENT_TYPE' => '',
'CONTENT_LENGTH' => '',
'SCRIPT_NAME' => '/doku.php',
'REQUEST_URI' => '/doku.php?id=',
'DOCUMENT_URI' => '/doku.php',
'DOCUMENT_ROOT' => DOKU_INC,
'SERVER_PROTOCOL' => 'HTTP/1.1',
'SERVER_SOFTWARE' => 'nginx/0.7.67',
'REMOTE_ADDR' => '87.142.120.6',
'REMOTE_PORT' => '21418',
'SERVER_ADDR' => '46.38.241.24',
'SERVER_PORT' => '443',
'SERVER_NAME' => 'wiki.example.com',
'REDIRECT_STATUS' => '200',
'SCRIPT_FILENAME' => DOKU_INC.'doku.php',
'HTTP_HOST' => 'wiki.example.com',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; OpenBSD amd64; rv:11.0) Gecko/20100101 Firefox/11.0',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_CACHE_CONTROL' => 'max-age=0',
'PHP_SELF' => '/doku.php',
'REQUEST_TIME' => time(),
);

// create temp directories
mkdir(TMP_DIR);
Expand Down Expand Up @@ -112,8 +142,13 @@ function fappend($file, $text) {
}
$dh->close();

// TODO setup default global variables
$_SERVER['REMOTE_ADDR'] = '173.194.69.138';
// setup default global variables
$_GET = array('id' => '');
$_POST = array();
$_REQUEST = array('id' => '');
foreach ($default_server_vars as $key => $value) {
$_SERVER[$key] = $value;
}

// load dw
require_once(DOKU_INC.'inc/init.php');
Expand All @@ -126,39 +161,13 @@ function ob_start_callback($buffer) {
$output_buffer .= $buffer;
}

// Helper class to provide basic functionality for tests
abstract class DokuWikiTest extends PHPUnit_Framework_TestCase {
// nothing for now, makes migration easy
}

// Helper class to execute a fake request
class TestRequest {
var $server_vars = array(
'REMOTE_ADDR' => '127.0.0.1',
);

var $get_vars = array();
var $post_vars = array();

function __construct($page = '') {
$this->setPage($page);
}

function setServerVar($varName, $varValue) {
$this->sevrer_vars[$varName] = $varValue;
}

function setGetVar($varName, $varValue) {
$this->get_vars[$varName] = $varValue;
}

function setPostVar($varName, $varValue) {
$this->post_vars[$varName] = $varValue;
}

function setPage($pageName) {
$this->setGetVar('id', $pageName);
}

function addLocalConf($text) {
$this->conf_local[] = $text;
}

function hook($hook, $step, $function) {
global $EVENT_HANDLER;
$null = null;
Expand All @@ -169,22 +178,6 @@ function execute() {
global $output_buffer;
$output_buffer = '';

// fake php environment
foreach ($this->server_vars as $key => $value) {
$_SERVER[$key] = $value;
}
$_REQUEST = array();
$_GET = array();
foreach ($this->get_vars as $key => $value) {
$_GET[$key] = $value;
$_REQUEST[$key] = $value;
}
$_POST = array();
foreach ($this->post_vars as $key => $value) {
$_POST[$key] = $value;
$_REQUEST[$key] = $value;
}

// now execute dokuwiki and grep the output
header_remove();
ob_start('ob_start_callback');
Expand Down Expand Up @@ -216,6 +209,4 @@ function getContent() {
function getHeaders() {
return $this->headers;
}

// TODO provide findById, findBy... (https://github.com/cosmocode/dokuwiki-plugin-scrape/blob/master/phpQuery-onefile.php)
}
2 changes: 1 addition & 1 deletion _testing/tests/testing/inttests_basic.test.php
Expand Up @@ -3,7 +3,7 @@
/**
* @group integration
*/
class InttestsBasicTest extends PHPUnit_Framework_TestCase {
class InttestsBasicTest extends DokuWikiTest {
/**
* Execute the simplest possible request and expect
* a dokuwiki page which obviously has the word "DokuWiki"
Expand Down
4 changes: 2 additions & 2 deletions _testing/tests/testing/inttests_globals.test.php
Expand Up @@ -8,7 +8,7 @@ class InttestsGlobalsTest extends PHPUnit_Framework_TestCase {
* Global variables should be restored for every test case.
*/
function testFirstRun() {
$this->assertEquals('173.194.69.138', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected');
$this->assertEquals('87.142.120.6', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected');

$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
}
Expand All @@ -17,6 +17,6 @@ function testFirstRun() {
* @depends testFirstRun
*/
function testSecondRun() {
$this->assertEquals('173.194.69.138', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected');
$this->assertEquals('87.142.120.6', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected');
}
}

0 comments on commit c5dcf21

Please sign in to comment.