From 49bdaee930f552b387c39992217519eff381447d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 20 Nov 2024 15:38:44 +0000 Subject: [PATCH] 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 --- tests/phpunit/event/EventTest.php | 65 ++++++++++++++ .../event/TestChildEvent.php} | 4 +- .../event/TestGrandchildEvent.php} | 4 +- .../event/TestParentEvent.php} | 4 +- .../src/EventHandlerInheritanceTest.php | 88 ------------------- tests/plugins/TesterPlugin/src/Main.php | 2 +- 6 files changed, 72 insertions(+), 95 deletions(-) create mode 100644 tests/phpunit/event/EventTest.php rename tests/{plugins/TesterPlugin/src/event/GrandchildEvent.php => phpunit/event/TestChildEvent.php} (90%) rename tests/{plugins/TesterPlugin/src/event/ParentEvent.php => phpunit/event/TestGrandchildEvent.php} (89%) rename tests/{plugins/TesterPlugin/src/event/ChildEvent.php => phpunit/event/TestParentEvent.php} (91%) delete mode 100644 tests/plugins/TesterPlugin/src/EventHandlerInheritanceTest.php diff --git a/tests/phpunit/event/EventTest.php b/tests/phpunit/event/EventTest.php new file mode 100644 index 000000000..259daaf5d --- /dev/null +++ b/tests/phpunit/event/EventTest.php @@ -0,0 +1,65 @@ +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"); + } +} diff --git a/tests/plugins/TesterPlugin/src/event/GrandchildEvent.php b/tests/phpunit/event/TestChildEvent.php similarity index 90% rename from tests/plugins/TesterPlugin/src/event/GrandchildEvent.php rename to tests/phpunit/event/TestChildEvent.php index 40c37c567..2b79f882f 100644 --- a/tests/plugins/TesterPlugin/src/event/GrandchildEvent.php +++ b/tests/phpunit/event/TestChildEvent.php @@ -21,8 +21,8 @@ declare(strict_types=1); -namespace pmmp\TesterPlugin\event; +namespace pocketmine\event; -class GrandchildEvent extends ChildEvent{ +class TestChildEvent extends TestParentEvent{ } diff --git a/tests/plugins/TesterPlugin/src/event/ParentEvent.php b/tests/phpunit/event/TestGrandchildEvent.php similarity index 89% rename from tests/plugins/TesterPlugin/src/event/ParentEvent.php rename to tests/phpunit/event/TestGrandchildEvent.php index 68f7df630..7685f3b0b 100644 --- a/tests/plugins/TesterPlugin/src/event/ParentEvent.php +++ b/tests/phpunit/event/TestGrandchildEvent.php @@ -21,8 +21,8 @@ declare(strict_types=1); -namespace pmmp\TesterPlugin\event; +namespace pocketmine\event; -class ParentEvent extends \pocketmine\event\Event{ +class TestGrandchildEvent extends TestChildEvent{ } diff --git a/tests/plugins/TesterPlugin/src/event/ChildEvent.php b/tests/phpunit/event/TestParentEvent.php similarity index 91% rename from tests/plugins/TesterPlugin/src/event/ChildEvent.php rename to tests/phpunit/event/TestParentEvent.php index b71d2627f..c770f6372 100644 --- a/tests/plugins/TesterPlugin/src/event/ChildEvent.php +++ b/tests/phpunit/event/TestParentEvent.php @@ -21,8 +21,8 @@ declare(strict_types=1); -namespace pmmp\TesterPlugin\event; +namespace pocketmine\event; -class ChildEvent extends ParentEvent{ +class TestParentEvent extends Event{ } diff --git a/tests/plugins/TesterPlugin/src/EventHandlerInheritanceTest.php b/tests/plugins/TesterPlugin/src/EventHandlerInheritanceTest.php deleted file mode 100644 index efe20f8d8..000000000 --- a/tests/plugins/TesterPlugin/src/EventHandlerInheritanceTest.php +++ /dev/null @@ -1,88 +0,0 @@ -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); - } - } -} diff --git a/tests/plugins/TesterPlugin/src/Main.php b/tests/plugins/TesterPlugin/src/Main.php index 26d3441f4..08b59dbac 100644 --- a/tests/plugins/TesterPlugin/src/Main.php +++ b/tests/plugins/TesterPlugin/src/Main.php @@ -57,7 +57,7 @@ class Main extends PluginBase implements Listener{ }), 10); $this->waitingTests = [ - new EventHandlerInheritanceTest($this), + //Add test objects here ]; }