From 361c18469af5f6782e622420797c4acd5fa70b97 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Sat, 26 Mar 2011 15:09:22 +0100 Subject: add new feature: allow unit test mode enabling via HTTP GET parameter --- tests/TestBase.php | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/TestBase.php b/tests/TestBase.php index 3e2e213..b85c189 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -22,6 +22,18 @@ */ class TestBase extends PHPUnit_Framework_TestCase { + /** + * Clean up after test + */ + public function tearDown() + { + if (file_exists($GLOBALS['datadir'] . '/config.unittest.php')) { + unlink($GLOBALS['datadir'] . '/config.unittest.php'); + } + } + + + /** * Create a new bookmark. * @@ -80,8 +92,25 @@ class TestBase extends PHPUnit_Framework_TestCase * @param string $password Password * * @return integer ID of user + * + * @uses addUserData() */ protected function addUser($username = null, $password = null) + { + return reset($this->addUserData($username, $password)); + } + + + + /** + * Creates a new user in the database and returns id, username and password. + * + * @param string $username Username + * @param string $password Password + * + * @return array ID of user, Name of user, password of user + */ + protected function addUserData($username = null, $password = null) { $us = SemanticScuttle_Service_Factory::get('User'); $rand = rand(); @@ -98,7 +127,7 @@ class TestBase extends PHPUnit_Framework_TestCase $password, 'unittest-' . $rand . '@example.org' ); - return $uid; + return array($uid, $username, $password); } @@ -129,6 +158,38 @@ class TestBase extends PHPUnit_Framework_TestCase return $uid; } + + + /** + * Writes a special unittest configuration file. + * The unittest config file is read when a GET request with unittestMode=1 + * is sent, and the user allowed unittestmode in config.php. + * + * @param array $arConfig Array with config names as key and their value as + * value + * + * @return void + */ + protected function setUnittestConfig($arConfig) + { + $str = '<' . "?php\r\n"; + foreach ($arConfig as $name => $value) { + $str .= '$' . $name . ' = ' + . var_export($value, true) . ";\n"; + } + + if (!is_dir($GLOBALS['datadir'])) { + $this->fail( + 'datadir not set or not a directory: ' . $GLOBALS['datadir'] + ); + } + + $this->assertInternalType( + 'integer', + file_put_contents($GLOBALS['datadir'] . '/config.unittest.php', $str), + 'Writing config.unittest.php failed' + ); + } } ?> \ No newline at end of file -- cgit v1.2.3