mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-03 00:25:04 +00:00
Move event handler inheritance test to PHPUnit using mock objects
this is dodgy and we shouldn't rely on it long term. relates to #6524
This commit is contained in:
65
tests/phpunit/event/EventTest.php
Normal file
65
tests/phpunit/event/EventTest.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\event;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use pocketmine\plugin\Plugin;
|
||||
use pocketmine\plugin\PluginManager;
|
||||
use pocketmine\Server;
|
||||
|
||||
final class EventTest extends TestCase{
|
||||
|
||||
public function testHandlerInheritance() : void{
|
||||
//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);
|
||||
$mockPlugin = self::createStub(Plugin::class);
|
||||
$mockPlugin->method('isEnabled')->willReturn(true);
|
||||
|
||||
$pluginManager = new PluginManager($mockServer, null);
|
||||
|
||||
$expectedOrder = [
|
||||
TestGrandchildEvent::class,
|
||||
TestChildEvent::class,
|
||||
TestParentEvent::class
|
||||
];
|
||||
$actualOrder = [];
|
||||
|
||||
foreach($expectedOrder as $class){
|
||||
$pluginManager->registerEvent(
|
||||
$class,
|
||||
function(TestParentEvent $event) use (&$actualOrder, $class) : void{
|
||||
$actualOrder[] = $class;
|
||||
},
|
||||
EventPriority::NORMAL,
|
||||
$mockPlugin
|
||||
);
|
||||
}
|
||||
|
||||
$event = new TestGrandchildEvent();
|
||||
$event->call();
|
||||
|
||||
self::assertSame($expectedOrder, $actualOrder, "Expected event handlers to be called from most specific to least specific");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user