Last active
October 28, 2019 02:21
-
-
Save philipmuir/cee61f041ca042820f14e886a6a34ba2 to your computer and use it in GitHub Desktop.
Mutation testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* A bank account. | |
* Class taken from the phpunit test suite, modified slightly | |
*/ | |
class Bank | |
{ | |
/** | |
* The bank account's balance. | |
* | |
* @var float | |
*/ | |
protected $balance = 0; | |
/** | |
* Bank constructor. | |
* @param float $initialBalance | |
*/ | |
public function __construct(float $initialBalance = 0.0) | |
{ | |
$this->balance = $initialBalance; | |
} | |
/** | |
* Returns the bank account's balance. | |
* | |
* @return float | |
*/ | |
public function getBalance() | |
{ | |
return $this->balance; | |
} | |
/** | |
* Deposits an amount of money to the bank account. | |
* | |
* @param float $balance | |
* | |
* @throws BankAccountException | |
*/ | |
public function depositMoney($balance) | |
{ | |
$this->setBalance($this->getBalance() + $balance); | |
return $this->getBalance(); | |
} | |
/** | |
* Withdraws an amount of money from the bank account. | |
* | |
* @param float $balance | |
* | |
* @throws BankAccountException | |
*/ | |
public function withdrawMoney($balance) | |
{ | |
$this->setBalance($this->getBalance() - $balance); | |
return $this->getBalance(); | |
} | |
/** | |
* Sets the bank account's balance. | |
* | |
* @param float $balance | |
* | |
* @throws BankAccountException | |
*/ | |
protected function setBalance($balance) | |
{ | |
if ($balance >= 0) { | |
$this->balance = $balance; | |
} else { | |
throw new BankAccountException(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Deputy\Insights\Bank; | |
use Deputy\Insights\BankAccountException; | |
use PHPUnit\Framework\TestCase; | |
class BankTest extends TestCase | |
{ | |
public function testDefaultBankbalanceIsZero() | |
{ | |
$bank = new Bank(); | |
$this->assertEquals(0.0, $bank->getBalance()); | |
} | |
public function testDepositMoneyUpdatesBankBalanceCorrectly() | |
{ | |
$bank = new Bank(); | |
$bank->depositMoney(1.1); | |
$this->assertEquals(1.1, $bank->getBalance()); | |
} | |
public function testWithdrawMoneyGreaterThanBalanceThrowsBankAccountException() | |
{ | |
$bank = new Bank(1); | |
$this->expectException(BankAccountException::class); | |
$bank->withdrawMoney(2); | |
} | |
public function testWithdrawMoneyShouldReduceBalance() | |
{ | |
$amount = 10.0; | |
$bank = new Bank(100); | |
$bank->withdrawMoney($amount); | |
$this->assertEquals(90.0, $bank->getBalance()); | |
} | |
public function testWithdrawMoneyWithFullBankBalanceShouldZeroBankAccount() | |
{ | |
$amount = 10.0; | |
$bank = new Bank($amount); | |
$bank->withdrawMoney($amount); | |
$this->assertEquals(0.0, $bank->getBalance()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
commenting out half the unit tests, just testing happy paths, still with a high code coverage: