|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * |
| 5 | + * ____ _ _ __ __ _ __ __ ____ |
| 6 | + * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ |
| 7 | + * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) | |
| 8 | + * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ |
| 9 | + * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Lesser General Public License as published by |
| 13 | + * the Free Software Foundation, either version 3 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * @author PocketMine Team |
| 17 | + * @link http://www.pocketmine.net/ |
| 18 | + * |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +declare(strict_types=1); |
| 23 | + |
| 24 | +namespace pocketmine\crafting; |
| 25 | + |
| 26 | +use Generator; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | +use pocketmine\item\Item; |
| 29 | +use pocketmine\item\VanillaItems; |
| 30 | +use function floor; |
| 31 | + |
| 32 | +class AnvilCraftTest extends TestCase{ |
| 33 | + public static function materialRepairRecipe() : Generator{ |
| 34 | + yield "No repair available" => [ |
| 35 | + VanillaItems::DIAMOND_PICKAXE(), |
| 36 | + VanillaItems::DIAMOND(), |
| 37 | + null |
| 38 | + ]; |
| 39 | + |
| 40 | + yield "Repair one damage" => [ |
| 41 | + VanillaItems::DIAMOND_PICKAXE()->setDamage(1), |
| 42 | + VanillaItems::DIAMOND(), |
| 43 | + new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE(), null) |
| 44 | + ]; |
| 45 | + |
| 46 | + yield "Repair one damage with more materials than expected" => [ |
| 47 | + VanillaItems::DIAMOND_PICKAXE()->setDamage(1), |
| 48 | + VanillaItems::DIAMOND()->setCount(2), |
| 49 | + new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE(), VanillaItems::DIAMOND()) |
| 50 | + ]; |
| 51 | + |
| 52 | + $diamondPickaxeQuarter = (int) floor(VanillaItems::DIAMOND_PICKAXE()->getMaxDurability() / 4); |
| 53 | + yield "Repair one quarter" => [ |
| 54 | + VanillaItems::DIAMOND_PICKAXE()->setDamage($diamondPickaxeQuarter), |
| 55 | + VanillaItems::DIAMOND()->setCount(1), |
| 56 | + new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE(), null) |
| 57 | + ]; |
| 58 | + |
| 59 | + yield "Repair one quarter plus 1" => [ |
| 60 | + VanillaItems::DIAMOND_PICKAXE()->setDamage($diamondPickaxeQuarter + 1), |
| 61 | + VanillaItems::DIAMOND()->setCount(1), |
| 62 | + new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE()->setDamage(1), null) |
| 63 | + ]; |
| 64 | + |
| 65 | + yield "Repair more than one quarter" => [ |
| 66 | + VanillaItems::DIAMOND_PICKAXE()->setDamage($diamondPickaxeQuarter * 2), |
| 67 | + VanillaItems::DIAMOND()->setCount(2), |
| 68 | + new AnvilCraftResult(2, VanillaItems::DIAMOND_PICKAXE(), null) |
| 69 | + ]; |
| 70 | + |
| 71 | + yield "Repair more than one quarter with more materials than expected" => [ |
| 72 | + VanillaItems::DIAMOND_PICKAXE()->setDamage($diamondPickaxeQuarter * 2), |
| 73 | + VanillaItems::DIAMOND()->setCount(3), |
| 74 | + new AnvilCraftResult(2, VanillaItems::DIAMOND_PICKAXE(), VanillaItems::DIAMOND()->setCount(1)) |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @dataProvider materialRepairRecipe |
| 80 | + */ |
| 81 | + public function testMaterialRepairRecipe(Item $base, Item $material, ?AnvilCraftResult $expected) : void{ |
| 82 | + $recipe = new MaterialRepairRecipe( |
| 83 | + new ExactRecipeIngredient((clone $base)->setCount(1)), |
| 84 | + new ExactRecipeIngredient((clone $material)->setCount(1)) |
| 85 | + ); |
| 86 | + $result = $recipe->getResultFor($base, $material); |
| 87 | + if($expected === null){ |
| 88 | + self::assertNull($result, "Recipe did not match expected result"); |
| 89 | + return; |
| 90 | + }else{ |
| 91 | + self::assertNotNull($result, "Recipe did not match expected result"); |
| 92 | + } |
| 93 | + self::assertEquals($expected->getXpCost(), $result->getXpCost(), "XP cost did not match expected result"); |
| 94 | + self::assertTrue($expected->getOutput()->equalsExact($result->getOutput()), "Recipe output did not match expected result"); |
| 95 | + $sacrificeResult = $expected->getSacrificeResult(); |
| 96 | + if($sacrificeResult !== null){ |
| 97 | + $resultExpected = $result->getSacrificeResult(); |
| 98 | + self::assertNotNull($resultExpected, "Recipe sacrifice result did not match expected result"); |
| 99 | + self::assertTrue($sacrificeResult->equalsExact($resultExpected), "Recipe sacrifice result did not match expected result"); |
| 100 | + }else{ |
| 101 | + self::assertNull($result->getSacrificeResult(), "Recipe sacrifice result did not match expected result"); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments