From 9d16863b1afcb0d100732c87469418adfbb47cb0 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 5 Jun 2018 19:33:21 +0100 Subject: [PATCH] Convert some TesterPlugin tests into PHPUnit tests, add PHPUnit configuration --- composer.json | 5 + tests/phpunit/block/BlockTest.php | 143 +++++++++++++++++++++++ tests/phpunit/block/MyCustomBlock.php | 31 +++++ tests/phpunit/block/OutOfBoundsBlock.php | 31 +++++ tests/phpunit/block/StrangeNewBlock.php | 30 +++++ tests/phpunit/item/ItemTest.php | 54 +++++++++ tests/plugins/PocketMine-TesterPlugin | 2 +- tests/travis.sh | 5 + 8 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/block/BlockTest.php create mode 100644 tests/phpunit/block/MyCustomBlock.php create mode 100644 tests/phpunit/block/OutOfBoundsBlock.php create mode 100644 tests/phpunit/block/StrangeNewBlock.php create mode 100644 tests/phpunit/item/ItemTest.php diff --git a/composer.json b/composer.json index 66e88f902..011adf8f0 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,11 @@ "": ["src"] } }, + "autoload-dev": { + "psr-4": { + "pocketmine\\": "tests/phpunit/" + } + }, "repositories": [ { "type": "vcs", diff --git a/tests/phpunit/block/BlockTest.php b/tests/phpunit/block/BlockTest.php new file mode 100644 index 000000000..222a4cc6c --- /dev/null +++ b/tests/phpunit/block/BlockTest.php @@ -0,0 +1,143 @@ +expectException(\RuntimeException::class); + BlockFactory::registerBlock($block); + } + + /** + * Test registering a block deliberately overwriting another block works as expected + */ + public function testDeliberateOverrideBlock() : void{ + $block = new MyCustomBlock(); + BlockFactory::registerBlock($block, true); + self::assertInstanceOf(MyCustomBlock::class, BlockFactory::get($block->getId())); + } + + /** + * Test registering a new block which does not yet exist + */ + public function testRegisterNewBlock() : void{ + for($i = 0; $i < 256; ++$i){ + if(!BlockFactory::isRegistered($i)){ + $b = new StrangeNewBlock($i); + BlockFactory::registerBlock($b); + self::assertInstanceOf(StrangeNewBlock::class, BlockFactory::get($b->getId())); + return; + } + } + + self::assertTrue(false, "Can't test registering new blocks because no unused spaces left"); + } + + /** + * Verifies that blocks with IDs larger than 255 can't be registered + */ + public function testRegisterIdTooLarge() : void{ + self::expectException(\RuntimeException::class); + BlockFactory::registerBlock(new OutOfBoundsBlock(25555)); + } + + /** + * Verifies that blocks with IDs smaller than 0 can't be registered + */ + public function testRegisterIdTooSmall() : void{ + self::expectException(\RuntimeException::class); + BlockFactory::registerBlock(new OutOfBoundsBlock(-1)); + } + + /** + * Test that the block factory doesn't return the same object twice - it has to clone it first + * This is necessary because the block factory currently holds lots of partially-initialized copies of block + * instances which would hold position data and other things, so it's necessary to clone them to avoid astonishing behaviour. + */ + public function testBlockFactoryClone() : void{ + for($i = 0; $i < 256; ++$i){ + $b1 = BlockFactory::get($i); + $b2 = BlockFactory::get($i); + self::assertNotSame($b1, $b2); + } + } + + /** + * @return array + */ + public function blockGetProvider() : array{ + return [ + [Block::STONE, Stone::ANDESITE], + [Block::STONE, 15], + [Block::GOLD_BLOCK, 5], + [Block::WOODEN_PLANKS, Planks::DARK_OAK], + [Block::SAND, 0] + ]; + } + + /** + * @dataProvider blockGetProvider + * @param int $id + * @param int $meta + */ + public function testBlockGet(int $id, int $meta) : void{ + $block = BlockFactory::get($id, $meta); + + self::assertEquals($id, $block->getId()); + self::assertEquals($meta, $block->getDamage()); + } + + /** + * Test that all blocks have correctly set names + */ + public function testBlockNames() : void{ + for($id = 0; $id < 256; ++$id){ + $b = BlockFactory::get($id); + self::assertTrue($b instanceof UnknownBlock or $b->getName() !== "Unknown", "Block with ID $id does not have a valid name"); + } + } + + /** + * Test that light filters in the static arrays have valid values. Wrong values can cause lots of unpleasant bugs + * (like freezes) when doing light population. + */ + public function testLightFiltersValid() : void{ + foreach(BlockFactory::$lightFilter as $id => $value){ + self::assertNotNull($value, "Light filter value missing for $id"); + self::assertLessThanOrEqual(15, $value, "Light filter value for $id is larger than the expected 15"); + self::assertGreaterThan(0, $value, "Light filter value for $id must be larger than 0"); + } + } +} diff --git a/tests/phpunit/block/MyCustomBlock.php b/tests/phpunit/block/MyCustomBlock.php new file mode 100644 index 000000000..58bb1a6eb --- /dev/null +++ b/tests/phpunit/block/MyCustomBlock.php @@ -0,0 +1,31 @@ +setCustomName("HI"); + $item2 = Item::nbtDeserialize($item->nbtSerialize()); + self::assertTrue($item2->equals($item)); + self::assertTrue($item->equals($item2)); + } + + /** + * Tests that blocks are considered to be valid registered items + */ + public function testItemBlockRegistered() : void{ + for($id = 0; $id < 256; ++$id){ + self::assertEquals(BlockFactory::isRegistered($id), ItemFactory::isRegistered($id)); + } + } +} diff --git a/tests/plugins/PocketMine-TesterPlugin b/tests/plugins/PocketMine-TesterPlugin index c2415cd2d..7d09625c9 160000 --- a/tests/plugins/PocketMine-TesterPlugin +++ b/tests/plugins/PocketMine-TesterPlugin @@ -1 +1 @@ -Subproject commit c2415cd2dc9cfbd7a099e3e1907e7f57be21f495 +Subproject commit 7d09625c97e645a40a0215db1c04deec10c594a2 diff --git a/tests/travis.sh b/tests/travis.sh index 9fb50c8e2..27da47fd5 100755 --- a/tests/travis.sh +++ b/tests/travis.sh @@ -21,6 +21,11 @@ if [ $? -ne 0 ]; then exit 1 fi +#Run PHPUnit tests +curl https://phar.phpunit.de/phpunit-7.phar --silent --location -o phpunit.phar +"$PHP_BINARY" phpunit.phar --bootstrap vendor/autoload.php tests/phpunit + +#Run-the-server tests DATA_DIR="test_data" PLUGINS_DIR="$DATA_DIR/plugins"