mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
Move API checking to its own class so it can be unit-tested
This commit is contained in:
parent
5f23fffdda
commit
bf2851f324
65
src/pocketmine/plugin/ApiVersion.php
Normal file
65
src/pocketmine/plugin/ApiVersion.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\plugin;
|
||||
|
||||
use pocketmine\utils\VersionString;
|
||||
|
||||
final class ApiVersion{
|
||||
|
||||
private function __construct(){
|
||||
//NOOP
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $myVersionStr
|
||||
* @param string[] $wantVersionsStr
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isCompatible(string $myVersionStr, array $wantVersionsStr) : bool{
|
||||
$myVersion = new VersionString($myVersionStr);
|
||||
foreach($wantVersionsStr as $versionStr){
|
||||
$version = new VersionString($versionStr);
|
||||
//Format: majorVersion.minorVersion.patch (3.0.0)
|
||||
// or: majorVersion.minorVersion.patch-devBuild (3.0.0-alpha1)
|
||||
if($version->getBaseVersion() !== $myVersion->getBaseVersion()){
|
||||
if($version->getMajor() !== $myVersion->getMajor() or $version->getSuffix() !== $myVersion->getSuffix()){
|
||||
continue;
|
||||
}
|
||||
|
||||
if($version->getMinor() > $myVersion->getMinor()){ //If the plugin requires new API features, being backwards compatible
|
||||
continue;
|
||||
}
|
||||
|
||||
if($version->getMinor() === $myVersion->getMinor() and $version->getPatch() > $myVersion->getPatch()){ //If the plugin requires bug fixes in patches, being backwards compatible
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -251,7 +251,7 @@ class PluginManager{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!$this->isCompatibleApi(...$description->getCompatibleApis())){
|
||||
if(!ApiVersion::isCompatible($this->server->getApiVersion(), $description->getCompatibleApis())){
|
||||
$this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [
|
||||
$name,
|
||||
$this->server->getLanguage()->translateString("%pocketmine.plugin.incompatibleAPI", [implode(", ", $description->getCompatibleApis())])
|
||||
|
55
tests/phpunit/plugin/ApiVersionTest.php
Normal file
55
tests/phpunit/plugin/ApiVersionTest.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\plugin;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ApiVersionTest extends TestCase{
|
||||
|
||||
public function compatibleApiProvider() : \Generator{
|
||||
yield ["3.0.0", "3.0.0", true];
|
||||
yield ["3.1.0", "3.0.0", true];
|
||||
yield ["3.0.0", "3.1.0", false];
|
||||
yield ["3.1.0", "3.0.1", true]; //old bug where minor wasn't respected when comparing patches
|
||||
yield ["3.0.0", "4.0.0", false];
|
||||
yield ["4.0.0", "3.0.0", false];
|
||||
yield ["3.0.0", "3.0.1", false]; //bug fix patch required
|
||||
yield ["3.0.1", "3.0.0", true];
|
||||
yield ["3.0.0-ALPHA1", "3.0.0-ALPHA2", false];
|
||||
yield ["3.0.0-ALPHA2", "3.0.0-ALPHA1", false];
|
||||
yield ["3.0.0-ALPHA1", "3.0.0-ALPHA1", true];
|
||||
yield ["3.0.0-ALPHA1", "4.0.0-ALPHA1", false];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider compatibleApiProvider
|
||||
*
|
||||
* @param string $myVersion
|
||||
* @param string $wantVersion
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testCompatibleApi(string $myVersion, string $wantVersion, bool $expected) : void{
|
||||
self::assertSame($expected, ApiVersion::isCompatible($myVersion, [$wantVersion]), "my version: $myVersion, their version: $wantVersion, expect " . ($expected ? "yes" : "no"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user