Skip to content

Instantly share code, notes, and snippets.

@maartenba
Last active February 6, 2017 01:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maartenba/8667324 to your computer and use it in GitHub Desktop.
Save maartenba/8667324 to your computer and use it in GitHub Desktop.
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