<?php

class ModuleManagerTest extends PHPUnit_Framework_TestCase
{
	/**
	 * @var \Tools\Context $context
	**/
	private $context;

	public function __construct()
	{
		parent::__construct();
		list($server, $mysqlConfig) = require("test/config.php");
		$this->dbh = new PDO($mysqlConfig[4][0], $mysqlConfig[1], $mysqlConfig[2]);
		$this->dbh->exec("DROP DATABASE ".$mysqlConfig[4][1]);
		$this->dbh->exec("CREATE DATABASE ".$mysqlConfig[4][1]);

		\Entity\ModelBase::init($mysqlConfig);
		$this->context = new \Tools\Context($server);
		\Entity\ModelBase::setup();
		$this->context->serve();
	}

	public function testEnv()
	{
		list($server, $mysqlConfig) = require("test/config.php");
		unset($server["phpUnit"]);
		$context = new \Tools\Context($server);
		$context->serve();
		$this->assertFalse($context->router->overrideUrl("test", "test"));
	}

	public function testInstall()
	{
		$moduleMan = $this->context->moduleManager;
		$this->assertNotNull($moduleMan);
		$this->assertTrue($this->context->router->overrideUrl("modulesPath", dirname(__FILE__)."/modules/"));
		$mod = $moduleMan->install("test");
		$this->assertNotFalse($mod);
		$this->assertEquals("realName", $mod->getName());
		$this->assertEquals("realName", $mod->getParentName());
		$this->assertNotNull($this->context->hookManager);
		$modHook = new \Entity\ModuleHook(1);
		$this->assertNotNull($modHook->id);
		$this->assertNotFalse($modHook->id);
		$this->assertNotNull($mod->entity->id);
		try {
			$mod->test;
			$this->fail("Excepted Exception");
		}
		catch (\Exception $e) {}
		$modEntity = new \Entity\Module($mod->entity->id);
		$this->assertEquals($mod->entity->id, $modEntity->id);
		$this->assertEquals("realName", $modEntity->name);
		$this->assertEquals("My description", $modEntity->description);
		$this->assertEquals("test", $modEntity->directory);
		$this->assertEquals(1, $modEntity->active);
		try {
			$mod->installHook();
			$this->fail("Expected exception");
		}
		catch (\Exception $e) {  }
		$mod = $moduleMan->install("test2");
		$this->assertFalse($mod);
	}

	public function testInstalled()
	{
		list($server, $mysqlConfig) = require("test/config.php");
		$modEntity = new \Entity\Module(1);
		$modEntity->directory = "test4";
		$modEntity->active = 1;
		$modEntity->save();
		$context = new \Tools\Context($server);
		$this->assertTrue($context->router->overrideUrl("modulesPath", dirname(__FILE__)."/modules/"));
		$modHook = new \Entity\ModuleHook(1);
		$this->assertNotNull($modHook->id);
		$this->assertNotFalse($modHook->id);
		$context->serve();
		$this->assertInternalType("array", \Entity\Module::getActivated());
		$this->assertCount(1, \Entity\Module::getActivated());
		$mman = $context->moduleManager;
		$this->assertNotNull($mman);
		$this->assertInternalType("array", $mman->modules);
		$this->assertCount(1, $mman->modules);
		$this->assertNotFalse($mman->getModuleFromId(1));
		$this->assertNotFalse($mman->{"_id_1"});
		$this->assertFalse($mman->getModuleFromId(-2));
		$this->assertFalse($mman->{"_id_-2"});
		$this->assertEquals("test 4", $mman->_id_1->getName());
		$this->assertFalse($mman->_id_1->hooked);
		$context->hookManager->trigger("testHook", array(1, 2, 3));
		$this->assertNotFalse($mman->_id_1->hooked);
		$this->assertInstanceOf("\Tools\HookEvent", $mman->_id_1->hooked);
		$this->assertEquals("testHook", $mman->_id_1->hooked->hookName);
		$this->assertInternalType("array", $mman->_id_1->hooked->params);
		$this->assertCount(3, $mman->_id_1->hooked->params);
		$this->assertEquals(array(1, 2, 3), $mman->_id_1->hooked->params);
		try {
			$mman->_id_1->hooked->fail;
			$this->fail("Expected exception");
		}
		catch (\Exception $e) { }
		$modEntity = new \Entity\Module(1);
		$modEntity->directory = "test3";
		$modEntity->save();
		$context = new \Tools\Context($server);
		$context->serve();
		$this->assertTrue($context->router->overrideUrl("modulesPath", dirname(__FILE__)."/modules/"));
		$this->assertInternalType("array", \Entity\Module::getActivated());
		$this->assertCount(0, \Entity\Module::getActivated());
		$mman = new \Tools\ModuleManager($context);
		$this->assertNotNull($mman);
		$this->assertInternalType("array", $mman->modules);
		$this->assertCount(0, $mman->modules);
		$this->assertFalse($mman->getModuleFromId(1));
		$modEntity = new \Entity\Module(1);
		$this->assertEquals(0, $modEntity->active);
	}
}

