<?php
/**
 * This file contains the PMO_MyDbms_Mysql tests.
 *
 * This file is part of the PhpMyObject project,
 * an Object-Relational Mapping (ORM) system.
 * 
 * Copyright (c) 2008 Louis Lapointe
 *
 * 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}.
 *
 * PhpMyObject 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.16
 * @version			$Revision$
 * @copyright		Copyright (C) 2008 Louis Lapointe
 * @license			GPLv3 {@link http://www.gnu.org/licenses/gpl}
 * @filesource
 *
 */ 

/**
 * setup this test case if called individually
 */
if (!defined('PMO_TEST_SUITE')) {
	require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'config.php');
	require_once(SIMPLETEST.DS.'autorun.php');
}


/**
 * requires the PMO_MyDbms file.
 */
require_once(PMO_CORE . DS . 'PMO_MyDbms.php');

/**
 * loads the Html reporter
 */
require_once(PMO_TESTS . DS . 'simpletest' . DS . 'PMO_HTMLReporter.php');

/**
 * This tests the PMO_MyDbms class.
 *
 */
class PMO_MyDbms_Test extends UnitTestCase
{

	/**
	 * constructor calls parent contructor
	 */
	function __constuct() {
		$this->UnitTestCase();
	}

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

	/**
	 * this method is called after each test
	 */
	function tearDown() {
	}

	function test_object_is_a_PMO_MyDbms_Mysql_object() {
		$config = PMO_MyConfig::factory();
		$config->set('PMO_MyDbms.DRIVER','mysql');
		$config->set('PMO_MyDbms.PDODRIVER', '');
		$config->set('PMO_MyDbms.HOST','localhost');
		$config->set('PMO_MyDbms.USER','pmo');
		$config->set('PMO_MyDbms.PASS','pmo');
		$config->set('PMO_MyDbms.BASE','sakila');
		$config->set('PMO_MyDbms.DSN','');
		$dbms = PMO_MyDbms::factory();
		$this->assertTrue($dbms instanceof PMO_Dbms, 'is an instance of PMO_Dbms');
		$this->assertTrue($dbms instanceof PMO_MyDbms, 'is an instance of PMO_MyDbms');
		$this->assertTrue($dbms instanceof PMO_Dbms_Mysql,'is an instance of PMO_Dbms_Mysql');
		$dbms->killInstance();
	}

	function test_object_is_a_PMO_MyDbms_PDO_Mysql_object() {
		$config = PMO_MyConfig::factory();
		$config->set('PMO_MyDbms.DRIVER','pdo');
		$config->set('PMO_MyDbms.PDODRIVER', 'mysql');
		$config->set('PMO_MyDbms.HOST','localhost');
		$config->set('PMO_MyDbms.USER','pmo');
		$config->set('PMO_MyDbms.PASS','pmo');
		$config->set('PMO_MyDbms.BASE','sakila');
		$config->set('PMO_MyDbms.DSN','');
		$dbms = PMO_MyDbms::factory();
		$this->assertTrue($dbms instanceof PMO_Dbms, 'is an instance of PMO_Dbms');
		$this->assertTrue($dbms instanceof PMO_MyDbms, 'is an instance of PMO_MyDbms');
		$this->assertTrue($dbms instanceof PMO_Dbms_Pdo,'is an instance of PMO_Dbms_Pdo');
		$dbms->killInstance();
	}

	function test_object_is_a_PMO_MyDbms_PDO_Sqlite_object() {
		$config = PMO_MyConfig::factory();
		$config->set('PMO_MyDbms.DRIVER','pdo');
		$config->set('PMO_MyDbms.PDODRIVER', 'sqlite');
		$config->set('PMO_MyDbms.HOST','');
		$config->set('PMO_MyDbms.USER','');
		$config->set('PMO_MyDbms.PASS','');
		$config->set('PMO_MyDbms.BASE','');
		$config->set('PMO_MyDbms.DSN','sqlite:'.dirname(__FILE__).DS.'test.sdb');
		$dbms = PMO_MyDbms::factory();
		$this->assertTrue($dbms instanceof PMO_Dbms, 'is an instance of PMO_Dbms');
		$this->assertTrue($dbms instanceof PMO_MyDbms, 'is an instance of PMO_MyDbms');
		$this->assertTrue($dbms instanceof PMO_Dbms_Sqlite,'is an instance of PMO_Dbms_Sqlite');
		$dbms->killInstance();
	}

	function test_object_is_not_a_PMO_supported_driver() {
		$config = PMO_MyConfig::factory();
		$config->set('PMO_MyDbms.DRIVER','mssql');
		$config->set('PMO_MyDbms.PDODRIVER', '');
		$config->set('PMO_MyDbms.HOST','');
		$config->set('PMO_MyDbms.USER','');
		$config->set('PMO_MyDbms.PASS','');
		$config->set('PMO_MyDbms.BASE','');
		$config->set('PMO_MyDbms.DSN','');
		try {
			$dbms = PMO_MyDbms::factory();
			$this->assertTrue($dbms instanceof PMO_Dbms, 'is an instance of PMO_Dbms');
			$this->assertTrue($dbms instanceof PMO_MyDbms, 'is an instance of PMO_MyDbms');
			$this->assertTrue($dbms instanceof PMO_Dbms_Sqlite,'is an instance of PMO_Dbms_Sqlite');
			$dbms->killInstance();
		}
		catch (Exception $e) {
			$this->assertPattern('/not a PMO supported driver/', $e->getMessage(), $e->getMessage());
		}
	}

}

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

	$test = new TestSuite('Test MyDbms with all drivers');
	$test->add(new PMO_MyDbms_Test);
	$test->run(new PMO_HTMLReporter($level));
}

