<?php
/**
 * This file contains the PMO_MyParser 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_MyParser file.
 */
require_once(PMO_CORE . DS . 'PMO_MyParser.php');

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

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

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

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

	function test_basic_query() {
		$parser = new PMO_MyParser;
		$this->assertTrue(is_a($parser, 'PMO_Parser'), 'the object is a PMO_parser object');

		$sql = 'select * from MyTable';
		$result = $parser->parseRequest($sql);
		$this->assertIdentical($result, true);

		$this->assertEqual($parser->fetchTable(), 'MyTable', 'fetchTable() returned "MyTable"');
		$this->assertEqual($parser->countFields(), 0, "countField = 0 for '$sql'");
		$this->assertEqual($parser->fetchField(), NULL, 'fetchField() returned NULL');
		$this->assertEqual($parser->fetchFunction(), NULL, 'fetchFunction() returned NULL');
	}

	function test_basic_query_with_where_clause() {
		$parser = new PMO_MyParser;

		$sql = 'select * from MyTable where id = 1 and one = "two"';
		$result = $parser->parseRequest($sql);
		$this->assertIdentical($result, true);

		$this->assertEqual($parser->fetchTable(), 'MyTable', 'fetchTable() returned "MyTable"');
		$this->assertEqual($parser->countFields(), 0, "countField = 0 for '$sql'");
		$this->assertEqual($parser->fetchField(), NULL, 'fetchField() returned NULL');
		$this->assertEqual($parser->fetchFunction(), NULL, 'fetchFunction() returned NULL');
	}

	function test_query_with_fields() {
		$parser = new PMO_MyParser;
		$sql = 'select one,two from MyTable';
		
		$result = $parser->parseRequest($sql);
		$this->assertEqual($parser->countFields(), 2, "countField = 2 for '$sql'");
		$this->assertEqual($parser->fetchField(), 'one', 'fetchField() returned "one"');
		$this->assertEqual($parser->fetchField(), 'two', 'fetchField() returned "two"');
	}

	function test_query_with_function() {
		$parser = new PMO_MyParser;
		$sql = 'select min(one),two from MyTable';

		$result = $parser->parseRequest($sql);
		$this->assertTrue($result, 'parseRequest returned TRUE');
		$this->assertEqual($parser->fetchTable(), 'MyTable', 'fetchTable() returned "MyTable"');
		$this->assertEqual($parser->countFields(), 1, "countField = 1 for '$sql'");
		$this->assertEqual($parser->fetchField(), 'two', 'fetchField() returned "two"');
		$this->assertEqual($parser->fetchFunction(), 'min(one)', 'fetchFunction() returned NULL');
	}

	function test_query_with_multiple_functions() {
		$parser = new PMO_MyParser;
		$sql = 'select id,min(one),max(two),other_data from MyTable';
		
		$result = $parser->parseRequest($sql);
		$this->assertEqual($parser->countFields(), 2, "countField = 2 for '$sql'");
		$this->assertEqual($parser->fetchField(), 'id', 'fetchField() returned "id"');
		$this->assertEqual($parser->fetchField(), 'other_data', 'fetchField() returned "other_data"');
		$this->assertEqual($parser->fetchFunction(), 'min(one)', 'fetchFunction() returned "min(one)"');
		$this->assertEqual($parser->fetchFunction(), 'max(two)', 'fetchFunction() returned "max(two)"');
	}

	function test_query_with_multiple_tables_and_fields() {
		$parser = new PMO_MyParser;
		$sql = 'select t1.id,t1.name,t2.dt_purchase,sum(t2.amount)';
		$sql .= ' from t1,t2';
		$sql .= ' where t2.t1_id = t1.id';

		$result = $parser->parseRequest($sql);
		$this->assertTrue($result, 'parseRequest returned TRUE');
		$this->assertEqual($parser->fetchTable(), 't1', 'fetchTable() returned "t1"');
		$this->assertEqual($parser->fetchTable(), 't2', 'fetchTable() returned "t2"');
		$this->assertEqual($parser->countFields(), 4, "countField = 3 for '$sql'");
		$this->assertEqual($parser->fetchField(), 'id', 'fetchField() returned "id"');
		$this->assertEqual($parser->fetchField(), 'name', 'fetchField() returned "name"');
		$this->assertEqual($parser->fetchField(), 'dt_purchase', 'fetchField() returned "dt_purchase"');
		$this->assertEqual($parser->fetchField(), NULL, 'fetchField() returned NULL');
		$this->assertEqual($parser->fetchFunction(), 'sum(t2.amount)', 'fetchFunction() returned "sum(t2.amount)"');
		//dump($parser);
	}
	
}

function dump($data) {
	echo '<div style="position:absolute;background:#fff;font-weight:bold;color:#009;z-index:-1000;"><pre>'
		.print_r($data,true)."</pre></div>";
}

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_MyParser Test');
	$test->add(new PMO_MyParser_Test);
	$test->run(new PMO_HTMLReporter($level));
}

