ModuleManagerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. class ModuleManagerTest extends PHPUnit_Framework_TestCase
  3. {
  4. /**
  5. * @var \Tools\Context $context
  6. **/
  7. private $context;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. list($server, $mysqlConfig) = require("test/config.php");
  12. $this->dbh = new PDO($mysqlConfig[4][0], $mysqlConfig[1], $mysqlConfig[2]);
  13. $this->dbh->exec("DROP DATABASE ".$mysqlConfig[4][1]);
  14. $this->dbh->exec("CREATE DATABASE ".$mysqlConfig[4][1]);
  15. \Entity\ModelBase::init($mysqlConfig);
  16. $this->context = new \Tools\Context($server);
  17. \Entity\ModelBase::setup();
  18. $this->context->serve();
  19. }
  20. public function testEnv()
  21. {
  22. list($server, $mysqlConfig) = require("test/config.php");
  23. unset($server["phpUnit"]);
  24. $context = new \Tools\Context($server);
  25. $context->serve();
  26. $this->assertFalse($context->router->overrideUrl("test", "test"));
  27. }
  28. public function testInstall()
  29. {
  30. $moduleMan = $this->context->moduleManager;
  31. $this->assertNotNull($moduleMan);
  32. $this->assertTrue($this->context->router->overrideUrl("modulesPath", dirname(__FILE__)."/modules/"));
  33. $mod = $moduleMan->install("test");
  34. $this->assertNotFalse($mod);
  35. $this->assertEquals("realName", $mod->getName());
  36. $this->assertEquals("realName", $mod->getParentName());
  37. $this->assertNotNull($this->context->hookManager);
  38. $modHook = new \Entity\ModuleHook(1);
  39. $this->assertNotNull($modHook->id);
  40. $this->assertNotFalse($modHook->id);
  41. $this->assertNotNull($mod->entity->id);
  42. try {
  43. $mod->test;
  44. $this->fail("Excepted Exception");
  45. }
  46. catch (\Exception $e) {}
  47. $modEntity = new \Entity\Module($mod->entity->id);
  48. $this->assertEquals($mod->entity->id, $modEntity->id);
  49. $this->assertEquals("realName", $modEntity->name);
  50. $this->assertEquals("My description", $modEntity->description);
  51. $this->assertEquals("test", $modEntity->directory);
  52. $this->assertEquals(1, $modEntity->active);
  53. try {
  54. $mod->installHook();
  55. $this->fail("Expected exception");
  56. }
  57. catch (\Exception $e) { }
  58. $mod = $moduleMan->install("test2");
  59. $this->assertFalse($mod);
  60. }
  61. public function testInstalled()
  62. {
  63. list($server, $mysqlConfig) = require("test/config.php");
  64. $modEntity = new \Entity\Module(1);
  65. $modEntity->directory = "test4";
  66. $modEntity->active = 1;
  67. $modEntity->save();
  68. $context = new \Tools\Context($server);
  69. $this->assertTrue($context->router->overrideUrl("modulesPath", dirname(__FILE__)."/modules/"));
  70. $modHook = new \Entity\ModuleHook(1);
  71. $this->assertNotNull($modHook->id);
  72. $this->assertNotFalse($modHook->id);
  73. $context->serve();
  74. $this->assertInternalType("array", \Entity\Module::getActivated());
  75. $this->assertCount(1, \Entity\Module::getActivated());
  76. $mman = $context->moduleManager;
  77. $this->assertNotNull($mman);
  78. $this->assertInternalType("array", $mman->modules);
  79. $this->assertCount(1, $mman->modules);
  80. $this->assertNotFalse($mman->getModuleFromId(1));
  81. $this->assertNotFalse($mman->{"_id_1"});
  82. $this->assertFalse($mman->getModuleFromId(-2));
  83. $this->assertFalse($mman->{"_id_-2"});
  84. $this->assertEquals("test 4", $mman->_id_1->getName());
  85. $this->assertFalse($mman->_id_1->hooked);
  86. $context->hookManager->trigger("testHook", array(1, 2, 3));
  87. $this->assertNotFalse($mman->_id_1->hooked);
  88. $this->assertInstanceOf("\Tools\HookEvent", $mman->_id_1->hooked);
  89. $this->assertEquals("testHook", $mman->_id_1->hooked->hookName);
  90. $this->assertInternalType("array", $mman->_id_1->hooked->params);
  91. $this->assertCount(3, $mman->_id_1->hooked->params);
  92. $this->assertEquals(array(1, 2, 3), $mman->_id_1->hooked->params);
  93. try {
  94. $mman->_id_1->hooked->fail;
  95. $this->fail("Expected exception");
  96. }
  97. catch (\Exception $e) { }
  98. $modEntity = new \Entity\Module(1);
  99. $modEntity->directory = "test3";
  100. $modEntity->save();
  101. $context = new \Tools\Context($server);
  102. $context->serve();
  103. $this->assertTrue($context->router->overrideUrl("modulesPath", dirname(__FILE__)."/modules/"));
  104. $this->assertInternalType("array", \Entity\Module::getActivated());
  105. $this->assertCount(0, \Entity\Module::getActivated());
  106. $mman = new \Tools\ModuleManager($context);
  107. $this->assertNotNull($mman);
  108. $this->assertInternalType("array", $mman->modules);
  109. $this->assertCount(0, $mman->modules);
  110. $this->assertFalse($mman->getModuleFromId(1));
  111. $modEntity = new \Entity\Module(1);
  112. $this->assertEquals(0, $modEntity->active);
  113. }
  114. }