<?php
/**
 *
 * This file is part of the PhpMyObject project,
 * an Object-Relational Mapping (ORM) system.
 * 
 * For questions, help, comments, discussion, etc., please join our
 * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770} 
 * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
 *
 * PhpMyProject 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see {@link http://www.gnu.org/licenses/}.
 *
 * @package			PhpMyObject
 * @subpackage 	PMO_Tests
 * @author			Louis Lapointe <laplix@gmail.com>
 * @link				http://pmo.developpez.com/
 * @since			PhpMyObject v0.15
 * @version			$Revision$
 * @copyright		Copyright (c) 2008 Louis Lapointe
 * @license			GPLv3 {@link http://www.gnu.org/licenses/gpl}
 * @filesource
 */

/**
 * requires the needed files
 */
require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'config.php');
require_once(SIMPLETEST.DS.'autorun.php');
require_once(PMO_CORE.DS.'PMO_MyConfig.php');
require_once(PMO_TESTS . DS . 'simpletest' . DS . 'PMO_HTMLReporter.php');


/**
 * This tests the PMO_MyConfig class.
 */
class PMO_MyConfig_Test extends UnitTestCase 
{
	/** constructor calls parent contructor. */
	function PMO_MyConfig_Test()
	{
		$this->UnitTestCase();
	}

	/** called before each test */
	function setUp() {
	}

	/**
	 * called after each test
	 * @todo will probably need this later to reset the config or something
	 */
	function tearDown() { }

	/**********
	 * exists has been deprecated
	function test_exist_empty_var_name()
	{
		$conf = PMO_MyConfig::factory();
		try {
			$conf->exists('');
			$this->fail('PMO_MyConfig::exists("") should have thrown an exception.');
		}
		catch(Exception $e) {
			$this->assertEqual($e->getMessage(), 'Error: Empty parameter',
				'Asking if an empty varname exists throws an exception');
		}
	}
	****************/

	/**
	 * this make sure we get an exception if we don't provide a parameter
	 * just to make sure ;)
	 */
	function test_set_empty_var_name()
	{
		$conf = PMO_MyConfig::factory();
		// empty args
		try {
			$conf->set('','');
			$this->fail('PMO_MyConfig::set("") should have thrown an exception.');
		}
		catch (Exception $e) {
			//echo "<pre>".str_replace(' ','.',$e->getMessage())."</pre><br>";
			$this->assertEqual($e->getMessage(),
				sprintf("Error: Parameter %s doesn't exist",''),
				'Writing to an empty varname throws an exception');
		}
	}

	/**
	 * this make sure PMO_MyConfig will throw an exception if we try
	 * to get an unsuported var.
	 */
	function test_get_empty_var_name()
	{
		$conf = PMO_MyConfig::factory();
		try {
			$conf->get('');
			$this->fail('PMO_MyConfig::get("") should have thrown an excepyion.');
		}
		catch (Exception $e) {
			$this->assertEqual($e->getMessage(),
				sprintf("Error: Parameter %s doesn't exist",''),
				"Reading an empty varname throws an exception");
		}
	}

	/**
	 * this make sure PMO_MyConfig will throw an exception if we try
	 * to set an unsuported var.
	 */
	function test_set_unsupported_var()
	{
		$conf = PMO_MyConfig::factory();
		try {
			$conf->set('MyVar', 'I exist');
			$this->fail('PMO_MyConfig::set("MyVar") should have thrown an exception');
		}
		catch(Exception $e) {
			$this->assertEqual($e->getMessage(), 
				sprintf("Error: Parameter %s doesn't exist",'MyVar'),
				"Writing to a non-existant var throws an exception");
		}
	}

	/**
	 * this make sure PMO_MyConfig will throw an exception of we ask
	 * for an unsuported var.
	 */
	function test_get_unsupported_var()
	{
		$conf = PMO_MyConfig::factory();
		try {
			$myvar = $conf->get('MyVar');
			$this->fail('PMO_MyConfig::get("MyVar") should have thrown an exception');
		}
		catch(Exception $e) {
			$this->assertEqual($e->getMessage(), 
				sprintf("Error: Parameter %s doesn't exist",'MyVar'),
				"Writing to a unknown var throws an exception");
		}
	}

