<?php

class EmptyModel extends \Entity\ModelBase
{
    public function install()
    {
        $dbPrefix = $this->getDbPrefix();
        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}emptymodel` (`id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)");
        if ($result === false)
            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
        return true;
    }
}

class ModelBaseTest extends PHPUnit_Framework_TestCase
{
	public function testModelCreation()
	{
		list($server, $mysqlConfig) = require("test/config.php");
		$dbh = new PDO($mysqlConfig[4][0], $mysqlConfig[1], $mysqlConfig[2]);
		$dbh->exec("DROP DATABASE ".$mysqlConfig[4][1]);
		$dbh->exec("CREATE DATABASE ".$mysqlConfig[4][1]);
		\Entity\ModelBase::init($mysqlConfig);
		$context = new \Tools\Context($server, false);
		\Entity\ModelBase::setup();
	}

	public function testInstallFailure()
	{
		list($server, $mysqlConfig) = require("test/config.php");
		$dbh = new PDO($mysqlConfig[4][0], $mysqlConfig[1], $mysqlConfig[2]);
		$dbh->exec("DROP DATABASE ".$mysqlConfig[4][1]);
		\Entity\ModelBase::init($mysqlConfig);
		$context = new \Tools\Context($server, false);
		$this->assertFalse(\Entity\ModelBase::setup());
	}

	public function testEmptyTable()
	{
		list($server, $mysqlConfig) = require("test/config.php");
		$dbh = new PDO($mysqlConfig[4][0], $mysqlConfig[1], $mysqlConfig[2]);
		$dbh->exec("CREATE DATABASE IF NOT EXISTS ".$mysqlConfig[4][1]);
		$table = new EmptyModel();
		$this->assertTrue($table->install());
		$item = new EmptyModel();
		$item->save();
		$item2 = new EmptyModel($item->id);
		$this->assertEquals($item->id, $item2->id);
	}

	public function testSelects()
	{
		//TODO
	}
}

