mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
Merge 'minor-next' into 'major-next'
Automatic merge performed by: https://github.com/pmmp/RestrictedActions/actions/runs/11944832380
This commit is contained in:
92
tests/phpunit/console/ConsoleReaderChildProcessUtilsTest.php
Normal file
92
tests/phpunit/console/ConsoleReaderChildProcessUtilsTest.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?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\console;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use function mt_rand;
|
||||
use function str_repeat;
|
||||
|
||||
final class ConsoleReaderChildProcessUtilsTest extends TestCase{
|
||||
|
||||
/**
|
||||
* @phpstan-return \Generator<int, array{string}, void, void>
|
||||
*/
|
||||
public static function commandStringProvider() : \Generator{
|
||||
yield ["stop"];
|
||||
yield ["pocketmine:status"];
|
||||
yield [str_repeat("s", 1000)];
|
||||
yield ["time set day"];
|
||||
yield ["give \"Steve\" golden_apple"];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider commandStringProvider
|
||||
*/
|
||||
public function testCreateParseSymmetry(string $input) : void{
|
||||
$counterCreate = $counterParse = mt_rand();
|
||||
$message = ConsoleReaderChildProcessUtils::createMessage($input, $counterCreate);
|
||||
$parsedInput = ConsoleReaderChildProcessUtils::parseMessage($message, $counterParse);
|
||||
|
||||
self::assertSame($input, $parsedInput);
|
||||
}
|
||||
|
||||
public function testCreateMessage() : void{
|
||||
$counter = 0;
|
||||
|
||||
ConsoleReaderChildProcessUtils::createMessage("", $counter);
|
||||
self::assertSame(1, $counter, "createMessage should always bump the counter");
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-return \Generator<int, array{string, bool}, void, void>
|
||||
*/
|
||||
public static function parseMessageProvider() : \Generator{
|
||||
$counter = 0;
|
||||
yield [ConsoleReaderChildProcessUtils::createMessage("", $counter), true];
|
||||
|
||||
yield ["", false]; //keepalive message, doesn't bump counter
|
||||
|
||||
$counter = 1;
|
||||
yield [ConsoleReaderChildProcessUtils::createMessage("", $counter), false]; //mismatched counter
|
||||
|
||||
$counter = 0;
|
||||
yield ["a" . ConsoleReaderChildProcessUtils::TOKEN_DELIMITER . "b", false]; //message with delimiter but not a valid IPC message
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider parseMessageProvider
|
||||
*/
|
||||
public static function testParseMessage(string $message, bool $valid) : void{
|
||||
$counter = $oldCounter = 0;
|
||||
|
||||
$input = ConsoleReaderChildProcessUtils::parseMessage($message, $counter);
|
||||
if(!$valid){
|
||||
self::assertNull($input, "Result should be null on invalid message");
|
||||
self::assertSame($oldCounter, $counter, "Counter shouldn't be bumped on invalid message");
|
||||
}else{
|
||||
self::assertNotNull($input, "This was a valid message, expected a result");
|
||||
self::assertSame($oldCounter + 1, $counter, "Counter should be bumped on valid message parse");
|
||||
}
|
||||
}
|
||||
}
|
79
tests/phpunit/event/EventTest.php
Normal file
79
tests/phpunit/event/EventTest.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?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\event;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use pocketmine\event\fixtures\TestChildEvent;
|
||||
use pocketmine\event\fixtures\TestGrandchildEvent;
|
||||
use pocketmine\event\fixtures\TestParentEvent;
|
||||
use pocketmine\plugin\Plugin;
|
||||
use pocketmine\plugin\PluginManager;
|
||||
use pocketmine\Server;
|
||||
|
||||
final class EventTest extends TestCase{
|
||||
|
||||
private Plugin $mockPlugin;
|
||||
private PluginManager $pluginManager;
|
||||
|
||||
protected function setUp() : void{
|
||||
HandlerListManager::global()->unregisterAll();
|
||||
|
||||
//TODO: this is a really bad hack and could break any time if PluginManager decides to access its Server field
|
||||
//we really need to make it possible to register events without a Plugin or Server context
|
||||
$mockServer = $this->createMock(Server::class);
|
||||
$this->mockPlugin = self::createStub(Plugin::class);
|
||||
$this->mockPlugin->method('isEnabled')->willReturn(true);
|
||||
|
||||
$this->pluginManager = new PluginManager($mockServer, null);
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass() : void{
|
||||
HandlerListManager::global()->unregisterAll();
|
||||
}
|
||||
|
||||
public function testHandlerInheritance() : void{
|
||||
$expectedOrder = [
|
||||
TestGrandchildEvent::class,
|
||||
TestChildEvent::class,
|
||||
TestParentEvent::class
|
||||
];
|
||||
$actualOrder = [];
|
||||
|
||||
foreach($expectedOrder as $class){
|
||||
$this->pluginManager->registerEvent(
|
||||
$class,
|
||||
function(TestParentEvent $event) use (&$actualOrder, $class) : void{
|
||||
$actualOrder[] = $class;
|
||||
},
|
||||
EventPriority::NORMAL,
|
||||
$this->mockPlugin
|
||||
);
|
||||
}
|
||||
|
||||
$event = new TestGrandchildEvent();
|
||||
$event->call();
|
||||
|
||||
self::assertSame($expectedOrder, $actualOrder, "Expected event handlers to be called from most specific to least specific");
|
||||
}
|
||||
}
|
@ -24,6 +24,12 @@ declare(strict_types=1);
|
||||
namespace pocketmine\event;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use pocketmine\event\fixtures\TestAbstractAllowHandleEvent;
|
||||
use pocketmine\event\fixtures\TestAbstractEvent;
|
||||
use pocketmine\event\fixtures\TestConcreteEvent;
|
||||
use pocketmine\event\fixtures\TestConcreteExtendsAbstractEvent;
|
||||
use pocketmine\event\fixtures\TestConcreteExtendsAllowHandleEvent;
|
||||
use pocketmine\event\fixtures\TestConcreteExtendsConcreteEvent;
|
||||
|
||||
class HandlerListManagerTest extends TestCase{
|
||||
|
||||
|
@ -21,7 +21,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
use pocketmine\event\Event;
|
||||
|
||||
/**
|
||||
* @allowHandle
|
@ -21,7 +21,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
use pocketmine\event\Event;
|
||||
|
||||
abstract class TestAbstractEvent extends Event{
|
||||
|
@ -21,8 +21,8 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pmmp\TesterPlugin\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
class ChildEvent extends ParentEvent{
|
||||
class TestChildEvent extends TestParentEvent{
|
||||
|
||||
}
|
@ -21,7 +21,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
use pocketmine\event\Event;
|
||||
|
||||
class TestConcreteEvent extends Event{
|
||||
|
@ -21,7 +21,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
class TestConcreteExtendsAbstractEvent extends TestAbstractEvent{
|
||||
|
@ -21,7 +21,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
class TestConcreteExtendsAllowHandleEvent extends TestAbstractAllowHandleEvent{
|
||||
|
@ -21,7 +21,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
class TestConcreteExtendsConcreteEvent extends TestConcreteEvent{
|
||||
|
@ -21,8 +21,8 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pmmp\TesterPlugin\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
class GrandchildEvent extends ChildEvent{
|
||||
class TestGrandchildEvent extends TestChildEvent{
|
||||
|
||||
}
|
@ -21,8 +21,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pmmp\TesterPlugin\event;
|
||||
namespace pocketmine\event\fixtures;
|
||||
|
||||
class ParentEvent extends \pocketmine\event\Event{
|
||||
use pocketmine\event\Event;
|
||||
|
||||
class TestParentEvent extends Event{
|
||||
|
||||
}
|
@ -1,88 +0,0 @@
|
||||
<?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 pmmp\TesterPlugin;
|
||||
|
||||
use pmmp\TesterPlugin\event\ChildEvent;
|
||||
use pmmp\TesterPlugin\event\GrandchildEvent;
|
||||
use pmmp\TesterPlugin\event\ParentEvent;
|
||||
use pocketmine\event\EventPriority;
|
||||
use function implode;
|
||||
|
||||
final class EventHandlerInheritanceTest extends Test{
|
||||
|
||||
private const EXPECTED_ORDER = [
|
||||
GrandchildEvent::class,
|
||||
ChildEvent::class,
|
||||
ParentEvent::class
|
||||
];
|
||||
|
||||
/** @var string[] */
|
||||
private array $callOrder = [];
|
||||
|
||||
public function getName() : string{
|
||||
return "Event Handler Inheritance Test";
|
||||
}
|
||||
|
||||
public function getDescription() : string{
|
||||
return "Tests that child events are correctly passed to parent event handlers";
|
||||
}
|
||||
|
||||
public function run() : void{
|
||||
$plugin = $this->getPlugin();
|
||||
$plugin->getServer()->getPluginManager()->registerEvent(
|
||||
ParentEvent::class,
|
||||
function(ParentEvent $event) : void{
|
||||
$this->callOrder[] = ParentEvent::class;
|
||||
},
|
||||
EventPriority::NORMAL,
|
||||
$plugin
|
||||
);
|
||||
$plugin->getServer()->getPluginManager()->registerEvent(
|
||||
ChildEvent::class,
|
||||
function(ChildEvent $event) : void{
|
||||
$this->callOrder[] = ChildEvent::class;
|
||||
},
|
||||
EventPriority::NORMAL,
|
||||
$plugin
|
||||
);
|
||||
$plugin->getServer()->getPluginManager()->registerEvent(
|
||||
GrandchildEvent::class,
|
||||
function(GrandchildEvent $event) : void{
|
||||
$this->callOrder[] = GrandchildEvent::class;
|
||||
},
|
||||
EventPriority::NORMAL,
|
||||
$plugin
|
||||
);
|
||||
|
||||
$event = new GrandchildEvent();
|
||||
$event->call();
|
||||
|
||||
if($this->callOrder === self::EXPECTED_ORDER){
|
||||
$this->setResult(Test::RESULT_OK);
|
||||
}else{
|
||||
$plugin->getLogger()->error("Expected order: " . implode(", ", self::EXPECTED_ORDER) . ", got: " . implode(", ", $this->callOrder));
|
||||
$this->setResult(Test::RESULT_FAILED);
|
||||
}
|
||||
}
|
||||
}
|
@ -57,7 +57,7 @@ class Main extends PluginBase implements Listener{
|
||||
}), 10);
|
||||
|
||||
$this->waitingTests = [
|
||||
new EventHandlerInheritanceTest($this),
|
||||
//Add test objects here
|
||||
];
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user