Skip to content
Browse files

- Sync BankAccount example with my course material.

  • Loading branch information...
1 parent 7f0ae5a commit 7654aeda4277f4ca771ad487f0dba134386bffb4 @sebastianbergmann committed
Showing with 34 additions and 66 deletions.
  1. +19 −21 PHPUnit/Samples/BankAccount/BankAccount.php
  2. +15 −45 PHPUnit/Samples/BankAccount/BankAccountTest.php
View
40 PHPUnit/Samples/BankAccount/BankAccount.php
@@ -44,8 +44,10 @@
* @since File available since Release 2.3.0
*/
+class BankAccountException extends RuntimeException {}
+
/**
- * A Bank Account.
+ * A bank account.
*
* @category Testing
* @package PHPUnit
@@ -62,9 +64,9 @@ class BankAccount
* The bank account's balance.
*
* @var float
- * @access private
+ * @access protected
*/
- private $balance = 0;
+ protected $balance = 0;
/**
* Returns the bank account's balance.
@@ -81,15 +83,15 @@ public function getBalance()
* Sets the bank account's balance.
*
* @param float $balance
- * @throws InvalidArgumentException
- * @access public
+ * @throws BankAccountException
+ * @access protected
*/
- public function setBalance($balance)
+ protected function setBalance($balance)
{
if ($balance >= 0) {
$this->balance = $balance;
} else {
- throw new InvalidArgumentException;
+ throw new BankAccountException;
}
}
@@ -97,32 +99,28 @@ public function setBalance($balance)
* Deposits an amount of money to the bank account.
*
* @param float $balance
- * @throws InvalidArgumentException
+ * @throws BankAccountException
* @access public
*/
- public function depositMoney($amount)
+ public function depositMoney($balance)
{
- if ($amount >= 0) {
- $this->balance += $amount;
- } else {
- throw new InvalidArgumentException;
- }
+ $this->setBalance($this->getBalance() + $balance);
+
+ return $this->getBalance();
}
/**
* Withdraws an amount of money from the bank account.
*
* @param float $balance
- * @throws InvalidArgumentException
+ * @throws BankAccountException
* @access public
*/
- public function withdrawMoney($amount)
+ public function withdrawMoney($balance)
{
- if ($amount >= 0 && $this->balance >= $amount) {
- $this->balance -= $amount;
- } else {
- throw new InvalidArgumentException;
- }
+ $this->setBalance($this->getBalance() - $balance);
+
+ return $this->getBalance();
}
}
?>
View
60 PHPUnit/Samples/BankAccount/BankAccountTest.php
@@ -61,86 +61,56 @@
*/
class BankAccountTest extends PHPUnit_Framework_TestCase
{
- /**
- * BankAccount object used as the tests' fixture.
- *
- * @var BankAccount
- * @access private
- */
- private $ba;
+ protected $ba;
- /**
- * Sets up the test fixture.
- *
- * @access protected
- */
protected function setUp()
{
$this->ba = new BankAccount;
}
- /**
- * Asserts that the balance is initially zero.
- *
- * @access public
- */
public function testBalanceIsInitiallyZero()
{
$this->assertEquals(0, $this->ba->getBalance());
}
- /**
- * Asserts that the balance cannot become negative.
- *
- * @access public
- */
public function testBalanceCannotBecomeNegative()
{
try {
$this->ba->withdrawMoney(1);
}
- catch (InvalidArgumentException $e) {
+ catch (BankAccountException $e) {
+ $this->assertEquals(0, $this->ba->getBalance());
+
return;
}
$this->fail();
}
- /**
- * Asserts that the balance cannot become negative.
- *
- * @access public
- */
public function testBalanceCannotBecomeNegative2()
{
try {
$this->ba->depositMoney(-1);
}
- catch (InvalidArgumentException $e) {
+ catch (BankAccountException $e) {
+ $this->assertEquals(0, $this->ba->getBalance());
+
return;
}
$this->fail();
}
-
- /**
- * Asserts that the balance cannot become negative.
- *
- * @access public
- */
- public function testBalanceCannotBecomeNegative3()
+/*
+ public function testDepositWithdrawMoney()
{
- try {
- $this->ba->setBalance(-1);
- }
-
- catch (InvalidArgumentException $e) {
- return;
- }
-
- $this->fail();
+ $this->assertEquals(0, $this->ba->getBalance());
+ $this->ba->depositMoney(1);
+ $this->assertEquals(1, $this->ba->getBalance());
+ $this->ba->withdrawMoney(1);
+ $this->assertEquals(0, $this->ba->getBalance());
}
+*/
}
?>

0 comments on commit 7654aed

Please sign in to comment.
Something went wrong with that request. Please try again.