diff --git a/phpstan.neon.dist b/phpstan.neon.dist index c12d5977f..a2f5351fb 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -20,6 +20,8 @@ parameters: checkExplicitMixed: true bootstrapFiles: - tests/phpstan/bootstrap.php + scanDirectories: + - tests/plugins/TesterPlugin scanFiles: - src/pocketmine/PocketMine.php - build/make-release.php @@ -29,6 +31,7 @@ parameters: - build/make-release.php - build/server-phar.php - tests/phpunit + - tests/plugins/TesterPlugin dynamicConstantNames: - pocketmine\IS_DEVELOPMENT_BUILD - pocketmine\DEBUG diff --git a/tests/phpstan/configs/l8-baseline.neon b/tests/phpstan/configs/l8-baseline.neon index 3956d0103..570ee17e9 100644 --- a/tests/phpstan/configs/l8-baseline.neon +++ b/tests/phpstan/configs/l8-baseline.neon @@ -1715,3 +1715,8 @@ parameters: count: 1 path: ../../phpunit/network/mcpe/StupidJsonDecodeTest.php + - + message: "#^Cannot call method cancel\\(\\) on pocketmine\\\\scheduler\\\\TaskHandler\\|null\\.$#" + count: 1 + path: ../../plugins/TesterPlugin/src/pmmp/TesterPlugin/CheckTestCompletionTask.php + diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php index c008b1498..41ca5512e 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php @@ -38,7 +38,7 @@ class Main extends PluginBase implements Listener{ /** @var int */ protected $currentTestNumber = 0; - public function onEnable(){ + public function onEnable() : void{ $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getScheduler()->scheduleRepeatingTask(new CheckTestCompletionTask($this), 10); @@ -49,7 +49,7 @@ class Main extends PluginBase implements Listener{ ]; } - public function onServerCommand(CommandEvent $event){ + public function onServerCommand(CommandEvent $event) : void{ //The CI will send this command as a failsafe to prevent the build from hanging if the tester plugin failed to //run. However, if the plugin loaded successfully we don't want to allow this to stop the server as there may //be asynchronous tests running. Instead we cancel this and stop the server of our own accord once all tests @@ -59,10 +59,7 @@ class Main extends PluginBase implements Listener{ } } - /** - * @return Test|null - */ - public function getCurrentTest(){ + public function getCurrentTest() : ?Test{ return $this->currentTest; } @@ -77,7 +74,7 @@ class Main extends PluginBase implements Listener{ return false; } - public function onTestCompleted(Test $test){ + public function onTestCompleted(Test $test) : void{ $message = "Finished test #" . $this->currentTestNumber . " (" . $test->getName() . "): "; switch($test->getResult()){ case Test::RESULT_OK: @@ -103,7 +100,7 @@ class Main extends PluginBase implements Listener{ $this->currentTest = null; } - public function onAllTestsCompleted(){ + public function onAllTestsCompleted() : void{ $this->getLogger()->notice("All tests finished, stopping the server"); $this->getServer()->shutdown(); } diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Test.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Test.php index 5ddb3d8a6..f20402336 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Test.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Test.php @@ -29,9 +29,13 @@ abstract class Test{ const RESULT_FAILED = 1; const RESULT_ERROR = 2; + /** @var Main */ private $plugin; + /** @var int */ private $result = Test::RESULT_WAITING; + /** @var int */ private $startTime; + /** @var int */ private $timeout = 60; //seconds public function __construct(Main $plugin){ @@ -42,7 +46,7 @@ abstract class Test{ return $this->plugin; } - final public function start(){ + final public function start() : void{ $this->startTime = time(); try{ $this->run(); @@ -55,11 +59,11 @@ abstract class Test{ } } - public function tick(){ + public function tick() : void{ } - abstract public function run(); + abstract public function run() : void; public function isFinished() : bool{ return $this->result !== Test::RESULT_WAITING; @@ -69,7 +73,7 @@ abstract class Test{ return !$this->isFinished() and time() - $this->timeout > $this->startTime; } - protected function setTimeout(int $timeout){ + protected function setTimeout(int $timeout) : void{ $this->timeout = $timeout; } @@ -77,7 +81,7 @@ abstract class Test{ return $this->result; } - public function setResult(int $result){ + public function setResult(int $result) : void{ $this->result = $result; } diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php index 27d3bdc49..3f52c6fd5 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php @@ -26,11 +26,13 @@ namespace pmmp\TesterPlugin\tests; use pmmp\TesterPlugin\Test; use pocketmine\scheduler\AsyncTask; use pocketmine\Server; +use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\MainLogger; +use function ob_get_contents; class AsyncTaskMainLoggerTest extends Test{ - public function run(){ + public function run() : void{ $this->getPlugin()->getServer()->getAsyncPool()->submitTask(new class($this) extends AsyncTask{ /** @var bool */ @@ -43,7 +45,9 @@ class AsyncTaskMainLoggerTest extends Test{ public function onRun(){ ob_start(); MainLogger::getLogger()->info("Testing"); - if(strpos(ob_get_contents(), "Testing") !== false){ + $contents = ob_get_contents(); + if($contents === false) throw new AssumptionFailedError("ob_get_contents() should not return false here"); + if(strpos($contents, "Testing") !== false){ $this->success = true; } ob_end_flush(); diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php index 2b7fd4eab..7970fc026 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php @@ -28,11 +28,11 @@ use pocketmine\scheduler\AsyncTask; class AsyncTaskMemoryLeakTest extends Test{ - public function run(){ + public function run() : void{ $this->getPlugin()->getServer()->getAsyncPool()->submitTask(new TestAsyncTask()); } - public function tick(){ + public function tick() : void{ if(TestAsyncTask::$destroyed === true){ $this->setResult(Test::RESULT_OK); } @@ -48,6 +48,7 @@ class AsyncTaskMemoryLeakTest extends Test{ } class TestAsyncTask extends AsyncTask{ + /** @var bool */ public static $destroyed = false; public function onRun(){ diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php index 8a71f8eee..f263c3f35 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php @@ -41,6 +41,7 @@ class AsyncTaskPublishProgressRaceTest extends Test{ //this test is racy, but it should fail often enough to be a pest if something is broken $this->getPlugin()->getServer()->getAsyncPool()->submitTask(new class($this) extends AsyncTask{ + /** @var bool */ private static $success = false; public function __construct(AsyncTaskPublishProgressRaceTest $t){