diff --git a/Tests/core/LoggerTest.php b/Tests/core/LoggerTest.php new file mode 100644 index 00000000..1bca9bde --- /dev/null +++ b/Tests/core/LoggerTest.php @@ -0,0 +1,15 @@ +assertEquals(Logger::getLogsFolder(), MANIACONTROL_PATH . 'logs' . DIRECTORY_SEPARATOR); + } +} diff --git a/Tests/core/ManiaControlTest.php b/Tests/core/ManiaControlTest.php new file mode 100644 index 00000000..c7e111fb --- /dev/null +++ b/Tests/core/ManiaControlTest.php @@ -0,0 +1,23 @@ +run(10); + + sleep(15); + + //$this->l + //$this->assertNull($maniaControl); + } + +/* public function testGetClient(){ + $maniaControl = new ManiaControl(); + $mcClient = $maniaControl->getClient(); + + //$maniaControl->connect(); + //$mpClient = new Maniaplanet\DedicatedServer\Connection(); + }*/ +} diff --git a/Tests/core/Update/UpdateManagerTest.php b/Tests/core/Update/UpdateManagerTest.php new file mode 100644 index 00000000..26234b58 --- /dev/null +++ b/Tests/core/Update/UpdateManagerTest.php @@ -0,0 +1,70 @@ +assertTrue($updateManager->setBuildDate("BuildDateTest-6543210")); + } + + $this->assertFileExists($fileName); + + $buildDate = $updateManager->getBuildDate(); + $this->assertStringEqualsFile($fileName, $buildDate); + + $this->assertTrue($updateManager->setBuildDate("BuildDateTest-0123456")); + $this->assertEquals($updateManager->getBuildDate(), "BuildDateTest-0123456"); + + $this->assertStringEqualsFile($fileName, $updateManager->getBuildDate()); + } + + public function testGetPluginUpdateManagerTest() { + $maniaControl = new ManiaControl(); + $updateManager = new UpdateManager($maniaControl); + + $pluginUpdateManager = $updateManager->getPluginUpdateManager(); + + $this->assertInstanceOf("ManiaControl\\Update\\PluginUpdateManager", $pluginUpdateManager); + } + + public function testIsNightlyUpdateChannel() { + $maniaControl = new ManiaControl(); + $updateManager = new UpdateManager($maniaControl); + + $this->assertTrue($updateManager->isNightlyUpdateChannel(UpdateManager::CHANNEL_NIGHTLY)); + + $isNightly = $updateManager->isNightlyUpdateChannel(null); + + $this->assertEquals($updateManager->isNightlyUpdateChannel($updateManager->getCurrentUpdateChannelSetting()), $isNightly); + } + + public function testCoreUpdateAsync() { + $maniaControl = new ManiaControl(); + + $updateManager = $maniaControl->getUpdateManager(); + + $called = false; + $function = function ($updateData) use (&$called){ + $called = true; + $this->assertNotNull($updateData); + $this->assertObjectHasAttribute("version", $updateData); + }; + + $updateManager->checkCoreUpdateAsync($function); + + $maniaControl->run(6); + + $this->assertTrue($called); + } +} diff --git a/core/ManiaControl.php b/core/ManiaControl.php index 2529289d..4575b239 100644 --- a/core/ManiaControl.php +++ b/core/ManiaControl.php @@ -64,7 +64,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, * Public properties */ /** @var SettingManager $settingManager - * @see getSettingManager() + * @see getSettingManager() * @deprecated use getSettingManager() */ public $settingManager = null; @@ -166,6 +166,8 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, private $requestQuitMessage = null; + private $startTime = -1; + /** * Construct a new ManiaControl instance */ @@ -547,11 +549,12 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, $this->requestQuitMessage = $message; } - /** * Run ManiaControl + * + * @param int $runTime Time in Seconds until its terminating (used for PHPUnit Tests) */ - public function run() { + public function run($runTime = -1) { Logger::log('Starting ManiaControl v' . self::VERSION . '!'); try { @@ -583,14 +586,22 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, Logger::log('Link: ' . $this->getServer()->getJoinLink()); $this->getChat()->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!'); + $this->startTime = time(); + // Main loop while (!$this->requestQuitMessage) { $this->loop(); + + //Used for PHPUnit Tests + if ($runTime > 0) { + if (time() > ($this->startTime + $runTime)) { + $this->requestQuit("Run Time Exceeded"); + } + } } // Shutdown - $this->quit($this->requestQuitMessage); - + $this->quit($this->requestQuitMessage); } catch (TransportException $exception) { Logger::logError('Connection interrupted!'); $this->getErrorHandler()->handleException($exception); @@ -679,7 +690,9 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, $loopStart = microtime(true); // Extend script timeout - set_time_limit(self::SCRIPT_TIMEOUT); + if(!defined('PHP_UNIT_TEST')){ + set_time_limit(self::SCRIPT_TIMEOUT); + } // Manage callbacks $this->getCallbackManager()->manageCallbacks(); diff --git a/core/Update/UpdateManager.php b/core/Update/UpdateManager.php index 301ea22e..02d30ea3 100644 --- a/core/Update/UpdateManager.php +++ b/core/Update/UpdateManager.php @@ -145,6 +145,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener Logger::logError('Error on UpdateCheck: ' . $error); return; } + $versions = json_decode($dataJson); if (!$versions || !isset($versions[0])) { call_user_func($function); diff --git a/core/Utils/SystemUtil.php b/core/Utils/SystemUtil.php index 5c0942d0..94d61224 100644 --- a/core/Utils/SystemUtil.php +++ b/core/Utils/SystemUtil.php @@ -117,7 +117,10 @@ class SystemUtil { Logger::log($message); } } - exit; + + if (!defined('PHP_UNIT_TEST')) { + exit; + } } /**