Last active
February 6, 2017 01:00
-
-
Save maartenba/8667324 to your computer and use it in GitHub Desktop.
BankAccount.php
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 | |
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; | |
} | |
} |
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 | |
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