Skip to content

Instantly share code, notes, and snippets.

@elishaukpong
Created June 24, 2022 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elishaukpong/4003b62fb9dd2b5c56505654ffa07c29 to your computer and use it in GitHub Desktop.
Save elishaukpong/4003b62fb9dd2b5c56505654ffa07c29 to your computer and use it in GitHub Desktop.
I broke down each separate implementation into their files; running details at the end of the file
//BaseTest.php file name (same convention is followed)
<?php
use PHPUnit\Framework\TestCase;
include'./Hero.php'; // i utilized includes to be swift
class BaseTest extends TestCase
{
public $defender;
public $attacker;
public $fightService;
const HEALTH = 50;
const ATTACK_ONE = 50;
const DEFENSE_ONE = 100;
const ATTACK_TWO = 50;
const DEFENSE_TWO = 30;
const UPDATED_HEALTH = 50;
protected function setUp(): void
{
parent::setUp();
$this->attacker = new Hero(self::ATTACK_ONE,self::DEFENSE_ONE,self::HEALTH);
$this->defender = new Hero(self::ATTACK_TWO,self::DEFENSE_TWO,self::HEALTH);
$this->fightService = new FightService;
}
}
//FightServiceTest.php
<?php
include './BaseTest.php';
class FightServiceTest extends BaseTest {
public function testFight(): void
{
$this->fightService->fight($this->attacker,$this->defender);
$this->assertNotEquals(self::HEALTH, $this->defender->getHealthPoints());
$this->assertEquals(self::UPDATED_HEALTH, $this->attacker->getHealthPoints());
}
}
//HeroTest.php
<?php
include './BaseTest.php';
class HeroTest extends BaseTest
{
public function testAttackAmount()
{
$this->assertEquals(self::ATTACK_ONE, $this->attacker->getAttack());
}
public function testDefenseAmount()
{
$this->assertEquals(self::DEFENSE_ONE, $this->attacker->getDefence());
}
public function testHealthPoint()
{
$this->assertEquals(self::HEALTH, $this->attacker->getHealthPoints());
}
public function testCanUpdateHealthPoint()
{
$this->assertEquals(self::HEALTH, $this->attacker->getHealthPoints());
$this->attacker->setHealthPoints(self::UPDATED_HEALTH);
$this->assertEquals(self::UPDATED_HEALTH, $this->attacker->getHealthPoints());
}
}
//Hero.php (content of this file can be further broken down but i didnt do that here)
<?php
interface HeroInterface
{
public function getAttack(): int;
public function getDefence(): int;
public function getHealthPoints(): int;
public function setHealthPoints(int $healthPoints);
}
class Hero implements HeroInterface
{
private int $attack;
private int $defence;
private int $healthPoint;
/**
* @param int $attack
* @param int $defence
* @param int $healthPoint
*/
public function __construct(int $attack, int $defence, int $healthPoint)
{
$this->attack = $attack;
$this->defence = $defence;
$this->healthPoint = $healthPoint;
}
/**
* @return int
*/
public function getAttack(): int
{
return $this->attack;
}
/**
* @return int
*/
public function getDefence(): int
{
return $this->defence;
}
/**
* @return int
*/
public function getHealthPoints(): int
{
return $this->healthPoint;
}
/**
* @param int $healthPoints
* @return mixed
*/
public function setHealthPoints(int $healthPoints): void
{
$this->healthPoint = $healthPoints;
}
}
class DamageCalculator
{
const DAMAGE_RAND_FACTOR = 0.2;
public static function calculateDamage(HeroInterface $attacker, HeroInterface $defender): int
{
$damage = 0;
if ($attacker->getAttack() > $defender->getDefence()) {
$baseDamage = $attacker->getAttack() - $defender->getDefence();
$factor = $baseDamage * self::DAMAGE_RAND_FACTOR;
$minDamage = $baseDamage - $factor;
$maxDamage = $baseDamage + $factor;
$damage = mt_rand($minDamage, $maxDamage);
}
return $damage;
}
}
class FightService
{
public function fight(HeroInterface $attacker, HeroInterface $defender)
{
$damage = DamageCalculator::calculateDamage($attacker, $defender);
$defender->setHealthPoints($defender->getHealthPoints() - $damage);
}
}
To run:
ensure php unit is globally available or use composer to pull it in from packagist (i used composer)
Unit Test for Hero: ./vendor/bin/phpunit HeroTest.php --color
Feature Test for FightService: ./vendor/bin/phpunit FightServiceTest.php --color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment