php unit tests

This commit is contained in:
kremsy 2017-04-15 22:01:27 +02:00
parent eb6dc3db1a
commit 26b508311c
5 changed files with 83 additions and 16 deletions

10
Tests/autoload.php Normal file
View File

@ -0,0 +1,10 @@
<?php
// Define base dir
define('MANIACONTROL_PATH', realpath(__DIR__ . '/..') . DIRECTORY_SEPARATOR);
define('DEV_MODE', true); // Development mode to not send error reports etc.
define('PHP_UNIT_TEST', true);
// Register AutoLoader
require_once MANIACONTROL_PATH . 'core' . DIRECTORY_SEPARATOR . 'AutoLoader.php';
\ManiaControl\AutoLoader::register();

View File

@ -1,10 +1,4 @@
<?php <?php
/**
* Created by PhpStorm.
* User: Lukas
* Date: 14. Apr. 2017
* Time: 22:41
*/
use ManiaControl\Logger; use ManiaControl\Logger;

View File

@ -4,6 +4,8 @@ namespace Tests\core\Update;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Update\UpdateData;
use ManiaControl\Update\UpdateManager; use ManiaControl\Update\UpdateManager;
final class UpdateManagerTest extends \PHPUnit_Framework_TestCase { final class UpdateManagerTest extends \PHPUnit_Framework_TestCase {
@ -14,7 +16,7 @@ final class UpdateManagerTest extends \PHPUnit_Framework_TestCase {
$fileName = MANIACONTROL_PATH . "core" . DIRECTORY_SEPARATOR . UpdateManager::BUILD_DATE_FILE_NAME; $fileName = MANIACONTROL_PATH . "core" . DIRECTORY_SEPARATOR . UpdateManager::BUILD_DATE_FILE_NAME;
if(!file_exists($fileName)){ if (!file_exists($fileName)) {
$this->assertTrue($updateManager->setBuildDate("BuildDateTest-6543210")); $this->assertTrue($updateManager->setBuildDate("BuildDateTest-6543210"));
} }
@ -49,13 +51,13 @@ final class UpdateManagerTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($updateManager->isNightlyUpdateChannel($updateManager->getCurrentUpdateChannelSetting()), $isNightly); $this->assertEquals($updateManager->isNightlyUpdateChannel($updateManager->getCurrentUpdateChannelSetting()), $isNightly);
} }
public function testCoreUpdateAsync() { public function testCheckCoreUpdateAsync() {
$maniaControl = new ManiaControl(); $maniaControl = new ManiaControl();
$updateManager = $maniaControl->getUpdateManager(); $updateManager = $maniaControl->getUpdateManager();
$called = false; $called = false;
$function = function ($updateData) use (&$called){ $function = function ($updateData) use (&$called) {
$called = true; $called = true;
$this->assertNotNull($updateData); $this->assertNotNull($updateData);
$this->assertObjectHasAttribute("version", $updateData); $this->assertObjectHasAttribute("version", $updateData);
@ -67,4 +69,38 @@ final class UpdateManagerTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue($called); $this->assertTrue($called);
} }
public function testPerformCoreUpdate() {
$maniaControl = new ManiaControl();
$updateManager = $maniaControl->getUpdateManager();
//No Update Data Available -> so Fail
$this->assertFalse($updateManager->performCoreUpdate());
//Should Also Fail with a Player
$player = new Player($maniaControl, true);
$this->assertFalse($updateManager->performCoreUpdate($player));
$message = '[ERROR] Update failed: No update Data available!';
$message = '[' . date('d-M-Y H:i:s e') . '] ' . $message . PHP_EOL;
//Check message
$this->assertContains($message, $this->getActualOutput());
$dataJson = '[{"id":"260","version":"0.166","channel":"nightly","min_dedicated_build":"2014-04-02_18_00","release_date":"2017-03-16 21:57:40","url":"https:\/\/download.maniacontrol.com\/nightly\/ManiaControl_nightly_0-166.zip"}]';
//Create and Test Core Update Data
$updateData = new UpdateData(json_decode($dataJson)[0]);
$this->assertEquals("0.166", $updateData->version);
$this->assertEquals("nightly", $updateData->channel);
$this->assertEquals("2014-04-02_18_00", $updateData->minDedicatedBuild);
$this->assertEquals("2017-03-16 21:57:40", $updateData->releaseDate);
$this->assertEquals("https://download.maniacontrol.com/nightly/ManiaControl_nightly_0-166.zip", $updateData->url);
$updateManager->setCoreUpdateData($updateData);
//Methods should return its non closure success
$this->assertTrue($updateManager->performCoreUpdate());
}
} }

25
Tests/phpunit.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="autoload.php" colors="true" verbose="true" debug="true"
beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutChangesToGlobalState="true">
<testsuites>
<testsuite name="ManiaControl">
<directory>Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">../core</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/logs/coverage.html"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<!--<ini name="error_reporting" value="E_ALL"/>
<ini name="display_errors" value="On"/>
<ini name="display_startup_errors" value="On"/>
<get name="test-get-name" value="test-get-value"/>
<post name="test-post-name" value="test-post-value"/>-->
</php>
</phpunit>

View File

@ -377,10 +377,12 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
return; return;
} }
if(!defined('PHP_UNIT_TEST')){
$zip->extractTo(MANIACONTROL_PATH); $zip->extractTo(MANIACONTROL_PATH);
$zip->close(); $zip->close();
unlink($updateFileName); unlink($updateFileName);
FileUtil::deleteTempFolder(); FileUtil::deleteTempFolder();
}
// Set the build date // Set the build date
$this->setBuildDate($updateData->releaseDate); $this->setBuildDate($updateData->releaseDate);