<?php
/**
 * This file contains the PMO_MyObject tests.
 *
 * 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}.
 *
 * 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.15
 * @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_MyController which includes everything needed
 */
require_once(PMO_CORE . DS . 'PMO_Controller.php');

/**
 * requires our basic PMO_MyTest template
 */
require_once(dirname(__FILE__).DS.'PMO_MyTest.php');

/**
 * Tests the PMO_MyObject class
 *
 * This will test everything PMO_MyObject is supposed to do
 */
class PMO_MyObject_Test extends PMO_MyTest
{
	/** constructor calls parent contructor. */
	function __construct() {
		parent::__construct();
		$this->setData();
	}

	function test_load_object() {

		$inventory = PMO_MyObject::factory('inventory');
		$this->assertTrue(is_object($inventory),'$object is an object');
		$this->assertTrue(($inventory instanceof PMO_MyObject), '$inventory is an instance of PMO_MyObject');

		$inventory->inventory_id = 99;
		$inventory->load();

		$this->assertEqual($inventory->film_id, 20);
		$this->assertEqual($inventory->store_id, 1);

	}

	function test_load_many_objects() {
		$inventory = PMO_MyObject::factory('inventory');
		$inventory->inventory_id = 99;
		$inventory->load();

		$film = PMO_MyObject::factory('film');
		$film->film_id = $inventory->film_id;
		$film->load();
		$this->assertEqual($film->title, 'AMELIE HELLFIGHTERS');

		$store = PMO_MyObject::factory('store');
		$store->store_id = $inventory->store_id;
		$store->load();
		$this->assertEqual($store->manager_staff_id, 1);
	}

	function test_all_columns_are_present() {
		$film = PMO_MyObject::factory('film');
		$film->film_id = 20;
		$film->load();

		$this->assertEqual($film->film_id, 20);
		$this->assertEqual($film->title, 'AMELIE HELLFIGHTERS');
		$this->AssertEqual($film->description,
								'A Boring Drama of a Woman And a Squirrel who must Conquer a Student in A Baloon');
		$this->assertEqual($film->release_year, 2006);
		$this->assertEqual($film->language_id, 1);
//pr($film);
		try {
			$this->assertEqual($film->original_language_id, '');
			$this->pass('$film->original_language_id is empty');
		}
		catch (Exception $e) {
			$this->fail('A NULL value is a perfecty legal value. It should not throw this exception since the attribute exits: '.$e->getMessage());
		}
		$this->assertEqual($film->rental_duration, 4);
		$this->assertEqual($film->rental_rate, 4.99);
		$this->assertEqual($film->length, 79);
		$this->assertEqual($film->replacement_cost, 23.99);
		$this->assertEqual($film->rating, 'R');
		$this->assertEqual($film->special_features, 'Commentaries,Deleted Scenes,Behind the Scenes');
		$this->assertEqual($film->last_update, '2006-02-15 05:03:42');

	}

	function test_load_and_save() {
		$this->setConfig('sqlite');
		$this->resetSqliteActor();

		$actor = PMO_MyObject::factory('actor');
		$actor->actor_id = 1;
		$actor->load();
		$this->assertEqual($actor->first_name, 'Nicolas');
		$this->assertEqual($actor->last_name, 'nico');

		$actor->last_name = 'Boiteux';
		$actor->save();
		unset($actor);

		$actor2 = PMO_MyObject::factory('actor');
		$actor2->actor_id = 1;
		$actor2->load();
		$this->assertEqual($actor2->last_name, 'Boiteux');
	}

	function test_insert_new_actor() {
		$this->setConfig('sqlite');
		$this->resetSqliteActor();

		$actor = PMO_MyObject::factory('actor');
		$actor->first_name = 'Louis';
		$actor->last_name = 'Lapointe';
		$now = date('Y-m-d H:i:s');
		$actor->last_update = $now;

		$actor->setNew(TRUE);
		$actor->save();

		$actor2 = PMO_MyObject::factory('actor');
		$actor2->actor_id = $actor->actor_id;
		$actor2->load();
		$this->assertEqual($actor2->actor_id, 21);
		$this->assertEqual($actor2->first_name, 'Louis');
		$this->assertEqual($actor2->last_name, 'Lapointe');
	}

	function test_delete_the_new_actor() {
		$this->setConfig('sqlite');
		$this->resetSqliteActor();

		$actor = PMO_MyObject::factory('actor');
		$actor->first_name = 'Louis';
		$actor->last_name = 'Lapointe';
		$now = date('Y-m-d H:i:s');
		$actor->last_update = $now;

		$actor->setNew(TRUE);
		$actor->save();

		$actor_id = $actor->actor_id;
		unset($actor);

		$actor = PMO_MyObject::factory('actor');
		$actor->actor_id = $actor_id;
		$actor->load();
		$this->assertEqual($actor->last_name, 'Lapointe', 'Just making sur we have a row');

		$actor->delete();
		unset($actor);

		$actor = PMO_MyObject::factory('actor');
		$actor->actor_id = $actor_id;
		try {
			$actor->load();
		}
		catch (Exception $e) {
			$this->pass($e->getMessage());
		}
	}
}

//
// TODO : POUSUIVRE (plein d'autres choses à tester, les getter et setter d'attributs,
// 		 les getOnjectXXX , etc.
//

class PMO_MyObject_attributes_test extends PMO_MyTest
{

	function __construct() {
		parent::__construct();
	}

	function test_getTable() {
		$actor = PMO_MyObject::factory('actor');
		$table = $actor->getTable();
		$this->assertTrue(($table instanceof PMO_Table), '$table is an instance or PMO_Table');
		$this->assertTrue(($table instanceof PMO_MyTable), '$table is an instance or PMO_MyTable');
		$this->assertFalse(($table instanceof PMO_MyTable_actor), '$table is NOT YET an instance oF PMO_MyTable_actor');
	}

	function test_getTable_not_persistant() {
		$file = 'actor.php';
		$cnf = PMO_MyConfig::factory();
		$prefix = $cnf->get('PMO_MyTable.CLASS_FILENAME_PREFIX');
		$classpath = $cnf->get('PMO_MyTable.CLASSPATH');
		$persist = $classpath.$prefix.$file;

		@unlink($persist);
		$actor = PMO_MyObject::factory('actor');
		$this->assertFalse(file_exists($persist), "Table ".basename($classpath)."/$prefix$file does not exist");
		
	}
}


class PMO_MyObject_get_objects_Test extends PMO_MyTest
{
}
	




// run the test cases if called individually
if (!defined('PMO_TEST_SUITE')) {
	$level = '';
	if (isset($_GET['level']))
		$level = $_GET['level'];

	$test = new TestSuite('PMO_MyObject Tests');
	$test->add(new PMO_MyObject_Test);
	$test->add(new PMO_MyObject_attributes_Test);
	//$test->add(new PMO_MyObject_get_objects_Test);
	$test->run(new PMO_HTMLReporter($level));
}

