Add setUp and tearDown for event unit tests

This commit is contained in:
Dylan K. Taylor 2024-11-20 16:42:50 +00:00
parent 844ba0ff9f
commit 1e3a858de6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -33,15 +33,26 @@ use pocketmine\Server;
final class EventTest extends TestCase{ final class EventTest extends TestCase{
public function testHandlerInheritance() : void{ 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 //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 //we really need to make it possible to register events without a Plugin or Server context
$mockServer = $this->createMock(Server::class); $mockServer = $this->createMock(Server::class);
$mockPlugin = self::createStub(Plugin::class); $this->mockPlugin = self::createStub(Plugin::class);
$mockPlugin->method('isEnabled')->willReturn(true); $this->mockPlugin->method('isEnabled')->willReturn(true);
$pluginManager = new PluginManager($mockServer, null); $this->pluginManager = new PluginManager($mockServer, null);
}
public static function tearDownAfterClass() : void{
HandlerListManager::global()->unregisterAll();
}
public function testHandlerInheritance() : void{
$expectedOrder = [ $expectedOrder = [
TestGrandchildEvent::class, TestGrandchildEvent::class,
TestChildEvent::class, TestChildEvent::class,
@ -50,13 +61,13 @@ final class EventTest extends TestCase{
$actualOrder = []; $actualOrder = [];
foreach($expectedOrder as $class){ foreach($expectedOrder as $class){
$pluginManager->registerEvent( $this->pluginManager->registerEvent(
$class, $class,
function(TestParentEvent $event) use (&$actualOrder, $class) : void{ function(TestParentEvent $event) use (&$actualOrder, $class) : void{
$actualOrder[] = $class; $actualOrder[] = $class;
}, },
EventPriority::NORMAL, EventPriority::NORMAL,
$mockPlugin $this->mockPlugin
); );
} }