public function deposit($money) { $this->bank_account->deposit($money); }
public function withdrawDataProvider() { $dummy_user = new User(); $bank_account_stub = new BankAccount(0, $dummy_user); $bank_account_stub->deposit(30); return array(array($bank_account_stub, 10, 20), array($bank_account_stub, 10, 10), array($bank_account_stub, 10, 0)); }
public function testDepositShouldRegisterWithAfipWhenAmoutGreaterThanAHundred() { // arrange $account = new BankAccount('Nicolas'); $value = 500; $afipMock = $this->getMock('Afip', array('registerTransaction')); $afipMock->expects($this->once())->method('registerTransaction')->with($this->equalTo('Nicolas'), $this->equalTo($value)); $account->setAfip($afipMock); $amountToTransfer = 500; // act $account->deposit(500); // assert $this->assertEquals($amountToTransfer, $account->getBalance()); }
echo "Error: insufficient funds!<br>\n"; } } function print_log() { $attr = array("date", "operation", "amount", "old_balance", "new_balance"); echo "<table>"; for ($i = 0; $i < count($this->_log); $i++) { echo "<tr>"; for ($j = 0; $j < count($attr); $j++) { echo "<td>" . $this->_log[$i][$attr[$j]] . "</td>"; } echo "</tr>"; } echo "</table>"; } } // test $ba = new BankAccount("my name", 1000); echo "Balance: " . $ba->get_balance() . "<br>\n"; $ba->deposit(200); $ba->deposit(300); echo "Balance: " . $ba->get_balance() . "<br>\n"; $ba->withdraw(500); echo "Balance: " . $ba->get_balance() . "<br>\n"; $ba->withdraw(2000); $ba->print_log(); ?> </body> </html>
public function test_deposit() { $ba = new BankAccount(); $ba->deposit(100); $this->assertEquals(100, $ba->getBalance()); }
/** *How much money this person has *@var float */ public $bankBalance; public function deposit($amount) { $this->bankBalance += $amount; return $this->bankBalance; } public function withdraw($amount) { if ($this->bankBalance >= $amount) { $this->bankBalance -= $amount; return $this->bankBalance; } else { throw new Exception('Insufficient funds'); } } } try { $myAccount = new BankAccount(); $myAccount->owner = 'Samir'; $myAccount->bankBalance = 200; $myAccount->deposit(10); $myAccount->withdraw(300); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); } echo "<pre>"; var_dump($myAccount);
return $this->bankBalance; } /** * Remove some money from your account * @param float $amount How much you want to remove * @return float * @throws Exception */ public function withdraw($amount) { if ($this->bankBalance < $amount) { // An exception is a PHP class that can be extended and worked with in a diverse number of ways throw new Exception('You can\'t withdraw ' . $amount); } $this->bankBalance -= $amount; return $this->bankBalance; } } // Because we have the potential of throwing an exception, all of the code that may throw an exception should be wrapped in a try tag // The try could be placed in the withdraw function but this is bad design because in more complicated structures you want each method to // only perform 1 function and route all exceptions through 1 place try { $myAccount = new BankAccount(); $myAccount->owner = 'Jared'; $myAccount->deposit(12345); $myAccount->withdraw(100000); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); } echo '<pre>'; print_r($myAccount);
* @todo: add validation to this to make sure they have enough money! * Throw an Exception if they are broke! * @throws Exception * @return float */ public function withdraw($amount) { if ($this->bankBalance < $amount) { throw new Exception('Go away'); } if ($this->owner == 'Samir') { throw new Exception('Toobroke'); } $this->bankBalance = $this->bankBalance - $amount; return $this->bankBalance; } } try { $myAccount = new BankAccount(); $myAccount->owner = 'Samir'; $myAccount->deposit(20); $myAccount->withdraw(20); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); } //echo '<pre>'; //print_r( $e); //die(); //echo '<pre>'; //print_r($myAccount); //die();
<?php namespace SoFloPHP; require '../vendor/autoload.php'; /** * CREATE TABLE account (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, balance INT NOT NULL DEFAULT 0); */ $service = new BankAccountService(new \PDO('mysql:dbname=soflophp;host=127.0.0.1', 'root')); $account = new BankAccount(); $account->deposit(500); $service->persist($account); /** * mysql> select * from account; * +----+---------+ * | id | balance | * +----+---------+ * | 1 | 500 | * +----+---------+ * 1 row in set (0.00 sec) */
/** * @expectedException Exception */ public function testDepositNegativeAmountThrowsException() { $this->_account->deposit(-200); }