mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 02:42:58 +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{
|
||||
|
28
tests/phpunit/event/fixtures/TestChildEvent.php
Normal file
28
tests/phpunit/event/fixtures/TestChildEvent.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?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\fixtures;
|
||||
|
||||
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{
|
||||
|
28
tests/phpunit/event/fixtures/TestGrandchildEvent.php
Normal file
28
tests/phpunit/event/fixtures/TestGrandchildEvent.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?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\fixtures;
|
||||
|
||||
class TestGrandchildEvent extends TestChildEvent{
|
||||
|
||||
}
|
30
tests/phpunit/event/fixtures/TestParentEvent.php
Normal file
30
tests/phpunit/event/fixtures/TestParentEvent.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?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\fixtures;
|
||||
|
||||
use pocketmine\event\Event;
|
||||
|
||||
class TestParentEvent extends Event{
|
||||
|
||||
}
|
Reference in New Issue
Block a user