	/**
	 * this tests that all PMO standard vars are correcly initialized
	 */
	function test_MyConfig_default_values() {
		$conf = PMO_MyConfig::factory();

		$this->assertEqual($conf->get('PMO_MyController.OBJECT_COLLECTOR_NAME'), 'collector');
		$this->assertEqual($conf->get('PMO_MyDbms.DRIVER'), '');
		$this->assertEqual($conf->get('PMO_MyDbms.PDODRIVER'), '');
		$this->assertEqual($conf->get('PMO_MyDbms.HOST'), '');
		$this->assertEqual($conf->get('PMO_MyDbms.BASE'), '');
		$this->assertEqual($conf->get('PMO_MyDbms.USER'), '');
		$this->assertEqual($conf->get('PMO_MyDbms.PASS'), '');
		$this->assertEqual($conf->get('PMO_MyDbms.DSN'), '');
		$this->assertFalse($conf->get('PMO_MyDbms.LOG'));
		$this->assertEqual($conf->get('PMO_MyDbms.LOG_FORMAT'), 'Y-m-d H:i:s');

		$path = realpath($conf->get('PMO_MyObject.CLASSPATH'));
		$expected = realpath(PMO_ROOT.DS.'class_loader'.DS);
		$this->assertEqual($path, $expected);

		$this->assertEqual($conf->get('PMO_MyObject.CLASS_FILENAME_PREFIX'), 'class_');
		$this->assertFalse($conf->get('PMO_MyMemCache.ACTIVE'));
		$this->assertEqual($conf->get('PMO_MyMemCache.HOST'), '');
		$this->assertEqual($conf->get('PMO_MyMemCache.PORT'), '');
		$this->assertEqual($conf->get('PMO_MyMemCache.TIMEOUT'), '10');

		$path = realpath($conf->get('PMO_MyTable.CLASSPATH'));
		$expected = realpath(PMO_CORE.DS.'PMO_MyTable'.DS);
		$this->assertEqual($path, $expected);

		$this->assertEqual($conf->get('PMO_MyTable.CLASS_FILENAME_PREFIX'), 'PMO_MyTable_');
		$this->assertFalse($conf->get('PMO_MyTable.CLASS_WRITE_ON_DISK'));
	}

	function test_setup_MyConfig() {
		$conf = PMO_MyConfig::factory();
		$conf->set('PMO_MyDbms.DRIVER', 'pdo');
		$conf->set('PMO_MyDbms.PDODRIVER', 'sqlite');
		$conf->set('PMO_MyDbms.HOST', 'myHost');
		$conf->set('PMO_MyDbms.USER', 'myUser');
		$conf->set('PMO_MyDbms.PASS', 'myPass');
		$conf->set('PMO_MyDbms.DSN',	'sqlite:/my/data/base.db');
		$conf->set('PMO_MyDbms.LOG', TRUE);
		$conf->set('PMO_MyDbms.LOG_FORMAT', 'd/m/Y H:m');
		$conf->set('PMO_MyObject.CLASSPATH', '/my/new/classpath');
		$conf->set('PMO_MyObject.CLASS_FILENAME_PREFIX', 'myFilePrefix_');
		$conf->set('PMO_MyTable.CLASS_WRITE_ON_DISK', TRUE);
		$conf->set('PMO_MyTable.CLASSPATH', 'my/pmo_classpath');
		$conf->set('PMO_MyTable.CLASS_FILENAME_PREFIX', 'MyFile_');
		$conf->set('PMO_MyMemCache.ACTIVE', TRUE);
		$conf->set('PMO_MyMemCache.HOST', 'memhost');
		$conf->set('PMO_MyMemCache.PORT', '3535');
		$conf->set('PMO_MyMemCache.TIMEOUT', 45);


		$this->assertEqual($conf->get('PMO_MyDbms.DRIVER'), 'pdo');
		$this->assertEqual($conf->get('PMO_MyDbms.PDODRIVER'), 'sqlite');
		$this->assertEqual($conf->get('PMO_MyDbms.HOST'), 'myHost');
		$this->assertEqual($conf->get('PMO_MyDbms.USER'), 'myUser');
		$this->assertEqual($conf->get('PMO_MyDbms.PASS'), 'myPass');
		$this->assertEqual($conf->get('PMO_MyDbms.DSN'), 'sqlite:/my/data/base.db');
		$this->assertTrue($conf->get('PMO_MyDbms.LOG'));
		$this->assertEqual($conf->get('PMO_MyDbms.LOG_FORMAT'), 'd/m/Y H:m');
		$this->assertEqual($conf->get('PMO_MyObject.CLASSPATH'), '/my/new/classpath');
		$this->assertEqual($conf->get('PMO_MyObject.CLASS_FILENAME_PREFIX'), 'myFilePrefix_');
		$this->assertTrue($conf->get('PMO_MyTable.CLASS_WRITE_ON_DISK'));
		$this->assertEqual($conf->get('PMO_MyTable.CLASSPATH'), 'my/pmo_classpath');
		$this->assertEqual($conf->get('PMO_MyTable.CLASS_FILENAME_PREFIX'), 'MyFile_');
		$this->assertTrue($conf->get('PMO_MyMemCache.ACTIVE'));
		$this->assertEqual($conf->get('PMO_MyMemCache.HOST'), 'memhost');
		$this->assertEqual($conf->get('PMO_MyMemCache.PORT'), '3535');
		$this->assertEqual($conf->get('PMO_MyMemCache.TIMEOUT'), 45);
	}


}

if (!defined('PMO_TEST_SUITE')) {
	$level = '';
	if (isset($_COOKIE['testLevel'])) {
		$level = $_COOKIE['testLevel'];
	}
	elseif (isset($_GET['level'])) {
		$level = $_GET['level'];
		setcookie('testLevel', $level, time()+60*60*24*30);
	}

	$test = new TestSuite('PMO_MyConfig Test');
	$test->add(new PMO_MyConfig_Test);
	$test->run(new PMO_HTMLReporter($level));
}

