presentkim / pmmp-test-utils
PHPUnit helpers for axolotl-pm/PocketMine-MP plugins and libraries: native extension polyfills, a fake player, packet and class load test cases
Requires
- php: ^8.4
- axolotl-pm/pocketmine-mp: ^5.51
- phpunit/phpunit: ^12.0
Requires (Dev)
- phpstan/phpstan: ^2.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
pmmp-test-utils
PHPUnit helpers for axolotl-pm/PocketMine-MP plugins and libraries
Packagist ยท Changelog ยท Report a bug ยท Request a feature
final class ClassLoadTest extends ClassLoadTestCase{} final class GreetTest extends PacketTestCase{ public function testGreets() : void{ $player = FakePlayer::create("Steve", session: $this->createSession()); Greeter::greet($player); self::assertSame(["Hello, Steve!"], $player->messages); self::assertRoundTrip($this->sentOne(PlaySoundPacket::class)); } }
๐ Why pmmp-test-utils?
PocketMine-MP needs native extensions (ext-encoding, ext-morton, ext-pmmpthread) that a plain PHP on CI does not
have, and a Player cannot be built without a server. Every plugin used to carry its own copy of the polyfills and
fake objects; this package keeps one copy.
- Bootstrap : links the server's
vendor/, loads the autoloader and the polyfills of the missing extensions - Polyfills : test-only stand-ins for ext-encoding (enough to encode and decode packets), ext-morton and the ext-pmmpthread base classes
- Fake objects :
FakePlayerwithout a server or a world,ServerInstancefor code that touchesServer::getInstance()in passing - Ready-made tests :
ClassLoadTestCase(every class links),PluginManifestTestCase(plugin.yml matches the code),PacketTestCase(records sent packets, checks round trips)
The polyfills are only for tests: never load them on a server.
๐ฆ Installation
composer require --dev presentkim/pmmp-test-utils
This also installs axolotl-pm/pocketmine-mp and phpunit/phpunit as dev dependencies.
| Requirement | Version |
|---|---|
| Server | axolotl-pm/PocketMine-MP (fork of PocketMine-MP 5) |
| PHP | 8.4 or newer |
| PHPUnit | 12 |
๐ Quick start
- Point PHPUnit at the bootstrap in
phpunit.xml.dist:
<phpunit bootstrap="vendor/presentkim/pmmp-test-utils/bootstrap.php">
When you need more setup, keep your own tests/bootstrap.php and require the package's bootstrap first:
require dirname(__DIR__) . "/vendor/presentkim/pmmp-test-utils/bootstrap.php";
-
Delete the copies this replaces:
tests/polyfill/encoding.php,morton.php,pmmpthread.php/thread.php, and the servervendor/link code oftests/bootstrap.php. -
Use the test cases and fake objects:
use kim\present\utils\pmmptest\ClassLoadTestCase; use kim\present\utils\pmmptest\PluginManifestTestCase; final class ClassLoadTest extends ClassLoadTestCase{} final class PluginManifestTest extends PluginManifestTestCase{ public function testNbtPermissionIsDeclared() : void{ // add plugin specific checks here } }
๐ API reference
All classes are in kim\present\utils\pmmptest.
| Member | Description |
|---|---|
bootstrap.php |
PHPUnit bootstrap: server vendor/ link, autoloader, polyfills |
FakePlayer::create(name = "Steve", ?Location, GameMode, ?array $permissions = null, ?NetworkSession, bool $connected = true, string $locale = "en_US") |
A player without a server; null permissions grant everything |
FakePlayer::$messages |
Messages sent with sendMessage() (a Translatable as its text key) |
ServerInstance::install() : Server |
Makes Server::getInstance() return an uninitialized server |
PacketTestCase::createSession() : NetworkSession |
A session stub that records sent packets in $this->sent |
PacketTestCase::sentOne(string $class) |
Asserts one packet of the class was sent and returns it |
PacketTestCase::assertRoundTrip(DataPacket $packet) |
Encode, decode, encode again: same bytes, nothing unread |
ClassLoadTestCase |
Loads every class of the project's PSR-4 autoload |
PluginManifestTestCase |
name/main/version/api present, main class is a plugin, src-namespace-prefix matches composer.json, api is EXPECTED_API ([5.51.0]) |
ProjectRoot::path() / composer() / psr4() / pluginManifest() |
Paths and settings of the project under test |
๐งช Development
composer install --ignore-platform-req='ext-*' composer test composer analyse # PHPStan
๐ License
Distributed under the MIT License. See LICENSE for more information.