Skip to content

Instantly share code, notes, and snippets.

@maartenba maartenba/BankAccount.php
Last active Feb 6, 2017

Embed
What would you like to do?
BankAccount.php
<?php
class BankAccount {
/** @var int */
protected $_balance;
function __construct()
{
$this->_balance = 0;
}
public function getBalance()
{
return $this->_balance;
}
public function deposit($amount)
{
if ($amount <= 0) {
throw new Exception("Amount has to be > 0.");
}
if ($amount > 100000) {
throw new Exception("Amounts over 100.000 have to be verified.");
}
$this->_balance += $amount;
}
public function withdraw($amount)
{
if ($amount <= 0) {
throw new Exception("Amount has to be > 0.");
}
$this->_balance -= $amount;
}
}
<?php
require_once 'BankAccount.php';
class BankAccountTest extends PHPUnit_Framework_TestCase {
/** @var BankAccount */
protected $_account;
protected function setUp()
{
$this->_account = new BankAccount();
}
public function testBalanceIsZeroWhenOpeningAccount() {
$this->assertEquals(0, $this->_account->getBalance());
}
public function testDepositIncreasesBalance() {
$this->_account->deposit(100);
$this->assertEquals(100, $this->_account->getBalance());
}
public function testWithdrawDecreasesBalance() {
$this->_account->withdraw(100);
$this->assertEquals(-100, $this->_account->getBalance());
}
/**
* @expectedException Exception
*/
public function testDepositNegativeAmountThrowsException() {
$this->_account->deposit(-200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.